Merge pull request #21 from AnalyticalGraphicsInc/promises-everywhere

Convert callbacks to promises
This commit is contained in:
Sean Lilley 2016-07-25 18:57:27 -07:00 committed by GitHub
commit 60ba99d4ec
9 changed files with 427 additions and 401 deletions

View File

@ -15,9 +15,10 @@ var convert = obj2gltf.convert;
var options = { var options = {
embedImage : false // Don't embed image in the converted glTF embedImage : false // Don't embed image in the converted glTF
} }
convert('model.obj', 'model.gltf', options, function() { convert('model.obj', 'model.gltf', options)
.then(function() {
console.log('Converted model'); console.log('Converted model');
}); });
``` ```
Using obj2gltf as a command-line tool: Using obj2gltf as a command-line tool:

View File

@ -40,6 +40,10 @@ var options = {
ao : ao ao : ao
}; };
convert(objFile, outputPath, options, function() { convert(objFile, outputPath, options)
.then(function() {
console.timeEnd('Total'); console.timeEnd('Total');
}); })
.catch(function(err) {
console.log(err);
});

View File

@ -9,7 +9,7 @@ var defaultValue = Cesium.defaultValue;
module.exports = convert; module.exports = convert;
function convert(objFile, outputPath, options, done) { function convert(objFile, outputPath, options) {
options = defaultValue(options, {}); options = defaultValue(options, {});
var binary = defaultValue(options.binary, false); var binary = defaultValue(options.binary, false);
var embed = defaultValue(options.embed, true); var embed = defaultValue(options.embed, true);
@ -37,24 +37,21 @@ function convert(objFile, outputPath, options, done) {
extension = binary ? '.glb' : '.gltf'; extension = binary ? '.glb' : '.gltf';
var gltfFile = path.join(outputPath, modelName + extension); var gltfFile = path.join(outputPath, modelName + extension);
parseObj(objFile, inputPath, function(data) { return parseObj(objFile, inputPath)
createGltf(data, inputPath, modelName, function(gltf) { .then(function(data) {
return createGltf(data, inputPath, modelName);
})
.then(function(gltf) {
var aoOptions = ao ? {} : undefined; var aoOptions = ao ? {} : undefined;
var options = { var options = {
binary : binary, binary: binary,
embed : embed, embed: embed,
embedImage : embedImage, embedImage: embedImage,
quantize : quantize, quantize: quantize,
aoOptions : aoOptions, aoOptions: aoOptions,
createDirectory : false, createDirectory: false,
basePath : inputPath basePath: inputPath
}; };
gltfPipeline.processJSONToDisk(gltf, gltfFile, options, function(error) { return gltfPipeline.processJSONToDisk(gltf, gltfFile, options);
if (error) {
throw error;
}
done();
});
});
}); });
} }

View File

@ -1,14 +1,18 @@
"use strict"; "use strict";
var Cesium = require('cesium');
var Promise = require('bluebird');
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require('path'); var path = require('path');
var Cesium = require('cesium');
var defined = Cesium.defined; var defined = Cesium.defined;
var defaultValue = Cesium.defaultValue; var defaultValue = Cesium.defaultValue;
var WebGLConstants = Cesium.WebGLConstants; var WebGLConstants = Cesium.WebGLConstants;
var fsWriteFile = Promise.promisify(fs.writeFile);
module.exports = createGltf; module.exports = createGltf;
function createGltf(data, inputPath, modelName, done) { function createGltf(data, inputPath, modelName) {
var vertexCount = data.vertexCount; var vertexCount = data.vertexCount;
var vertexArray = data.vertexArray; var vertexArray = data.vertexArray;
var positionMin = data.positionMin; var positionMin = data.positionMin;
@ -328,13 +332,7 @@ function createGltf(data, inputPath, modelName, done) {
if (bufferSeparate) { if (bufferSeparate) {
var bufferPath = path.join(inputPath, modelName + '.bin'); var bufferPath = path.join(inputPath, modelName + '.bin');
fs.writeFile(bufferPath, buffer, function(err) { return fsWriteFile(bufferPath, buffer);
if (err) {
throw err;
}
done(gltf);
});
} else {
done(gltf);
} }
return gltf;
} }

View File

@ -1,7 +1,10 @@
"use strict"; "use strict";
var Promise = require('bluebird');
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require('path'); var path = require('path');
var fsReadFile = Promise.promisify(fs.readFile);
module.exports = loadImage; module.exports = loadImage;
function getChannels(colorType) { function getChannels(colorType) {
@ -34,12 +37,9 @@ function getUriType(extension) {
} }
} }
function loadImage(imagePath, done) { function loadImage(imagePath) {
fs.readFile(imagePath, function(error, data) { return fsReadFile(imagePath)
if (error) { .then(function(data) {
throw(error);
}
var extension = path.extname(imagePath).slice(1); var extension = path.extname(imagePath).slice(1);
var uriType = getUriType(extension); var uriType = getUriType(extension);
var uri = uriType + ';base64,' + data.toString('base64'); var uri = uriType + ';base64,' + data.toString('base64');
@ -58,7 +58,6 @@ function loadImage(imagePath, done) {
info.channels = channels; info.channels = channels;
info.transparent = (channels === 4); info.transparent = (channels === 4);
} }
return info;
done(info);
}); });
} }

