From cab02b9c03a285c20e84e497b0dca9ddd1a1234c Mon Sep 17 00:00:00 2001 From: Tarek Sherif Date: Mon, 25 Apr 2016 13:01:07 -0400 Subject: [PATCH] Optimizations for depth sortin --- examples/benchmarks_40000boxes.html | 4 ++-- examples/benchmarks_5000boxes.html | 4 ++-- examples/transforms_modelling_translate.html | 4 ++-- src/core/display/object.js | 11 ++++------ src/core/math.js | 14 ++----------- src/core/scene/modelXFormStack.js | 22 +++++++++----------- 6 files changed, 22 insertions(+), 37 deletions(-) diff --git a/examples/benchmarks_40000boxes.html b/examples/benchmarks_40000boxes.html index 6edf7e01..dd791612 100644 --- a/examples/benchmarks_40000boxes.html +++ b/examples/benchmarks_40000boxes.html @@ -16,7 +16,7 @@ - + @@ -82,4 +82,4 @@ - \ No newline at end of file + diff --git a/examples/benchmarks_5000boxes.html b/examples/benchmarks_5000boxes.html index 07aa6138..91a588d0 100644 --- a/examples/benchmarks_5000boxes.html +++ b/examples/benchmarks_5000boxes.html @@ -16,7 +16,7 @@ - + @@ -81,4 +81,4 @@ - \ No newline at end of file + diff --git a/examples/transforms_modelling_translate.html b/examples/transforms_modelling_translate.html index caf3157b..99955882 100644 --- a/examples/transforms_modelling_translate.html +++ b/examples/transforms_modelling_translate.html @@ -12,7 +12,7 @@ -webkit-user-select: none; } - + @@ -91,4 +91,4 @@ - \ No newline at end of file + diff --git a/src/core/display/object.js b/src/core/display/object.js index 3b30cfff..965fd102 100644 --- a/src/core/display/object.js +++ b/src/core/display/object.js @@ -62,22 +62,19 @@ var SceneJS_Object = function(id) { }; (function() { - var tempVec4 = SceneJS_math_vec4(); - var tempMat4 = SceneJS_math_mat4(); + var tempVec4 = new SceneJS_math_vec4(); SceneJS_Object.prototype.getDepth = function() { if (!this.centroid) { this.centroid = this._calculateCentroid(this); } - this.modelTransform.build(); - this.viewTransform.rebuild(); - var modelMatrix = this.modelTransform.mat; var viewMatrix = this.viewTransform.mat; - var mvMatrix = SceneJS_math_mulMat4(viewMatrix, modelMatrix, tempMat4); - var viewCentroid = SceneJS_math_transformVector4(mvMatrix, this.centroid, tempVec4); + var viewCentroid = SceneJS_math_transformVector4(modelMatrix, this.centroid, tempVec4); + + SceneJS_math_transformVector4(viewMatrix, viewCentroid, viewCentroid); return -viewCentroid[2]; }; diff --git a/src/core/math.js b/src/core/math.js index d7a4f2f5..8dc53392 100644 --- a/src/core/math.js +++ b/src/core/math.js @@ -616,22 +616,12 @@ return SceneJS_math_mulVec3Scalar(v, f, dest); }; -// @private + // @private window.SceneJS_math_normalizeVec2 = function (v, dest) { var f = 1.0 / SceneJS_math_lenVec2(v); return SceneJS_math_mulVec2Scalar(v, f, dest); }; - /** @private */ - window.SceneJS_math_mat4 = function () { - return new Array(16); - }; - - /** @private */ - window.SceneJS_math_dupMat4 = function (m) { - return m.slice(0, 16); - }; - /** @private */ window.SceneJS_math_getCellMat4 = function (m, row, col) { return m[row + col * 4]; @@ -2616,4 +2606,4 @@ return tangents; } -})(); \ No newline at end of file +})(); diff --git a/src/core/scene/modelXFormStack.js b/src/core/scene/modelXFormStack.js index b35c2912..e481cd88 100644 --- a/src/core/scene/modelXFormStack.js +++ b/src/core/scene/modelXFormStack.js @@ -119,6 +119,9 @@ var SceneJS_modelXFormStack = new (function () { /** * Pre-multiply matrices at cores on path up to root into matrix at this core */ + + var matrix = new SceneJS_math_mat4(); + core.build = function () { if (core.matrixDirty) { @@ -130,11 +133,9 @@ var SceneJS_modelXFormStack = new (function () { var parent = core.parent; - var matrix; - if (parent) { - matrix = core.matrix.slice(0); + matrix.set(core.matrix); while (parent) { @@ -144,9 +145,8 @@ var SceneJS_modelXFormStack = new (function () { parent.buildMatrix(); } parent.mat.set(parent.matrix); - parent.normalMat.set( - SceneJS_math_transposeMat4( - SceneJS_math_inverseMat4(parent.matrix, SceneJS_math_mat4()))); + SceneJS_math_inverseMat4(parent.matrix, parent.normalMat); + SceneJS_math_transposeMat4(parent.normalMat, parent.normalMat); parent.matrixDirty = false; } @@ -164,7 +164,7 @@ var SceneJS_modelXFormStack = new (function () { } else { - matrix = core.matrix; + matrix.set(core.matrix); } // if (!core.mat) { @@ -178,12 +178,10 @@ var SceneJS_modelXFormStack = new (function () { core.mat.set(matrix); - core.normalMat.set( - SceneJS_math_transposeMat4( - SceneJS_math_inverseMat4(matrix, SceneJS_math_mat4()))); - //} + SceneJS_math_inverseMat4(matrix, core.normalMat); + SceneJS_math_transposeMat4(core.normalMat, core.normalMat); - core.dirty = false; + core.dirty = false; }; };