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,7 +15,8 @@ var convert = obj2gltf.convert;
var options = {
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');
});
```

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,10 @@
"use strict";
var Promise = require('bluebird');
var fs = require('fs-extra');
var defined = require('cesium').defined;
var fsReadFile = Promise.promisify(fs.readFile);
module.exports = {
getDefault : getDefault,
parse : parse
@ -31,17 +34,11 @@ function getDefault() {
return material;
}
function parse(mtlPath, done) {
fs.readFile(mtlPath, 'utf8', function (error, contents) {
if (error) {
console.log('Could not read material file at ' + mtlPath + '. Using default material instead.');
done({});
return;
}
function parse(mtlPath) {
return fsReadFile(mtlPath, 'utf8')
.then(function (contents) {
var materials = {};
var material;
var values;
var value;
var lines = contents.split('\n');
@ -109,11 +106,13 @@ function parse(mtlPath, done) {
material.alphaMap = line.substring(6).trim();
}
}
if (defined(material.alpha)) {
material.diffuseColor[3] = material.alpha;
}
done(materials);
return 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";
var async = require('async');
var Cesium = require('cesium');
var Promise = require('bluebird');
var byline = require('byline');
var fs = require('fs-extra');
var path = require('path');
var loadImage = require('./image');
var Material = require('./mtl');
var Cesium = require('cesium');
var Cartesian3 = Cesium.Cartesian3;
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)
function parseObj(objFile, inputPath, done) {
getObjInfo(objFile, inputPath, function(info, materials, images) {
processObj(objFile, info, materials, images, done);
function parseObj(objFile, inputPath) {
return getObjInfo(objFile, inputPath)
.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,
// but these indexes may be different. This maps the separate indexes to a single index.
var vertexCache = {};
@ -223,7 +231,7 @@ function processObj(objFile, info, materials, images, done) {
});
stream.on('end', function () {
done({
resolve({
vertexCount: vertexCount,
vertexArray: vertexArray,
positionMin: positionMin,
@ -235,9 +243,10 @@ function processObj(objFile, info, materials, images, done) {
images: images
});
});
});
}
function getImages(inputPath, materials, done) {
function getImages(inputPath, materials) {
// Collect all the image files from the materials
var images = [];
for (var name in materials) {
@ -259,46 +268,47 @@ function getImages(inputPath, materials, done) {
}
// Load the image files
var promises = [];
var imagesInfo = {};
async.each(images, function (image, callback) {
var imagePath = image;
var imagesLength = images.length;
for (var i = 0; i < imagesLength; i++) {
var imagePath = images[i];
if (!path.isAbsolute(imagePath)) {
imagePath = path.join(inputPath, image);
imagePath = path.join(inputPath, imagePath);
}
loadImage(imagePath, function(info) {
imagesInfo[image] = info;
callback();
});
}, function (error) {
if (error) {
throw error;
promises.push(loadImage(imagePath));
}
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) {
done({});
return;
}
if (defined(mtlPath)) {
Material.parse(mtlPath, function(materials) {
done(materials);
});
} else {
done({});
return Material.parse(mtlPath);
}
}
function getObjInfo(objFile, inputPath, done) {
function getObjInfo(objFile, inputPath) {
var mtlPath;
var materials;
var info;
var hasMaterialGroups = false;
var hasPositions = false;
var hasNormals = false;
var hasUVs = false;
return new Promise(function(resolve, reject) {
var stream = byline(fs.createReadStream(objFile, {encoding: 'utf8'}));
stream.on('data', function (line) {
if (!defined(mtlPath)) {
@ -325,18 +335,33 @@ function getObjInfo(objFile, inputPath, done) {
}
});
stream.on('error', function(err) {
reject(err);
});
stream.on('end', function () {
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,
hasUVs: hasUVs
};
getMaterials(mtlPath, hasMaterialGroups, function(materials) {
getImages(inputPath, materials, function(images) {
done(info, materials, images);
});
resolve();
});
})
.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": {
"async": "2.0.0-rc.6",
"bluebird": "^3.4.1",
"byline": "4.2.1",
"cesium": "1.23.0",
"fs-extra": "0.30.0",

View File

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