فهرست منبع

Fixing cache issue with computeWorldMatrix

David catuhe 9 سال پیش
والد
کامیت
8971e61102

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 14 - 14
dist/preview release/babylon.core.js


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 1911 - 1910
dist/preview release/babylon.d.ts


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 21 - 21
dist/preview release/babylon.js


+ 50 - 29
dist/preview release/babylon.max.js

@@ -7808,7 +7808,7 @@ var BABYLON;
             if (this._isDirty) {
                 return false;
             }
-            if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
+            if (this.billboardMode !== this._cache.billboardMode || this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
                 return false;
             if (this._cache.pivotMatrixUpdated) {
                 return false;
@@ -7837,6 +7837,7 @@ var BABYLON;
             this._cache.scaling = BABYLON.Vector3.Zero();
             this._cache.rotation = BABYLON.Vector3.Zero();
             this._cache.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 0);
+            this._cache.billboardMode = -1;
         };
         AbstractMesh.prototype.markAsDirty = function (property) {
             if (property === "rotation") {
@@ -7869,6 +7870,7 @@ var BABYLON;
             this._cache.position.copyFrom(this.position);
             this._cache.scaling.copyFrom(this.scaling);
             this._cache.pivotMatrixUpdated = false;
+            this._cache.billboardMode = this.billboardMode;
             this._currentRenderId = this.getScene().getRenderId();
             this._isDirty = false;
             // Scaling
@@ -15023,6 +15025,14 @@ var BABYLON;
         };
         // Cylinder and cone
         Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, subdivisions, scene, updatable, sideOrientation) {
+            if (scene === undefined || !(scene instanceof BABYLON.Scene)) {
+                if (scene !== undefined) {
+                    sideOrientation = updatable || Mesh.DEFAULTSIDE;
+                    updatable = scene;
+                }
+                scene = subdivisions;
+                subdivisions = 1;
+            }
             var options = {
                 height: height,
                 diameterTop: diameterTop,
@@ -15112,7 +15122,7 @@ var BABYLON;
             var options = {
                 shape: shape,
                 radius: radius,
-                tesselation: tessellation,
+                tessellation: tessellation,
                 sideOrientation: sideOrientation,
                 updatable: updatable
             };
@@ -15266,8 +15276,9 @@ var BABYLON;
             var matWeightIdx = 0;
             var inf;
             for (var index = 0; index < positionsData.length; index += 3, matWeightIdx += 4) {
+                var weight;
                 for (inf = 0; inf < 4; inf++) {
-                    var weight = matricesWeightsData[matWeightIdx + inf];
+                    weight = matricesWeightsData[matWeightIdx + inf];
                     if (weight > 0) {
                         BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[matWeightIdx + inf] * 16, weight, tempMatrix);
                         finalMatrix.addToSelf(tempMatrix);
@@ -15277,7 +15288,7 @@ var BABYLON;
                 }
                 if (needExtras) {
                     for (inf = 0; inf < 4; inf++) {
-                        var weight = matricesWeightsExtraData[matWeightIdx + inf];
+                        weight = matricesWeightsExtraData[matWeightIdx + inf];
                         if (weight > 0) {
                             BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesExtraData[matWeightIdx + inf] * 16, weight, tempMatrix);
                             finalMatrix.addToSelf(tempMatrix);
@@ -23781,6 +23792,7 @@ var BABYLON;
             var diameterBottom = options.diameterBottom || options.diameter || 1;
             var tessellation = options.tessellation || 24;
             var subdivisions = options.subdivisions || 1;
+            var hasRings = options.hasRings;
             var arc = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
             var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
             var faceUV = options.faceUV || new Array(3);
@@ -23809,36 +23821,43 @@ var BABYLON;
             // positions, normals, uvs
             var i;
             var j;
+            var r;
+            var ringIdx = 1;
             for (i = 0; i <= subdivisions; i++) {
                 h = i / subdivisions;
                 radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
-                for (j = 0; j <= tessellation; j++) {
-                    angle = j * angle_step;
-                    ringVertex.x = Math.cos(-angle) * radius;
-                    ringVertex.y = -height / 2 + h * height;
-                    ringVertex.z = Math.sin(-angle) * radius;
-                    if (diameterTop === 0 && i === subdivisions) {
-                        // if no top cap, reuse former normals
-                        ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
-                        ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
-                        ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
-                    }
-                    else {
-                        ringNormal.x = ringVertex.x;
-                        ringNormal.z = ringVertex.z;
-                        ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
-                        ringNormal.normalize();
-                    }
-                    positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
-                    normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
-                    uvs.push(faceUV[1].x + (faceUV[1].z - faceUV[1].x) * j / tessellation, faceUV[1].y + (faceUV[1].w - faceUV[1].y) * h);
-                    if (faceColors) {
-                        colors.push(faceColors[1].r, faceColors[1].g, faceColors[1].b, faceColors[1].a);
+                ringIdx = (hasRings && i !== 0 && i !== subdivisions) ? 2 : 1;
+                for (r = 0; r < ringIdx; r++) {
+                    for (j = 0; j <= tessellation; j++) {
+                        angle = j * angle_step;
+                        ringVertex.x = Math.cos(-angle) * radius;
+                        ringVertex.y = -height / 2 + h * height;
+                        ringVertex.z = Math.sin(-angle) * radius;
+                        if (diameterTop === 0 && i === subdivisions) {
+                            // if no top cap, reuse former normals
+                            ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
+                            ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
+                            ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
+                        }
+                        else {
+                            ringNormal.x = ringVertex.x;
+                            ringNormal.z = ringVertex.z;
+                            ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
+                            ringNormal.normalize();
+                        }
+                        positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
+                        normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
+                        uvs.push(faceUV[1].x + (faceUV[1].z - faceUV[1].x) * j / tessellation, faceUV[1].y + (faceUV[1].w - faceUV[1].y) * h);
+                        if (faceColors) {
+                            colors.push(faceColors[1].r, faceColors[1].g, faceColors[1].b, faceColors[1].a);
+                        }
                     }
                 }
             }
             // indices
-            for (i = 0; i < subdivisions; i++) {
+            var s;
+            i = 0;
+            for (s = 0; s < subdivisions; s++) {
                 for (j = 0; j < tessellation; j++) {
                     var i0 = i * (tessellation + 1) + j;
                     var i1 = (i + 1) * (tessellation + 1) + j;
@@ -23847,6 +23866,7 @@ var BABYLON;
                     indices.push(i0, i1, i2);
                     indices.push(i3, i2, i1);
                 }
+                i = (hasRings) ? (i + 2) : (i + 1);
             }
             // Caps
             var createCylinderCap = function (isTop) {
@@ -24301,14 +24321,15 @@ var BABYLON;
             // prepare array of 3 vector (empty) (to be worked in place, shared for each face)
             var face_vertex_pos = new Array(3);
             var face_vertex_uv = new Array(3);
-            for (var v012 = 0; v012 < 3; v012++) {
+            var v012;
+            for (v012 = 0; v012 < 3; v012++) {
                 face_vertex_pos[v012] = BABYLON.Vector3.Zero();
                 face_vertex_uv[v012] = BABYLON.Vector2.Zero();
             }
             // create all with normals
             for (var face = 0; face < 20; face++) {
                 // 3 vertex per face
-                for (var v012 = 0; v012 < 3; v012++) {
+                for (v012 = 0; v012 < 3; v012++) {
                     // look up vertex 0,1,2 to its index in 0 to 11
                     var v_id = ico_indices[3 * face + v012];
                     // vertex have 3D position (x,y,z)

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 21 - 21
dist/preview release/babylon.noworker.js


+ 3 - 1
src/Mesh/babylon.abstractMesh.js

@@ -354,7 +354,7 @@ var BABYLON;
             if (this._isDirty) {
                 return false;
             }
-            if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
+            if (this.billboardMode !== this._cache.billboardMode || this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
                 return false;
             if (this._cache.pivotMatrixUpdated) {
                 return false;
@@ -383,6 +383,7 @@ var BABYLON;
             this._cache.scaling = BABYLON.Vector3.Zero();
             this._cache.rotation = BABYLON.Vector3.Zero();
             this._cache.rotationQuaternion = new BABYLON.Quaternion(0, 0, 0, 0);
+            this._cache.billboardMode = -1;
         };
         AbstractMesh.prototype.markAsDirty = function (property) {
             if (property === "rotation") {
@@ -415,6 +416,7 @@ var BABYLON;
             this._cache.position.copyFrom(this.position);
             this._cache.scaling.copyFrom(this.scaling);
             this._cache.pivotMatrixUpdated = false;
+            this._cache.billboardMode = this.billboardMode;
             this._currentRenderId = this.getScene().getRenderId();
             this._isDirty = false;
             // Scaling

+ 3 - 1
src/Mesh/babylon.abstractMesh.ts

@@ -376,7 +376,7 @@
                 return false;
             }
 
-            if (this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
+            if (this.billboardMode !== this._cache.billboardMode || this.billboardMode !== AbstractMesh.BILLBOARDMODE_NONE)
                 return false;
 
             if (this._cache.pivotMatrixUpdated) {
@@ -412,6 +412,7 @@
             this._cache.scaling = Vector3.Zero();
             this._cache.rotation = Vector3.Zero();
             this._cache.rotationQuaternion = new Quaternion(0, 0, 0, 0);
+            this._cache.billboardMode = -1;
         }
 
         public markAsDirty(property: string): void {
@@ -454,6 +455,7 @@
             this._cache.position.copyFrom(this.position);
             this._cache.scaling.copyFrom(this.scaling);
             this._cache.pivotMatrixUpdated = false;
+            this._cache.billboardMode = this.billboardMode;
             this._currentRenderId = this.getScene().getRenderId();
             this._isDirty = false;
 

+ 12 - 3
src/Mesh/babylon.mesh.js

@@ -1100,6 +1100,14 @@ var BABYLON;
         };
         // Cylinder and cone
         Mesh.CreateCylinder = function (name, height, diameterTop, diameterBottom, tessellation, subdivisions, scene, updatable, sideOrientation) {
+            if (scene === undefined || !(scene instanceof BABYLON.Scene)) {
+                if (scene !== undefined) {
+                    sideOrientation = updatable || Mesh.DEFAULTSIDE;
+                    updatable = scene;
+                }
+                scene = subdivisions;
+                subdivisions = 1;
+            }
             var options = {
                 height: height,
                 diameterTop: diameterTop,
@@ -1189,7 +1197,7 @@ var BABYLON;
             var options = {
                 shape: shape,
                 radius: radius,
-                tesselation: tessellation,
+                tessellation: tessellation,
                 sideOrientation: sideOrientation,
                 updatable: updatable
             };
@@ -1343,8 +1351,9 @@ var BABYLON;
             var matWeightIdx = 0;
             var inf;
             for (var index = 0; index < positionsData.length; index += 3, matWeightIdx += 4) {
+                var weight;
                 for (inf = 0; inf < 4; inf++) {
-                    var weight = matricesWeightsData[matWeightIdx + inf];
+                    weight = matricesWeightsData[matWeightIdx + inf];
                     if (weight > 0) {
                         BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[matWeightIdx + inf] * 16, weight, tempMatrix);
                         finalMatrix.addToSelf(tempMatrix);
@@ -1354,7 +1363,7 @@ var BABYLON;
                 }
                 if (needExtras) {
                     for (inf = 0; inf < 4; inf++) {
-                        var weight = matricesWeightsExtraData[matWeightIdx + inf];
+                        weight = matricesWeightsExtraData[matWeightIdx + inf];
                         if (weight > 0) {
                             BABYLON.Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesExtraData[matWeightIdx + inf] * 16, weight, tempMatrix);
                             finalMatrix.addToSelf(tempMatrix);

+ 8 - 6
src/Mesh/babylon.mesh.ts

@@ -1295,7 +1295,7 @@
                 }
                 scene = <Scene>subdivisions;
                 subdivisions = 1;
-            }            
+            }
 
             var options = {
                 height: height,
@@ -1580,10 +1580,11 @@
             var tempMatrix = new Matrix();
 
             var matWeightIdx = 0;
-            var inf : number;
+            var inf: number;
             for (var index = 0; index < positionsData.length; index += 3, matWeightIdx += 4) {
+                var weight: number;
                 for (inf = 0; inf < 4; inf++) {
-                    var weight = matricesWeightsData[matWeightIdx + inf];
+                    weight = matricesWeightsData[matWeightIdx + inf];
                     if (weight > 0) {
                         Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesData[matWeightIdx + inf] * 16, weight, tempMatrix);
                         finalMatrix.addToSelf(tempMatrix);
@@ -1592,15 +1593,15 @@
                 }
                 if (needExtras) {
                     for (inf = 0; inf < 4; inf++) {
-                        var weight = matricesWeightsExtraData[matWeightIdx + inf];
+                        weight = matricesWeightsExtraData[matWeightIdx + inf];
                         if (weight > 0) {
                             Matrix.FromFloat32ArrayToRefScaled(skeletonMatrices, matricesIndicesExtraData[matWeightIdx + inf] * 16, weight, tempMatrix);
                             finalMatrix.addToSelf(tempMatrix);
 
-                        } else break;           
+                        } else break;
                     }
                 }
-                
+
                 Vector3.TransformCoordinatesFromFloatsToRef(this._sourcePositions[index], this._sourcePositions[index + 1], this._sourcePositions[index + 2], finalMatrix, tempVector3);
                 tempVector3.toArray(positionsData, index);
 
@@ -1715,3 +1716,4 @@
 
 
 
+

+ 35 - 25
src/Mesh/babylon.mesh.vertexData.js

@@ -662,6 +662,7 @@ var BABYLON;
             var diameterBottom = options.diameterBottom || options.diameter || 1;
             var tessellation = options.tessellation || 24;
             var subdivisions = options.subdivisions || 1;
+            var hasRings = options.hasRings;
             var arc = (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
             var sideOrientation = (options.sideOrientation === 0) ? 0 : options.sideOrientation || BABYLON.Mesh.DEFAULTSIDE;
             var faceUV = options.faceUV || new Array(3);
@@ -690,36 +691,43 @@ var BABYLON;
             // positions, normals, uvs
             var i;
             var j;
+            var r;
+            var ringIdx = 1;
             for (i = 0; i <= subdivisions; i++) {
                 h = i / subdivisions;
                 radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
-                for (j = 0; j <= tessellation; j++) {
-                    angle = j * angle_step;
-                    ringVertex.x = Math.cos(-angle) * radius;
-                    ringVertex.y = -height / 2 + h * height;
-                    ringVertex.z = Math.sin(-angle) * radius;
-                    if (diameterTop === 0 && i === subdivisions) {
-                        // if no top cap, reuse former normals
-                        ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
-                        ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
-                        ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
-                    }
-                    else {
-                        ringNormal.x = ringVertex.x;
-                        ringNormal.z = ringVertex.z;
-                        ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
-                        ringNormal.normalize();
-                    }
-                    positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
-                    normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
-                    uvs.push(faceUV[1].x + (faceUV[1].z - faceUV[1].x) * j / tessellation, faceUV[1].y + (faceUV[1].w - faceUV[1].y) * h);
-                    if (faceColors) {
-                        colors.push(faceColors[1].r, faceColors[1].g, faceColors[1].b, faceColors[1].a);
+                ringIdx = (hasRings && i !== 0 && i !== subdivisions) ? 2 : 1;
+                for (r = 0; r < ringIdx; r++) {
+                    for (j = 0; j <= tessellation; j++) {
+                        angle = j * angle_step;
+                        ringVertex.x = Math.cos(-angle) * radius;
+                        ringVertex.y = -height / 2 + h * height;
+                        ringVertex.z = Math.sin(-angle) * radius;
+                        if (diameterTop === 0 && i === subdivisions) {
+                            // if no top cap, reuse former normals
+                            ringNormal.x = normals[normals.length - (tessellation + 1) * 3];
+                            ringNormal.y = normals[normals.length - (tessellation + 1) * 3 + 1];
+                            ringNormal.z = normals[normals.length - (tessellation + 1) * 3 + 2];
+                        }
+                        else {
+                            ringNormal.x = ringVertex.x;
+                            ringNormal.z = ringVertex.z;
+                            ringNormal.y = Math.sqrt(ringNormal.x * ringNormal.x + ringNormal.z * ringNormal.z) * tan;
+                            ringNormal.normalize();
+                        }
+                        positions.push(ringVertex.x, ringVertex.y, ringVertex.z);
+                        normals.push(ringNormal.x, ringNormal.y, ringNormal.z);
+                        uvs.push(faceUV[1].x + (faceUV[1].z - faceUV[1].x) * j / tessellation, faceUV[1].y + (faceUV[1].w - faceUV[1].y) * h);
+                        if (faceColors) {
+                            colors.push(faceColors[1].r, faceColors[1].g, faceColors[1].b, faceColors[1].a);
+                        }
                     }
                 }
             }
             // indices
-            for (i = 0; i < subdivisions; i++) {
+            var s;
+            i = 0;
+            for (s = 0; s < subdivisions; s++) {
                 for (j = 0; j < tessellation; j++) {
                     var i0 = i * (tessellation + 1) + j;
                     var i1 = (i + 1) * (tessellation + 1) + j;
@@ -728,6 +736,7 @@ var BABYLON;
                     indices.push(i0, i1, i2);
                     indices.push(i3, i2, i1);
                 }
+                i = (hasRings) ? (i + 2) : (i + 1);
             }
             // Caps
             var createCylinderCap = function (isTop) {
@@ -1182,14 +1191,15 @@ var BABYLON;
             // prepare array of 3 vector (empty) (to be worked in place, shared for each face)
             var face_vertex_pos = new Array(3);
             var face_vertex_uv = new Array(3);
-            for (var v012 = 0; v012 < 3; v012++) {
+            var v012;
+            for (v012 = 0; v012 < 3; v012++) {
                 face_vertex_pos[v012] = BABYLON.Vector3.Zero();
                 face_vertex_uv[v012] = BABYLON.Vector2.Zero();
             }
             // create all with normals
             for (var face = 0; face < 20; face++) {
                 // 3 vertex per face
-                for (var v012 = 0; v012 < 3; v012++) {
+                for (v012 = 0; v012 < 3; v012++) {
                     // look up vertex 0,1,2 to its index in 0 to 11
                     var v_id = ico_indices[3 * face + v012];
                     // vertex have 3D position (x,y,z)

+ 44 - 71
src/Mesh/babylon.mesh.vertexData.ts

@@ -848,7 +848,7 @@
             for (i = 0; i <= subdivisions; i++) {
                 h = i / subdivisions;
                 radius = (h * (diameterTop - diameterBottom) + diameterBottom) / 2;
-                ringIdx = (hasRings && i != 0 && i != subdivisions) ? 2 : 1;
+                ringIdx = (hasRings && i !== 0 && i !== subdivisions) ? 2 : 1;
                 for (r = 0; r < ringIdx; r++) {
                     for (j = 0; j <= tessellation; j++) {
                         angle = j * angle_step;
@@ -892,7 +892,7 @@
                 i = (hasRings) ? (i + 2) : (i + 1);
             }
 
-             // Caps
+            // Caps
             var createCylinderCap = isTop => {
                 var radius = isTop ? diameterTop / 2 : diameterBottom / 2;
                 if (radius === 0) {
@@ -1394,7 +1394,7 @@
             return vertexData;
         }
 
-        public static CreateIcoSphere(options: {radius?: number, radiusX?: number, radiusY?: number, radiusZ?: number, flat?: number, subdivisions?: number, sideOrientation?: number}): VertexData {
+        public static CreateIcoSphere(options: { radius?: number, radiusX?: number, radiusY?: number, radiusZ?: number, flat?: number, subdivisions?: number, sideOrientation?: number }): VertexData {
             var sideOrientation = options.sideOrientation || Mesh.DEFAULTSIDE;
             var radius = options.radius || 1;
             var flat = (options.flat === undefined) ? true : options.flat;
@@ -1451,14 +1451,15 @@
             // prepare array of 3 vector (empty) (to be worked in place, shared for each face)
             var face_vertex_pos = new Array(3);
             var face_vertex_uv = new Array(3);
-            for (var v012 = 0; v012 < 3; v012++) {
+            var v012: number;
+            for (v012 = 0; v012 < 3; v012++) {
                 face_vertex_pos[v012] = Vector3.Zero();
                 face_vertex_uv[v012] = Vector2.Zero();
             }
             // create all with normals
             for (var face = 0; face < 20; face++) {
                 // 3 vertex per face
-                for (var v012 = 0; v012 < 3; v012++) {
+                for (v012 = 0; v012 < 3; v012++) {
                     // look up vertex 0,1,2 to its index in 0 to 11
                     var v_id = ico_indices[3 * face + v012];
                     // vertex have 3D position (x,y,z)
@@ -1533,7 +1534,7 @@
                 // centroid of triangle is needed to get help normal computation
                 //  (c1,c2) are used for centroid location
 
-                var interp_vertex = (i1: number, i2: number, c1: number, c2: number) =>{
+                var interp_vertex = (i1: number, i2: number, c1: number, c2: number) => {
                     // vertex is interpolated from
                     //   - face_vertex_pos[0..2]
                     //   - face_vertex_uv[0..2]
@@ -1834,21 +1835,12 @@
         public static ComputeNormals(positions: any, indices: any, normals: any) {
             var index = 0;
             
-            var p1p2x = 0.0;
-            var p1p2y = 0.0;
-            var p1p2z = 0.0;
-            var p3p2x = 0.0;
-            var p3p2y = 0.0;
-            var p3p2z = 0.0;
-            var faceNormalx = 0.0;
-            var faceNormaly = 0.0;
-            var faceNormalz = 0.0;
-
-            var length = 0.0;
-
-            var i1 = 0;
-            var i2 = 0;
-            var i3 = 0;
+            // temp Vector3
+            var p1p2 = Vector3.Zero();
+            var p3p2 = Vector3.Zero();
+            var faceNormal = Vector3.Zero();
+
+            var vertexNormali1 = Vector3.Zero();
 
             for (index = 0; index < positions.length; index++) {
                 normals[index] = 0.0;
@@ -1857,54 +1849,39 @@
             // indice triplet = 1 face
             var nbFaces = indices.length / 3;
             for (index = 0; index < nbFaces; index++) {
-                i1 = indices[index * 3];            // get the indexes of each vertex of the face
-                i2 = indices[index * 3 + 1];
-                i3 = indices[index * 3 + 2];
-
-                p1p2x = positions[i1 * 3] - positions[i2 * 3];          // compute two vectors per face
-                p1p2y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
-                p1p2z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
-
-                p3p2x = positions[i3 * 3] - positions[i2 * 3];
-                p3p2y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
-                p3p2z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
-
-                faceNormalx = p1p2y * p3p2z - p1p2z * p3p2y;            // compute the face normal with cross product
-                faceNormaly = p1p2z * p3p2x - p1p2x * p3p2z;
-                faceNormalz = p1p2x * p3p2y - p1p2y * p3p2x;
-
-                length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
-                length = (length === 0) ? 1.0 : length;
-                faceNormalx /= length;                                  // normalize this normal
-                faceNormaly /= length;
-                faceNormalz /= length;
-
-                normals[i1 * 3] += faceNormalx;                         // accumulate all the normals per face
-                normals[i1 * 3 + 1] += faceNormaly;
-                normals[i1 * 3 + 2] += faceNormalz;
-                normals[i2 * 3] += faceNormalx;
-                normals[i2 * 3 + 1] += faceNormaly;
-                normals[i2 * 3 + 2] += faceNormalz;
-                normals[i3 * 3] += faceNormalx;
-                normals[i3 * 3 + 1] += faceNormaly;
-                normals[i3 * 3 + 2] += faceNormalz;
+                var i1 = indices[index * 3];
+                var i2 = indices[index * 3 + 1];
+                var i3 = indices[index * 3 + 2];
+
+                p1p2.x = positions[i1 * 3] - positions[i2 * 3];
+                p1p2.y = positions[i1 * 3 + 1] - positions[i2 * 3 + 1];
+                p1p2.z = positions[i1 * 3 + 2] - positions[i2 * 3 + 2];
+
+                p3p2.x = positions[i3 * 3] - positions[i2 * 3];
+                p3p2.y = positions[i3 * 3 + 1] - positions[i2 * 3 + 1];
+                p3p2.z = positions[i3 * 3 + 2] - positions[i2 * 3 + 2];
+
+                Vector3.CrossToRef(p1p2, p3p2, faceNormal);
+                faceNormal.normalize();
+
+                normals[i1 * 3] += faceNormal.x;
+                normals[i1 * 3 + 1] += faceNormal.y;
+                normals[i1 * 3 + 2] += faceNormal.z;
+                normals[i2 * 3] += faceNormal.x;
+                normals[i2 * 3 + 1] += faceNormal.y;
+                normals[i2 * 3 + 2] += faceNormal.z;
+                normals[i3 * 3] += faceNormal.x;
+                normals[i3 * 3 + 1] += faceNormal.y;
+                normals[i3 * 3 + 2] += faceNormal.z;
             }
             
-            // last normalization of each normal
+            // last normalization
             for (index = 0; index < normals.length / 3; index++) {
-                faceNormalx = normals[index * 3];
-                faceNormaly = normals[index * 3 + 1];
-                faceNormalz = normals[index * 3 + 2];
-
-                length = Math.sqrt(faceNormalx * faceNormalx + faceNormaly * faceNormaly + faceNormalz * faceNormalz);
-                length = (length === 0) ? 1.0 : length;
-                faceNormalx /= length;                                 
-                faceNormaly /= length;
-                faceNormalz /= length;
-
-                normals[index * 3] = faceNormalx;
-                normals[index * 3 + 1] = faceNormaly;
-                normals[index * 3 + 2] = faceNormalz;
+                Vector3.FromFloatsToRef(normals[index * 3], normals[index * 3 + 1], normals[index * 3 + 2], vertexNormali1);
+                vertexNormali1.normalize();
+                normals[index * 3] = vertexNormali1.x;
+                normals[index * 3 + 1] = vertexNormali1.y;
+                normals[index * 3 + 2] = vertexNormali1.z;
             }
         }
 
@@ -1962,8 +1939,4 @@
             }
         }
     }
-}
-
-
-
-
+}