Skip to content

Commit

Permalink
Merge branch 'master' of github.com:AnalyticalGraphicsInc/gltf-pipeli…
Browse files Browse the repository at this point in the history
…ne into web3d-quantized-attributes
  • Loading branch information
lasalvavida committed Jun 9, 2016
2 parents 5d7e99f + 30a5565 commit c4d3626
Show file tree
Hide file tree
Showing 40 changed files with 745 additions and 306 deletions.
1 change: 1 addition & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.idea
/.jshintrc
/.npmignore
/.travis.yml
/build
/coverage
/doc
/gulpfile.js
/output
/spec
/specs
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,23 @@ Command-line Example:
node ./bin/gltf-pipeline.js ./specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.gltf -o output.gltf
```

## Using the JavaScript library
## Usage

### Using the JavaScript library

Include `build/gltf-pipeline.js` it with a `script` tag. For a simple example, see [build/index.html](build/index.html).


###Command line flags:

|Flag|Description|Required|
|----|-----------|--------|
|`-i`|Path to the input glTF file.| :white_check_mark: Yes|
|`-o`|Directory or filename for the exported glTF file.|No|
|`-b`|Write binary glTF file.|No, default `false`|
|`-s`|Writes out separate geometry/animation data files, shader files and textures instead of embedding them in the glTF file.|No, default `false`|
|`-h`|Display help|No|

## Build Instructions

Run the tests:
Expand Down Expand Up @@ -66,6 +79,18 @@ To rebuild, run:
npm run build
```

## Limitations

This tool is still in development. We plan on adding additional features such as AO baking, and other functionality.


## Contributions

