Skip to content

Commit

Permalink
Make loadGltfUris use promises
Browse files Browse the repository at this point in the history
  • Loading branch information
lasalvavida committed Jul 12, 2016
1 parent 9aad592 commit 8ce9cc6
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 150 deletions.
23 changes: 11 additions & 12 deletions lib/gltfPipeline.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';
var Cesium = require('cesium');
var Promise = require('bluebird');
var async = require('async');
var path = require('path');

var defaultValue = Cesium.defaultValue;
var Promise = require('promise');

var OptimizationStatistics = require('./OptimizationStatistics');
var async = require('async');
var path = require('path');
var addDefaults = require('./addDefaults');
var addPipelineExtras = require('./addPipelineExtras');
var cacheOptimization = require('./cacheOptimization');
Expand Down Expand Up @@ -45,15 +46,14 @@ function writeSources(gltf, callback) {

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

Expand Down Expand Up @@ -135,13 +135,12 @@ function writeFile(gltf, outputPath, options, callback) {

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

Expand Down
213 changes: 103 additions & 110 deletions lib/loadGltfUris.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,99 @@
'use strict';
var Cesium = require('cesium');
var Jimp = require('jimp');
var Promise = require('bluebird');
var dataUriToBuffer = require('data-uri-to-buffer');
var fs = require('fs');
var path = require('path');
var Cesium = require('cesium');

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

var isDataUri = require('./isDataUri');
var dataUriToBuffer = require('data-uri-to-buffer');
var async = require('async');
var Jimp = require('jimp');

module.exports = loadGltfUris;

function loadGltfUris(gltf, options, callback) {
var basePath = options.basePath;

var wrappedCallback = function(err, gltf) {
gltf.extras._pipeline.jimpScratch = new Jimp(1, 1, function(jimpErr, image) {
if (jimpErr) {
throw jimpErr;
}
gltf.extras._pipeline.jimpScratch = image;
callback(err, gltf);
function loadGltfUris(gltf, options) {
return new Promise(function(resolve, reject) {
var basePath = options.basePath;
var loadURIs = [
loadURI(gltf, basePath, 'buffers'),
loadURI(gltf, basePath, 'images'),
loadURI(gltf, basePath, 'shaders')
];
Promise.all(loadURIs).then(function() {
gltf.extras._pipeline.jimpScratch = new Jimp(1, 1, function(err) {
if (defined(err)) {
reject(err);
} else {
resolve();
}
});
}).catch(function(err) {
reject(err);
});
};

async.each(['buffers', 'images', 'shaders'], function(name, asyncCallback) {
loadURI(gltf, basePath, name, asyncCallback);
}, function(err) {
wrappedCallback(err, gltf);
});
}

function loadURI(gltf, basePath, name, callback) {

function loadURI(gltf, basePath, name) {
var objects = gltf[name];

//Iterate through each object and load its uri
//Iterate through each object and get a promise to load its uri
var promises = [];
if (defined(objects)) {
var ids = Object.keys(objects);
async.each(ids, function(id, asyncCallback) {
var object = objects[id];
var uri = object.uri;
object.extras = defaultValue(object.extras, {});
object.extras._pipeline = defaultValue(object.extras._pipeline, {});

//Load the uri into the extras object based on the uri type
if (isDataUri(uri)) {
if (!defined(object.extras._pipeline.source)) {
object.extras._pipeline.source = dataUriToBuffer(uri);
}

if (!defined(object.extras._pipeline.extension)) {
switch (name) {
case 'buffers':
object.extras._pipeline.extension = '.bin';
break;
case 'images':
object.extras._pipeline.extension = getImageDataUriFormat(uri);
break;
case 'shaders':
object.extras._pipeline.extension = '.glsl';
break;
for (var id in objects) {
if (objects.hasOwnProperty(id)) {
var object = objects[id];
var uri = object.uri;
object.extras = defaultValue(object.extras, {});
object.extras._pipeline = defaultValue(object.extras._pipeline, {});
//Load the uri into the extras object based on the uri type
if (isDataUri(uri)) {
if (!defined(object.extras._pipeline.source)) {
object.extras._pipeline.source = dataUriToBuffer(uri);
}
}

if (name === 'images') {
generateJimpImage(object, function() {
process.nextTick(function() {
asyncCallback();
});
});
} else {
process.nextTick(function() {
asyncCallback();
});
}
}
else {
var uriPath = uri;
if (!path.isAbsolute(uriPath)) {
if (!defined(basePath)) {
throw new DeveloperError('glTF model references external files but no basePath is supplied');
if (!defined(object.extras._pipeline.extension)) {
switch (name) {
case 'buffers':
object.extras._pipeline.extension = '.bin';
break;
case 'images':
object.extras._pipeline.extension = getImageDataUriFormat(uri);
break;
case 'shaders':
object.extras._pipeline.extension = '.glsl';
break;
}
}
uriPath = path.join(basePath, uriPath);
}

fs.readFile(uriPath, function (err, data) {
if (err) {
asyncCallback(err);
if (name === 'images') {
promises.push(generateJimpImage(object));
}
else {
object.extras._pipeline.source = data;
object.extras._pipeline.extension = path.extname(uri);
if (name === 'images') {
generateJimpImage(object, asyncCallback);
} else {
asyncCallback();
} else {
var uriPath = uri;
if (!path.isAbsolute(uriPath)) {
if (!defined(basePath)) {
throw new DeveloperError('glTF model references external files but no basePath is supplied');
}
uriPath = path.join(basePath, uriPath);
}
});
}
}, function(err) {
if (err) {
if (callback) {
callback(err);
promises.push(readFromFile(object, name, uriPath));
}
else{
throw err;
}
}
else if (callback) {
callback();
}
});
}
else {
if (callback) {
callback();
}
}
return Promise.all(promises);
}

//Return the extension of the data uri
/**
* Return the extension of the data uri
* @param {string} uri
* @returns {string}
*/
function getImageDataUriFormat(uri) {
var extension = uri.match('\/([^)]+)\;');
if (extension === null) {
throw new DeveloperError('No available image file extension');
}

return '.' + extension[1];
}

Expand All @@ -147,20 +112,48 @@ function isTransparent(image) {
return false;
}

//Generate a jimp image for png, jpeg, or bmp
function generateJimpImage(object, callback) {
function readFromFile(object, name, uriPath) {
return new Promise(function(resolve, reject) {
fs.readFile(uriPath, function (err, data) {
if (defined(err)) {
reject(err);
} else {
object.extras._pipeline.source = data;
object.extras._pipeline.extension = path.extname(uriPath);
if (name === 'images') {
generateJimpImage(object).then(function () {
resolve();
}).catch(function (err) {
reject(err);
});
} else {
resolve();
}
}
});
});
}

/**
* Generate a jimp image for png, jpeg, or bmp
*
* @param {Object} object
* @returns {Promise}
*/
function generateJimpImage(object) {
var pipelineExtras = object.extras._pipeline;
if (pipelineExtras.extension === '.gif') {
throw new DeveloperError('gltf pipeline image processing does not currently support gifs.');
}
Jimp.read(pipelineExtras.source, function(jimpErr, image) {
if (jimpErr) {
throw jimpErr;
}

pipelineExtras.jimpImage = image;
pipelineExtras.imageChanged = false;
pipelineExtras.transparent = isTransparent(image);
callback();
return new Promise(function(resolve, reject) {
Jimp.read(pipelineExtras.source, function (err, image) {
if (defined(err)) {
reject(err);
}
pipelineExtras.jimpImage = image;
pipelineExtras.imageChanged = false;
pipelineExtras.transparent = isTransparent(image);
resolve();
});
});
}
7 changes: 3 additions & 4 deletions lib/readGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ function readGltf(gltfPath, options, callback) {
gltf = JSON.parse(data);
addPipelineExtras(gltf);
}
loadGltfUris(gltf, options, function(err, gltf) {
if (err) {
throw err;
}
loadGltfUris(gltf, options).then(function() {
callback(gltf);
}).catch(function(err) {
throw err;
});
});
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"version": "0.0.0",
"description": "Content pipeline tools for optimizing glTF assets.",
"license": "Apache-2.0",
"contributors": [{
"name": "Richard Lee, Analytical Graphics, Inc., and Contributors",
"url": "https://github.com/AnalyticalGraphicsInc/gltf-pipeline/graphs/contributors"
}],
"contributors": [
{
"name": "Richard Lee, Analytical Graphics, Inc., and Contributors",
"url": "https://github.com/AnalyticalGraphicsInc/gltf-pipeline/graphs/contributors"
}
],
"keywords": [
"glTF",
"WebGL"
Expand Down Expand Up @@ -36,6 +38,7 @@
},
"dependencies": {
"async": "^1.5.2",
"bluebird": "^3.4.1",
"buffer-equal": "^1.0.0",
"cesium": "^1.22.0",
"data-uri-to-buffer": "0.0.4",
Expand Down
7 changes: 3 additions & 4 deletions specs/lib/addDefaultsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,13 @@ describe('addDefaults', function() {
};

addPipelineExtras(gltf);
loadGltfUris(gltf, {}, function(err, gltf) {
if (err) {
throw err;
}
loadGltfUris(gltf, {}).then(function() {
addDefaults(gltf);
var technique = gltf.techniques[Object.keys(gltf.techniques)[0]];
expect(technique.states).toEqual(alphaBlendState);
done();
}).catch(function(err) {
throw err;
});
});

Expand Down
4 changes: 2 additions & 2 deletions specs/lib/encodeImagesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('encodeImages', function() {
var gltfClone = clone(gltf);

addPipelineExtras(gltf);
loadGltfUris(gltfClone, options, function() {
loadGltfUris(gltfClone, options).then(function() {
var pipelineExtras0001 = gltfClone.images.Image0001.extras._pipeline;
var pipelineExtras0002 = gltfClone.images.Image0002.extras._pipeline;

Expand All @@ -50,7 +50,7 @@ describe('encodeImages', function() {
var gltfClone = clone(gltf);

addPipelineExtras(gltf);
loadGltfUris(gltfClone, options, function() {
loadGltfUris(gltfClone, options).then(function() {
var pipelineExtras0001 = gltfClone.images.Image0001.extras._pipeline;
var jimpImage001 = pipelineExtras0001.jimpImage;
jimpImage001.resize(10, 10, Jimp.RESIZE_BEZIER);
Expand Down

0 comments on commit 8ce9cc6

Please sign in to comment.