Переглянути джерело

same index trick for materials in scene and instance mesh in source mesh array

Julien Barrois 6 роки тому
батько
коміт
dd7168a5aa

+ 12 - 13
src/Materials/babylon.material.ts

@@ -765,6 +765,9 @@ module BABYLON {
          */
         protected _uniformBuffer: UniformBuffer;
 
+        /** @hidden */
+        public _indexInSceneMaterialArray = -1;
+
         /**
          * Creates a material instance
          * @param name defines the name of the material
@@ -788,8 +791,7 @@ module BABYLON {
             this._useUBO = this.getScene().getEngine().supportsUniformBuffers;
 
             if (!doNotAdd) {
-                this._scene.materials.push(this);
-                this._scene.onNewMaterialAddedObservable.notifyObservers(this);
+                this._scene.addMaterial(this);
             }
         }
 
@@ -1261,20 +1263,17 @@ module BABYLON {
          * @param forceDisposeTextures specifies if textures should be forcefully disposed
          */
         public dispose(forceDisposeEffect?: boolean, forceDisposeTextures?: boolean): void {
+            const scene = this.getScene();
             // Animations
-            this.getScene().stopAnimation(this);
-            this.getScene().freeProcessedMaterials();
+            scene.stopAnimation(this);
+            scene.freeProcessedMaterials();
 
             // Remove from scene
-            var index = this._scene.materials.indexOf(this);
-            if (index >= 0) {
-                this._scene.materials.splice(index, 1);
-            }
-            this._scene.onMaterialRemovedObservable.notifyObservers(this);
+            scene.removeMaterial(this);
 
             // Remove from meshes
-            for (index = 0; index < this._scene.meshes.length; index++) {
-                var mesh = this._scene.meshes[index];
+            for (let index = 0; index < scene.meshes.length; index++) {
+                var mesh = scene.meshes[index];
 
                 if (mesh.material === this) {
                     mesh.material = null;
@@ -1286,7 +1285,7 @@ module BABYLON {
                             for (var subMesh of mesh.subMeshes) {
                                 geometry._releaseVertexArrayObject(subMesh._materialEffect);
                                 if (forceDisposeEffect && subMesh._materialEffect) {
-                                    this._scene.getEngine()._releaseEffect(subMesh._materialEffect);
+                                    scene.getEngine()._releaseEffect(subMesh._materialEffect);
                                 }
                             }
                         } else {
@@ -1301,7 +1300,7 @@ module BABYLON {
             // Shader are kept in cache for further use but we can get rid of this by using forceDisposeEffect
             if (forceDisposeEffect && this._effect) {
                 if (!this.storeEffectOnSubMeshes) {
-                    this._scene.getEngine()._releaseEffect(this._effect);
+                    scene.getEngine()._releaseEffect(this._effect);
                 }
 
                 this._effect = null;

+ 14 - 2
src/Mesh/babylon.instancedMesh.ts

@@ -7,9 +7,12 @@ module BABYLON {
         private _sourceMesh: Mesh;
         private _currentLOD: Mesh;
 
+        private _indexInSourceMeshInstanceArray = -1;
+
         constructor(name: string, source: Mesh) {
             super(name, source.getScene());
 
+            this._indexInSourceMeshInstanceArray = source.instances.length;
             source.instances.push(this);
 
             this._sourceMesh = source;
@@ -319,8 +322,17 @@ module BABYLON {
         public dispose(doNotRecurse?: boolean, disposeMaterialAndTextures = false): void {
 
             // Remove from mesh
-            var index = this._sourceMesh.instances.indexOf(this);
-            this._sourceMesh.instances.splice(index, 1);
+            const index = this._indexInSourceMeshInstanceArray;
+            if (index != -1) {
+                if (index !== this._sourceMesh.instances.length - 1) {
+                    const last = this._sourceMesh.instances[this._sourceMesh.instances.length - 1];
+                    this._sourceMesh.instances[index] = last;
+                    last._indexInSceneTransformNodesArray = index;
+
+                }
+                this._indexInSourceMeshInstanceArray = -1;
+                this._sourceMesh.instances.pop();
+            }
 
             super.dispose(doNotRecurse, disposeMaterialAndTextures);
         }

+ 6 - 5
src/Mesh/babylon.transformNode.ts

@@ -95,6 +95,7 @@ module BABYLON {
 
         /** @hidden */
         public _indexInSceneTransformNodesArray = -1;
+
         /**
         * An event triggered after the world matrix is updated
         */
@@ -224,7 +225,7 @@ module BABYLON {
             }
 
             if (this.billboardMode !== this._cache.billboardMode || this.billboardMode !== TransformNode.BILLBOARDMODE_NONE) {
-                return false;
+                return false;
             }
 
             if (this._cache.pivotMatrixUpdated) {
@@ -236,21 +237,21 @@ module BABYLON {
             }
 
             if (!this._cache.position.equals(this._position)) {
-                return false;
+                return false;
             }
 
             if (this._rotationQuaternion) {
                 if (!this._cache.rotationQuaternion.equals(this._rotationQuaternion)) {
-                    return false;
+                    return false;
                 }
             }
 
             if (!this._cache.rotation.equals(this._rotation)) {
-                return false;
+                return false;
             }
 
             if (!this._cache.scaling.equals(this._scaling)) {
-                return false;
+                return false;
             }
 
             return true;