Browse Source

arrayBuilder and some other minor updates

Julien Barrois 7 năm trước cách đây
mục cha
commit
6482402a4c

+ 2 - 2
src/Bones/babylon.bone.ts

@@ -5,9 +5,9 @@ module BABYLON {
      */
     export class Bone extends Node {
 
-        private static _tmpVecs: Vector3[] = [Vector3.Zero(), Vector3.Zero()];
+        private static _tmpVecs: Vector3[] = Tools.BuildArray(2, Vector3);
         private static _tmpQuat = Quaternion.Identity();
-        private static _tmpMats: Matrix[] = [Matrix.Identity(), Matrix.Identity(), Matrix.Identity(), Matrix.Identity(), Matrix.Identity()];
+        private static _tmpMats: Matrix[] = Tools.BuildArray(5, Matrix);
 
         /**
          * Gets the list of child bones

+ 2 - 2
src/Bones/babylon.boneLookController.ts

@@ -5,9 +5,9 @@ module BABYLON {
      */
     export class BoneLookController {
 
-        private static _tmpVecs: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
+        private static _tmpVecs: Vector3[] = Tools.BuildArray(10, Vector3);
         private static _tmpQuat = Quaternion.Identity();
-        private static _tmpMats: Matrix[] = [Matrix.Identity(), Matrix.Identity(), Matrix.Identity(), Matrix.Identity(), Matrix.Identity()];
+        private static _tmpMats: Matrix[] = Tools.BuildArray(5, Matrix);
 
         /**
          * The target Vector3 that the bone will look at

+ 13 - 13
src/Culling/babylon.boundingBox.ts

@@ -6,7 +6,7 @@ module BABYLON {
         /**
          * Gets the 8 vectors representing the bounding box in local space
          */
-        public vectors: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
+        public vectors: Vector3[] = Tools.BuildArray(8, Vector3);
         /**
          * Gets the center of the bounding box in local space
          */
@@ -26,11 +26,11 @@ module BABYLON {
         /**
          * Gets the OBB (object bounding box) directions
          */
-        public directions: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
+        public directions: Vector3[] = Tools.BuildArray(3, Vector3);
         /**
          * Gets the 8 vectors representing the bounding box in world space
          */
-        public vectorsWorld: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
+        public vectorsWorld: Vector3[] = Tools.BuildArray(8, Vector3);
         /**
          * Gets the minimum vector in world space
          */
@@ -203,15 +203,15 @@ module BABYLON {
             var delta = -Epsilon;
 
             if (this.maximumWorld.x - point.x < delta || delta > point.x - this.minimumWorld.x) {
-                return false;
+                return false;
             }
 
             if (this.maximumWorld.y - point.y < delta || delta > point.y - this.minimumWorld.y) {
-                return false;
+                return false;
             }
 
             if (this.maximumWorld.z - point.z < delta || delta > point.z - this.minimumWorld.z) {
-                return false;
+                return false;
             }
 
             return true;
@@ -234,15 +234,15 @@ module BABYLON {
          */
         public intersectsMinMax(min: Vector3, max: Vector3): boolean {
             if (this.maximumWorld.x < min.x || this.minimumWorld.x > max.x) {
-                return false;
+                return false;
             }
 
             if (this.maximumWorld.y < min.y || this.minimumWorld.y > max.y) {
-                return false;
+                return false;
             }
 
             if (this.maximumWorld.z < min.z || this.minimumWorld.z > max.z) {
-                return false;
+                return false;
             }
 
             return true;
@@ -258,15 +258,15 @@ module BABYLON {
          */
         public static Intersects(box0: BoundingBox, box1: BoundingBox): boolean {
             if (box0.maximumWorld.x < box1.minimumWorld.x || box0.minimumWorld.x > box1.maximumWorld.x) {
-                return false;
+                return false;
             }
 
             if (box0.maximumWorld.y < box1.minimumWorld.y || box0.minimumWorld.y > box1.maximumWorld.y) {
-                return false;
+                return false;
             }
 
             if (box0.maximumWorld.z < box1.minimumWorld.z || box0.minimumWorld.z > box1.maximumWorld.z) {
-                return false;
+                return false;
             }
 
             return true;
@@ -321,7 +321,7 @@ module BABYLON {
                     }
                 }
                 if (inCount === 0) {
-                    return false;
+                    return false;
                 }
             }
             return true;

+ 29 - 25
src/Math/babylon.math.ts

@@ -1625,9 +1625,20 @@ module BABYLON {
          * @returns the current updated Vector3
          */
         public addInPlace(otherVector: Vector3): Vector3 {
-            this.x += otherVector.x;
-            this.y += otherVector.y;
-            this.z += otherVector.z;
+            return this.addInPlaceFromFloats(otherVector.x, otherVector.y, otherVector.z);
+        }
+
+        /**
+         * Adds the given coordinates to the current Vector3 
+         * @param x defines the x coordinate of the operand
+         * @param y defines the y coordinate of the operand
+         * @param z defines the z coordinate of the operand
+         * @returns the current updated Vector3
+         */
+        public addInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
+            this.x += x;
+            this.y += y;
+            this.z += z;
             return this;
         }
 
@@ -2034,10 +2045,7 @@ module BABYLON {
          * @returns the current updated Vector3
          */
         public copyFrom(source: Vector3): Vector3 {
-            this.x = source.x;
-            this.y = source.y;
-            this.z = source.z;
-            return this;
+            return this.copyFromFloats(source.x, source.y, source.z);
         }
 
         /**
@@ -2694,13 +2702,13 @@ module BABYLON {
          */
         constructor(
             /** x value of the vector */
-            public x: number,
+            public x: number = 0,
             /** y value of the vector */
-            public y: number,
+            public y: number = 0,
             /** z value of the vector */
-            public z: number,
+            public z: number = 0,
             /** w value of the vector */
-            public w: number
+            public w: number = 0
         ) { }
 
         /**
@@ -7179,25 +7187,21 @@ module BABYLON {
      * @hidden
      */
     export class Tmp {
-        public static Color3: Color3[] = [Color3.Black(), Color3.Black(), Color3.Black()];
-        public static Color4: Color4[] = [new Color4(0, 0, 0, 0), new Color4(0, 0, 0, 0)];
-        public static Vector2: Vector2[] = [Vector2.Zero(), Vector2.Zero(), Vector2.Zero()];  // 3 temp Vector2 at once should be enough
-        public static Vector3: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(),
-        Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];    // 9 temp Vector3 at once should be enough
-        public static Vector4: Vector4[] = [Vector4.Zero(), Vector4.Zero(), Vector4.Zero()];  // 3 temp Vector4 at once should be enough
-        public static Quaternion: Quaternion[] = [Quaternion.Zero(), Quaternion.Zero()];                // 2 temp Quaternion at once should be enough
-        public static Matrix: Matrix[] = [Matrix.Zero(), Matrix.Zero(),
-        Matrix.Zero(), Matrix.Zero(),
-        Matrix.Zero(), Matrix.Zero(),
-        Matrix.Zero(), Matrix.Zero()];                      // 6 temp Matrices at once should be enough
+        public static Color3: Color3[] = Tools.BuildArray(3, Color3);
+        public static Color4: Color4[] = Tools.BuildArray(3, Color4, 0, 0, 0, 0);
+        public static Vector2: Vector2[] = Tools.BuildArray(3, Vector2); // 3 temp Vector2 at once should be enough
+        public static Vector3: Vector3[] = Tools.BuildArray(9, Vector3); // 9 temp Vector3 at once should be enough
+        public static Vector4: Vector4[] = Tools.BuildArray(3, Vector4); // 3 temp Vector4 at once should be enough
+        public static Quaternion: Quaternion[] = Tools.BuildArray(2, Quaternion); // 2 temp Quaternion at once should be enough
+        public static Matrix: Matrix[] = Tools.BuildArray(6, Matrix); // 6 temp Matrices at once should be enough
     }
     /**
      * @hidden
      * Same as Tmp but not exported to keep it only for math functions to avoid conflicts
      */
     class MathTmp {
-        public static Vector3: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
-        public static Matrix: Matrix[] = [Matrix.Zero(), Matrix.Zero()];
-        public static Quaternion: Quaternion[] = [Quaternion.Zero(), Quaternion.Zero(), Quaternion.Zero()];
+        public static Vector3: Vector3[] = Tools.BuildArray(6, Vector3);;
+        public static Matrix: Matrix[] = Tools.BuildArray(2, Matrix);
+        public static Quaternion: Quaternion[] = Tools.BuildArray(3, Quaternion);
     }
 }

+ 2 - 4
src/Mesh/babylon.geometry.ts

@@ -397,10 +397,8 @@ module BABYLON {
             const count = this._totalVertices * vertexBuffer.getSize();
 
             if (vertexBuffer.type !== VertexBuffer.FLOAT || vertexBuffer.byteStride !== tightlyPackedByteStride) {
-                const copy = new Array<number>(count);
-                vertexBuffer.forEach(count, (value, index) => {
-                    copy[index] = value;
-                });
+                const copy : number[] = [];
+                vertexBuffer.forEach(count, value => copy.push(value));
                 return copy;
             }
 

+ 1 - 1
src/Physics/babylon.physicsImpostor.ts

@@ -167,7 +167,7 @@ module BABYLON {
 
         private _isDisposed = false;
 
-        private static _tmpVecs: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero()];
+        private static _tmpVecs: Vector3[] = Tools.BuildArray(3, Vector3);
         private static _tmpQuat: Quaternion = Quaternion.Identity();
 
         /**

+ 18 - 12
src/Tools/babylon.tools.ts

@@ -518,12 +518,8 @@ module BABYLON {
             }
 
             if (bias) {
-                minimum.x -= minimum.x * bias.x + bias.y;
-                minimum.y -= minimum.y * bias.x + bias.y;
-                minimum.z -= minimum.z * bias.x + bias.y;
-                maximum.x += maximum.x * bias.x + bias.y;
-                maximum.y += maximum.y * bias.x + bias.y;
-                maximum.z += maximum.z * bias.x + bias.y;
+                minimum.scaleInPlace(1-bias.x).addInPlaceFromFloats(-bias.y, -bias.y, -bias.y)
+                maximum.scaleInPlace(1+bias.x).addInPlaceFromFloats(bias.y, bias.y, bias.y)
             }
 
             return {
@@ -558,12 +554,8 @@ module BABYLON {
             }
 
             if (bias) {
-                minimum.x -= minimum.x * bias.x + bias.y;
-                minimum.y -= minimum.y * bias.x + bias.y;
-                minimum.z -= minimum.z * bias.x + bias.y;
-                maximum.x += maximum.x * bias.x + bias.y;
-                maximum.y += maximum.y * bias.x + bias.y;
-                maximum.z += maximum.z * bias.x + bias.y;
+                minimum.scaleInPlace(1-bias.x).addInPlaceFromFloats(-bias.y, -bias.y, -bias.y)
+                maximum.scaleInPlace(1+bias.x).addInPlaceFromFloats(bias.y, bias.y, bias.y)
             }
 
             return {
@@ -587,6 +579,20 @@ module BABYLON {
         }
 
         /**
+         * Returns an array of the given size filled with element built from the given constructor and the paramters
+         * @param size the number of element to construct and put in the array
+         * @param konstructor the class name (or constructor function)
+         * @param kparams the arguments to pass to each call to the constructor
+         * @returns a new array filled with new objects
+         */
+        public static BuildArray = <T, P extends any[]>(size: number, konstructor: new (...p: P)=>T, ...kparams: P): Array<T> => {
+            const a: T[] = [];
+            for (let i = 0; i < size; ++i)
+                a.push(new konstructor(...kparams));
+            return a;
+        }
+
+        /**
          * Gets the pointer prefix to use
          * @returns "pointer" if touch is enabled. Else returns "mouse"
          */