Pull requests are appreciated! Please use the same [Contributor License Agreement (CLA)](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/CONTRIBUTING.md) and [Coding Guide](https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Documentation/Contributors/CodingGuide/README.md) used for [Cesium](http://cesiumjs.org/).

---

Developed by the Cesium team.
<p align="center">
<a href="http://cesiumjs.org/"><img src="doc/cesium.png" /></a>
</p>
137 changes: 38 additions & 99 deletions bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,53 @@
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var argv = require('yargs').argv;
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
var addDefaults = require('../').addDefaults;
var removeUnusedImages = require('../').removeUnusedImages;
var removeUnusedSamplers = require('../').removeUnusedSamplers;
var removeUnusedShaders = require('../').removeUnusedShaders;
var removeUnusedTechniques = require('../').removeUnusedTechniques;
var removeUnusedPrograms = require('../').removeUnusedPrograms;
var removeUnusedBuffers = require('../').removeUnusedBuffers;
var removeUnusedBufferViews = require('../').removeUnusedBufferViews;
var removeUnusedMaterials = require('../').removeUnusedMaterials;
var removeUnusedSkins = require('../').removeUnusedSkins;
var removeUnusedCameras = require('../').removeUnusedCameras;
var removeUnusedTextures = require('../').removeUnusedTextures;
var removeUnusedMeshes = require('../').removeUnusedMeshes;
var removeUnusedNodes = require('../').removeUnusedNodes;
var removeUnusedAccessors = require('../').removeUnusedAccessors;
var removeUnused = require('../').removeUnused;
var loadGltfUris = require('../').loadGltfUris;
var writeGltf = require('../').writeGltf;
var parseBinaryGltf = require('../').parseBinaryGltf;
var addPipelineExtras = require('../').addPipelineExtras;
var convertDagToTree = require('../').convertDagToTree;
var combineMeshes = require('../').combineMeshes;
var combinePrimitives = require('../').combinePrimitives;
var removeUnusedVertices = require('../').removeUnusedVertices;
var OptimizationStatistics = require('../').OptimizationStatistics;
var Cesium = require('cesium');
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;

if (!defined(argv._[0]) || defined(argv.h) || defined(argv.help)) {
var help =
var DeveloperError = Cesium.DeveloperError;
var writeGltf = require('../lib/writeGltf');
var writeBinaryGltf = require('../lib/writeBinaryGltf');
var gltfPipeline = require('../lib/gltfPipeline');
var processFileToDisk = gltfPipeline.processFileToDisk;
var addPipelineExtras = require('../lib/addPipelineExtras');
var readGltf = require('../lib/readGltf');

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' +
' -o=PATH Write optimized glTF to the specified file.\n';
' -i, input=PATH Read unoptimized glTF from the specified file.\n ' +
' -o, output=PATH write optimized glTF to the specified file.\n' +
' -b, write binary glTF file.\n' +
' -s, writes out separate geometry/animation data files, shader files and textures instead of embedding them in the glTF file.\n ';
process.stdout.write(help);
return;
}

var gltfPath = argv._[0];

fs.readFile(gltfPath, function (err, data) {
if (err) {
throw err;
}

var fileExtension = path.extname(gltfPath);
var fileName = path.basename(gltfPath, fileExtension);
var filePath = path.dirname(gltfPath);

var gltf;
if (fileExtension === '.glb') {
gltf = parseBinaryGltf(data);
}
else if (fileExtension === '.gltf') {
gltf = JSON.parse(data);
}
else {
throw new Error('Invalid glTF file.');
}

var stats = new OptimizationStatistics();

addDefaults(gltf, stats);

// TODO: custom pipeline based on arguments / config
removeUnused(gltf, stats);
printStats(stats);
var gltfPath = defaultValue(argv._[0], argv.i);
var fileExtension = path.extname(gltfPath);
var fileName = path.basename(gltfPath, fileExtension);
var filePath = path.dirname(gltfPath);

addPipelineExtras(gltf);
convertDagToTree(gltf);
var outputPath = defaultValue(argv._[1], argv.o);
var isSeparate = defaultValue(argv.s, false);
var isBinary = defaultValue(argv.b, false);

gltf = loadGltfUris(gltf, filePath, function(err) {
if (err) {
throw err;
}

combineMeshes(gltf);
combinePrimitives(gltf);

removeUnusedVertices(gltf);
if (!defined(gltfPath)) {
throw new DeveloperError('Input path is undefined.');
}

var outputPath = argv.o;
if (!defined(outputPath)) {
// Default output. For example, path/asset.gltf becomes path/asset-optimized.gltf
outputPath = path.join(filePath, fileName + '-optimized' + fileExtension);
}
if (fileExtension !== '.glb' && fileExtension !== '.gltf') {
throw new DeveloperError('Invalid glTF file.');
}

//Run removeUnused stage again after all pipeline stages have been run to remove objects that become unused
removeUnused(gltf);
if (!defined(outputPath)) {
// Default output. For example, path/asset.gltf becomes path/asset-optimized.gltf
outputPath = path.join(filePath, fileName + '-optimized' + fileExtension);
}

var isEmbedded = true;
writeGltf(gltf, outputPath, isEmbedded, true);
});
});
var options = {
isBinary : isBinary,
isEmbedded : !isSeparate
};

function printStats(stats) {
process.stdout.write('Nodes removed: ' + stats.numberRemoved.nodes + '\n');
process.stdout.write('Skins removed: ' + stats.numberRemoved.skins + '\n');
process.stdout.write('Cameras removed: ' + stats.numberRemoved.cameras + '\n');
process.stdout.write('Meshes removed: ' + stats.numberRemoved.meshes + '\n');
process.stdout.write('Accessors removed: ' + stats.numberRemoved.accessors + '\n');
process.stdout.write('Materials removed: ' + stats.numberRemoved.materials + '\n');
process.stdout.write('BufferViews removed: ' + stats.numberRemoved.bufferViews + '\n');
process.stdout.write('Techniques removed: ' + stats.numberRemoved.techniques + '\n');
process.stdout.write('Textures removed: ' + stats.numberRemoved.textures + '\n');
process.stdout.write('Buffers removed: ' + stats.numberRemoved.buffers + '\n');
process.stdout.write('Programs removed: ' + stats.numberRemoved.programs + '\n');
process.stdout.write('Images removed: ' + stats.numberRemoved.images + '\n');
process.stdout.write('Samplers removed: ' + stats.numberRemoved.samplers + '\n');
process.stdout.write('Shaders removed: ' + stats.numberRemoved.shaders + '\n');
}
processFileToDisk(gltfPath, outputPath, options);
Binary file added doc/cesium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 4 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
'use strict';

module.exports = {
addDefaults : require('./lib/addDefaults'),
removeUnusedImages : require('./lib/removeUnusedImages'),
removeUnusedSamplers : require('./lib/removeUnusedSamplers'),
removeUnusedShaders : require('./lib/removeUnusedShaders'),
removeUnusedTechniques : require('./lib/removeUnusedTechniques'),
removeUnusedPrograms : require('./lib/removeUnusedPrograms'),
removeUnusedBuffers : require('./lib/removeUnusedBuffers'),
removeUnusedBufferViews : require('./lib/removeUnusedBufferViews'),
removeUnusedMaterials : require('./lib/removeUnusedMaterials'),
removeUnusedSkins : require('./lib/removeUnusedSkins'),
removeUnusedCameras : require('./lib/removeUnusedCameras'),
removeUnusedTextures : require('./lib/removeUnusedTextures'),
removeUnusedMeshes : require('./lib/removeUnusedMeshes'),
removeUnusedNodes : require('./lib/removeUnusedNodes'),
removeUnusedAccessors : require('./lib/removeUnusedAccessors'),
removeUnused : require('./lib/removeUnused'),
loadGltfUris : require('./lib/loadGltfUris'),
writeGltf : require('./lib/writeGltf'),
parseBinaryGltf : require('./lib/parseBinaryGltf'),
gltfPipeline : require('./lib/gltfPipeline'),
readGltf : require('./lib/readGltf'),
writeBinaryGltf : require('./lib/writeBinaryGltf'),
addPipelineExtras : require('./lib/addPipelineExtras'),
convertDagToTree : require('./lib/convertDagToTree'),
combineMeshes : require('./lib/combineMeshes'),
combinePrimitives : require('./lib/combinePrimitives'),
removeUnusedVertices : require('./lib/removeUnusedVertices'),
OptimizationStatistics : require('./lib/OptimizationStatistics')
};
writeGltf : require('./lib/writeGltf')
};
124 changes: 124 additions & 0 deletions lib/gltfPipeline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
'use strict';
var fs = require('fs');
var addDefaults = require('./addDefaults');
var removeUnusedImages = require('./removeUnusedImages');
var removeUnusedSamplers = require('./removeUnusedSamplers');
var removeUnusedShaders = require('./removeUnusedShaders');
var removeUnusedTechniques = require('./removeUnusedTechniques');
var removeUnusedPrograms = require('./removeUnusedPrograms');
var removeUnusedBuffers = require('./removeUnusedBuffers');
var removeUnusedBufferViews = require('./removeUnusedBufferViews');
var removeUnusedMaterials = require('./removeUnusedMaterials');
var removeUnusedSkins = require('./removeUnusedSkins');
var removeUnusedCameras = require('./removeUnusedCameras');
var removeUnusedTextures = require('./removeUnusedTextures');
var removeUnusedMeshes = require('./removeUnusedMeshes');
var removeUnusedNodes = require('./removeUnusedNodes');
var removeUnusedAccessors = require('./removeUnusedAccessors');
var removeUnused = require('./removeUnused');
var parseBinaryGltf = require('./parseBinaryGltf');
var addPipelineExtras = require('./addPipelineExtras');
var convertDagToTree = require('./convertDagToTree');
var combineMeshes = require('./combineMeshes');
var combinePrimitives = require('./combinePrimitives');
var removeUnusedVertices = require('./removeUnusedVertices');
var OptimizationStatistics = require('./OptimizationStatistics');
var writeGltf = require('./writeGltf');
var writeBinaryGltf = require('./writeBinaryGltf');
var readGltf = require('./readGltf');
var removePipelineExtras = require('./removePipelineExtras');
var loadGltfUris = require('./loadGltfUris');
var Cesium = require('cesium');
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;

