Skip to content

Commit

Permalink
Merge pull request #170 from AnalyticalGraphicsInc/full-yargs-cli
Browse files Browse the repository at this point in the history
Full yargs cli
  • Loading branch information
lilleyse authored Nov 11, 2016
2 parents e2aa024 + eea64ce commit 707ca06
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 69 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ npm install

Command-Line Example:
```
node ./bin/gltf-pipeline.js ./specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.gltf -o output.gltf
node ./bin/gltf-pipeline.js -i ./specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.gltf -o output.gltf
```

### Command-Line Flags

|Flag|Description|Required|
|----|-----------|--------|
|`-i`, `--input`|Path to the input glTF file.| :white_check_mark: Yes|
|`-o`, `--output`|Directory or filename for the exported glTF file.|No|
|`-b`, `--binary`|Write binary glTF file.|No, default `false`|
|`-s`, `--separate`|Writes out separate geometry/animation data files, shader files and textures instead of embedding them in the glTF file.|No, default `false`|
|`-h`, `--help`|Display help|No|
|`-t`, `--separateTexture`|Write out separate textures, but embed geometry/animation data files, and shader files.|No, default `false`|
|`-q`, `--quantize`|Quantize attributes using WEB3D_quantized_attributes extension.|No, default `false`|
|`-n`, `--encodeNormals`|Oct-encode the normals of this model.|No, default `false`|
|`-c`, `--compressTextureCoordinates`|Compress the texture coordinates of this model.|No, default `false`|
|`-r`, `--removeNormals`|Strips off existing normals, allowing them to be regenerated.|No, default `false`|
|`-f`, `--faceNormals`|If normals are missing, they should be generated using the face normal.|No, default `false`|
|`--ao`|Bake ambient occlusion to vertex data using default settings __only__. When specifying other settings, do not use `--ao` on its own. Advanced settings in `lib/bakeAmbientOcclusion.js`|No, default `false`|
|`--help`, `-h`|Display help|No|
|`--input`, `-i`|input=PATH, Read unoptimized glTF from the specified file.|:white_check_mark: Yes|
|`--output`, `-o`|output=PATH, Write optimized glTF to the specified file.|No|
|`--binary`, `-b`|Write binary glTF file using KHR_binary_glTF extension.|No, default `false`|
|`--separate`, `-s`|Write separate geometry/animation data files, shader files, and textures instead of embedding them in the glTF asset.|No, default `false`|
|`--separateTexture`, `-t`|Write out separate textures, but embeds geometry/animation data files and shader files in the glTF asset.|No, default `false`|
|`--quantize`, `-q`|Quantize the attributes of this glTF asset using the WEB3D_quantized_attributes extension.|No, default `false`|
|`--encodeNormals`, `-n`|Oct-encode the normals of this glTF asset.|No, default `false`|
|`--compressTextureCoordinates`, `-c`|Compress the testure coordinates of this glTF asset.|No, default `false`|
|`--removeNormals`, `-r`|Strips off existing normals, allowing them to be regenerated.|No, default `false`|
|`--faceNormals`, `-f`|If normals are missing, they should be generated using the face normal.|No, default `false`|
|`--cesium`, `-c`|Optimize the glTF for Cesium by using the sun as a default light source.|No, default `false`|
|`--ao.enable`|Bake ambient occlusion (to vertex data by default). If other `ao` flags are enabled, this is implicitly true. Advanced settings in `lib/bakeAmbientOcclusion.js`|No, default `false`|
|`--ao.toTexture`|Bake AO to existing diffuse textures instead of to vertices. Does not modify shaders.|No, default `false`|
|`--ao.groundPlane`|Simulate a groundplane at the lowest point of the model when baking AO.|No, default `false`|
|`--ao.ambientShadowContribution`|Amount of AO to show when blending between shader computed lighting and AO. 1.0 is full AO, 0.5 is a 50/50 blend.|No, default `0.5`|
|`--ao.quality`|Valid settings are high, medium, and low.|No, default `low`|
|`--ao.quality`|Quality to use when baking AO. Valid settings are high, medium, and low.|No, default `low`|

## Build Instructions

Expand Down
173 changes: 122 additions & 51 deletions bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,130 @@
#!/usr/bin/env node
'use strict';
var Cesium = require('cesium');
var argv = require('yargs').argv;
var path = require('path');
var yargs = require('yargs');
var Pipeline = require('../lib/Pipeline');

var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;

var Pipeline = require('../lib/Pipeline');

var processFileToDisk = Pipeline.processFileToDisk;

