mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2025-02-18 16:43:52 -05:00
Handle file path errors and better promise handling
This commit is contained in:
parent
72c20eb6ee
commit
965402c535
@ -130,4 +130,7 @@ console.time('Total');
|
|||||||
convert(objPath, gltfPath, options)
|
convert(objPath, gltfPath, options)
|
||||||
.then(function() {
|
.then(function() {
|
||||||
console.timeEnd('Total');
|
console.timeEnd('Total');
|
||||||
|
})
|
||||||
|
.catch(function(error) {
|
||||||
|
console.log(error.message);
|
||||||
});
|
});
|
||||||
|
143
lib/convert.js
143
lib/convert.js
@ -48,79 +48,92 @@ var defaultLogger = function(message) {
|
|||||||
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
|
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
|
||||||
*/
|
*/
|
||||||
function convert(objPath, gltfPath, options) {
|
function convert(objPath, gltfPath, options) {
|
||||||
options = defaultValue(options, {});
|
return new Promise(function(resolve, reject) {
|
||||||
var binary = defaultValue(options.binary, false);
|
options = defaultValue(options, {});
|
||||||
var separate = defaultValue(options.separate, false);
|
var binary = defaultValue(options.binary, false);
|
||||||
var separateTextures = defaultValue(options.separateTextures, false) || separate;
|
var separate = defaultValue(options.separate, false);
|
||||||
var compress = defaultValue(options.compress, false);
|
var separateTextures = defaultValue(options.separateTextures, false) || separate;
|
||||||
var optimize = defaultValue(options.optimize, false);
|
var compress = defaultValue(options.compress, false);
|
||||||
var optimizeForCesium = defaultValue(options.optimizeForCesium, false);
|
var optimize = defaultValue(options.optimize, false);
|
||||||
var generateNormals = defaultValue(options.generateNormals, false);
|
var optimizeForCesium = defaultValue(options.optimizeForCesium, false);
|
||||||
var ao = defaultValue(options.ao, false);
|
var generateNormals = defaultValue(options.generateNormals, false);
|
||||||
var kmc = defaultValue(options.kmc, false);
|
var ao = defaultValue(options.ao, false);
|
||||||
var textureCompressionOptions = options.textureCompressionOptions;
|
var kmc = defaultValue(options.kmc, false);
|
||||||
var bypassPipeline = defaultValue(options.bypassPipeline, false);
|
var textureCompressionOptions = options.textureCompressionOptions;
|
||||||
var logger = defaultValue(options.logger, defaultLogger);
|
var bypassPipeline = defaultValue(options.bypassPipeline, false);
|
||||||
options.logger = logger;
|
var logger = defaultValue(options.logger, defaultLogger);
|
||||||
options.hasTransparency = defaultValue(options.hasTransparency, false);
|
options.logger = logger;
|
||||||
options.secure = defaultValue(options.secure, false);
|
options.hasTransparency = defaultValue(options.hasTransparency, false);
|
||||||
|
options.secure = defaultValue(options.secure, false);
|
||||||
|
|
||||||
if (!defined(objPath)) {
|
if (!defined(objPath)) {
|
||||||
throw new DeveloperError('objPath is required');
|
throw new DeveloperError('objPath is required');
|
||||||
}
|
|
||||||
|
|
||||||
if (!defined(gltfPath)) {
|
|
||||||
throw new DeveloperError('gltfPath is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
var basePath = path.dirname(gltfPath);
|
|
||||||
var modelName = path.basename(gltfPath, path.extname(gltfPath));
|
|
||||||
var extension = path.extname(gltfPath);
|
|
||||||
if (extension === '.glb') {
|
|
||||||
binary = true;
|
|
||||||
if (bypassPipeline) {
|
|
||||||
options.logger('--bypassPipeline does not convert to binary glTF, saving as .gltf');
|
|
||||||
extension = '.gltf';
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
gltfPath = path.join(path.dirname(gltfPath), modelName + extension);
|
|
||||||
|
|
||||||
var aoOptions = ao ? {} : undefined;
|
if (!defined(gltfPath)) {
|
||||||
|
throw new DeveloperError('gltfPath is required');
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: gltf-pipeline uses the same kmc options for each material and doesn't recognize the transparent flag
|
var objExtension = path.extname(objPath).toLowerCase();
|
||||||
var kmcOptions = kmc ? {} : undefined;
|
if (objExtension !== '.obj') {
|
||||||
|
throw new DeveloperError('Invalid obj path "' + objPath + '"');
|
||||||
|
}
|
||||||
|
|
||||||
var pipelineOptions = {
|
var extension = path.extname(gltfPath).toLowerCase();
|
||||||
createDirectory : false,
|
if (extension !== '.gltf' && extension !== '.glb') {
|
||||||
basePath : basePath,
|
throw new DeveloperError('Invalid gltf path "' + gltfPath + '"');
|
||||||
binary : binary,
|
}
|
||||||
embed : !separate,
|
|
||||||
embedImage : !separateTextures,
|
|
||||||
quantize : compress,
|
|
||||||
compressTextureCoordinates : compress,
|
|
||||||
encodeNormals : compress,
|
|
||||||
preserve : !optimize,
|
|
||||||
optimizeForCesium : optimizeForCesium,
|
|
||||||
smoothNormals : generateNormals,
|
|
||||||
aoOptions : aoOptions,
|
|
||||||
kmcOptions : kmcOptions,
|
|
||||||
textureCompressionOptions : textureCompressionOptions
|
|
||||||
};
|
|
||||||
|
|
||||||
return loadObj(objPath, options)
|
var basePath = path.dirname(gltfPath);
|
||||||
.then(function(objData) {
|
var modelName = path.basename(gltfPath, path.extname(gltfPath));
|
||||||
return createGltf(objData);
|
if (extension === '.glb') {
|
||||||
})
|
binary = true;
|
||||||
.then(function(gltf) {
|
|
||||||
return writeUris(gltf, gltfPath, separate, separateTextures, logger);
|
|
||||||
})
|
|
||||||
.then(function(gltf) {
|
|
||||||
if (bypassPipeline) {
|
if (bypassPipeline) {
|
||||||
return convert._outputJson(gltfPath, gltf);
|
logger('--bypassPipeline does not convert to binary glTF, saving as .gltf');
|
||||||
} else {
|
extension = '.gltf';
|
||||||
return GltfPipeline.processJSONToDisk(gltf, gltfPath, pipelineOptions);
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
gltfPath = path.join(path.dirname(gltfPath), modelName + extension);
|
||||||
|
|
||||||
|
var aoOptions = ao ? {} : undefined;
|
||||||
|
|
||||||
|
// TODO: gltf-pipeline uses the same kmc options for each material and doesn't recognize the transparent flag
|
||||||
|
var kmcOptions = kmc ? {} : undefined;
|
||||||
|
|
||||||
|
var pipelineOptions = {
|
||||||
|
createDirectory : false,
|
||||||
|
basePath : basePath,
|
||||||
|
binary : binary,
|
||||||
|
embed : !separate,
|
||||||
|
embedImage : !separateTextures,
|
||||||
|
quantize : compress,
|
||||||
|
compressTextureCoordinates : compress,
|
||||||
|
encodeNormals : compress,
|
||||||
|
preserve : !optimize,
|
||||||
|
optimizeForCesium : optimizeForCesium,
|
||||||
|
smoothNormals : generateNormals,
|
||||||
|
aoOptions : aoOptions,
|
||||||
|
kmcOptions : kmcOptions,
|
||||||
|
textureCompressionOptions : textureCompressionOptions
|
||||||
|
};
|
||||||
|
|
||||||
|
return loadObj(objPath, options)
|
||||||
|
.then(function(objData) {
|
||||||
|
return createGltf(objData);
|
||||||
|
})
|
||||||
|
.then(function(gltf) {
|
||||||
|
return writeUris(gltf, gltfPath, separate, separateTextures, logger);
|
||||||
|
})
|
||||||
|
.then(function(gltf) {
|
||||||
|
if (bypassPipeline) {
|
||||||
|
return convert._outputJson(gltfPath, gltf);
|
||||||
|
} else {
|
||||||
|
return GltfPipeline.processJSONToDisk(gltf, gltfPath, pipelineOptions);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(resolve)
|
||||||
|
.catch(reject);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,12 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
var Cesium = require('cesium');
|
||||||
var GltfPipeline = require('gltf-pipeline').Pipeline;
|
var GltfPipeline = require('gltf-pipeline').Pipeline;
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var convert = require('../../lib/convert');
|
var convert = require('../../lib/convert');
|
||||||
var writeUris = require('../../lib/writeUris');
|
var writeUris = require('../../lib/writeUris');
|
||||||
|
|
||||||
|
var DeveloperError = Cesium.DeveloperError;
|
||||||
|
|
||||||
var objPath = 'specs/data/box-textured/box-textured.obj';
|
var objPath = 'specs/data/box-textured/box-textured.obj';
|
||||||
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
|
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
|
||||||
var glbPath = 'specs/data/box-textured/box-textured.glb';
|
var glbPath = 'specs/data/box-textured/box-textured.glb';
|
||||||
|
var objPathInvalid = 'invalid/';
|
||||||
|
var gltfPathInvalid = 'invalid/model.invalid';
|
||||||
|
var objPathNonExistent = 'specs/data/non-existent.obj';
|
||||||
|
var gltfPathNonExistent = 'specs/data/non-existent.gltf';
|
||||||
|
|
||||||
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
|
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
|
||||||
|
|
||||||
@ -116,7 +123,7 @@ describe('convert', function() {
|
|||||||
}), done).toResolve();
|
}), done).toResolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Uses a custom logger', function(done) {
|
it('uses a custom logger', function(done) {
|
||||||
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
|
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
|
||||||
var logCount = 0;
|
var logCount = 0;
|
||||||
var options = {
|
var options = {
|
||||||
@ -131,15 +138,23 @@ describe('convert', function() {
|
|||||||
}), done).toResolve();
|
}), done).toResolve();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws if objPath is undefined', function() {
|
it('rejects if objPath is undefined', function(done) {
|
||||||
expect(function() {
|
expect(convert(undefined, gltfPath), done).toRejectWith(DeveloperError);
|
||||||
convert(undefined, gltfPath);
|
|
||||||
}).toThrowDeveloperError();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws if gltfPath is undefined', function() {
|
it('rejects if gltfPath is undefined', function(done) {
|
||||||
expect(function() {
|
expect(convert(objPath, undefined), done).toRejectWith(DeveloperError);
|
||||||
convert(objPath, undefined);
|
});
|
||||||
}).toThrowDeveloperError();
|
|
||||||
|
it('rejects if obj path is invalid', function(done) {
|
||||||
|
expect(convert(objPathInvalid, gltfPath), done).toRejectWith(DeveloperError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects if gltf path is invalid', function(done) {
|
||||||
|
expect(convert(objPath, gltfPathInvalid), done).toRejectWith(DeveloperError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('rejects if obj path does not exist', function(done) {
|
||||||
|
expect(convert(objPathNonExistent, gltfPath), done).toRejectWith(Error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user