obj2gltf/bin/obj2gltf.js

193 lines
7.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';
2019-02-05 20:59:09 -05:00
const Cesium = require('cesium');
const fsExtra = require('fs-extra');
const path = require('path');
const yargs = require('yargs');
const obj2gltf = require('../lib/obj2gltf');
2015-10-16 17:32:23 -04:00
2019-02-05 20:59:09 -05:00
const defaultValue = Cesium.defaultValue;
const defined = Cesium.defined;
2017-03-13 15:28:51 -04:00
2019-02-05 20:59:09 -05:00
const defaults = obj2gltf.defaults;
2017-04-10 17:57:56 -04:00
2019-02-05 20:59:09 -05:00
const args = process.argv;
2015-10-16 17:32:23 -04:00
2019-02-05 20:59:09 -05:00
const argv = yargs
2017-03-13 15:28:51 -04:00
.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 : {
alias : 'i',
describe : 'Path to the obj file.',
type : 'string',
2018-03-31 11:27:47 -04:00
demandOption : true,
coerce : function (p) {
if (p.length === 0) {
throw new Error('Input path must be a file name');
}
return path.resolve(p);
}
2017-03-13 15:28:51 -04:00
},
2017-04-04 15:09:58 -04:00
output : {
alias : 'o',
describe : 'Path of the converted glTF or glb file.',
type : 'string',
2018-03-31 11:27:47 -04:00
coerce : function (p) {
if (p.length === 0) {
throw new Error('Output path must be a file name');
}
return path.resolve(p);
}
2017-03-13 15:28:51 -04:00
},
2017-04-04 15:09:58 -04:00
binary : {
alias : 'b',
describe : 'Save as binary glTF (.glb)',
type : 'boolean',
default : defaults.binary
2017-03-13 15:28:51 -04:00
},
2017-04-04 15:09:58 -04:00
separate : {
alias : 's',
describe : 'Write separate buffers and textures instead of embedding them in the glTF.',
type : 'boolean',
default : defaults.separate
2017-03-13 15:28:51 -04:00
},
2017-04-04 15:09:58 -04:00
separateTextures : {
alias : 't',
describe : 'Write out separate textures only.',
type : 'boolean',
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.',
type : 'boolean',
default : defaults.checkTransparency
2017-04-04 16:45:21 -04:00
},
secure : {
describe : 'Prevent the converter from reading textures or mtl files outside of the input obj directory.',
type : 'boolean',
default : defaults.secure
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',
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',
default : defaults.specularGlossiness
2017-05-04 15:39:01 -04:00
},
2017-07-24 18:21:01 -04:00
metallicRoughnessOcclusionTexture : {
describe : 'Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material',
type : 'string',
normalize : true
2017-07-24 18:21:01 -04:00
},
specularGlossinessTexture : {
describe : 'Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.',
type : 'string',
normalize : true
2017-07-24 18:21:01 -04:00
},
occlusionTexture : {
describe : 'Path to the occlusion texture that should override textures in the .mtl file.',
type : 'string',
normalize : true
2017-07-24 18:21:01 -04:00
},
normalTexture : {
describe : 'Path to the normal texture that should override textures in the .mtl file.',
type : 'string',
normalize : true
2017-07-24 18:21:01 -04:00
},
baseColorTexture : {
describe : 'Path to the baseColor/diffuse texture that should override textures in the .mtl file.',
type : 'string',
normalize : true
2017-07-24 18:21:01 -04:00
},
emissiveTexture : {
describe : 'Path to the emissive texture that should override textures in the .mtl file.',
type : 'string',
normalize : true
2018-01-03 20:41:25 -05:00
},
alphaTexture : {
describe : 'Path to the alpha texture that should override textures in the .mtl file.'
2018-08-30 15:55:32 -04:00
},
unlit : {
describe : 'The glTF will be saved with the KHR_materials_unlit extension.',
type : 'boolean',
default : defaults.unlit
2018-12-04 14:33:10 -05:00
}
2017-03-13 15:28:51 -04:00
}).parse(args);
2018-08-30 15:55:32 -04:00
if (argv.metallicRoughness + argv.specularGlossiness > 1) {
console.error('Only one material type may be set from [--metallicRoughness, --specularGlossiness].');
process.exit(1);
}
if (defined(argv.metallicRoughnessOcclusionTexture) && defined(argv.specularGlossinessTexture)) {
console.error('--metallicRoughnessOcclusionTexture and --specularGlossinessTexture cannot both be set.');
process.exit(1);
}
2019-02-05 20:59:09 -05:00
const objPath = argv.input;
let gltfPath = argv.output;
2015-10-16 17:32:23 -04:00
2019-02-05 20:59:09 -05:00
const filename = defaultValue(gltfPath, objPath);
const name = path.basename(filename, path.extname(filename));
const outputDirectory = path.dirname(filename);
const binary = argv.binary || path.extname(filename).toLowerCase() === '.glb';
const extension = binary ? '.glb' : '.gltf';
2015-10-19 13:35:28 -04:00
gltfPath = path.join(outputDirectory, name + extension);
2019-02-05 20:59:09 -05:00
const overridingTextures = {
2017-07-28 16:56:28 -04:00
metallicRoughnessOcclusionTexture : argv.metallicRoughnessOcclusionTexture,
specularGlossinessTexture : argv.specularGlossinessTexture,
occlusionTexture : argv.occlusionTexture,
normalTexture : argv.normalTexture,
baseColorTexture : argv.baseColorTexture,
2018-01-03 20:41:25 -05:00
emissiveTexture : argv.emissiveTexture,
alphaTexture : argv.alphaTexture
2017-07-28 16:56:28 -04:00
};
2019-02-05 20:59:09 -05:00
const options = {
2018-01-11 13:26:12 -05:00
binary : binary,
2017-07-28 16:56:28 -04:00
separate : argv.separate,
separateTextures : argv.separateTextures,
checkTransparency : argv.checkTransparency,
secure : argv.secure,
packOcclusion : argv.packOcclusion,
metallicRoughness : argv.metallicRoughness,
specularGlossiness : argv.specularGlossiness,
2018-08-30 15:55:32 -04:00
unlit : argv.unlit,
overridingTextures : overridingTextures,
outputDirectory : outputDirectory
2017-07-28 16:56:28 -04:00
};
2017-03-13 15:28:51 -04:00
console.time('Total');
obj2gltf(objPath, options)
.then(function(gltf) {
2018-01-11 13:26:12 -05:00
if (binary) {
// gltf is a glb buffer
return fsExtra.outputFile(gltfPath, gltf);
}
2019-02-05 20:59:09 -05:00
const jsonOptions = {
spaces : 2
};
return fsExtra.outputJson(gltfPath, gltf, jsonOptions);
})
.then(function() {
console.timeEnd('Total');
})
.catch(function(error) {
console.log(error.message);
process.exit(1);
});