Skip to content

Commit

Permalink
Merge branch 'master' into absolute-paths
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse authored Oct 29, 2019
2 parents 5f012d4 + 341797b commit dd163b7
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 24 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Change Log
==========

### 3.?.? - 2019-??-??
### 3.?.? ????-??-??

* Added back `inputUpAxis` and `outputUpAxis`. [#211](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/211)
* Fixed handling of mtl and texture absolute paths. [#219](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/219)
* Fixed specular image not being decoded when referenced by other textures. [#217](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/217)
* Fixed parsing faces that reference non-existing attributes. [#218](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/218)

### 3.0.4 - 2019-07-22

Expand Down
7 changes: 3 additions & 4 deletions lib/createGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,10 @@ function addTexture(gltf, texture) {

function getTexture(gltf, texture) {
let textureIndex;
const name = texture.name;
const textures = gltf.textures;
const length = textures.length;
const images = gltf.images;
const length = images.length;
for (let i = 0; i < length; ++i) {
if (textures[i].name === name) {
if (images[i].extras._obj2gltf === texture) {
textureIndex = i;
break;
}
Expand Down
32 changes: 25 additions & 7 deletions lib/loadMtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const readLines = require('./readLines');
const Texture = require('./Texture');

const CesiumMath = Cesium.Math;
const clone = Cesium.clone;
const combine = Cesium.combine;
const defaultValue = Cesium.defaultValue;
const defined = Cesium.defined;
Expand Down Expand Up @@ -178,13 +179,30 @@ function loadMtl(mtlPath, options) {
material.ambientTexture = undefined;
}

loadMaterialTexture(material, 'diffuseTexture', diffuseAlphaTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'ambientTexture', ambientTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'emissiveTexture', emissiveTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'specularTexture', specularTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'specularShininessTexture', specularShinessTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'normalTexture', normalTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
loadMaterialTexture(material, 'alphaTexture', alphaTextureOptions, mtlDirectory, texturePromiseMap, texturePromises, options);
const textureNames = ['diffuseTexture', 'ambientTexture', 'emissiveTexture', 'specularTexture', 'specularShininessTexture', 'normalTexture', 'alphaTexture'];
const textureOptions = [diffuseAlphaTextureOptions, ambientTextureOptions, emissiveTextureOptions, specularTextureOptions, specularShinessTextureOptions, normalTextureOptions, alphaTextureOptions];

const sharedOptions = {};
textureNames.forEach(function(name, index) {
const texturePath = material[name];
const originalOptions = textureOptions[index];
if (defined(texturePath) && defined(originalOptions)) {
if (!defined(sharedOptions[texturePath])) {
sharedOptions[texturePath] = clone(originalOptions);
}
const options = sharedOptions[texturePath];
options.checkTransparency = options.checkTransparency || originalOptions.checkTransparency;
options.decode = options.decode || originalOptions.decode;
options.keepSource = options.keepSource || !originalOptions.decode || !originalOptions.checkTransparency;
}
});

textureNames.forEach(function(name) {
const texturePath = material[name];
if (defined(texturePath)) {
loadMaterialTexture(material, name, sharedOptions[texturePath], mtlDirectory, texturePromiseMap, texturePromises, options);
}
});
}

return readLines(mtlPath, parseLine)
Expand Down
15 changes: 12 additions & 3 deletions lib/loadObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ function loadObj(objPath, options) {

function createVertex(p, u, n) {
// Positions
if (defined(p)) {
if (defined(p) && (globalPositions.length > 0)) {
if (p * 3 >= globalPositions.length) {
throw new RuntimeError(`Position index ${p} is out of bounds`);
}
const px = globalPositions.get(p * 3);
const py = globalPositions.get(p * 3 + 1);
const pz = globalPositions.get(p * 3 + 2);
Expand All @@ -205,7 +208,10 @@ function loadObj(objPath, options) {
}

// Normals
if (defined(n)) {
if (defined(n) && (globalNormals.length > 0)) {
if (n * 3 >= globalNormals.length) {
throw new RuntimeError(`Normal index ${n} is out of bounds`);
}
const nx = globalNormals.get(n * 3);
const ny = globalNormals.get(n * 3 + 1);
const nz = globalNormals.get(n * 3 + 2);
Expand All @@ -215,7 +221,10 @@ function loadObj(objPath, options) {
}

// UVs
if (defined(u)) {
if (defined(u) && (globalUvs.length > 0)) {
if (u * 2 >= globalUvs.length) {
throw new RuntimeError(`UV index ${u} is out of bounds`);
}
const ux = globalUvs.get(u * 2);
const uy = globalUvs.get(u * 2 + 1);
primitive.uvs.push(ux);
Expand Down
15 changes: 12 additions & 3 deletions lib/loadTexture.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = loadTexture;
* @param {Object} [options] An object with the following properties:
* @param {Boolean} [options.checkTransparency=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
* @param {Boolean} [options.decode=false] Whether to decode the texture.
* @param {Boolean} [options.keepSource=false] Whether to keep the source image contents in memory.
* @returns {Promise} A promise resolving to a Texture object.
*
* @private
Expand All @@ -27,6 +28,7 @@ function loadTexture(texturePath, options) {
options = defaultValue(options, {});
options.checkTransparency = defaultValue(options.checkTransparency, false);
options.decode = defaultValue(options.decode, false);
options.keepSource = defaultValue(options.keepSource, false);

return fsExtra.readFile(texturePath)
.then(function(source) {
Expand All @@ -46,7 +48,10 @@ function loadTexture(texturePath, options) {
}

if (defined(decodePromise)) {
return decodePromise.thenReturn(texture);
return decodePromise
.then(function() {
return texture;
});
}

return texture;
Expand Down Expand Up @@ -109,7 +114,9 @@ function decodePng(texture, options) {
texture.pixels = decodedResults.data;
texture.width = decodedResults.width;
texture.height = decodedResults.height;
texture.source = undefined; // Unload resources
if (!options.keepSource) {
texture.source = undefined; // Unload resources
}
}
});
}
Expand All @@ -122,6 +129,8 @@ function decodeJpeg(texture, options) {
texture.pixels = decodedResults.data;
texture.width = decodedResults.width;
texture.height = decodedResults.height;
texture.source = undefined; // Unload resources
if (!options.keepSource) {
texture.source = undefined; // Unload resources
}
}
}
11 changes: 10 additions & 1 deletion lib/readLines.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ function readLines(path, callback) {
const lineReader = readline.createInterface({
input : stream
});
lineReader.on('line', callback);

const callbackWrapper = function(line) {
try {
callback(line);
} catch (error) {
reject(error);
}
};

lineReader.on('line', callbackWrapper);
});
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
"cloc": "^2.5.0",
"coveralls": "^3.0.4",
"eslint": "^6.0.0",
"eslint-config-cesium": "^7.0.0",
"eslint-config-cesium": "^8.0.0",
"gulp": "^4.0.2",
"jasmine": "^3.4.0",
"jasmine-spec-reporter": "^4.2.1",
"jsdoc": "^3.6.2",
"nyc": "^14.1.1",
"open": "^6.3.0",
"open": "^7.0.0",
"requirejs": "^2.3.6"
},
"scripts": {
Expand Down
36 changes: 36 additions & 0 deletions specs/data/box-incomplete-attributes/box-incomplete-normals.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 0.0000
vt 0.0000 1.0000
vn 0.000000 0.000000 1.000000
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/5/2 4/6/2 8/7/2 7/8/2
f 7/9/3 8/10/3 6/11/3 5/12/3
f 5/13/4 6/14/4 2/15/4 1/16/4
f 3/5/5 7/17/5 5/18/5 1/16/5
f 8/19/6 4/6/6 2/15/6 6/20/6
34 changes: 34 additions & 0 deletions specs/data/box-incomplete-attributes/box-incomplete-positions.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
o Cube
v -1.000000 -1.000000 1.000000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 0.0000
vt 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/5/2 4/6/2 8/7/2 7/8/2
f 7/9/3 8/10/3 6/11/3 5/12/3
f 5/13/4 6/14/4 2/15/4 1/16/4
f 3/5/5 7/17/5 5/18/5 1/16/5
f 8/19/6 4/6/6 2/15/6 6/20/6
22 changes: 22 additions & 0 deletions specs/data/box-incomplete-attributes/box-incomplete-uvs.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vt 0.0000 0.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/5/2 4/6/2 8/7/2 7/8/2
f 7/9/3 8/10/3 6/11/3 5/12/3
f 5/13/4 6/14/4 2/15/4 1/16/4
f 3/5/5 7/17/5 5/18/5 1/16/5
f 8/19/6 4/6/6 2/15/6 6/20/6
12 changes: 12 additions & 0 deletions specs/data/box-missing-attributes/box-missing-attributes.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Blender MTL File: 'box.blend'
# Material Count: 1

newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
20 changes: 20 additions & 0 deletions specs/data/box-missing-attributes/box-missing-attributes.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Blender v2.78 (sub 0) OBJ File: 'box.blend'
# www.blender.org
mtllib box-missing-attributes.mtl
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
usemtl Material
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/5/2 4/6/2 8/7/2 7/8/2
f 7/9/3 8/10/3 6/11/3 5/12/3
f 5/13/4 6/14/4 2/15/4 1/16/4
f 3/5/5 7/17/5 5/18/5 1/16/5
f 8/19/6 4/6/6 2/15/6 6/20/6
14 changes: 14 additions & 0 deletions specs/data/box-shared-textures-2/box-shared-textures-2.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Blender MTL File: 'box.blend'
# Material Count: 1

newmtl Material
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd cesium.png
map_Ke cesium.png
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Blender v2.78 (sub 0) OBJ File: 'box.blend'
# www.blender.org
mtllib box-diffuse-ambient-same.mtl
mtllib box-shared-textures-2.mtl
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ d 1.000000
illum 2
map_Kd cesium.png
map_Ka cesium.png
map_Ks cesium.png
46 changes: 46 additions & 0 deletions specs/data/box-shared-textures/box-shared-textures.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Blender v2.78 (sub 0) OBJ File: 'box.blend'
# www.blender.org
mtllib box-shared-textures.mtl
o Cube
v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 0.0000 0.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 1.0000
vt 1.0000 0.0000
vt 1.0000 1.0000
vt 0.0000 0.0000
vt 0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
usemtl Material
s off
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/5/2 4/6/2 8/7/2 7/8/2
f 7/9/3 8/10/3 6/11/3 5/12/3
f 5/13/4 6/14/4 2/15/4 1/16/4
f 3/5/5 7/17/5 5/18/5 1/16/5
f 8/19/6 4/6/6 2/15/6 6/20/6
Binary file added specs/data/box-shared-textures/cesium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit dd163b7

Please sign in to comment.