obj2gltf/bin/obj2gltf.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-10-16 17:32:23 -04:00
#!/usr/bin/env node
2017-03-13 15:28:51 -04:00
'use strict';
2016-06-23 16:25:40 -04:00
var Cesium = require('cesium');
2017-03-13 15:28:51 -04:00
var path = require('path');
var yargs = require('yargs');
2016-06-09 13:33:08 -04:00
var convert = require('../lib/convert');
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var args = process.argv;
args = args.slice(2, args.length);
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
var argv = yargs
.usage('Usage: node $0 -i inputPath -o outputPath')
.example('node $0 -i ./specs/data/box/box.obj -o box.gltf')
.help('h')
.alias('h', 'help')
.options({
2017-04-04 15:09:58 -04:00
input : {
2017-03-13 15:28:51 -04:00
alias: 'i',
describe: 'Path to the obj file.',
type: 'string',
normalize: true
},
2017-04-04 15:09:58 -04:00
output : {
2017-03-13 15:28:51 -04:00
alias: 'o',
describe: 'Path of the converted glTF file.',
type: 'string',
normalize: true
},
2017-04-04 15:09:58 -04:00
binary : {
2017-03-13 15:28:51 -04:00
alias: 'b',
describe: 'Save as binary glTF.',
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
separate : {
2017-03-13 15:28:51 -04:00
alias: 's',
describe: 'Write separate geometry data files, shader files, and textures instead of embedding them in the glTF.',
2017-03-13 15:28:51 -04:00
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
separateTextures : {
2017-03-13 15:28:51 -04:00
alias: 't',
describe: 'Write out separate textures only.',
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
compress : {
2017-03-13 15:28:51 -04:00
alias: 'c',
describe: 'Quantize positions, compress texture coordinates, and oct-encode normals.',
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
optimize : {
2017-03-13 15:28:51 -04:00
alias: 'z',
describe: 'Use the optimization stages in the glTF pipeline.',
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
cesium : {
2017-03-13 15:28:51 -04:00
describe: 'Optimize the glTF for Cesium by using the sun as a default light source.',
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
generateNormals : {
2017-03-13 15:28:51 -04:00
alias: 'n',
describe: 'Generate normals if they are missing.',
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
ao : {
2017-03-13 15:28:51 -04:00
describe: 'Apply ambient occlusion to the converted model.',
type: 'boolean',
default: false
},
2017-04-04 15:09:58 -04:00
bypassPipeline : {
2017-03-13 15:28:51 -04:00
describe: 'Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.',
type: 'boolean',
default: false
2017-03-17 16:05:51 -04:00
},
2017-04-04 15:09:58 -04:00
hasTransparency : {
2017-03-17 16:05:51 -04:00
describe: 'Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures with an alpha channel are considered to be transparent.',
type: 'boolean',
default: false
2017-03-13 15:28:51 -04:00
}
}).parse(args);
var objPath = defaultValue(argv.i, argv._[0]);
var gltfPath = defaultValue(argv.o, argv._[1]);
if (!defined(objPath)) {
yargs.showHelp();
return;
2015-10-16 17:32:23 -04:00
}
2017-03-13 15:28:51 -04:00
if (!defined(gltfPath)) {
var extension = argv.b ? '.glb' : '.gltf';
var modelName = path.basename(objPath, path.extname(objPath));
gltfPath = path.join(path.dirname(objPath), modelName + extension);
}
2015-10-19 13:35:28 -04:00
2016-06-09 13:33:08 -04:00
var options = {
2017-03-13 15:28:51 -04:00
binary : argv.b,
separate : argv.s,
separateTextures : argv.t,
compress : argv.c,
optimize : argv.z,
generateNormals : argv.n,
ao : argv.ao,
optimizeForCesium : argv.cesium,
2017-03-17 16:05:51 -04:00
bypassPipeline : argv.bypassPipeline,
2017-03-21 14:37:52 -04:00
hasTransparency : argv.hasTransparency
2016-06-09 13:33:08 -04:00
};
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
console.time('Total');
convert(objPath, gltfPath, options)
2016-07-22 14:09:13 -04:00
.then(function() {
console.timeEnd('Total');
});