mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2025-03-18 01:10:46 -04:00
Custom logger
This commit is contained in:
parent
3c958e1d86
commit
c7b4fc3cb1
@ -34,10 +34,10 @@ module.exports = convert;
|
||||
* @param {Boolean} [options.bypassPipeline=false] Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.
|
||||
* @param {Boolean} [options.hasTransparency=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
|
||||
* @param {Boolean} [options.secure=false] Prevent the converter from reading image or mtl files outside of the input obj directory.
|
||||
* @param {Logger} [options.logger] A callback function for handling logged messages. Defaults to console.log.
|
||||
*/
|
||||
|
||||
function convert(objPath, gltfPath, options) {
|
||||
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
|
||||
options = defaultValue(options, {});
|
||||
var binary = defaultValue(options.binary, false);
|
||||
var separate = defaultValue(options.separate, false);
|
||||
var separateTextures = defaultValue(options.separateTextures, false) || separate;
|
||||
@ -48,6 +48,10 @@ function convert(objPath, gltfPath, options) {
|
||||
var ao = defaultValue(options.ao, false);
|
||||
var textureCompressionOptions = options.textureCompressionOptions;
|
||||
var bypassPipeline = defaultValue(options.bypassPipeline, false);
|
||||
var logger = defaultValue(options.logger, defaultLogger);
|
||||
options.logger = logger;
|
||||
options.hasTransparency = defaultValue(options.hasTransparency, false);
|
||||
options.secure = defaultValue(options.secure, false);
|
||||
|
||||
if (!defined(objPath)) {
|
||||
throw new DeveloperError('objPath is required');
|
||||
@ -63,7 +67,7 @@ function convert(objPath, gltfPath, options) {
|
||||
if (extension === '.glb') {
|
||||
binary = true;
|
||||
if (bypassPipeline) {
|
||||
console.log('--bypassPipeline does not convert to binary glTF, saving as .gltf');
|
||||
options.logger('--bypassPipeline does not convert to binary glTF, saving as .gltf');
|
||||
extension = '.gltf';
|
||||
}
|
||||
}
|
||||
@ -92,7 +96,7 @@ function convert(objPath, gltfPath, options) {
|
||||
return createGltf(objData);
|
||||
})
|
||||
.then(function(gltf) {
|
||||
return writeUris(gltf, gltfPath, separate, separateTextures);
|
||||
return writeUris(gltf, gltfPath, separate, separateTextures, logger);
|
||||
})
|
||||
.then(function(gltf) {
|
||||
if (bypassPipeline) {
|
||||
@ -109,3 +113,13 @@ function convert(objPath, gltfPath, options) {
|
||||
* @private
|
||||
*/
|
||||
convert._outputJson = fsExtraOutputJson;
|
||||
|
||||
/**
|
||||
* A callback function that logs messages.
|
||||
* @callback Logger
|
||||
*
|
||||
* @param {String} message The message to log.
|
||||
*/
|
||||
var defaultLogger = function(message) {
|
||||
console.log(message);
|
||||
};
|
||||
|
15
lib/obj.js
15
lib/obj.js
@ -54,15 +54,20 @@ var facePattern4 = /f( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(-?\d+))( +(-?\d+)\/\/(
|
||||
* @param {Object} [options] An object with the following properties:
|
||||
* @param {Boolean} [options.hasTransparency=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
|
||||
* @param {Boolean} [options.secure=false] Prevent the converter from reading image or mtl files outside of the input obj directory.
|
||||
* @param {Boolean} [options.logger] A callback function for handling logged messages. Defaults to console.log.
|
||||
* @returns {Promise} A promise resolving to the obj data.
|
||||
* @exception {RuntimeError} The file does not have any geometry information in it.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
function loadObj(objPath, options) {
|
||||
// The defaults are set in convert as well, this just helps with testing loadObj individually
|
||||
options = combine(options, {
|
||||
hasTransparency : false,
|
||||
secure : false
|
||||
secure : false,
|
||||
logger : function(message) {
|
||||
console.log(message);
|
||||
}
|
||||
});
|
||||
|
||||
// Global store of vertex attributes listed in the obj file
|
||||
@ -320,7 +325,7 @@ function loadMaterials(mtlPaths, objPath, options) {
|
||||
return Promise.map(mtlPaths, function(mtlPath) {
|
||||
mtlPath = getAbsolutePath(mtlPath, objPath);
|
||||
if (options.secure && outsideDirectory(mtlPath, objPath)) {
|
||||
console.log('Could not read mtl file at ' + mtlPath + ' because it is outside of the obj directory and the secure flag is true. Using default material instead.');
|
||||
options.logger('Could not read mtl file at ' + mtlPath + ' because it is outside of the obj directory and the secure flag is true. Using default material instead.');
|
||||
return;
|
||||
}
|
||||
return loadMtl(mtlPath)
|
||||
@ -328,7 +333,7 @@ function loadMaterials(mtlPaths, objPath, options) {
|
||||
materials = combine(materials, materialsInMtl);
|
||||
})
|
||||
.catch(function() {
|
||||
console.log('Could not read mtl file at ' + mtlPath + '. Using default material instead.');
|
||||
options.logger('Could not read mtl file at ' + mtlPath + '. Using default material instead.');
|
||||
});
|
||||
}).then(function() {
|
||||
return materials;
|
||||
@ -339,7 +344,7 @@ function loadImages(imagePaths, objPath, options) {
|
||||
var images = {};
|
||||
return Promise.map(imagePaths, function(imagePath) {
|
||||
if (options.secure && outsideDirectory(imagePath, objPath)) {
|
||||
console.log('Could not read image file at ' + imagePath + ' because it is outside of the obj directory and the secure flag is true. Material will ignore this image.');
|
||||
options.logger('Could not read image file at ' + imagePath + ' because it is outside of the obj directory and the secure flag is true. Material will ignore this image.');
|
||||
return;
|
||||
}
|
||||
return loadImage(imagePath, options)
|
||||
@ -349,7 +354,7 @@ function loadImages(imagePaths, objPath, options) {
|
||||
}
|
||||
})
|
||||
.catch(function() {
|
||||
console.log('Could not read image file at ' + imagePath + '. Material will ignore this image.');
|
||||
options.logger('Could not read image file at ' + imagePath + '. Material will ignore this image.');
|
||||
return undefined;
|
||||
});
|
||||
}).then(function() {
|
||||
|
@ -15,11 +15,12 @@ module.exports = writeUris;
|
||||
* @param {String} gltfPath Path where the glTF will be saved.
|
||||
* @param {Boolean} separateBuffers Writes out separate buffers.
|
||||
* @param {Boolean} separateTextures Writes out separate textures.
|
||||
* @param {Logger} logger A callback function for handling logged messages. Defaults to console.log.
|
||||
* @returns {Promise} A promise that resolves to the glTF asset.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
function writeUris(gltf, gltfPath, separateBuffers, separateTextures) {
|
||||
function writeUris(gltf, gltfPath, separateBuffers, separateTextures, logger) {
|
||||
var promises = [];
|
||||
|
||||
var buffer = gltf.buffers[Object.keys(gltf.buffers)[0]];
|
||||
@ -37,7 +38,7 @@ function writeUris(gltf, gltfPath, separateBuffers, separateTextures) {
|
||||
var exceedsMaximum = (texturesByteLength + bufferByteLength > 201326580);
|
||||
|
||||
if (exceedsMaximum) {
|
||||
console.log('Buffers and textures are too large to encode in the glTF, saving as separate resources.');
|
||||
logger('Buffers and textures are too large to encode in the glTF, saving as separate resources.');
|
||||
}
|
||||
|
||||
if (separateBuffers || exceedsMaximum) {
|
||||
|
@ -8,6 +8,8 @@ var objPath = 'specs/data/box-textured/box-textured.obj';
|
||||
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
|
||||
var glbPath = 'specs/data/box-textured/box-textured.glb';
|
||||
|
||||
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
|
||||
|
||||
describe('convert', function() {
|
||||
it('converts an obj to gltf', function(done) {
|
||||
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
|
||||
@ -111,6 +113,21 @@ describe('convert', function() {
|
||||
}), done).toResolve();
|
||||
});
|
||||
|
||||
it('Uses a custom logger', function(done) {
|
||||
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
|
||||
var logCount = 0;
|
||||
var options = {
|
||||
secure : true, // Needs to be set to trigger messages
|
||||
logger : function() {
|
||||
logCount++;
|
||||
}
|
||||
};
|
||||
expect(convert(objExternalResourcesPath, gltfPath, options)
|
||||
.then(function() {
|
||||
expect(logCount).toEqual(2);
|
||||
}), done).toResolve();
|
||||
});
|
||||
|
||||
it('throws if objPath is undefined', function() {
|
||||
expect(function() {
|
||||
convert(undefined, gltfPath);
|
||||
|
Loading…
x
Reference in New Issue
Block a user