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' ,
2017-03-14 16:42:42 -04:00
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 15:09:58 -04:00
bypassPipeline : {
2017-04-20 10:23:00 -04:00
describe : 'Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above.' ,
2017-03-13 15:28:51 -04:00
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-05-03 17:59:24 -04:00
} ,
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
} ,
2017-05-03 17:59:24 -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 : {
2017-05-03 17:59:24 -04:00
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-03 17:59:24 -04:00
} ,
2017-05-04 15:39:01 -04:00
specularGlossiness : {
2017-05-03 17:59:24 -04:00
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-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-03-17 16:05:51 -04:00
bypassPipeline : argv . bypassPipeline ,
2017-04-10 17:57:56 -04:00
checkTransparency : argv . checkTransparency ,
2017-05-03 17:59:24 -04:00
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-04-12 16:55:03 -04:00
obj2gltf ( objPath , gltfPath , options )
2016-07-22 14:09:13 -04:00
. then ( function ( ) {
console . timeEnd ( 'Total' ) ;
2017-04-05 10:44:28 -04:00
} )
. catch ( function ( error ) {
2017-05-03 17:59:24 -04:00
console . log ( error ) ;
2016-07-22 14:09:13 -04:00
} ) ;