Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize normals #136

Merged
merged 2 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

### 2.3.0 ???

* Fixed handling of unnormalized input normals. [#136](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/136)

### 2.2.0 2017-01-29

* Fixed handling of materials where the diffuse and ambient texture are the same. [#127](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/127)
Expand Down
12 changes: 9 additions & 3 deletions lib/loadObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,15 @@ function loadObj(objPath, options) {
positions.push(parseFloat(result[2]));
positions.push(parseFloat(result[3]));
} else if ((result = normalPattern.exec(line) ) !== null) {
normals.push(parseFloat(result[1]));
normals.push(parseFloat(result[2]));
normals.push(parseFloat(result[3]));
var 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);
}
normals.push(normal.x);
normals.push(normal.y);
normals.push(normal.z);
} else if ((result = uvPattern.exec(line)) !== null) {
uvs.push(parseFloat(result[1]));
uvs.push(1.0 - parseFloat(result[2])); // Flip y so 0.0 is the bottom of the image
Expand Down
12 changes: 12 additions & 0 deletions specs/data/box-unnormalized/box-unnormalized.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Blender MTL File: 'None'
# Material Count: 1

newmtl Material
Ns 96.078431
Ka 0.100000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.100000
Ni 1.000000
d 1.000000
illum 2
46 changes: 46 additions & 0 deletions specs/data/box-unnormalized/box-unnormalized.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Blender v2.78 (sub 0) OBJ File: ''
# www.blender.org
mtllib box-unnormalized.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 0.0000 0.0000 0.0000
vn 0.0000 0.0000 0.5
vn 1.0000 0.1000 0.0000
vn 0.0000 0.0000 -0.9
vn 0.0000 1.0000 0.0000
vn 10.0000 -10.0000 5.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
20 changes: 20 additions & 0 deletions specs/lib/loadObjSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var Promise = require('bluebird');
var loadObj = require('../../lib/loadObj');
var obj2gltf = require('../../lib/obj2gltf');

var Cartesian3 = Cesium.Cartesian3;
var CesiumMath = Cesium.Math;
var clone = Cesium.clone;
var RuntimeError = Cesium.RuntimeError;

Expand Down Expand Up @@ -33,6 +35,7 @@ var objMissingTexturePath = 'specs/data/box-missing-texture/box-missing-texture.
var objSubdirectoriesPath = 'specs/data/box-subdirectories/box-textured.obj';
var objInvalidContentsPath = 'specs/data/box/box.mtl';
var objConcavePath = 'specs/data/concave/concave.obj';
var objUnnormalizedPath = 'specs/data/box-unnormalized/box-unnormalized.obj';
var objInvalidPath = 'invalid.obj';

function getMeshes(data) {
Expand Down Expand Up @@ -107,6 +110,23 @@ describe('loadObj', function() {
}), done).toResolve();
});

it('normalizes normals', function(done) {
expect(loadObj(objUnnormalizedPath, options)
.then(function(data) {
var scratchNormal = new Cesium.Cartesian3();
var mesh = getMeshes(data)[0];
var normals = mesh.normals;
var normalsLength = normals.length / 3;
for (var i = 0; i < normalsLength; ++i) {
var normalX = normals.get(i * 3);
var normalY = normals.get(i * 3 + 1);
var normalZ = normals.get(i * 3 + 2);
var normal = Cartesian3.fromElements(normalX, normalY, normalZ, scratchNormal);
expect(Cartesian3.magnitude(normal)).toEqualEpsilon(1.0, CesiumMath.EPSILON5);
}
}), done).toResolve();
});

it('loads obj with uvs', function(done) {
expect(loadObj(objUvsPath, options)
.then(function(data) {
Expand Down