View File

@ -1,7 +1,10 @@
"use strict"; "use strict";
var Promise = require('bluebird');
var fs = require('fs-extra'); var fs = require('fs-extra');
var defined = require('cesium').defined; var defined = require('cesium').defined;
var fsReadFile = Promise.promisify(fs.readFile);
module.exports = { module.exports = {
getDefault : getDefault, getDefault : getDefault,
parse : parse parse : parse
@ -31,17 +34,11 @@ function getDefault() {
return material; return material;
} }
function parse(mtlPath, done) { function parse(mtlPath) {
fs.readFile(mtlPath, 'utf8', function (error, contents) { return fsReadFile(mtlPath, 'utf8')
if (error) { .then(function (contents) {
console.log('Could not read material file at ' + mtlPath + '. Using default material instead.');
done({});
return;
}
var materials = {}; var materials = {};
var material; var material;
var values; var values;
var value; var value;
var lines = contents.split('\n'); var lines = contents.split('\n');
@ -109,11 +106,13 @@ function parse(mtlPath, done) {
material.alphaMap = line.substring(6).trim(); material.alphaMap = line.substring(6).trim();
} }
} }
if (defined(material.alpha)) { if (defined(material.alpha)) {
material.diffuseColor[3] = material.alpha; material.diffuseColor[3] = material.alpha;
} }
return materials;
done(materials); })
.catch(function() {
console.log('Could not read material file at ' + mtlPath + '. Using default material instead.');
return {};
}); });
} }

View File

