obj2gltf/lib/mtl.js

113 lines
4.1 KiB
JavaScript
Raw Normal View History

2017-03-13 15:28:51 -04:00
'use strict';
var path = require('path');
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
function Material() {
this.ambientColor = undefined; // Ka
this.emissionColor = undefined; // Ke
this.diffuseColor = undefined; // Kd
this.specularColor = undefined; // Ks
this.specularShininess = undefined; // Ns
this.alpha = undefined; // d / Tr
this.ambientColorMap = undefined; // map_Ka
this.emissionColorMap = undefined; // map_Ke
this.diffuseColorMap = undefined; // map_Kd
this.specularColorMap = undefined; // map_Ks
this.specularShininessMap = undefined; // map_Ns
this.normalMap = undefined; // map_Bump
this.alphaMap = undefined; // map_d
2015-10-16 17:32:23 -04:00
}
2017-03-13 15:28:51 -04:00
/**
* Parse an mtl file.
*
* @param {String} mtlPath Path to the mtl file.
* @returns {Promise} A promise resolving to the materials, or an empty object if the mtl file doesn't exist.
*
* @private
*/
function loadMtl(mtlPath) {
var material;
var values;
var value;
var materials = {};
function parseLine(line) {
line = line.trim();
if (/^newmtl /i.test(line)) {
var name = line.substring(7).trim();
material = new Material();
materials[name] = material;
} 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(' ');
material.emissionColor = [
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();
material.alpha = parseFloat(value);
} else if (/^map_Ka /i.test(line)) {
material.ambientColorMap = getAbsolutePath(line.substring(7).trim(), mtlPath);
} else if (/^map_Ke /i.test(line)) {
material.emissionColorMap = getAbsolutePath(line.substring(7).trim(), mtlPath);
} else if (/^map_Kd /i.test(line)) {
material.diffuseColorMap = getAbsolutePath(line.substring(7).trim(), mtlPath);
} else if (/^map_Ks /i.test(line)) {
material.specularColorMap = getAbsolutePath(line.substring(7).trim(), mtlPath);
} else if (/^map_Ns /i.test(line)) {
material.specularShininessMap = getAbsolutePath(line.substring(7).trim(), mtlPath);
} else if (/^map_Bump /i.test(line)) {
material.normalMap = getAbsolutePath(line.substring(9).trim(), mtlPath);
} else if (/^map_d /i.test(line)) {
material.alphaMap = getAbsolutePath(line.substring(6).trim(), mtlPath);
}
}
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
}
2017-03-13 15:28:51 -04:00
function getAbsolutePath(imagePath, mtlPath) {
if (!path.isAbsolute(imagePath)) {
imagePath = path.join(path.dirname(mtlPath), imagePath);
}
return imagePath;
}