obj2gltf/lib/loadMtl.js

101 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-03-13 15:28:51 -04:00
'use strict';
var path = require('path');
2017-04-10 17:57:56 -04:00
var Material = require('./Material');
2017-03-13 15:28:51 -04:00
var readLines = require('./readLines');
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
module.exports = loadMtl;
2016-07-22 14:09:13 -04:00
2017-03-13 15:28:51 -04:00
/**
* Parse an mtl file.
*
* @param {String} mtlPath Path to the mtl file.
2017-05-04 17:58:13 -04:00
* @param {Object} options An object with the following properties:
* @param {Boolean} options.metallicRoughness The values in the mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.
2017-07-28 16:56:28 -04:00
* @returns {Promise} A promise resolving to an array of materials.
2017-03-13 15:28:51 -04:00
*
* @private
*/
2017-05-04 17:58:13 -04:00
function loadMtl(mtlPath, options) {
2017-03-13 15:28:51 -04:00
var material;
var values;
var value;
2017-04-10 17:57:56 -04:00
var mtlDirectory = path.dirname(mtlPath);
var materials = [];
2017-03-13 15:28:51 -04:00
2017-05-04 17:58:13 -04:00
var defaultSpecularShininess = 0.0;
if (options.metallicRoughness) {
defaultSpecularShininess = 1.0; // Fully rough
}
2017-03-13 15:28:51 -04:00
function parseLine(line) {
line = line.trim();
if (/^newmtl /i.test(line)) {
var name = line.substring(7).trim();
material = new Material();
material.name = name;
2017-05-04 17:58:13 -04:00
material.specularShininess = defaultSpecularShininess;
materials.push(material);
2017-03-13 15:28:51 -04:00
} else if (/^Ka /i.test(line)) {
values = line.substring(3).trim().split(' ');
material.ambientColor = [
parseFloat(values[0]),
parseFloat(values[1]),
parseFloat(values[2]),
1.0
];
} else if (/^Ke /i.test(line)) {
values = line.substring(3).trim().split(' ');
2017-04-18 11:56:08 -04:00
material.emissiveColor = [
2017-03-13 15:28:51 -04:00
parseFloat(values[0]),
parseFloat(values[1]),
parseFloat(values[2]),
1.0
];
} else if (/^Kd /i.test(line)) {
values = line.substring(3).trim().split(' ');
material.diffuseColor = [
parseFloat(values[0]),
parseFloat(values[1]),
parseFloat(values[2]),
1.0
];
} else if (/^Ks /i.test(line)) {
values = line.substring(3).trim().split(' ');
material.specularColor = [
parseFloat(values[0]),
parseFloat(values[1]),
parseFloat(values[2]),
1.0
];
} else if (/^Ns /i.test(line)) {
value = line.substring(3).trim();
material.specularShininess = parseFloat(value);
} else if (/^d /i.test(line)) {
value = line.substring(2).trim();
material.alpha = parseFloat(value);
} else if (/^Tr /i.test(line)) {
value = line.substring(3).trim();
2017-05-18 09:32:06 -04:00
material.alpha = 1.0 - parseFloat(value);
2017-03-13 15:28:51 -04:00
} else if (/^map_Ka /i.test(line)) {
2017-04-10 17:57:56 -04:00
material.ambientTexture = path.resolve(mtlDirectory, line.substring(7).trim());
2017-03-13 15:28:51 -04:00
} else if (/^map_Ke /i.test(line)) {
2017-04-18 11:56:08 -04:00
material.emissiveTexture = path.resolve(mtlDirectory, line.substring(7).trim());
2017-03-13 15:28:51 -04:00
} else if (/^map_Kd /i.test(line)) {
2017-04-10 17:57:56 -04:00
material.diffuseTexture = path.resolve(mtlDirectory, line.substring(7).trim());
2017-03-13 15:28:51 -04:00
} else if (/^map_Ks /i.test(line)) {
2017-04-10 17:57:56 -04:00
material.specularTexture = path.resolve(mtlDirectory, line.substring(7).trim());
2017-03-13 15:28:51 -04:00
} else if (/^map_Ns /i.test(line)) {
2017-04-18 11:56:08 -04:00
material.specularShininessTexture = path.resolve(mtlDirectory, line.substring(7).trim());
2017-03-13 15:28:51 -04:00
} else if (/^map_Bump /i.test(line)) {
2017-04-18 11:56:08 -04:00
material.normalTexture = path.resolve(mtlDirectory, line.substring(9).trim());
2017-03-13 15:28:51 -04:00
} else if (/^map_d /i.test(line)) {
2017-04-18 11:56:08 -04:00
material.alphaTexture = path.resolve(mtlDirectory, line.substring(6).trim());
2017-03-13 15:28:51 -04:00
}
}
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
return readLines(mtlPath, parseLine)
.then(function() {
2016-07-22 14:09:13 -04:00
return materials;
});
2015-10-16 17:32:23 -04:00
}