Skip to content

Commit

Permalink
Merge pull request #94 from AnalyticalGraphicsInc/notWritingSource
Browse files Browse the repository at this point in the history
Fix bug where gltfPipeline was not writingSources in all cases
  • Loading branch information
lilleyse authored Jun 22, 2016
2 parents 1dd869d + d6970eb commit 9356817
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
38 changes: 32 additions & 6 deletions lib/gltfPipeline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var async = require('async');
var addDefaults = require('./addDefaults');
var addPipelineExtras = require('./addPipelineExtras');
var removeUnused = require('./removeUnused');
Expand All @@ -16,6 +17,7 @@ var loadGltfUris = require('./loadGltfUris');
var quantizeAttributes = require('./quantizeAttributes');
var cacheOptimizeIndices = require('./cacheOptimizeIndices');
var encodeImages = require('./encodeImages');
var writeSource = require('./writeSource');
var Cesium = require('cesium');
var defaultValue = Cesium.defaultValue;

Expand All @@ -26,13 +28,27 @@ module.exports = {
processFileToDisk : processFileToDisk
};

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

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

Expand Down Expand Up @@ -71,7 +87,9 @@ function processJSONWithExtras(gltfWithExtras, options, callback) {
function processFile(inputPath, options, callback) {
readGltf(inputPath, options, function(gltf) {
processJSONWithExtras(gltf, options, function(gltf) {
callback(gltf);
writeSources(gltf, function() {
callback(gltf);
});
});
});
}
Expand All @@ -89,14 +107,22 @@ function writeFile(gltf, outputPath, options, callback) {
}

function processJSONToDisk(gltf, outputPath, options, callback) {
processJSON(gltf, options, function(gltf) {
writeFile(gltf, outputPath, options, callback);
addPipelineExtras(gltf);
loadGltfUris(gltf, options, function(err, gltf) {
if (err) {
throw err;
}
processJSONWithExtras(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);
readGltf(inputPath, options, function(gltf) {
processJSONWithExtras(gltf, options, function(gltf) {
writeFile(gltf, outputPath, options, callback);
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/writeGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var removePipelineExtras = require('./removePipelineExtras');
module.exports = writeGltf;

function writeGltf(gltf, outputPath, embed, embedImage, createDirectory, callback) {
//Create the output directory if specified
// Create the output directory if specified
if (createDirectory) {
outputPath = path.join(path.dirname(outputPath), 'output', path.basename(outputPath));
}
Expand Down
4 changes: 2 additions & 2 deletions lib/writeSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = writeSource;
function writeSource(gltf, basePath, name, embed, embedImage, callback) {
var objects = gltf[name];

//Iterate through each object and write out its uri
// Iterate through each object and write out its uri
if (defined(objects)) {
var ids = Object.keys(objects);
async.each(ids, function(id, asyncCallback) {
Expand All @@ -24,7 +24,7 @@ function writeSource(gltf, basePath, name, embed, embedImage, callback) {
var extension = object.extras._pipeline.extension;

// Write the source object as a data or file uri depending on the embed flag
if (embed && (embedImage || name !== 'images')) {
if (embed && (embedImage || name !== 'images') || !defined(basePath)) {
var sourceDataUri = new dataUri();
if (name === 'shaders') {
sourceDataUri.format('.txt', source);
Expand Down
30 changes: 27 additions & 3 deletions specs/lib/gltfPipelineSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,43 @@ describe('gltfPipeline', function() {
createDirectory : false
};
readGltf(gltfPath, options, function(gltf) {
var options = { basePath : path.dirname(gltfPath) };
processJSONToDisk(gltf, outputPath, options, function() {
expect(path.normalize(spy.calls.first().args[0])).toEqual(path.normalize('./output/'));
expect(path.normalize(spy.calls.first().args[0])).toEqual(path.normalize('output/output'));
done();
});
});
});

it('will write sources from JSON', function(done) {
var options = {};
readGltf(gltfEmbeddedPath, options, function (gltf) {
var initialUri = gltf['buffers'].CesiumTexturedBoxTest.uri;
processJSON(gltf, options, function () {
var finalUri = gltf['buffers'].CesiumTexturedBoxTest.uri;
expect(initialUri).not.toEqual(finalUri);
done();
});
});
});

it('will write sources from file', function(done) {
var options = {};
readGltf(gltfEmbeddedPath, options, function (gltf) {
var initialUri = gltf['buffers'].CesiumTexturedBoxTest.uri;
processFile(gltfEmbeddedPath, options, function (gltfFinal) {
var finalUri = gltfFinal['buffers'].CesiumTexturedBoxTest.uri;
expect(initialUri).not.toEqual(finalUri);
done();
});
});
});

it('will add image processing extras if this is a pipeline with image processing', function(done) {
var options = {
imageProcess: true
imageProcess : true
};
readGltf(gltfEmbeddedPath, options, function(gltf) {
var gltfCopy = clone(gltf);
processJSON(gltf, options, function (gltf) {
expect(gltf).toBeDefined();
var images = gltf.images;
Expand Down

0 comments on commit 9356817

Please sign in to comment.