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 5 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
1 change: 1 addition & 0 deletions lib/compressTextureCoordinates.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var DeveloperError = Cesium.DeveloperError;
var ShaderSource = Cesium.ShaderSource;
var WebGLConstants = Cesium.WebGLConstants;
var defined = Cesium.defined;
// TODO: Switch this to Bluebird
var Promise = require('promise');

var byteLengthForComponentType = require('./byteLengthForComponentType');
Expand Down
29 changes: 15 additions & 14 deletions lib/getBinaryGltf.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var Cesium = require('cesium');
var Promise = require('bluebird');
var defined = Cesium.defined;
var defaultValue = Cesium.defaultValue;
var sizeOf = require('image-size');
Expand All @@ -18,13 +19,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 +41,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 +57,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 +67,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 +104,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
};
}
51 changes: 26 additions & 25 deletions lib/gltfPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var Cesium = require('cesium');
var Promise = require('bluebird');
var async = require('async');
var path = require('path');

var defaultValue = Cesium.defaultValue;

var OptimizationStatistics = require('./OptimizationStatistics');
Expand Down Expand Up @@ -108,52 +107,54 @@ function processJSONWithExtras(gltfWithExtras, options, callback) {
}

function processFile(inputPath, options, callback) {
readGltf(inputPath, options, function(gltf) {
processJSONWithExtras(gltf, options, function(gltf) {
writeSources(gltf, function() {
callback(gltf);
});
readGltf(inputPath, options)
.then(function(gltf) {
processJSONWithExtras(gltf, options, function(gltf) {
writeSources(gltf, function() {
callback(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)
return loadGltfUris(gltf, options)
.then(function() {
processJSONWithExtras(gltf, options, function(gltf) {
writeFile(gltf, outputPath, options, callback);
return writeFile(gltf, outputPath, options);
});
})
.catch(function(err) {
}).catch(function(err) {
throw err;
});
}

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) {
processJSONWithExtras(gltf, options, function() {
writeFile(gltf, outputPath, options);
});
});
});
}

function printStats(stats) {
Expand Down
41 changes: 19 additions & 22 deletions lib/readGltf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';
var path = require('path');
var Promise = require('bluebird');
var fs = require('fs');
var readFile = Promise.promisify(fs.readFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this fsReadFile for consistency with the node guide.

var parseBinaryGltf = require('./parseBinaryGltf');
var addPipelineExtras = require('./addPipelineExtras');
var loadGltfUris = require('./loadGltfUris');
Expand All @@ -10,7 +12,7 @@ var DeveloperError = Cesium.DeveloperError;

module.exports = readGltf;

function readGltf(gltfPath, options, callback) {
function readGltf(gltfPath, options) {
if (!defined(gltfPath)) {
throw new DeveloperError('Input path is undefined.');
}
Expand All @@ -24,26 +26,21 @@ function readGltf(gltfPath, options, callback) {
if (fileExtension !== '.glb' && fileExtension !== '.gltf') {
throw new DeveloperError('Invalid glTF file.');
}

fs.readFile(gltfPath, function(err, data) {
if (err) {
var gltf;
return readFile(gltfPath)
.then(function(data) {
if (fileExtension === '.glb') {
gltf = parseBinaryGltf(data);
}
else if (fileExtension === '.gltf') {
gltf = JSON.parse(data);
addPipelineExtras(gltf);
}
return loadGltfUris(gltf, options);
}).then(function() {
return gltf;
})
.catch(function(err) {
throw err;
}

var gltf;
if (fileExtension === '.glb') {
gltf = parseBinaryGltf(data);
}
else if (fileExtension === '.gltf') {
gltf = JSON.parse(data);
addPipelineExtras(gltf);
}
loadGltfUris(gltf, options)
.then(function() {
callback(gltf);
})
.catch(function(err) {
throw err;
});
});
});
}
49 changes: 17 additions & 32 deletions lib/writeBinaryGltf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var fsExtra = require('fs-extra');
var Promise = require('bluebird');
var fsExtra = Promise.promisifyAll(require('fs-extra'));
var path = require('path');
var async = require('async');
var getBinaryGltf = require('./getBinaryGltf');
var writeSource = require('./writeSource');
var Cesium = require('cesium');
Expand All @@ -10,50 +10,35 @@ var DeveloperError = Cesium.DeveloperError;

module.exports = writeBinaryGltf;

function writeBinaryGltf(gltf, options, callback) {
function writeBinaryGltf(gltf, options) {
var outputPath = options.outputPath;
var embed = options.embed;
var embedImage = options.embedImage;
var createDirectory = options.createDirectory;

if (!defined(outputPath)) {
throw new DeveloperError('Output path is undefined.');
}

var outputExtension = path.extname(outputPath);
if (outputExtension !== '.glb') {
throw new DeveloperError('Invalid output path extension.');
}

// Create the output directory if specified
if (createDirectory) {
outputPath = path.join(path.dirname(outputPath), 'output', path.basename(outputPath));
}

var basePath = path.dirname(outputPath);
async.each(['images', 'shaders'], function (name, asyncCallback) {
writeSource(gltf, basePath, name, embed, embedImage, asyncCallback);
}, function (err) {
if (err) {
if (callback) {
callback(err);
} else {
throw err;
}
} else {
var glb = getBinaryGltf(gltf, embed, embedImage, function (header, scene, body) {
fsExtra.outputFile(outputPath, glb, function (err) {
if (err) {
if (callback) {
callback(err);
} else {
throw err;
}
} else if (callback) {
callback(undefined, header, scene, body);
}
});
});
}
});

var writeSources = [
writeSource(gltf, basePath, 'images', embed, embedImage),
writeSource(gltf, basePath, 'shaders', embed, embedImage)
];

return Promise.all(writeSources)
.then(function() {
var glbData = getBinaryGltf(gltf, embed, embedImage);
var glb = glbData.glb;
return fsExtra.outputFile(outputPath, glb);
});
}
46 changes: 23 additions & 23 deletions lib/writeGltf.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
'use strict';
var fsExtra = require('fs-extra');
var path = require('path');
var async = require('async');
var Promise = require('bluebird');
var fsExtra = Promise.promisifyAll(require('fs-extra'));

var writeSource = require('./writeSource');
// var outputJson = Promise.promisify(fsExtra.outputJson);

var removePipelineExtras = require('./removePipelineExtras');
var Cesium = require('cesium');
var defined = Cesium.defined;
var DeveloperError = Cesium.DeveloperError;

module.exports = writeGltf;

function writeGltf(gltf, outputPath, embed, embedImage, createDirectory, callback) {
function writeGltf(gltf, options) {
var outputPath = options.outputPath;
var embed = options.embed;
var embedImage = options.embedImage;
var createDirectory = options.createDirectory;

if (!defined(outputPath)) {
throw new DeveloperError('Output path is undefined.');
}
Expand All @@ -19,31 +27,23 @@ function writeGltf(gltf, outputPath, embed, embedImage, createDirectory, callbac
if (outputExtension !== '.gltf') {
throw new DeveloperError('Invalid output path extension.');
}

// Create the output directory if specified
if (createDirectory) {
outputPath = path.join(path.dirname(outputPath), 'output', path.basename(outputPath));
}
var basePath = path.dirname(outputPath);

async.each(['buffers', 'images', 'shaders'], function(name, asyncCallback) {
writeSource(gltf, basePath, name, embed, embedImage, asyncCallback);
}, function(err) {
if (err) {
if (callback) {
callback(err);
} else {
throw err;
}
} else {
var writeSources = [
writeSource(gltf, basePath, 'buffers', embed, embedImage),
writeSource(gltf, basePath, 'images', embed, embedImage),
writeSource(gltf, basePath, 'shaders', embed, embedImage)
];


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra line.

return Promise.all(writeSources)
.then(function() {
removePipelineExtras(gltf);
fsExtra.outputJson(outputPath, gltf, function (err) {
if (err) {
throw err;
}
if (callback) {
callback();
}
});
}
});
return fsExtra.outputJson(outputPath, gltf);
});
}
Loading