obj2gltf/bin/obj2gltf.js

134 lines
4.2 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 defined = Cesium.defined;
2017-04-10 17:57:56 -04:00
var defaults = convert.defaults;
2017-03-13 15:28:51 -04:00
var args = process.argv;
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',
2017-04-10 17:57:56 -04:00
normalize: true,
demandOption: true
2017-03-13 15:28:51 -04:00
},
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',
2017-04-10 17:57:56 -04:00
default: defaults.binary
2017-03-13 15:28:51 -04:00
},
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',
2017-04-10 17:57:56 -04:00
default: defaults.separate
2017-03-13 15:28:51 -04:00
},
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',
2017-04-10 17:57:56 -04:00
default: defaults.separateTextures
2017-03-13 15:28:51 -04:00
},
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',
2017-04-10 17:57:56 -04:00
default: defaults.compress
2017-03-13 15:28:51 -04:00
},
2017-04-04 15:09:58 -04:00
optimize : {
2017-03-13 15:28:51 -04:00
alias: 'z',
2017-04-10 17:57:56 -04:00
describe: 'Optimize the glTF for size and runtime performance.',
2017-03-13 15:28:51 -04:00
type: 'boolean',
2017-04-10 17:57:56 -04:00
default: defaults.optimize
2017-03-13 15:28:51 -04:00
},
2017-04-10 17:57:56 -04:00
optimizeForCesium : {
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',
2017-04-10 17:57:56 -04:00
default: defaults.optimizeForCesium
2017-03-13 15:28:51 -04:00
},
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',
2017-04-10 17:57:56 -04:00
default: defaults.generateNormals
2017-03-13 15:28:51 -04:00
},
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',
2017-04-10 17:57:56 -04:00
default: defaults.ao
2017-03-13 15:28:51 -04:00
},
2017-04-04 17:55:00 -04:00
kmc : {
describe: 'Output glTF with the KHR_materials_common extension.',
type: 'boolean',
2017-04-10 17:57:56 -04:00
default: defaults.kmc
2017-04-04 17:55:00 -04:00
},
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',
2017-04-10 17:57:56 -04:00
default: defaults.bypassPipeline
2017-03-17 16:05:51 -04:00
},
2017-04-10 17:57:56 -04:00
checkTransparency : {
describe: 'Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. By default textures are considered to be opaque.',
2017-03-17 16:05:51 -04:00
type: 'boolean',
2017-04-10 17:57:56 -04:00
default: defaults.checkTransparency
2017-04-04 16:45:21 -04:00
},
secure : {
describe: 'Prevent the converter from reading image or mtl files outside of the input obj directory.',
type: 'boolean',
2017-04-10 17:57:56 -04:00
default: defaults.secure
2017-03-13 15:28:51 -04:00
}
}).parse(args);
2017-04-10 17:57:56 -04:00
var objPath = argv.i;
var gltfPath = argv.o;
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-04-10 17:57:56 -04:00
binary : argv.binary,
separate : argv.separate,
separateTextures : argv.separateTextures,
compress : argv.compress,
optimize : argv.optimize,
optimizeForCesium : argv.optimizeForCesium,
generateNormals : argv.generateNormals,
2017-03-13 15:28:51 -04:00
ao : argv.ao,
2017-04-10 17:57:56 -04:00
kmc : argv.kmc,
2017-03-17 16:05:51 -04:00
bypassPipeline : argv.bypassPipeline,
2017-04-10 17:57:56 -04:00
checkTransparency : argv.checkTransparency,
2017-04-04 16:45:21 -04:00
secure : argv.secure
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');
})
.catch(function(error) {
console.log(error.message);
2016-07-22 14:09:13 -04:00
});