if (process.argv.length < 3 || defined(argv.h) || defined(argv.help)) {
var help =
'Usage: node ' + path.basename(__filename) + ' [path-to.gltf or path-to.bgltf] [OPTIONS]\n' +
' -i --input, input=PATH Read unoptimized glTF from the specified file.\n' +
' -o --output, output=PATH write optimized glTF to the specified file.\n' +
' -b --binary, write binary glTF file.\n' +
' -s --separate, writes out separate geometry/animation data files, shader files and textures instead of embedding them in the glTF file.\n' +
' -t --separateImage, write out separate textures, but embed geometry/animation data files, and shader files.\n' +
' -q --quantize, quantize the attributes of this model.\n' +
' -n --encodeNormals, oct-encode the normals of this model.\n' +
' -c --compressTextureCoordinates, compress the texture coordinates of this model.\n' +
' -r --removeNormals, strips off existing normals, allowing them to be regenerated.\n' +
' -f --faceNormals, if normals are missing, they should be generated using the face normal.\n' +
' --ao: Bake ambient occlusion to vertex data using default settings ONLY. When specifying other settings, do not use `--ao` on its own. Default: inactive.\n' +
' --ao.toTexture: Bake AO to existing diffuse textures instead of to vertices. Does not modify shaders. Default: inactive.\n' +
' --ao.groundPlane: Simulate a ground plane at the lowest point of the model when baking AO. Default: inactive.\n' +
' --ao.ambientShadowContribution: Amount of AO to show when blending between shader computed lighting and AO. 1.0 is full AO, 0.5 is a 50/50 blend. Default: 0.5\n' +
' --ao.quality: Valid settings are high, medium, and low. Default: low\n' +
' --cesium, optimize the glTF for Cesium by using the sun as a default light source. \n';
process.stdout.write(help);
return;
var argv = yargs
.usage('Usage: node $0 -i inputPath -o outputPath')
.example('node $0 -i ./specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.gltf -o output.gltf')
.help('h')
.alias('h', 'help')
.options({
'input': {
alias: 'i',
describe: 'input=PATH, Read unoptimized glTF from the specified file.',
group: 'Options:', // make sure 'Options:' is listed first
normalize: true,
type: 'string'
},
'output': {
alias: 'o',
describe: 'output=PATH, Write optimized glTF to the specified file.',
normalize: true,
type: 'string'
},
'binary': {
alias: 'b',
describe: 'Write binary glTF file using KHR_binary_glTF extension.',
type: 'boolean'
},
'separate': {
alias: 's',
describe: 'Write separate geometry/animation data files, shader files, and textures instead of embedding them in the glTF asset.',
type: 'boolean'
},
'separateImage': {
alias: 't',
describe: 'Write out separate textures, but embeds geometry/animation data files and shader files in the glTF asset.',
type: 'boolean'
},
'quantize': {
alias: 'q',
describe: 'Quantize the attributes of this glTF asset using the WEB3D_quantized_attributes extension.',
type: 'boolean'
},
'encodeNormals': {
alias: 'n',
describe: 'Oct-encode the normals of this glTF asset.',
type: 'boolean'
},
'compressTextureCoordinates': {
alias: 'c',
describe: 'Compress the testure coordinates of this glTF asset.',
type: 'boolean'
},
'removeNormals': {
alias: 'r',
describe: 'Strips off existing normals, allowing them to be regenerated.',
type: 'boolean'
},
'faceNormals': {
alias: 'f',
describe: 'If normals are missing, they should be generated using the face normal.',
type: 'boolean'
},
'cesium': {
describe: 'Optimize the glTF for Cesium by using the sun as a default light source.',
type: 'boolean'
},
'ao.enable': {
default: false,
describe: 'Bake ambient occlusion (to vertex data by default). If other `ao` flags are enabled, this is implicitly true.',
group: 'Options: Ambient Occlusion'
},
'ao.toTexture': {
default: false,
describe: 'Bake AO to existing diffuse textures instead of vertices. Does not modify shaders.',
group: 'Options: Ambient Occlusion'
},
'ao.groundPlane': {
default: false,
describe: 'Simulate a ground plane at the lowest point of the model when baking AO.',
group: 'Options: Ambient Occlusion'
},
'ao.ambientShadowContribution': {
default: 0.5,
describe: 'Amount of AO to show when blending shader computed lighting and AO. 1.0 is full AO, 0.5 is a 50/50 blend.',
group: 'Options: Ambient Occlusion',
nargs: 1,
type: 'number'
},
'ao.quality': {
choices: ['high', 'medium', 'low'],
default: 'low',
describe: 'Quality to use when baking AO. Valid settings are high, medium, and low.',
group: 'Options: Ambient Occlusion',
nargs: 1,
type: 'string'
}
})
.argv;

// If any raw ao parameters were specified, ao is enabled
var nargs = process.argv.length;
for (var i = 0; i < nargs; i++) {
var arg = process.argv[i];
if (arg.indexOf('ao') >= 0) {
argv.ao.enable = true;
}
}

var gltfPath = defaultValue(argv._[0], defaultValue(argv.i, argv.input));
var outputPath = defaultValue(argv._[1], defaultValue(argv.o, argv.output));
var binary = defaultValue(defaultValue(argv.b, argv.binary), false);
var separate = defaultValue(defaultValue(argv.s, argv.separate), false);
var separateImage = defaultValue(defaultValue(argv.t, argv.separateImage), false);
var quantize = defaultValue(defaultValue(argv.q, argv.quantize), false);
var encodeNormals = defaultValue(defaultValue(argv.n, argv.encodeNormals), false);
var compressTextureCoordinates = defaultValue(defaultValue(argv.c, argv.compressTextureCoordinates), false);
var removeNormals = defaultValue(defaultValue(argv.r, argv.removeNormals), false);
var faceNormals = defaultValue(defaultValue(argv.f, argv.faceNormals), false);
var aoOptions = argv.ao;
var typeofAoOptions = typeof(aoOptions);
if (typeofAoOptions === 'boolean' || typeofAoOptions === 'string') {
aoOptions = {};
var gltfPath = defaultValue(argv.i, argv._[0]);
var outputPath = defaultValue(argv.o, argv._[1]);

if (!defined(gltfPath)) {
yargs.showHelp();
return;
}
var optimizeForCesium = defaultValue(argv.cesium, false);

if (!defined(outputPath)) {
var outputFileExtension;
if (binary) {
if (argv.b) {
outputFileExtension = '.glb';
} else {
outputFileExtension = '.gltf';
Expand All @@ -66,16 +137,16 @@ if (!defined(outputPath)) {
}

var options = {
aoOptions : aoOptions,
binary : binary,
compressTextureCoordinates : compressTextureCoordinates,
embed : !separate,
embedImage : !separateImage,
encodeNormals : encodeNormals,
removeNormals : removeNormals,
faceNormals : faceNormals,
optimizeForCesium : optimizeForCesium,
quantize : quantize
aoOptions: argv.ao,
binary: argv.b,
compressTextureCoordinates: argv.c,
embed: !argv.s,
embedImage: !argv.t,
encodeNormals: argv.n,
faceNormals: argv.f,
removeNormals: argv.r,
optimizeForCesium: argv.cesium,
quantize: argv.q
};

console.time('optimize');
Expand Down
3 changes: 1 addition & 2 deletions lib/Pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ Pipeline.processJSONWithExtras = function(gltfWithExtras, options) {

// run AO after optimizeForVertexCache since AO adds new attributes.
var aoOptions = options.aoOptions;
if (defined(aoOptions)) {
aoOptions.gltfWithExtras = gltfWithExtras;
if (defined(aoOptions) && aoOptions.enabled) {
bakeAmbientOcclusion(gltfWithExtras, aoOptions);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/bakeAmbientOcclusion.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = bakeAmbientOcclusion;
* @param {Number} [aoOptions.rayDistance] Ray bottom-out distance in world-space units. Default: 20% of the model's smallest Axis Aligned Bounding Box dimension.
* @param {Number} [aoOptions.nearCull=0.0001] Near cull depth for ray-triangle collisions. Specified in scene space coordinates.
* @param {String} [aoOptions.shaderMode='blend'] How should the shader use per-vertex AO? Valid settings are multiply, replace, and blend.
* @param {String} [aoOptions.scene=aoOptions.gltfWithExtras.scene] Which scene to use when baking AO.
* @param {String} [aoOptions.scene=gltf.scene] Which scene to use when baking AO.
*
* @see addPipelineExtras
* @see loadGltfUris
Expand Down Expand Up @@ -124,7 +124,6 @@ function generateOptions(gltf, aoOptions) {
nearCull : 0.0001,
shaderMode : 'blend',
sceneID : gltf.scene,
gltfWithExtras : aoOptions.gltfWithExtras
};

if (defined(aoOptions)) {
Expand Down

0 comments on commit 707ca06

Please sign in to comment.