Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async to promises. writeGltf, writeBinaryGltf, writeFile -- callbacks to Promises #135

Merged
merged 22 commits into from
Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/bin/env node
'use strict';
var Cesium = require('cesium');
var argv = require('yargs').argv;
var path = require('path');
var Cesium = require('cesium');

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

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

var processFileToDisk = gltfPipeline.processFileToDisk;

if (process.argv.length < 3 || defined(argv.h) || defined(argv.help)) {
Expand Down Expand Up @@ -69,4 +72,5 @@ var options = {
optimizeForCesium : optimizeForCesium
};

// Node automatically waits for all promises to terminate
processFileToDisk(gltfPath, outputPath, options);
5 changes: 3 additions & 2 deletions lib/compressTextureCoordinates.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
'use strict';
var Cesium = require('cesium');
var Promise = require('bluebird');

var AttributeCompression = Cesium.AttributeCompression;
var Cartesian2 = Cesium.Cartesian2;
var DeveloperError = Cesium.DeveloperError;
var ShaderSource = Cesium.ShaderSource;
var WebGLConstants = Cesium.WebGLConstants;
var defined = Cesium.defined;
var Promise = require('promise');

var byteLengthForComponentType = require('./byteLengthForComponentType');
var findAccessorMinMax = require('./findAccessorMinMax');
var getAccessorByteStride = require('./getAccessorByteStride');
var getAccessorsForSemantic = require('./getAccessorsForSemantic');
var numberOfComponentsForType = require('./numberOfComponentsForType');
var readBufferComponentType = require('./readBufferComponentType');
var writeBufferComponentType = require('./writeBufferComponentType');
var uninterleaveAndPackBuffers = require('./uninterleaveAndPackBuffers');
var writeBufferComponentType = require('./writeBufferComponentType');

module.exports = compressTextureCoordinates;

Expand Down
5 changes: 4 additions & 1 deletion lib/encodeImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ function encodeImages(gltf) {
}
}
}
return Promise.all(promises);
return Promise.all(promises)
.then(function() {
return gltf;
});
}

