Browse Source

Tiny optimizations here and there

David Catuhe 7 years ago
parent
commit
bb118fcb55

File diff suppressed because it is too large
+ 6037 - 6032
dist/preview release/babylon.d.ts


File diff suppressed because it is too large
+ 6037 - 6032
dist/preview release/babylon.module.d.ts


+ 2 - 2
src/Culling/Octrees/babylon.octree.ts

@@ -8,12 +8,12 @@
         public dynamicContent = new Array<T>();
 
         private _maxBlockCapacity: number;
-        private _selectionContent: SmartArray<T>;       
+        private _selectionContent: SmartArrayNoDuplicate<T>;       
         private _creationFunc: (entry: T, block: OctreeBlock<T>) => void;
 
         constructor(creationFunc: (entry: T, block: OctreeBlock<T>) => void, maxBlockCapacity?: number, public maxDepth = 2) {
             this._maxBlockCapacity = maxBlockCapacity || 64;
-            this._selectionContent = new SmartArray<T>(1024);
+            this._selectionContent = new SmartArrayNoDuplicate<T>(1024);
             this._creationFunc = creationFunc;
         }
 

+ 3 - 3
src/Culling/Octrees/babylon.octreeBlock.ts

@@ -79,7 +79,7 @@
             }
         }
 
