Explorar o código

code polishing

Julien Barrois %!s(int64=6) %!d(string=hai) anos
pai
achega
b9f7970aa0
Modificáronse 1 ficheiros con 207 adicións e 297 borrados
  1. 207 297
      src/Math/babylon.math.ts

+ 207 - 297
src/Math/babylon.math.ts

@@ -4317,16 +4317,20 @@ module BABYLON {
          * @returns the matrix determinant
          */
         public determinant(): number {
-            var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
-            var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
-            var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
-            var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
-            var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
-            var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
-
-            return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
-                (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
-                (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
+            const m = this.m;
+            const temp1 = m[10] * m[15] - m[11] * m[14];
+            const temp2 = m[9] * m[15] - m[11] * m[13];
+            const temp3 = m[9] * m[14] - m[10] * m[13];
+            const temp4 = m[8] * m[15] - m[11] * m[12];
+            const temp5 = m[8] * m[14] - m[10] * m[12];
+            const temp6 = m[8] * m[13] - m[9] * m[12];
+
+            return (
+              m[0] * (m[5] * temp1 - m[6] * temp2 + m[7] * temp3) -
+              m[1] * (m[4] * temp1 - m[6] * temp4 + m[7] * temp5) +
+              m[2] * (m[4] * temp2 - m[5] * temp4 + m[7] * temp6) -
+              m[3] * (m[4] * temp3 - m[5] * temp5 + m[6] * temp6)
+            );
         }
 
         // Methods
@@ -4411,62 +4415,53 @@ module BABYLON {
          * @returns the unmodified current matrix
          */
         public invertToRef(other: Matrix): Matrix {
-            var l1 = this.m[0];
-            var l2 = this.m[1];
-            var l3 = this.m[2];
-            var l4 = this.m[3];
-            var l5 = this.m[4];
-            var l6 = this.m[5];
-            var l7 = this.m[6];
-            var l8 = this.m[7];
-            var l9 = this.m[8];
-            var l10 = this.m[9];
-            var l11 = this.m[10];
-            var l12 = this.m[11];
-            var l13 = this.m[12];
-            var l14 = this.m[13];
-            var l15 = this.m[14];
-            var l16 = this.m[15];
-            var l17 = (l11 * l16) - (l12 * l15);
-            var l18 = (l10 * l16) - (l12 * l14);
-            var l19 = (l10 * l15) - (l11 * l14);
-            var l20 = (l9 * l16) - (l12 * l13);
-            var l21 = (l9 * l15) - (l11 * l13);
-            var l22 = (l9 * l14) - (l10 * l13);
-            var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
-            var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
-            var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
-            var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
-            var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
-            var l28 = (l7 * l16) - (l8 * l15);
-            var l29 = (l6 * l16) - (l8 * l14);
-            var l30 = (l6 * l15) - (l7 * l14);
-            var l31 = (l5 * l16) - (l8 * l13);
-            var l32 = (l5 * l15) - (l7 * l13);
-            var l33 = (l5 * l14) - (l6 * l13);
-            var l34 = (l7 * l12) - (l8 * l11);
-            var l35 = (l6 * l12) - (l8 * l10);
-            var l36 = (l6 * l11) - (l7 * l10);
-            var l37 = (l5 * l12) - (l8 * l9);
-            var l38 = (l5 * l11) - (l7 * l9);
-            var l39 = (l5 * l10) - (l6 * l9);
-
-            other.m[0] = l23 * l27;
-            other.m[4] = l24 * l27;
-            other.m[8] = l25 * l27;
-            other.m[12] = l26 * l27;
-            other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
-            other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
-            other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
-            other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
-            other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
-            other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
-            other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
-            other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
-            other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
-            other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
-            other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
-            other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
+            const m = this.m;
+            const l1 = m[0], l2 = m[1], l3 = m[2], l4 = m[3];
+            const l5 = m[4], l6 = m[5], l7 = m[6], l8 = m[7];
+            const l9 = m[8], l10 = m[9], l11 = m[10], l12 = m[11];
+            const l13 = m[12], l14 = m[13], l15 = m[14], l16 = m[15];
+
+            const l17 = l11 * l16 - l12 * l15;
+            const l18 = l10 * l16 - l12 * l14;
+            const l19 = l10 * l15 - l11 * l14;
+            const l20 = l9 * l16 - l12 * l13;
+            const l21 = l9 * l15 - l11 * l13;
+            const l22 = l9 * l14 - l10 * l13;
+            const l23 = l6 * l17 - l7 * l18 + l8 * l19;
+            const l24 = -(l5 * l17 - l7 * l20 + l8 * l21);
+            const l25 = l5 * l18 - l6 * l20 + l8 * l22;
+            const l26 = -(l5 * l19 - l6 * l21 + l7 * l22);
+            const l27 = 1 / (l1 * l23 + l2 * l24 + l3 * l25 + l4 * l26);
+            const l28 = l7 * l16 - l8 * l15;
+            const l29 = l6 * l16 - l8 * l14;
+            const l30 = l6 * l15 - l7 * l14;
+            const l31 = l5 * l16 - l8 * l13;
+            const l32 = l5 * l15 - l7 * l13;
+            const l33 = l5 * l14 - l6 * l13;
+            const l34 = l7 * l12 - l8 * l11;
+            const l35 = l6 * l12 - l8 * l10;
+            const l36 = l6 * l11 - l7 * l10;
+            const l37 = l5 * l12 - l8 * l9;
+            const l38 = l5 * l11 - l7 * l9;
+            const l39 = l5 * l10 - l6 * l9;
+
+            const otherM = other.m;
+            otherM[0] = l23 * l27;
+            otherM[4] = l24 * l27;
+            otherM[8] = l25 * l27;
+            otherM[12] = l26 * l27;
+            otherM[1] = -(l2 * l17 - l3 * l18 + l4 * l19) * l27;
+            otherM[5] = (l1 * l17 - l3 * l20 + l4 * l21) * l27;
+            otherM[9] = -(l1 * l18 - l2 * l20 + l4 * l22) * l27;
+            otherM[13] = (l1 * l19 - l2 * l21 + l3 * l22) * l27;
+            otherM[2] = (l2 * l28 - l3 * l29 + l4 * l30) * l27;
+            otherM[6] = -(l1 * l28 - l3 * l31 + l4 * l32) * l27;
+            otherM[10] = (l1 * l29 - l2 * l31 + l4 * l33) * l27;
+            otherM[14] = -(l1 * l30 - l2 * l32 + l3 * l33) * l27;
+            otherM[3] = -(l2 * l34 - l3 * l35 + l4 * l36) * l27;
+            otherM[7] = (l1 * l34 - l3 * l37 + l4 * l38) * l27;
+            otherM[11] = -(l1 * l35 - l2 * l37 + l4 * l39) * l27;
+            otherM[15] = (l1 * l36 - l2 * l38 + l3 * l39) * l27;
 
             other._markAsUpdated();
             return this;
@@ -4593,39 +4588,18 @@ module BABYLON {
          * @returns the current matrix
          */
         public multiplyToArray(other: Readonly<Matrix>, result: Float32Array, offset: number): Matrix {
-            var tm0 = this.m[0];
-            var tm1 = this.m[1];
-            var tm2 = this.m[2];
-            var tm3 = this.m[3];
-            var tm4 = this.m[4];
-            var tm5 = this.m[5];
-            var tm6 = this.m[6];
-            var tm7 = this.m[7];
-            var tm8 = this.m[8];
-            var tm9 = this.m[9];
-            var tm10 = this.m[10];
-            var tm11 = this.m[11];
-            var tm12 = this.m[12];
-            var tm13 = this.m[13];
-            var tm14 = this.m[14];
-            var tm15 = this.m[15];
-
-            var om0 = other.m[0];
-            var om1 = other.m[1];
-            var om2 = other.m[2];
-            var om3 = other.m[3];
-            var om4 = other.m[4];
-            var om5 = other.m[5];
-            var om6 = other.m[6];
-            var om7 = other.m[7];
-            var om8 = other.m[8];
-            var om9 = other.m[9];
-            var om10 = other.m[10];
-            var om11 = other.m[11];
-            var om12 = other.m[12];
-            var om13 = other.m[13];
-            var om14 = other.m[14];
-            var om15 = other.m[15];
+            const m = this.m;
+            const otherM = other.m;
+
+            var tm0 = m[0], tm1 = m[1], tm2 = m[2], tm3 = m[3];
+            var tm4 = m[4], tm5 = m[5], tm6 = m[6], tm7 = m[7];
+            var tm8 = m[8], tm9 = m[9], tm10 = m[10], tm11 = m[11];
+            var tm12 = m[12], tm13 = m[13], tm14 = m[14], tm15 = m[15];
+
+            var om0 = otherM[0], om1 = otherM[1], om2 = otherM[2], om3 = otherM[3];
+            var om4 = otherM[4], om5 = otherM[5], om6 = otherM[6], om7 = otherM[7];
+            var om8 = otherM[8], om9 = otherM[9], om10 = otherM[10], om11 = otherM[11];
+            var om12 = otherM[12], om13 = otherM[13], om14 = otherM[14], om15 = otherM[15];
 
             result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
             result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
@@ -4655,11 +4629,14 @@ module BABYLON {
          * @returns true is the current matrix and the given one values are strictly equal
          */
         public equals(value: Matrix): boolean {
-            return value &&
-                (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
-                    this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
-                    this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
-                    this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
+            const m = this.m;
+            const om = value.m;
+            return (
+                m[0]  === om[0]  && m[1]  === om[1]  && m[2]  === om[2]  && m[3]  === om[3] &&
+                m[4]  === om[4]  && m[5]  === om[5]  && m[6]  === om[6]  && m[7]  === om[7] &&
+                m[8]  === om[8]  && m[9]  === om[9]  && m[10] === om[10] && m[11] === om[11] &&
+                m[12] === om[12] && m[13] === om[13] && m[14] === om[14] && m[15] === om[15]
+            );
         }
 
         /**
@@ -4667,10 +4644,12 @@ module BABYLON {
          * @returns a new matrix from the current matrix
          */
         public clone(): Matrix {
-            return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
-                this.m[4], this.m[5], this.m[6], this.m[7],
-                this.m[8], this.m[9], this.m[10], this.m[11],
-                this.m[12], this.m[13], this.m[14], this.m[15]);
+            const matrix = Matrix.FromArray(this.m);
+            matrix._isIdentityDirty = this._isIdentityDirty;
+            matrix._isIdentity = this._isIdentity;
+            matrix._isIdentity3x2Dirty = this._isIdentity3x2Dirty;
+            matrix._isIdentity3x2 = this._isIdentity3x2;
+            return matrix;
         }
 
         /**
@@ -4701,16 +4680,15 @@ module BABYLON {
          * @returns true if operation was successful
          */
         public decompose(scale?: Vector3, rotation?: Quaternion, translation?: Vector3): boolean {
+            const m = this.m;
             if (translation) {
-                translation.x = this.m[12];
-                translation.y = this.m[13];
-                translation.z = this.m[14];
+                translation.copyFromFloats(m[12], m[13], m[14]);
             }
 
             scale = scale || MathTmp.Vector3[0];
-            scale.x = Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
-            scale.y = Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
-            scale.z = Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
+            scale.x = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
+            scale.y = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
+            scale.z = Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
 
             if (this.determinant() <= 0) {
                 scale.y *= -1;
@@ -4718,21 +4696,20 @@ module BABYLON {
 
             if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
                 if (rotation) {
-                    rotation.x = 0;
-                    rotation.y = 0;
-                    rotation.z = 0;
-                    rotation.w = 1;
+                    rotation.copyFromFloats(0.0, 0.0, 0.0, 1.0);
                 }
-
                 return false;
             }
 
             if (rotation) {
+                const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
                 Matrix.FromValuesToRef(
-                    this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
-                    this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
-                    this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
-                    0, 0, 0, 1, MathTmp.Matrix[0]);
+                    m[0] * sx, m[1] * sx,  m[2] * sx, 0.0,
+                    m[4] * sy, m[5] * sy,  m[6] * sy, 0.0,
+                    m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
+                          0.0,       0.0,        0.0, 1.0,
+                    MathTmp.Matrix[0]
+                );
 
                 Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
             }
@@ -4760,18 +4737,7 @@ module BABYLON {
          * @returns the updated current matrix
          */
         public setRow(index: number, row: Vector4): Matrix {
-            if (index < 0 || index > 3) {
-                return this;
-            }
-            var i = index * 4;
-            this.m[i + 0] = row.x;
-            this.m[i + 1] = row.y;
-            this.m[i + 2] = row.z;
-            this.m[i + 3] = row.w;
-
-            this._markAsUpdated();
-
-            return this;
+            return this.setRowFromFloats(index, row.x, row.y, row.z, row.w);
         }
 
         /**
@@ -4865,10 +4831,12 @@ module BABYLON {
             tmp.transposeToRef(ref);
             var m = ref.m;
             Matrix.FromValuesToRef(
-                m[0], m[1], m[2], 0,
-                m[4], m[5], m[6], 0,
-                m[8], m[9], m[10], 0,
-                0, 0, 0, 1, ref);
+                m[0], m[1],  m[2], 0.0,
+                m[4], m[5],  m[6], 0.0,
+                m[8], m[9], m[10], 0.0,
+                 0.0,  0.0,   0.0, 1.0,
+                ref
+            );
         }
 
         /**
@@ -4887,27 +4855,21 @@ module BABYLON {
          * @returns the current matrix
          */
         public getRotationMatrixToRef(result: Matrix): Matrix {
-            var m = this.m;
-
-            var sx = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
-            var sy = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
-            var sz = Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
-
-            if (this.determinant() <= 0) {
-                sy *= -1;
-            }
-
-            if (sx === 0 || sy === 0 || sz === 0) {
+            const scale = MathTmp.Vector3[0];
+            if (!this.decompose(scale)) {
                 Matrix.IdentityToRef(result);
-            }
-            else {
-                Matrix.FromValuesToRef(
-                    m[0] / sx, m[1] / sx, m[2] / sx, 0,
-                    m[4] / sy, m[5] / sy, m[6] / sy, 0,
-                    m[8] / sz, m[9] / sz, m[10] / sz, 0,
-                    0, 0, 0, 1, result);
+                return this;
             }
 
+            const m = this.m;
+            const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
+            Matrix.FromValuesToRef(
+                m[0] * sx, m[1] * sx,  m[2] * sx, 0.0,
+                m[4] * sy, m[5] * sy,  m[6] * sy, 0.0,
+                m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
+                      0.0,       0.0,        0.0, 1.0,
+                result
+            );
             return this;
         }
 
@@ -4915,18 +4877,25 @@ module BABYLON {
          * Toggles model matrix from being right handed to left handed in place and vice versa
          */
         public toggleModelMatrixHandInPlace() {
-            [2, 6, 8, 9, 14].forEach((num) => {
-                this.m[num] *= -1;
-            });
+            const m = this.m;
+            m[2] *= -1;
+            m[6] *= -1;
+            m[8] *= -1;
+            m[9] *= -1;
+            m[14] *= -1;
+            this._markAsUpdated();
         }
 
         /**
          * Toggles projection matrix from being right handed to left handed in place and vice versa
          */
         public toggleProjectionMatrixHandInPlace() {
-            [8, 9, 10, 11].forEach((num) => {
-                this.m[num] *= -1;
-            });
+            var m = this.m;
+            m[8] *= -1;
+            m[9] *= -1;
+            m[10] *= -1;
+            m[11] *= -1;
+            this._markAsUpdated();
         }
 
         // Statics
@@ -4974,6 +4943,13 @@ module BABYLON {
         }
 
         /**
+         * Gets an identity matrix that must not be updated
+         */
+        public static get IdentityReadOnly(): Readonly<Matrix> {
+            return Matrix._identityReadOnly;
+        }
+
+        /**
          * Stores a list of values (16) inside a given matrix
          * @param initialM11 defines 1st value of 1st row
          * @param initialM12 defines 2nd value of 1st row
@@ -4998,34 +4974,16 @@ module BABYLON {
             initialM31: number, initialM32: number, initialM33: number, initialM34: number,
             initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
 
-            result.m[0] = initialM11;
-            result.m[1] = initialM12;
-            result.m[2] = initialM13;
-            result.m[3] = initialM14;
-            result.m[4] = initialM21;
-            result.m[5] = initialM22;
-            result.m[6] = initialM23;
-            result.m[7] = initialM24;
-            result.m[8] = initialM31;
-            result.m[9] = initialM32;
-            result.m[10] = initialM33;
-            result.m[11] = initialM34;
-            result.m[12] = initialM41;
-            result.m[13] = initialM42;
-            result.m[14] = initialM43;
-            result.m[15] = initialM44;
+            const m = result.m;
+            m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
+            m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
+            m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
+            m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
 
             result._markAsUpdated();
         }
 
         /**
-         * Gets an identity matrix that must not be updated
-         */
-        public static get IdentityReadOnly(): Readonly<Matrix> {
-            return Matrix._identityReadOnly;
-        }
-
-        /**
          * Creates new matrix from a list of values (16)
          * @param initialM11 defines 1st value of 1st row
          * @param initialM12 defines 2nd value of 1st row
@@ -5051,23 +5009,11 @@ module BABYLON {
             initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
 
             var result = new Matrix();
-
-            result.m[0] = initialM11;
-            result.m[1] = initialM12;
-            result.m[2] = initialM13;
-            result.m[3] = initialM14;
-            result.m[4] = initialM21;
-            result.m[5] = initialM22;
-            result.m[6] = initialM23;
-            result.m[7] = initialM24;
-            result.m[8] = initialM31;
-            result.m[9] = initialM32;
-            result.m[10] = initialM33;
-            result.m[11] = initialM34;
-            result.m[12] = initialM41;
-            result.m[13] = initialM42;
-            result.m[14] = initialM43;
-            result.m[15] = initialM44;
+            const m = result.m;
+            m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
+            m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
+            m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
+            m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
 
             return result;
         }
@@ -5093,11 +5039,7 @@ module BABYLON {
          * @param result defines the target matrix
          */
         public static ComposeToRef(scale: Vector3, rotation: Quaternion, translation: Vector3, result: Matrix): void {
-            Matrix.FromValuesToRef(scale.x, 0, 0, 0,
-                0, scale.y, 0, 0,
-                0, 0, scale.z, 0,
-                0, 0, 0, 1, MathTmp.Matrix[1]);
-
+            Matrix.ScalingToRef(scale.x, scale.y, scale.z, MathTmp.Matrix[1]);
             rotation.toRotationMatrix(MathTmp.Matrix[0]);
             MathTmp.Matrix[1].multiplyToRef(MathTmp.Matrix[0], result);
 
@@ -5122,7 +5064,8 @@ module BABYLON {
          * @param result defines the target matrix
          */
         public static IdentityToRef(result: Matrix): void {
-            Matrix.FromValuesToRef(1.0, 0.0, 0.0, 0.0,
+            Matrix.FromValuesToRef(
+                1.0, 0.0, 0.0, 0.0,
                 0.0, 1.0, 0.0, 0.0,
                 0.0, 0.0, 1.0, 0.0,
                 0.0, 0.0, 0.0, 1.0, result);
@@ -5134,7 +5077,8 @@ module BABYLON {
          * @returns a new zero matrix
          */
         public static Zero(): Matrix {
-            const zero = Matrix.FromValues(0.0, 0.0, 0.0, 0.0,
+            const zero = Matrix.FromValues(
+                0.0, 0.0, 0.0, 0.0,
                 0.0, 0.0, 0.0, 0.0,
                 0.0, 0.0, 0.0, 0.0,
                 0.0, 0.0, 0.0, 0.0);
@@ -5172,25 +5116,13 @@ module BABYLON {
         public static RotationXToRef(angle: number, result: Matrix): void {
             var s = Math.sin(angle);
             var c = Math.cos(angle);
-
-            result.m[0] = 1.0;
-            result.m[15] = 1.0;
-
-            result.m[5] = c;
-            result.m[10] = c;
-            result.m[9] = -s;
-            result.m[6] = s;
-
-            result.m[1] = 0.0;
-            result.m[2] = 0.0;
-            result.m[3] = 0.0;
-            result.m[4] = 0.0;
-            result.m[7] = 0.0;
-            result.m[8] = 0.0;
-            result.m[11] = 0.0;
-            result.m[12] = 0.0;
-            result.m[13] = 0.0;
-            result.m[14] = 0.0;
+            Matrix.FromValuesToRef(
+                1.0, 0.0, 0.0, 0.0,
+                0.0,   c,   s, 0.0,
+                0.0,  -s,   c, 0.0,
+                0.0, 0.0, 0.0, 1.0,
+                result
+            );
 
             result._markAsUpdated();
         }
@@ -5214,25 +5146,13 @@ module BABYLON {
         public static RotationYToRef(angle: number, result: Matrix): void {
             var s = Math.sin(angle);
             var c = Math.cos(angle);
-
-            result.m[5] = 1.0;
-            result.m[15] = 1.0;
-
-            result.m[0] = c;
-            result.m[2] = -s;
-            result.m[8] = s;
-            result.m[10] = c;
-
-            result.m[1] = 0.0;
-            result.m[3] = 0.0;
-            result.m[4] = 0.0;
-            result.m[6] = 0.0;
-            result.m[7] = 0.0;
-            result.m[9] = 0.0;
-            result.m[11] = 0.0;
-            result.m[12] = 0.0;
-            result.m[13] = 0.0;
-            result.m[14] = 0.0;
+            Matrix.FromValuesToRef(
+                c, 0.0,  -s, 0.0,
+              0.0, 1.0, 0.0, 0.0,
+                s, 0.0,   c, 0.0,
+              0.0, 0.0, 0.0, 1.0,
+              result
+          );
 
             result._markAsUpdated();
         }
@@ -5256,25 +5176,13 @@ module BABYLON {
         public static RotationZToRef(angle: number, result: Matrix): void {
             var s = Math.sin(angle);
             var c = Math.cos(angle);
-
-            result.m[10] = 1.0;
-            result.m[15] = 1.0;
-
-            result.m[0] = c;
-            result.m[1] = s;
-            result.m[4] = -s;
-            result.m[5] = c;
-
-            result.m[2] = 0.0;
-            result.m[3] = 0.0;
-            result.m[6] = 0.0;
-            result.m[7] = 0.0;
-            result.m[8] = 0.0;
-            result.m[9] = 0.0;
-            result.m[11] = 0.0;
-            result.m[12] = 0.0;
-            result.m[13] = 0.0;
-            result.m[14] = 0.0;
+            Matrix.FromValuesToRef(
+                c,   s, 0.0, 0.0,
+               -s,   c, 0.0, 0.0,
+              0.0, 0.0, 1.0, 0.0,
+              0.0, 0.0, 0.0, 1.0,
+             result
+            );
 
             result._markAsUpdated();
         }
@@ -5370,22 +5278,13 @@ module BABYLON {
          * @param result defines the target matrix
          */
         public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
-            result.m[0] = x;
-            result.m[1] = 0.0;
-            result.m[2] = 0.0;
-            result.m[3] = 0.0;
-            result.m[4] = 0.0;
-            result.m[5] = y;
-            result.m[6] = 0.0;
-            result.m[7] = 0.0;
-            result.m[8] = 0.0;
-            result.m[9] = 0.0;
-            result.m[10] = z;
-            result.m[11] = 0.0;
-            result.m[12] = 0.0;
-            result.m[13] = 0.0;
-            result.m[14] = 0.0;
-            result.m[15] = 1.0;
+            Matrix.FromValuesToRef(
+                x, 0.0, 0.0, 0.0,
+              0.0,   y, 0.0, 0.0,
+              0.0, 0.0,   z, 0.0,
+              0.0, 0.0, 0.0, 1.0,
+              result
+          );
 
             result._markAsUpdated();
         }
@@ -5411,10 +5310,13 @@ module BABYLON {
          * @param result defines the target matrix
          */
         public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
-            Matrix.FromValuesToRef(1.0, 0.0, 0.0, 0.0,
+            Matrix.FromValuesToRef(
+                1.0, 0.0, 0.0, 0.0,
                 0.0, 1.0, 0.0, 0.0,
                 0.0, 0.0, 1.0, 0.0,
-                x, y, z, 1.0, result);
+                  x,   y,   z, 1.0,
+                result
+            );
         }
 
         /**
@@ -5526,10 +5428,11 @@ module BABYLON {
             // X axis
             Vector3.CrossToRef(up, zAxis, xAxis);
 
-            if (xAxis.lengthSquared() === 0) {
+            const xSquareLength = xAxis.lengthSquared();
+            if (xSquareLength === 0) {
                 xAxis.x = 1.0;
             } else {
-                xAxis.normalize();
+                xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
             }
 
             // Y axis
@@ -5541,10 +5444,13 @@ module BABYLON {
             var ey = -Vector3.Dot(yAxis, eye);
             var ez = -Vector3.Dot(zAxis, eye);
 
-            return Matrix.FromValuesToRef(xAxis.x, yAxis.x, zAxis.x, 0,
-                xAxis.y, yAxis.y, zAxis.y, 0,
-                xAxis.z, yAxis.z, zAxis.z, 0,
-                ex, ey, ez, 1, result);
+            Matrix.FromValuesToRef(
+                xAxis.x, yAxis.x, zAxis.x, 0.0,
+                xAxis.y, yAxis.y, zAxis.y, 0.0,
+                xAxis.z, yAxis.z, zAxis.z, 0.0,
+                     ex,      ey,      ez, 1.0,
+                result
+            );
         }
 
         /**
@@ -5581,10 +5487,11 @@ module BABYLON {
             // X axis
             Vector3.CrossToRef(up, zAxis, xAxis);
 
-            if (xAxis.lengthSquared() === 0) {
+            const xSquareLength = xAxis.lengthSquared();
+            if (xSquareLength === 0) {
                 xAxis.x = 1.0;
             } else {
-                xAxis.normalize();
+                xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
             }
 
             // Y axis
@@ -5596,10 +5503,13 @@ module BABYLON {
             var ey = -Vector3.Dot(yAxis, eye);
             var ez = -Vector3.Dot(zAxis, eye);
 
-            return Matrix.FromValuesToRef(xAxis.x, yAxis.x, zAxis.x, 0,
-                xAxis.y, yAxis.y, zAxis.y, 0,
-                xAxis.z, yAxis.z, zAxis.z, 0,
-                ex, ey, ez, 1, result);
+            Matrix.FromValuesToRef(
+                xAxis.x, yAxis.x, zAxis.x, 0.0,
+                xAxis.y, yAxis.y, zAxis.y, 0.0,
+                xAxis.z, yAxis.z, zAxis.z, 0.0,
+                     ex,      ey,      ez, 1.0,
+                result
+            );
         }
 
         /**