diff --git a/lib/gltfPipeline.js b/lib/gltfPipeline.js index 866882b6..2dba0f15 100644 --- a/lib/gltfPipeline.js +++ b/lib/gltfPipeline.js @@ -1,4 +1,5 @@ 'use strict'; +var async = require('async'); var addDefaults = require('./addDefaults'); var addPipelineExtras = require('./addPipelineExtras'); var removeUnused = require('./removeUnused'); @@ -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; @@ -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); + }); + }); }); } @@ -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); + }); }); }); } @@ -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); + }); }); } diff --git a/lib/writeGltf.js b/lib/writeGltf.js index 8932fe2c..9f59ab30 100755 --- a/lib/writeGltf.js +++ b/lib/writeGltf.js @@ -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)); } diff --git a/lib/writeSource.js b/lib/writeSource.js index 4468a821..a57c2d42 100644 --- a/lib/writeSource.js +++ b/lib/writeSource.js @@ -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) { @@ -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); diff --git a/specs/lib/gltfPipelineSpec.js b/specs/lib/gltfPipelineSpec.js index e5394b0c..bbe92a4f 100644 --- a/specs/lib/gltfPipelineSpec.js +++ b/specs/lib/gltfPipelineSpec.js @@ -107,8 +107,33 @@ 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(); }); }); @@ -116,10 +141,9 @@ describe('gltfPipeline', function() { 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;