module.exports = {
processJSON : processJSON,
processFile : processFile,
processJSONToDisk : processJSONToDisk,
processFileToDisk : processFileToDisk
};

function processJSON(gltf, options, callback) {
if (defined(options.resourcePath)) {
loadGltfUris(gltf, options.resourcePath, function(err, gltf) {
if (err) {
throw err;
}
processJSONWithExtras(gltf, options, callback);
});
} else {
// If a resourcePath is not passed in, assume JSON is already embedded
processJSONWithExtras(gltf, options, callback);
}
}

function processJSONWithExtras(gltfWithExtras, options, callback) {
var stats = new OptimizationStatistics();

addDefaults(gltfWithExtras, stats);

removeUnused(gltfWithExtras, stats);
if (options.printStats) {
printStats(stats);
}
convertDagToTree(gltfWithExtras);

removeUnusedVertices(gltfWithExtras);

combineMeshes(gltfWithExtras);
combinePrimitives(gltfWithExtras);

//Run removeUnused stage again after all pipeline stages have been run to remove objects that become unused
removeUnused(gltfWithExtras);

callback(gltfWithExtras);
}

function processFile(inputPath, options, callback) {
readGltf(inputPath, function(gltf) {
processJSONWithExtras(gltf, options, function(gltf) {
callback(gltf);
});
});
}