function loadImageSource(pipelineExtras, mime) {
Expand Down
2 changes: 1 addition & 1 deletion lib/getAccessorsForSemantic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
var Promise = require('promise');
var Promise = require('bluebird');

module.exports = getAccessorsForSemantic;

Expand Down
34 changes: 18 additions & 16 deletions lib/getBinaryGltf.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
var Cesium = require('cesium');
var mime = require('mime');
var sizeOf = require('image-size');

var defined = Cesium.defined;
var defaultValue = Cesium.defaultValue;
var sizeOf = require('image-size');
var mime = require('mime');

var mergeBuffers = require('./mergeBuffers');
var removePipelineExtras = require('./removePipelineExtras');

Expand All @@ -18,13 +20,13 @@ function updateBinaryObject(gltf, pipelineExtras, name, state) {
if (objects.hasOwnProperty(objectId)) {
var object = objects[objectId];

//Update object with binary format
// Update object with binary format
object.uri = 'data:,';
object.extensions = defaultValue(object.extensions, {});
object.extensions.KHR_binary_glTF = defaultValue(object.extensions.KHR_binary_glTF, {});
var KHR_binary_glTF = object.extensions.KHR_binary_glTF;

//Create a bufferView based on the byte length and current offset
// Create a bufferView based on the byte length and current offset
var bufferViewKeys = Object.keys(bufferViews);
while (bufferViewKeys.indexOf('binary_bufferView' + state.currentBinaryView) !== -1) {
state.currentBinaryView++;
Expand All @@ -40,10 +42,10 @@ function updateBinaryObject(gltf, pipelineExtras, name, state) {
};
state.offset += objectSource.length;

//Append the object source to the binary body
// Append the object source to the binary body
pipelineExtras.source = Buffer.concat([pipelineExtras.source, objectSource]);

//Add additional properties for images
// Add additional properties for images
if (name === 'images') {
KHR_binary_glTF.mimeType = mime.lookup(object.extras._pipeline.extension);

Expand All @@ -56,7 +58,7 @@ function updateBinaryObject(gltf, pipelineExtras, name, state) {
}
}

function getBinaryGltf(gltf, embed, embedImage, callback) {
function getBinaryGltf(gltf, embed, embedImage) {
// Create the special binary buffer from the existing buffers
gltf.bufferViews = defaultValue(gltf.bufferViews, {});
gltf.buffers = defaultValue(gltf.buffers, {});
Expand All @@ -66,8 +68,8 @@ function getBinaryGltf(gltf, embed, embedImage, callback) {
var currentOffset = buffers.binary_glTF.byteLength;
var pipelineExtras = buffers.binary_glTF.extras._pipeline;
var state = {
offset : currentOffset,
currentBinaryView : 0
offset: currentOffset,
currentBinaryView: 0
};

if (embed) {
Expand Down Expand Up @@ -103,11 +105,11 @@ function getBinaryGltf(gltf, embed, embedImage, callback) {
// Create scene buffer and overall buffer
var scene = new Buffer(sceneString);
var glb = Buffer.concat([header, scene, body], glbLength);
if (callback) {
process.nextTick(function () {
callback(header, scene, body);
});
}
return glb;

return {
glb : glb,
header : header,
scene : scene,
body : body
};
}
130 changes: 64 additions & 66 deletions lib/gltfPipeline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
var Cesium = require('cesium');
var Promise = require('bluebird');
var async = require('async');
var path = require('path');

var defaultValue = Cesium.defaultValue;
Expand Down Expand Up @@ -38,32 +37,32 @@ module.exports = {
processFileToDisk : processFileToDisk
};

function writeSources(gltf, callback) {
function writeSources(gltf) {
var embed = true;
var embedImage = true;
async.each(['buffers', 'images', 'shaders'], function(name, asyncCallback) {
writeSource(gltf, undefined, name, embed, embedImage, asyncCallback);
}, function() {
callback();
});
var writeSourcePromises = [
writeSource(gltf, undefined, 'buffers', embed, embedImage),
writeSource(gltf, undefined, 'images', embed, embedImage),
writeSource(gltf, undefined, 'shaders', embed, embedImage)
];
return Promise.all(writeSourcePromises)
.then(function() {
return gltf;
});
}

function processJSON(gltf, options, callback) {
function processJSON(gltf, options) {
addPipelineExtras(gltf);
loadGltfUris(gltf, options)
.then(function() {
processJSONWithExtras(gltf, options, function() {
writeSources(gltf, function() {
callback(gltf);
});
});
return loadGltfUris(gltf, options)
.then(function(gltf) {
return processJSONWithExtras(gltf, options);
})
.catch(function(err) {
throw err;
.then(function(gltf) {
return writeSources(gltf);
});
}

function processJSONWithExtras(gltfWithExtras, options, callback) {
function processJSONWithExtras(gltfWithExtras, options) {
var stats = new OptimizationStatistics();
addDefaults(gltfWithExtras, options);
removeUnused(gltfWithExtras, stats);
Expand Down Expand Up @@ -104,76 +103,75 @@ function processJSONWithExtras(gltfWithExtras, options, callback) {
waitForStages.push(compressIntegerAccessors(gltfWithExtras, {
semantics : ["JOINT"]
}));
Promise.all(waitForStages).then(function() {
if (options.quantize) {
var quantizedOptions = {
findMinMax : true,
exclude : [
'JOINT',
'_OCCLUSION'
]
};
if (options.compressTextureCoordinates) {
quantizedOptions.exclude.push('TEXCOORD');
return Promise.all(waitForStages)
.then(function() {
if (options.quantize) {
var quantizedOptions = {
findMinMax : true,
exclude : [
'JOINT',
'_OCCLUSION'
]
};
if (options.compressTextureCoordinates) {
quantizedOptions.exclude.push('TEXCOORD');
}
quantizeAttributes(gltfWithExtras, quantizedOptions);
}
quantizeAttributes(gltfWithExtras, quantizedOptions);
}
// Remove duplicates again after all stages to minimize the buffer size
mergeDuplicateVertices(gltfWithExtras);
encodeImages(gltfWithExtras)
.then(function() {
callback(gltfWithExtras);
});
// Remove duplicates again after all stages to minimize the buffer size
mergeDuplicateVertices(gltfWithExtras);
return encodeImages(gltfWithExtras);
});
}

function processFile(inputPath, options, callback) {
readGltf(inputPath, options, function(gltf) {
processJSONWithExtras(gltf, options, function(gltf) {
writeSources(gltf, function() {
callback(gltf);
});
function processFile(inputPath, options) {
return readGltf(inputPath, options)
.then(function(gltf) {
return processJSONWithExtras(gltf, options);
})
.then(function(gltf) {
return writeSources(gltf);
});
});
}

function writeFile(gltf, outputPath, options, callback) {
function writeFile(gltf, outputPath, options) {
var fileExtension = path.extname(outputPath);
var binary = defaultValue(options.binary, false);
var embed = defaultValue(options.embed, true);
var embedImage = defaultValue(options.embedImage, true);
var createDirectory = defaultValue(options.createDirectory, true);
var writeOptions = {
outputPath : outputPath,
embed : embed,
embedImage : embedImage,
createDirectory : createDirectory
};

if (binary || fileExtension === '.glb') {
writeBinaryGltf(gltf, {
outputPath : outputPath,
embed : embed,
embedImage : embedImage,
createDirectory : createDirectory
}, callback);
} else {
writeGltf(gltf, outputPath, embed, embedImage, createDirectory, callback);
return writeBinaryGltf(gltf, writeOptions);
}
return writeGltf(gltf, writeOptions);
}

function processJSONToDisk(gltf, outputPath, options, callback) {
function processJSONToDisk(gltf, outputPath, options) {
addPipelineExtras(gltf);
loadGltfUris(gltf, options)
.then(function() {
processJSONWithExtras(gltf, options, function(gltf) {
writeFile(gltf, outputPath, options, callback);
});
return loadGltfUris(gltf, options)
.then(function(gltf) {
return processJSONWithExtras(gltf, options);
})
.catch(function(err) {
throw err;
.then(function(gltf) {
return writeFile(gltf, outputPath, options);
});
}

function processFileToDisk(inputPath, outputPath, options, callback) {
readGltf(inputPath, options, function(gltf) {
processJSONWithExtras(gltf, options, function(gltf) {
writeFile(gltf, outputPath, options, callback);
function processFileToDisk(inputPath, outputPath, options) {
return readGltf(inputPath, options)
.then(function(gltf) {
return processJSONWithExtras(gltf, options);
})
.then(function(gltf) {
return writeFile(gltf, outputPath, options);
});
});
}

function printStats(stats) {
Expand Down
1 change: 1 addition & 0 deletions lib/loadGltfUris.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function loadGltfUris(gltf, options) {
return Promise.all(loadURIs)
.then(function() {
gltf.extras._pipeline.jimpScratch = new Jimp(1, 1);
return gltf;
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/octEncodeNormals.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var DeveloperError = Cesium.DeveloperError;
var ShaderSource = Cesium.ShaderSource;
var WebGLConstants = Cesium.WebGLConstants;
var defined = Cesium.defined;
var Promise = require('promise');
var Promise = require('bluebird');

var byteLengthForComponentType = require('./byteLengthForComponentType');
var findAccessorMinMax = require('./findAccessorMinMax');
Expand Down
Loading