Преглед изворни кода

Merge pull request #5274 from barroij/arrayBuilder

Add an ArrayBuilder utility function
David Catuhe пре 6 година
родитељ
комит
c3f1524d29

+ 1 - 1
Tools/Gulp/config.json

@@ -155,6 +155,7 @@
                 "../../src/babylon.types.js",
                 "../../src/Events/babylon.keyboardEvents.js",
                 "../../src/Events/babylon.pointerEvents.js",
+                "../../src/Tools/babylon.tools.js",
                 "../../src/Math/babylon.math.js",
                 "../../src/Math/babylon.math.scalar.js",
                 "../../src/babylon.mixins.js",
@@ -163,7 +164,6 @@
                 "../../src/Tools/babylon.deferred.js",
                 "../../src/Tools/babylon.observable.js",
                 "../../src/Tools/babylon.smartArray.js",
-                "../../src/Tools/babylon.tools.js",
                 "../../src/Tools/babylon.promise.js",
                 "../../src/Tools/babylon.workerPool.js",
                 "../../src/States/babylon.alphaCullingState.js",

+ 2 - 0
dist/preview release/what's new.md

@@ -10,6 +10,8 @@
 
 ### Core Engine
 
+- Added utility function `Tools.BuildArray` for array initialisation ([barroij](https://github.com/barroij))
+
 ### glTF Loader
 
 ### glTF Serializer

+ 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.Zero);
         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.Identity);
 
         /**
          * 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.Zero);
         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.Identity);
 
         /**
          * 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.Zero);
         /**
          * 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.Zero);
         /**
          * 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.Zero);
         /**
          * 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;

+ 10 - 14
src/Math/babylon.math.ts

@@ -7179,25 +7179,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.Black);
+        public static Color4: Color4[] = Tools.BuildArray(3, () => new Color4(0, 0, 0, 0));
+        public static Vector2: Vector2[] = Tools.BuildArray(3, Vector2.Zero); // 3 temp Vector2 at once should be enough
+        public static Vector3: Vector3[] = Tools.BuildArray(9, Vector3.Zero); // 9 temp Vector3 at once should be enough
+        public static Vector4: Vector4[] = Tools.BuildArray(3, Vector4.Zero); // 3 temp Vector4 at once should be enough
+        public static Quaternion: Quaternion[] = Tools.BuildArray(2, Quaternion.Zero); // 2 temp Quaternion at once should be enough
+        public static Matrix: Matrix[] = Tools.BuildArray(6, Matrix.Identity); // 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.Zero);
+        public static Matrix: Matrix[] = Tools.BuildArray(2, Matrix.Identity);
+        public static Quaternion: Quaternion[] = Tools.BuildArray(3, Quaternion.Zero);
     }
 }

+ 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.Zero);
         private static _tmpQuat: Quaternion = Quaternion.Identity();
 
         /**

+ 16 - 19
src/Tools/babylon.tools.ts

@@ -587,6 +587,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 itemBuilder a callback responsible for creating new instance of item. Called once per array entry.
+         * @returns a new array filled with new objects
+         */
+        public static BuildArray<T>(size: number, itemBuilder: () => T): Array<T> {
+            const a: T[] = [];
+            for (let i = 0; i < size; ++i) {
+                a.push(itemBuilder());
+            }
+            return a;
+        }
+
+        /**
          * Gets the pointer prefix to use
          * @returns "pointer" if touch is enabled. Else returns "mouse"
          */
@@ -1089,25 +1103,8 @@ module BABYLON {
          * @param max defines the maximum range
          */
         public static CheckExtends(v: Vector3, min: Vector3, max: Vector3): void {
-            if (v.x < min.x) {
-                min.x = v.x;
-            }
-            if (v.y < min.y) {
-                min.y = v.y;
-            }
-            if (v.z < min.z) {
-                min.z = v.z;
-            }
-
-            if (v.x > max.x) {
-                max.x = v.x;
-            }
-            if (v.y > max.y) {
-                max.y = v.y;
-            }
-            if (v.z > max.z) {
-                max.z = v.z;
-            }
+            min.minimizeInPlace(v);
+            max.maximizeInPlace(v);
         }
 
         /**