function writeFile(gltf, outputPath, options, callback) {
var isBinary = defaultValue(options.isBinary, false);
var isEmbedded = defaultValue(options.isEmbedded, true);
var createDirectory = defaultValue(options.createDirectory, true);
if (isBinary) {
writeBinaryGltf(gltf, outputPath, createDirectory, callback);
} else {
writeGltf(gltf, outputPath, isEmbedded, createDirectory, callback);
}
}

function processJSONToDisk(gltf, outputPath, options, callback) {
processJSON(gltf, options, function(gltf) {
writeFile(gltf, outputPath, options, callback);
});
}

function processFileToDisk(inputPath, outputPath, options, callback) {
processFile(inputPath, options, function(gltf) {
writeFile(gltf, outputPath, options, callback);
});
}

function printStats(stats) {
process.stdout.write('Nodes removed: ' + stats.numberRemoved.nodes + '\n');
process.stdout.write('Skins removed: ' + stats.numberRemoved.skins + '\n');
process.stdout.write('Cameras removed: ' + stats.numberRemoved.cameras + '\n');
process.stdout.write('Meshes removed: ' + stats.numberRemoved.meshes + '\n');
process.stdout.write('Accessors removed: ' + stats.numberRemoved.accessors + '\n');
process.stdout.write('Materials removed: ' + stats.numberRemoved.materials + '\n');
process.stdout.write('BufferViews removed: ' + stats.numberRemoved.bufferViews + '\n');
process.stdout.write('Techniques removed: ' + stats.numberRemoved.techniques + '\n');
process.stdout.write('Textures removed: ' + stats.numberRemoved.textures + '\n');
process.stdout.write('Buffers removed: ' + stats.numberRemoved.buffers + '\n');
process.stdout.write('Programs removed: ' + stats.numberRemoved.programs + '\n');
process.stdout.write('Images removed: ' + stats.numberRemoved.images + '\n');
process.stdout.write('Samplers removed: ' + stats.numberRemoved.samplers + '\n');
process.stdout.write('Shaders removed: ' + stats.numberRemoved.shaders + '\n');
}
Loading

0 comments on commit c4d3626

Please sign in to comment.