obj2gltf/bin/obj2gltf.js

68 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-10-16 17:32:23 -04:00
#!/usr/bin/env node
"use strict";
var fs = require('fs');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
var parseObj = require('../lib/obj');
var createGltf = require('../lib/gltf');
var util = require('../lib/util');
var defined = util.defined;
var defaultValue = util.defaultValue;
// TODO : support zlib
// TODO : support binary export
if (process.argv.length < 3 || defined(argv.h) || defined(argv.help)) {
console.log('Usage: ./bin/obj2gltf.js [INPUT] [OPTIONS]\n');
console.log(' -i, --input Path to obj file');
console.log(' -o, --output Directory or filename for the exported glTF file');
console.log(' -b, --binary Output binary glTF');
2015-10-20 09:55:15 -04:00
console.log(' -e, --embed Embed glTF resources into a single file');
2015-10-19 17:38:55 -04:00
console.log(' -t, --technique Shading technique. Possible values are lambert, phong, blinn, constant');
2015-10-16 17:32:23 -04:00
console.log(' -h, --help Display this help');
process.exit(0);
}
var objFile = defaultValue(argv._[0], defaultValue(argv.i, argv.input));
2015-10-19 13:35:28 -04:00
var outputPath = defaultValue(argv._[1], defaultValue(argv.o, argv.output));
2015-10-16 17:32:23 -04:00
var binary = defaultValue(defaultValue(argv.b, argv.binary), false);
2015-10-20 09:55:15 -04:00
var embed = defaultValue(defaultValue(argv.e, argv.embed), false);
2015-10-19 13:35:28 -04:00
var technique = defaultValue(argv.t, argv.technique);
2015-10-16 17:32:23 -04:00
if (!defined(objFile)) {
console.error('-i or --input argument is required. See --help for details.');
2015-10-16 17:32:23 -04:00
process.exit(1);
}
if (!defined(outputPath)) {
outputPath = path.dirname(objFile);
}
2015-10-19 13:35:28 -04:00
if (defined(technique)) {
technique = technique.toUpperCase();
if ((technique !== 'LAMBERT') && (technique !== 'PHONG') && (technique !== 'BLINN') && (technique !== 'CONSTANT')) {
console.log('Unrecognized technique \'' + technique + '\'. Using default instead.');
}
}
2015-10-16 17:32:23 -04:00
var inputPath = path.dirname(objFile);
var modelName = path.basename(objFile, '.obj');
var outputIsGltf = /.gltf$/.test(outputPath);
if (outputIsGltf) {
modelName = path.basename(outputPath, '.gltf');
outputPath = path.dirname(outputPath);
}
fs.mkdir(outputPath, function(){
console.time('Total');
console.time('Parse Obj');
parseObj(objFile, inputPath, function(data) {
console.timeEnd('Parse Obj');
console.time('Create glTF');
2015-10-20 09:55:15 -04:00
createGltf(data, modelName, inputPath, outputPath, binary, embed, technique, function() {
2015-10-16 17:32:23 -04:00
console.timeEnd('Create glTF');
console.timeEnd('Total');
});
});
});