@ -1,11 +1,14 @@
"use strict"; "use strict";
var async = require('async');
var Cesium = require('cesium');
var Promise = require('bluebird');
var byline = require('byline'); var byline = require('byline');
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require('path'); var path = require('path');
var loadImage = require('./image'); var loadImage = require('./image');
var Material = require('./mtl'); var Material = require('./mtl');
var Cesium = require('cesium');
var Cartesian3 = Cesium.Cartesian3; var Cartesian3 = Cesium.Cartesian3;
var defined = Cesium.defined; var defined = Cesium.defined;
@ -13,13 +16,18 @@ module.exports = parseObj;
// OBJ regex patterns are from ThreeJS (https://github.com/mrdoob/three.js/blob/master/examples/js/loaders/OBJLoader.js) // OBJ regex patterns are from ThreeJS (https://github.com/mrdoob/three.js/blob/master/examples/js/loaders/OBJLoader.js)
function parseObj(objFile, inputPath, done) { function parseObj(objFile, inputPath) {
getObjInfo(objFile, inputPath, function(info, materials, images) { return getObjInfo(objFile, inputPath)
processObj(objFile, info, materials, images, done); .then(function(result) {
var info = result.info;
var materials = result.materials;
var images = result.images;
return processObj(objFile, info, materials, images);
}); });
} }
function processObj(objFile, info, materials, images, done) { function processObj(objFile, info, materials, images) {
return new Promise(function(resolve) {
// A vertex is specified by indexes into each of the attribute arrays, // A vertex is specified by indexes into each of the attribute arrays,
// but these indexes may be different. This maps the separate indexes to a single index. // but these indexes may be different. This maps the separate indexes to a single index.
var vertexCache = {}; var vertexCache = {};
@ -166,7 +174,7 @@ function processObj(objFile, info, materials, images, done) {
var facePattern4 = /f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/; var facePattern4 = /f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))?/;
var stream = byline(fs.createReadStream(objFile, {encoding: 'utf8'})); var stream = byline(fs.createReadStream(objFile, {encoding: 'utf8'}));
stream.on('data', function(line) { stream.on('data', function (line) {
line = line.trim(); line = line.trim();
var result; var result;
if ((line.length === 0) || (line.charAt(0) === '#')) { if ((line.length === 0) || (line.charAt(0) === '#')) {
@ -222,22 +230,23 @@ function processObj(objFile, info, materials, images, done) {
} }
}); });
stream.on('end', function() { stream.on('end', function () {
done({ resolve({
vertexCount : vertexCount, vertexCount: vertexCount,
vertexArray : vertexArray, vertexArray: vertexArray,
positionMin : positionMin, positionMin: positionMin,
positionMax : positionMax, positionMax: positionMax,
hasUVs : hasUVs, hasUVs: hasUVs,
hasNormals : hasNormals, hasNormals: hasNormals,
materialGroups : materialGroups, materialGroups: materialGroups,
materials : materials, materials: materials,
images : images images: images
});
}); });
}); });
} }
function getImages(inputPath, materials, done) { function getImages(inputPath, materials) {
// Collect all the image files from the materials // Collect all the image files from the materials
var images = []; var images = [];
for (var name in materials) { for (var name in materials) {
@ -259,48 +268,49 @@ function getImages(inputPath, materials, done) {
} }
// Load the image files // Load the image files
var promises = [];
var imagesInfo = {}; var imagesInfo = {};
async.each(images, function (image, callback) { var imagesLength = images.length;
var imagePath = image; for (var i = 0; i < imagesLength; i++) {
var imagePath = images[i];
if (!path.isAbsolute(imagePath)) { if (!path.isAbsolute(imagePath)) {
imagePath = path.join(inputPath, image); imagePath = path.join(inputPath, imagePath);
} }
loadImage(imagePath, function(info) { promises.push(loadImage(imagePath));
imagesInfo[image] = info;
callback();
});
}, function (error) {
if (error) {
throw error;
} }
done(imagesInfo); return Promise.all(promises)
.then(function(imageInfoArray) {
var imageInfoArrayLength = imageInfoArray.length;
for (var j = 0; j < imageInfoArrayLength; j++) {
var image = images[j];
var imageInfo = imageInfoArray[j];
imagesInfo[image] = imageInfo;
}
return imagesInfo;
}); });
} }
function getMaterials(mtlPath, hasMaterialGroups, done) { function getMaterials(mtlPath, hasMaterialGroups) {
if (!hasMaterialGroups) { if (!hasMaterialGroups) {
done({});
return; return;
} }
if (defined(mtlPath)) { if (defined(mtlPath)) {
Material.parse(mtlPath, function(materials) { return Material.parse(mtlPath);
done(materials);
});
} else {
done({});
} }
} }
function getObjInfo(objFile, inputPath, done) { function getObjInfo(objFile, inputPath) {
var mtlPath; var mtlPath;
var materials;
var info;
var hasMaterialGroups = false; var hasMaterialGroups = false;
var hasPositions = false; var hasPositions = false;
var hasNormals = false; var hasNormals = false;
var hasUVs = false; var hasUVs = false;
return new Promise(function(resolve, reject) {
var stream = byline(fs.createReadStream(objFile, {encoding: 'utf8'})); var stream = byline(fs.createReadStream(objFile, {encoding: 'utf8'}));
stream.on('data', function(line) { stream.on('data', function (line) {
if (!defined(mtlPath)) { if (!defined(mtlPath)) {
var mtllibMatches = line.match(/^mtllib.*/gm); var mtllibMatches = line.match(/^mtllib.*/gm);
if (mtllibMatches !== null) { if (mtllibMatches !== null) {
@ -325,18 +335,33 @@ function getObjInfo(objFile, inputPath, done) {
} }
}); });
stream.on('end', function() { stream.on('error', function(err) {
reject(err);
});
stream.on('end', function () {
if (!hasPositions) { if (!hasPositions) {
throw new Error('Could not process OBJ file, no positions.'); reject(new Error('Could not process OBJ file, no positions.'));
} }
var info = { info = {
hasNormals : hasNormals, hasNormals: hasNormals,
hasUVs : hasUVs hasUVs: hasUVs
}; };
getMaterials(mtlPath, hasMaterialGroups, function(materials) { resolve();
getImages(inputPath, materials, function(images) {
done(info, materials, images);
});
}); });
})
.then(function() {
return getMaterials(mtlPath, hasMaterialGroups);
})
.then(function(returnedMaterials) {
materials = returnedMaterials;
return getImages(inputPath, materials);
})
.then(function(images) {
return {
info : info,
materials : materials,
images : images
};
}); });
} }

View File

@ -27,6 +27,7 @@
}, },
"dependencies": { "dependencies": {
"async": "2.0.0-rc.6", "async": "2.0.0-rc.6",
"bluebird": "^3.4.1",
"byline": "4.2.1", "byline": "4.2.1",
"cesium": "1.23.0", "cesium": "1.23.0",
"fs-extra": "0.30.0", "fs-extra": "0.30.0",

View File

@ -1,4 +1,6 @@
'use strict'; 'use strict';
var Promise = require('bluebird');
var gltfPipeline = require('gltf-pipeline').gltfPipeline; var gltfPipeline = require('gltf-pipeline').gltfPipeline;
var path = require('path'); var path = require('path');
var convert = require('../../lib/convert'); var convert = require('../../lib/convert');
@ -8,14 +10,14 @@ var gltfFile = './specs/data/BoxTextured/BoxTextured.gltf';
describe('convert', function() { describe('convert', function() {
it('converts an obj to gltf', function(done) { it('converts an obj to gltf', function(done) {
var spy = spyOn(gltfPipeline, 'processJSONToDisk').and.callFake(function(gltf, gltfFile, options, callback) { var spy = spyOn(gltfPipeline, 'processJSONToDisk').and.callFake(function(gltf, outputPath, options, callback) {
callback(); return;
}); });
convert(objFile, gltfFile, {}, function() { expect(convert(objFile, gltfFile, {})
.then(function() {
var args = spy.calls.first().args; var args = spy.calls.first().args;
expect(args[0]).toBeDefined(); expect(args[0]).toBeDefined();
expect(path.normalize(args[1])).toEqual(path.normalize(gltfFile)); expect(path.normalize(args[1])).toEqual(path.normalize(gltfFile));
done(); }), done).toResolve();
});
}); });
}); });