Restored lost code and fixed tests

This commit is contained in:
Jesse Vander Does 2019-08-19 16:33:12 -07:00
parent 3cf338107d
commit 876cbefe74
3 changed files with 99 additions and 60 deletions

View File

@ -66,9 +66,62 @@ const argv = yargs
default : defaults.checkTransparency default : defaults.checkTransparency
}, },
secure : { secure : {
describe: 'Prevent the converter from reading image or mtl files outside of the input obj directory.', describe : 'Prevent the converter from reading textures or mtl files outside of the input obj directory.',
type: 'boolean', type : 'boolean',
default: defaults.secure 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 : { inputUpAxis : {
describe: 'Up axis of the obj.', describe: 'Up axis of the obj.',
@ -121,6 +174,12 @@ const options = {
separateTextures : argv.separateTextures, separateTextures : argv.separateTextures,
checkTransparency : argv.checkTransparency, checkTransparency : argv.checkTransparency,
secure : argv.secure, secure : argv.secure,
packOcclusion : argv.packOcclusion,
metallicRoughness : argv.metallicRoughness,
specularGlossiness : argv.specularGlossiness,
unlit : argv.unlit,
overridingTextures : overridingTextures,
outputDirectory : outputDirectory,
inputUpAxis : argv.inputUpAxis, inputUpAxis : argv.inputUpAxis,
outputUpAxis : argv.outputUpAxis outputUpAxis : argv.outputUpAxis
}; };

View File

@ -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 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" 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. * Parse an obj file.
* *
* @param {String} objPath Path to the obj file. * @param {String} objPath Path to the obj file.
<<<<<<< HEAD
* @param {Object} options The options object passed along from lib/obj2gltf.js * @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. * @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 * @private
*/ */
function loadObj(objPath, options) { 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 // Global store of vertex attributes listed in the obj file
let globalPositions = new ArrayStorage(ComponentDatatype.FLOAT); let globalPositions = new ArrayStorage(ComponentDatatype.FLOAT);
@ -383,10 +372,12 @@ function loadObj(objPath, options) {
globalPositions.push(position.y); globalPositions.push(position.y);
globalPositions.push(position.z); globalPositions.push(position.z);
} else if ((result = normalPattern.exec(line) ) !== null) { } else if ((result = normalPattern.exec(line) ) !== null) {
const normal = scratchCartesian; const normal = Cartesian3.fromElements(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3]), scratchNormal);
normal.x = parseFloat(result[1]); if (Cartesian3.equals(normal, Cartesian3.ZERO)) {
normal.y = parseFloat(result[2]); Cartesian3.clone(Cartesian3.UNIT_Z, normal);
normal.z = parseFloat(result[3]); } else {
Cartesian3.normalize(normal, normal);
}
if (defined(axisTransform)) { if (defined(axisTransform)) {
Matrix4.multiplyByPointAsVector(axisTransform, normal, normal); Matrix4.multiplyByPointAsVector(axisTransform, normal, normal);
} }

View File

@ -10,6 +10,7 @@ const clone = Cesium.clone;
const RuntimeError = Cesium.RuntimeError; const RuntimeError = Cesium.RuntimeError;
const objPath = 'specs/data/box/box.obj'; 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 objNormalsPath = 'specs/data/box-normals/box-normals.obj';
const objUvsPath = 'specs/data/box-uvs/box-uvs.obj'; const objUvsPath = 'specs/data/box-uvs/box-uvs.obj';
const objPositionsOnlyPath = 'specs/data/box-positions-only/box-positions-only.obj'; const objPositionsOnlyPath = 'specs/data/box-positions-only/box-positions-only.obj';
@ -445,7 +446,6 @@ describe('loadObj', () => {
expect(baseColorTexture.source).toBeDefined(); expect(baseColorTexture.source).toBeDefined();
}); });
<<<<<<< HEAD
it('separates faces that don\'t use the same attributes as other faces in the primitive', async () => { it('separates faces that don\'t use the same attributes as other faces in the primitive', async () => {
const data = await loadObj(objMixedAttributesPath, options); const data = await loadObj(objMixedAttributesPath, options);
const primitives = getPrimitives(data); const primitives = getPrimitives(data);
@ -454,57 +454,46 @@ describe('loadObj', () => {
expect(primitives[1].indices.length).toBe(6); // 2 faces expect(primitives[1].indices.length).toBe(6); // 2 faces
expect(primitives[2].indices.length).toBe(6); // 2 faces expect(primitives[2].indices.length).toBe(6); // 2 faces
expect(primitives[3].indices.length).toBe(6); // 2 faces expect(primitives[3].indices.length).toBe(6); // 2 faces
======= });
function getFirstPosition(data) { function getFirstPosition(data) {
var positions = data.nodes[0].meshes[0].positions; const primitive = getPrimitives(data)[0];
return new Cartesian3(positions.get(0), positions.get(1), positions.get(2)); return new Cartesian3(primitive.positions.get(0), primitive.positions.get(1), primitive.positions.get(2));
} }
function getFirstNormal(data) { function getFirstNormal(data) {
var normals = data.nodes[0].meshes[0].normals; const primitive = getPrimitives(data)[0];
return new Cartesian3(normals.get(0), normals.get(1), normals.get(2)); 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 sameAxis = (inputUpAxis === outputUpAxis);
var options = clone(defaultOptions);
options.inputUpAxis = inputUpAxis; options.inputUpAxis = inputUpAxis;
options.outputUpAxis = outputUpAxis; options.outputUpAxis = outputUpAxis;
return loadObj(objRotatedUrl, options) const data = await loadObj(objRotatedUrl, options);
.then(function(data) { const rotatedPosition = getFirstPosition(data);
var rotatedPosition = getFirstPosition(data); const rotatedNormal = getFirstNormal(data);
var rotatedNormal = getFirstNormal(data); if (sameAxis) {
if (sameAxis) { expect(rotatedPosition).toEqual(position);
expect(rotatedPosition).toEqual(position); expect(rotatedNormal).toEqual(normal);
expect(rotatedNormal).toEqual(normal); } else {
} else { expect(rotatedPosition).not.toEqual(position);
expect(rotatedPosition).not.toEqual(position); expect(rotatedNormal).not.toEqual(normal);
expect(rotatedNormal).not.toEqual(normal); }
}
});
} }
it('performs up axis conversion', function(done) { it('performs up axis conversion', async () => {
expect(loadObj(objRotatedUrl, defaultOptions) const data = await loadObj(objRotatedUrl, options);
.then(function(data) { const position = getFirstPosition(data);
var position = getFirstPosition(data); const normal = getFirstNormal(data);
var normal = getFirstNormal(data);
var axes = ['X', 'Y', 'Z']; const axes = ['X', 'Y', 'Z'];
var axesLength = axes.length; const axesLength = axes.length;
var promises = []; for (let i = 0; i < axesLength; ++i) {
for (var i = 0; i < axesLength; ++i) { for (let j = 0; j < axesLength; ++j) {
for (var j = 0; j < axesLength; ++j) { await checkAxisConversion(axes[i], axes[j], position, normal);
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
}); });
it('throws when file has invalid contents', async () => { it('throws when file has invalid contents', async () => {