obj2gltf/bin/obj2gltf.js

160 lines
6.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');
2017-04-12 16:55:03 -04:00
var obj2gltf = require('../lib/obj2gltf');
2015-10-16 17:32:23 -04:00
2017-03-13 15:28:51 -04:00
var defined = Cesium.defined;
2017-04-12 16:55:03 -04:00
var defaults = obj2gltf.defaults;
2017-04-10 17:57:56 -04:00
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-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-04-20 14:41:39 -04:00
inputUpAxis : {
describe: 'Up axis of the obj.',
choices: ['X', 'Y', 'Z'],
type: 'string',
default: 'Y'
},
outputUpAxis : {
describe: 'Up axis of the converted glTF.',
choices: ['X', 'Y', 'Z'],
type: 'string',
default: 'Y'
2017-05-04 14:34:33 -04:00
},
packOcclusion : {
describe: 'Pack the occlusion texture in the red channel of metallic-roughness texture.',
type: 'boolean',
default: defaults.packOcclusion
},
2017-05-04 15:39:01 -04:00
metallicRoughness : {
describe: '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.',
type: 'boolean',
2017-05-04 15:39:01 -04:00
default: defaults.metallicRoughness
},
2017-05-04 15:39:01 -04:00
specularGlossiness : {
describe: 'The values in the mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension.',
type: 'boolean',
2017-05-04 15:39:01 -04:00
default: defaults.specularGlossiness
},
materialsCommon : {
describe: 'The glTF will be saved with the KHR_materials_common extension.',
type: 'boolean',
default: defaults.materialsCommon
2017-07-24 18:21:01 -04:00
},
metallicRoughnessOcclusionTexture : {
describe: 'Path to the metallic-roughness-occlusion texture used by the model, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. This may be used instead of setting texture paths in the .mtl file. The model will be saved with a pbrMetallicRoughness material.',
type: 'string',
normalize: true
},
specularGlossinessTexture : {
describe: 'Path to the specular-glossiness texture used by the model, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. This may be used instead of setting texture paths in the .mtl file. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.',
type: 'string',
normalize: true
},
occlusionTexture : {
describe: 'Path to the occlusion texture used by the model. This may be used instead of setting texture paths in the .mtl file. Ignored if metallicRoughnessOcclusionTexture is also set.',
type: 'string',
normalize: true
},
normalTexture : {
describe: 'Path to the normal texture used by the model. This may be used instead of setting texture paths in the .mtl file.',
type: 'string',
normalize: true
},
baseColorTexture : {
describe: 'Path to the baseColor/diffuse texture used by the model. This may be used instead of setting texture paths in the .mtl file.',
type: 'string',
normalize: true
},
emissiveTexture : {
describe: 'Path to the emissive texture used by the model. This may be used instead of setting texture paths in the .mtl file.',
type: 'string',
normalize: true
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,
checkTransparency : argv.checkTransparency,
secure : argv.secure,
2017-04-20 14:41:39 -04:00
inputUpAxis : argv.inputUpAxis,
2017-05-04 14:34:33 -04:00
outputUpAxis : argv.outputUpAxis,
packOcclusion : argv.packOcclusion,
2017-05-04 15:39:01 -04:00
metallicRoughness : argv.metallicRoughness,
specularGlossiness : argv.specularGlossiness
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');
2017-07-19 17:56:24 -04:00
try {
obj2gltf(objPath, gltfPath, options)
.then(function() {
console.timeEnd('Total');
})
.catch(function(error) {
console.log(error);
});
} catch(error) {
console.log(error);
}