From 876cbefe7431e1e7af6497ed4a39434500cdfc99 Mon Sep 17 00:00:00 2001 From: Jesse Vander Does Date: Mon, 19 Aug 2019 16:33:12 -0700 Subject: [PATCH] Restored lost code and fixed tests --- bin/obj2gltf.js | 65 +++++++++++++++++++++++++++++++++++-- lib/loadObj.js | 25 +++++---------- specs/lib/loadObjSpec.js | 69 +++++++++++++++++----------------------- 3 files changed, 99 insertions(+), 60 deletions(-) diff --git a/bin/obj2gltf.js b/bin/obj2gltf.js index 91694b3..40ffe76 100644 --- a/bin/obj2gltf.js +++ b/bin/obj2gltf.js @@ -66,9 +66,62 @@ const argv = yargs default : defaults.checkTransparency }, secure : { - describe: 'Prevent the converter from reading image or mtl files outside of the input obj directory.', - type: 'boolean', - default: defaults.secure + describe : 'Prevent the converter from reading textures or mtl files outside of the input obj directory.', + type : 'boolean', + default : defaults.secure + }, + packOcclusion : { + describe : 'Pack the occlusion texture in the red channel of metallic-roughness texture.', + type : 'boolean', + default : defaults.packOcclusion + }, + metallicRoughness : { + describe : 'The values in the .mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.', + type : 'boolean', + default : defaults.metallicRoughness + }, + specularGlossiness : { + describe : 'The values in the .mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension.', + type : 'boolean', + default : defaults.specularGlossiness + }, + metallicRoughnessOcclusionTexture : { + describe : 'Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material', + type : 'string', + normalize : true + }, + specularGlossinessTexture : { + describe : 'Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.', + type : 'string', + normalize : true + }, + occlusionTexture : { + describe : 'Path to the occlusion texture that should override textures in the .mtl file.', + type : 'string', + normalize : true + }, + normalTexture : { + describe : 'Path to the normal texture that should override textures in the .mtl file.', + type : 'string', + normalize : true + }, + baseColorTexture : { + describe : 'Path to the baseColor/diffuse texture that should override textures in the .mtl file.', + type : 'string', + normalize : true + }, + emissiveTexture : { + describe : 'Path to the emissive texture that should override textures in the .mtl file.', + type : 'string', + normalize : true + }, + alphaTexture : { + describe : 'Path to the alpha texture that should override textures in the .mtl file.' + }, + unlit : { + describe : 'The glTF will be saved with the KHR_materials_unlit extension.', + type : 'boolean', + default : defaults.unlit }, inputUpAxis : { describe: 'Up axis of the obj.', @@ -121,6 +174,12 @@ const options = { separateTextures : argv.separateTextures, checkTransparency : argv.checkTransparency, secure : argv.secure, + packOcclusion : argv.packOcclusion, + metallicRoughness : argv.metallicRoughness, + specularGlossiness : argv.specularGlossiness, + unlit : argv.unlit, + overridingTextures : overridingTextures, + outputDirectory : outputDirectory, inputUpAxis : argv.inputUpAxis, outputUpAxis : argv.outputUpAxis }; diff --git a/lib/loadObj.js b/lib/loadObj.js index 2207685..67e8a8b 100644 --- a/lib/loadObj.js +++ b/lib/loadObj.js @@ -50,30 +50,19 @@ const normalPattern = /vn( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\ const uvPattern = /vt( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/; // vt float float const facePattern = /(-?\d+)\/?(-?\d*)\/?(-?\d*)/g; // for any face format "f v", "f v/v", "f v//v", "f v/v/v" -var scratchCartesian = new Cartesian3(); +const scratchCartesian = new Cartesian3(); /** * Parse an obj file. * * @param {String} objPath Path to the obj file. -<<<<<<< HEAD * @param {Object} options The options object passed along from lib/obj2gltf.js * @returns {Promise} A promise resolving to the obj data, which includes an array of nodes containing geometry information and an array of materials. -======= - * @param {Object} options An object with the following properties: - * @param {Boolean} options.checkTransparency Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel. - * @param {Boolean} options.secure Prevent the converter from reading image or mtl files outside of the input obj directory. - * @param {String} options.inputUpAxis Up axis of the obj. - * @param {String} options.outputUpAxis Up axis of the converted glTF. - * @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. ->>>>>>> 93dac5e... Convert up axis * * @private */ function loadObj(objPath, options) { - var axisTransform = getAxisTransform(options.inputUpAxis, options.outputUpAxis); + const axisTransform = getAxisTransform(options.inputUpAxis, options.outputUpAxis); // Global store of vertex attributes listed in the obj file let globalPositions = new ArrayStorage(ComponentDatatype.FLOAT); @@ -383,10 +372,12 @@ function loadObj(objPath, options) { globalPositions.push(position.y); globalPositions.push(position.z); } else if ((result = normalPattern.exec(line) ) !== null) { - const normal = scratchCartesian; - normal.x = parseFloat(result[1]); - normal.y = parseFloat(result[2]); - normal.z = parseFloat(result[3]); + const normal = Cartesian3.fromElements(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3]), scratchNormal); + if (Cartesian3.equals(normal, Cartesian3.ZERO)) { + Cartesian3.clone(Cartesian3.UNIT_Z, normal); + } else { + Cartesian3.normalize(normal, normal); + } if (defined(axisTransform)) { Matrix4.multiplyByPointAsVector(axisTransform, normal, normal); } diff --git a/specs/lib/loadObjSpec.js b/specs/lib/loadObjSpec.js index 720e644..cfcdebc 100644 --- a/specs/lib/loadObjSpec.js +++ b/specs/lib/loadObjSpec.js @@ -10,6 +10,7 @@ const clone = Cesium.clone; const RuntimeError = Cesium.RuntimeError; const objPath = 'specs/data/box/box.obj'; +const objRotatedUrl = 'specs/data/box-rotated/box-rotated.obj'; const objNormalsPath = 'specs/data/box-normals/box-normals.obj'; const objUvsPath = 'specs/data/box-uvs/box-uvs.obj'; const objPositionsOnlyPath = 'specs/data/box-positions-only/box-positions-only.obj'; @@ -445,7 +446,6 @@ describe('loadObj', () => { expect(baseColorTexture.source).toBeDefined(); }); -<<<<<<< HEAD it('separates faces that don\'t use the same attributes as other faces in the primitive', async () => { const data = await loadObj(objMixedAttributesPath, options); const primitives = getPrimitives(data); @@ -454,57 +454,46 @@ describe('loadObj', () => { expect(primitives[1].indices.length).toBe(6); // 2 faces expect(primitives[2].indices.length).toBe(6); // 2 faces expect(primitives[3].indices.length).toBe(6); // 2 faces -======= + }); + function getFirstPosition(data) { - var positions = data.nodes[0].meshes[0].positions; - return new Cartesian3(positions.get(0), positions.get(1), positions.get(2)); + const primitive = getPrimitives(data)[0]; + return new Cartesian3(primitive.positions.get(0), primitive.positions.get(1), primitive.positions.get(2)); } function getFirstNormal(data) { - var normals = data.nodes[0].meshes[0].normals; - return new Cartesian3(normals.get(0), normals.get(1), normals.get(2)); + const primitive = getPrimitives(data)[0]; + return new Cartesian3(primitive.normals.get(0), primitive.normals.get(1), primitive.normals.get(2)); } - function checkAxisConversion(inputUpAxis, outputUpAxis, position, normal) { + async function checkAxisConversion(inputUpAxis, outputUpAxis, position, normal) { var sameAxis = (inputUpAxis === outputUpAxis); - var options = clone(defaultOptions); options.inputUpAxis = inputUpAxis; options.outputUpAxis = outputUpAxis; - return loadObj(objRotatedUrl, options) - .then(function(data) { - var rotatedPosition = getFirstPosition(data); - var rotatedNormal = getFirstNormal(data); - if (sameAxis) { - expect(rotatedPosition).toEqual(position); - expect(rotatedNormal).toEqual(normal); - } else { - expect(rotatedPosition).not.toEqual(position); - expect(rotatedNormal).not.toEqual(normal); - } - }); + const data = await loadObj(objRotatedUrl, options); + const rotatedPosition = getFirstPosition(data); + const rotatedNormal = getFirstNormal(data); + if (sameAxis) { + expect(rotatedPosition).toEqual(position); + expect(rotatedNormal).toEqual(normal); + } else { + expect(rotatedPosition).not.toEqual(position); + expect(rotatedNormal).not.toEqual(normal); + } } - it('performs up axis conversion', function(done) { - expect(loadObj(objRotatedUrl, defaultOptions) - .then(function(data) { - var position = getFirstPosition(data); - var normal = getFirstNormal(data); + it('performs up axis conversion', async () => { + const data = await loadObj(objRotatedUrl, options); + const position = getFirstPosition(data); + const normal = getFirstNormal(data); - var axes = ['X', 'Y', 'Z']; - var axesLength = axes.length; - var promises = []; - for (var i = 0; i < axesLength; ++i) { - for (var j = 0; j < axesLength; ++j) { - promises.push(checkAxisConversion(axes[i], axes[j], position, normal)); - } - } - return Promise.all(promises); - }), done).toResolve(); - }); - - it('throws when file has invalid contents', function(done) { - expect(loadObj(objInvalidContentsUrl, defaultOptions), done).toRejectWith(RuntimeError); ->>>>>>> 93dac5e... Convert up axis + const axes = ['X', 'Y', 'Z']; + const axesLength = axes.length; + for (let i = 0; i < axesLength; ++i) { + for (let j = 0; j < axesLength; ++j) { + await checkAxisConversion(axes[i], axes[j], position, normal); + } + } }); it('throws when file has invalid contents', async () => {