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 : {
2017-07-29 13:23:33 -04:00
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 : {
2017-07-29 13:23:33 -04:00
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 : {
2017-07-29 13:23:33 -04:00
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 : {
2017-07-29 13:23:33 -04:00
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 : {
2017-07-29 13:23:33 -04:00
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 : {
2017-07-29 13:23:33 -04:00
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 : {
2017-04-20 14:41:39 -04:00
describe : 'Prevent the converter from reading image or mtl files outside of the input obj directory.' ,
type : 'boolean' ,
default : defaults . secure
} ,
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'
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].' ) ;
2017-07-28 17:34:56 -04:00
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
2017-07-29 13:23:33 -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 ,
2017-04-20 14:41:39 -04:00
inputUpAxis : argv . inputUpAxis ,
outputUpAxis : argv . outputUpAxis
2017-07-28 16:56:28 -04:00
} ;
2017-03-13 15:28:51 -04:00
console . time ( 'Total' ) ;
2017-07-29 13:23:33 -04:00
obj2gltf ( objPath , options )
. then ( function ( gltf ) {
2018-01-11 13:26:12 -05:00
if ( binary ) {
2017-07-29 13:23:33 -04:00
// gltf is a glb buffer
return fsExtra . outputFile ( gltfPath , gltf ) ;
}
2019-02-05 20:59:09 -05:00
const jsonOptions = {
2017-07-29 13:23:33 -04:00
spaces : 2
} ;
return fsExtra . outputJson ( gltfPath , gltf , jsonOptions ) ;
} )
2017-07-28 17:34:56 -04:00
. then ( function ( ) {
console . timeEnd ( 'Total' ) ;
} )
. catch ( function ( error ) {
console . log ( error . message ) ;
2017-07-29 13:23:33 -04:00
process . exit ( 1 ) ;
2017-07-28 17:34:56 -04:00
} ) ;