Browse Source

Merge pull request #5301 from barroij/transformNodeIdxInScene

Speed-up addition and removal of Transfrom node from scenes
David Catuhe 6 năm trước cách đây
mục cha
commit
563fe3e536

+ 3 - 1
dist/preview release/what's new.md

@@ -16,7 +16,9 @@
 - Added utility function `Tools.BuildArray` for array initialisation ([barroij](https://github.com/barroij))
 - Introduced a new `IOfflineSupport` interface to hide IndexedDB ([Deltakosh](https://github.com/deltakosh))
 - `PBRMaterial` and `StandardMaterial` now use hot swapping feature for shaders. This means they can keep using a previous shader while a new one is being compiled ([Deltakosh](https://github.com/deltakosh))
-- Performance oriented change (prevent avoidable matrix inversion or square root computation) ([barroij](https://github.com/barroij))
+- Performance oriented changes ([barroij](https://github.com/barroij))
+  - prevent avoidable matrix inversion or square root computation.
+  - enable a removal in O(1) from the `transformNodes`array of the Scene.
 
 ### glTF Loader
 

+ 2 - 0
src/Mesh/babylon.transformNode.ts

@@ -93,6 +93,8 @@ module BABYLON {
 
         protected _isWorldMatrixFrozen = false;
 
+        /** @hidden */
+        public _indexInSceneTransformNodesArray = -1;
         /**
         * An event triggered after the world matrix is updated
         */

+ 3 - 0
src/babylon.abstractScene.ts

@@ -155,6 +155,9 @@ module BABYLON {
 
         /**
         * All of the tranform nodes added to this scene
+        * In the context a the Scene, it is not supposed to be modified manually.
+        * Any addition or removal should be done using the addTransformNode and removeTransformNode Scene methods.
+        * Note also that the order of the TransformNode wihin the array is not significant and might change.
         * @see http://doc.babylonjs.com/how_to/transformnode
         */
         public transformNodes = new Array<TransformNode>();

+ 10 - 3
src/babylon.scene.ts

@@ -3000,6 +3000,7 @@ module BABYLON {
          * @param newTransformNode defines the transform node to add
          */
         public addTransformNode(newTransformNode: TransformNode) {
+            newTransformNode._indexInSceneTransformNodesArray = this.transformNodes.length;
             this.transformNodes.push(newTransformNode);
 
             this.onNewTransformNodeAddedObservable.notifyObservers(newTransformNode);
@@ -3011,10 +3012,16 @@ module BABYLON {
          * @returns the index where the transform node was in the transform node list
          */
         public removeTransformNode(toRemove: TransformNode): number {
-            var index = this.transformNodes.indexOf(toRemove);
+            var index = toRemove._indexInSceneTransformNodesArray;
             if (index !== -1) {
-                // Remove from the scene if found
-                this.transformNodes.splice(index, 1);
+                if (index !== this.transformNodes.length - 1) {
+                    const lastNode = this.transformNodes[this.transformNodes.length - 1];
+                    this.transformNodes[index] = lastNode;
+                    lastNode._indexInSceneTransformNodesArray = index;
+                }
+
+                toRemove._indexInSceneTransformNodesArray = -1;
+                this.transformNodes.pop();
             }
 
             this.onTransformNodeRemovedObservable.notifyObservers(toRemove);