Skip to content

Commit

Permalink
Merge pull request xeolabs#470 from tsherif/optimize-tangents
Browse files Browse the repository at this point in the history
Optimize buildTangents function
  • Loading branch information
xeolabs committed Mar 24, 2016
2 parents 0048dd5 + 99de3a3 commit 10595bf
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/core/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -2600,7 +2600,7 @@
*/
window.SceneJS_math_buildTangents = function (positions, indices, uv) {

var tangents = [];
var tangents = new Float32Array(positions.length);

// The vertex arrays needs to be calculated
// before the calculation of the tangents
Expand All @@ -2611,18 +2611,18 @@

var index = indices[location];

var v0 = [positions[index * 3], positions[(index * 3) + 1], positions[(index * 3) + 2]];
var uv0 = [uv[index * 2], uv[(index * 2) + 1]];
var v0 = positions.subarray(index * 3, index * 3 + 3);
var uv0 = uv.subarray(index * 2, index * 2 + 2);

index = indices[location + 1];

var v1 = [positions[index * 3], positions[(index * 3) + 1], positions[(index * 3) + 2]];
var uv1 = [uv[index * 2], uv[(index * 2) + 1]];
var v1 = positions.subarray(index * 3, index * 3 + 3);
var uv1 = uv.subarray(index * 2, index * 2 + 2);

index = indices[location + 2];

var v2 = [positions[index * 3], positions[(index * 3) + 1], positions[(index * 3) + 2]];
var uv2 = [uv[index * 2], uv[(index * 2) + 1]];
var v2 = positions.subarray(index * 3, index * 3 + 3);
var uv2 = uv.subarray(index * 2, index * 2 + 2);

var deltaPos1 = SceneJS_math_subVec3(v1, v0, tempVec3);
var deltaPos2 = SceneJS_math_subVec3(v2, v0, tempVec3b);
Expand All @@ -2639,29 +2639,20 @@
tempVec3g
),
r,
[]
tempVec3f
);

// Average the value of the vectors outs
for (var v = 0; v < 3; v++) {
var addTo = indices[location + v];
if (typeof tangents[addTo] != "undefined") {
tangents[addTo] = SceneJS_math_addVec3(tangents[addTo], tangent, []);
} else {
tangents[addTo] = tangent;
}
}
}
var addTo = indices[location + v] * 3;

// Deconstruct the vectors back into 1D arrays for WebGL

var tangents2 = [];

for (var i = 0; i < tangents.length; i++) {
tangents2 = tangents2.concat(tangents[i]);
tangents[addTo] += tangent[0];
tangents[addTo + 1] += tangent[1];
tangents[addTo + 2] += tangent[2];
}
}

return tangents2;
return tangents;
}

})();

0 comments on commit 10595bf

Please sign in to comment.