-        public select(frustumPlanes: Plane[], selection: SmartArray<T>, allowDuplicate?: boolean): void {
+        public select(frustumPlanes: Plane[], selection: SmartArrayNoDuplicate<T>, allowDuplicate?: boolean): void {
             if (BoundingBox.IsInFrustum(this._boundingVectors, frustumPlanes)) {
                 if (this.blocks) {
                     for (var index = 0; index < this.blocks.length; index++) {
@@ -97,7 +97,7 @@
             }
         }
 
-        public intersects(sphereCenter: Vector3, sphereRadius: number, selection: SmartArray<T>, allowDuplicate?: boolean): void {
+        public intersects(sphereCenter: Vector3, sphereRadius: number, selection: SmartArrayNoDuplicate<T>, allowDuplicate?: boolean): void {
             if (BoundingBox.IntersectsSphere(this._minPoint, this._maxPoint, sphereCenter, sphereRadius)) {
                 if (this.blocks) {
                     for (var index = 0; index < this.blocks.length; index++) {
@@ -115,7 +115,7 @@
             }
         }
 
-        public intersectsRay(ray: Ray, selection: SmartArray<T>): void {
+        public intersectsRay(ray: Ray, selection: SmartArrayNoDuplicate<T>): void {
             if (ray.intersectsBoxMinMax(this._minPoint, this._maxPoint)) {
                 if (this.blocks) {
                     for (var index = 0; index < this.blocks.length; index++) {

+ 4 - 3
src/Materials/babylon.effect.ts

@@ -759,11 +759,12 @@
         }
 
         public bindUniformBuffer(buffer: WebGLBuffer, name: string): void {
-            if (Effect._baseCache[this._uniformBuffersNames[name]] === buffer) {
+            let bufferName = this._uniformBuffersNames[name];
+            if (Effect._baseCache[bufferName] === buffer) {
                 return;
             }
-            Effect._baseCache[this._uniformBuffersNames[name]] = buffer;
-            this._engine.bindUniformBufferBase(buffer, this._uniformBuffersNames[name]);
+            Effect._baseCache[bufferName] = buffer;
+            this._engine.bindUniformBufferBase(buffer, bufferName);
         }
 
         public bindUniformBlock(blockName: string, index: number): void {

+ 46 - 32
src/Tools/babylon.smartArray.ts

@@ -3,8 +3,7 @@
         public data: Array<T>;
         public length: number = 0;
 
-        private _id: number;
-        private _duplicateId = 0;
+        protected _id: number;
 
         [index: number]: T;
 
@@ -19,12 +18,6 @@
             if (this.length > this.data.length) {
                 this.data.length *= 2;
             }
-
-            if (!(<any>value).__smartArrayFlags) {
-                (<any>value).__smartArrayFlags = {};
-            }
-
-            (<any>value).__smartArrayFlags[this._id] = this._duplicateId;
         }
 
         public forEach(func: (content: T) => void): void {
@@ -32,22 +25,13 @@
                 func(this.data[index]);
             }
         }
-
-        public pushNoDuplicate(value: T): boolean {
-            if ((<any>value).__smartArrayFlags && (<any>value).__smartArrayFlags[this._id] === this._duplicateId) {
-                return false;
-            }
-            this.push(value);
-            return true;
-        }
-
+    
         public sort(compareFn: (a: T, b: T) => number): void {
             this.data.sort(compareFn);
         }
 
         public reset(): void {
             this.length = 0;
-            this._duplicateId++;
         }
 
         public dispose(): void {
@@ -72,20 +56,6 @@
             }
         }
 
-        public concatWithNoDuplicate(array: any): void {
-            if (array.length === 0) {
-                return;
-            }
-            if (this.length + array.length > this.data.length) {
-                this.data.length = (this.length + array.length) * 2;
-            }
-
-            for (var index = 0; index < array.length; index++) {
-                var item = (array.data || array)[index];
-                this.pushNoDuplicate(item);
-            }
-        }
-
         public indexOf(value: T): number {
             var position = this.data.indexOf(value);
 
@@ -103,4 +73,48 @@
         // Statics
         private static _GlobalId = 0;
     }
+
+    export class SmartArrayNoDuplicate<T> extends SmartArray<T> {
+        private _duplicateId = 0;
+
+        [index: number]: T;
+
+        public push(value: T): void {
+            super.push(value);
+
+            if (!(<any>value).__smartArrayFlags) {
+                (<any>value).__smartArrayFlags = {};
+            }
+
+            (<any>value).__smartArrayFlags[this._id] = this._duplicateId;
+        }
+
+
+        public pushNoDuplicate(value: T): boolean {
+            if ((<any>value).__smartArrayFlags && (<any>value).__smartArrayFlags[this._id] === this._duplicateId) {
+                return false;
+            }
+            this.push(value);
+            return true;
+        }
+
+        public reset(): void {
+            super.reset();
+            this._duplicateId++;
+        }
+
+        public concatWithNoDuplicate(array: any): void {
+            if (array.length === 0) {
+                return;
+            }
+            if (this.length + array.length > this.data.length) {
+                this.data.length = (this.length + array.length) * 2;
+            }
+
+            for (var index = 0; index < array.length; index++) {
+                var item = (array.data || array)[index];
+                this.pushNoDuplicate(item);
+            }
+        }
+    }    
 } 

+ 4 - 4
src/babylon.scene.ts

@@ -785,7 +785,7 @@
         public actionManager: ActionManager;
 
         public _actionManagers = new Array<ActionManager>();
-        private _meshesForIntersections = new SmartArray<AbstractMesh>(256);
+        private _meshesForIntersections = new SmartArrayNoDuplicate<AbstractMesh>(256);
 
         // Procedural textures
         public proceduralTexturesEnabled = true;
@@ -845,10 +845,10 @@
 
         private _activeMeshes = new SmartArray<AbstractMesh>(256);
         private _processedMaterials = new SmartArray<Material>(256);
-        private _renderTargets = new SmartArray<RenderTargetTexture>(256);
+        private _renderTargets = new SmartArrayNoDuplicate<RenderTargetTexture>(256);
         public _activeParticleSystems = new SmartArray<IParticleSystem>(256);
-        private _activeSkeletons = new SmartArray<Skeleton>(32);
-        private _softwareSkinnedMeshes = new SmartArray<Mesh>(32);
+        private _activeSkeletons = new SmartArrayNoDuplicate<Skeleton>(32);
+        private _softwareSkinnedMeshes = new SmartArrayNoDuplicate<Mesh>(32);
 
         private _renderingManager: RenderingManager;
         private _physicsEngine: Nullable<PhysicsEngine>;