浏览代码

Merge pull request #5377 from barroij/materialBlockDirty

Material block dirty
David Catuhe 6 年之前
父节点
当前提交
834ed896c8
共有 2 个文件被更改,包括 55 次插入20 次删除
  1. 2 0
      dist/preview release/what's new.md
  2. 53 20
      src/Materials/babylon.material.ts

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

@@ -42,7 +42,9 @@
   - Added `blockfreeActiveMeshesAndRenderingGroups` property in the `Scene`, following the same model as `blockMaterialDirtyMechanism`. This is to avoid calling `Scene.freeActiveMeshes` and `Scene.freeRenderingGroups` for each disposed mesh when we dispose several meshes in a row. One have to set `blockfreeActiveMeshesAndRenderingGroups` to `true` just before disposing the meshes, and set it back to `false` just after
   - Prevented code from doing useless and possible time consuming computation when disposing the `ShaderMaterial` of a `LinesMesh`
   - Make a better use of the `isIdentity` cached value wihtin a `Matrix`
+  - Make sure we browse all the submeshes only once in `Material.markAsDirty` function
 - Align `BoundingBox` and `BoundingSphere` API and behavior for clarity and simplicity. As a consequence, the `BoundingBox`'s method `setWorldMatrix` has been removed and the underlying world matrix cannot be modified but by calling `reConstruct` or `update`. ([barroij](https://github.com/barroij))
+- Make sure that `Material.markAsDirty` and all the `markXXXDirty` methods early out when `scene.blockMaterialDirtyMechanism` is true. ([barroij](https://github.com/barroij))
 
 ### glTF Loader
 

+ 53 - 20
src/Materials/babylon.material.ts

@@ -1154,29 +1154,63 @@ module BABYLON {
             });
         }
 
+        private static readonly _ImageProcessingDirtyCallBack = (defines: MaterialDefines) => defines.markAsImageProcessingDirty();
+        private static readonly _TextureDirtyCallBack = (defines: MaterialDefines) => defines.markAsTexturesDirty();
+        private static readonly _FresnelDirtyCallBack = (defines: MaterialDefines) => defines.markAsFresnelDirty();
+        private static readonly _MiscDirtyCallBack = (defines: MaterialDefines) => defines.markAsMiscDirty();
+        private static readonly _LightsDirtyCallBack = (defines: MaterialDefines) => defines.markAsLightDirty();
+        private static readonly _AttributeDirtyCallBack = (defines: MaterialDefines) => defines.markAsAttributesDirty();
+
+        private static _FresnelAndMiscDirtyCallBack = (defines: MaterialDefines) => {
+            Material._FresnelDirtyCallBack(defines);
+            Material._MiscDirtyCallBack(defines);
+        }
+
+        private static _TextureAndMiscDirtyCallBack = (defines: MaterialDefines) => {
+            Material._TextureDirtyCallBack(defines);
+            Material._MiscDirtyCallBack(defines);
+        }
+
+        private static readonly _DirtyCallbackArray: Array<(defines: MaterialDefines) => void> = [];
+        private static readonly _RunDirtyCallBacks = (defines: MaterialDefines) => {
+            for (const cb of Material._DirtyCallbackArray) {
+                cb(defines);
+            }
+        }
+
         /**
          * Marks a define in the material to indicate that it needs to be re-computed
          * @param flag defines a flag used to determine which parts of the material have to be marked as dirty
          */
         public markAsDirty(flag: number): void {
+            if (this.getScene().blockMaterialDirtyMechanism) {
+                return;
+            }
+
+            Material._DirtyCallbackArray.length = 0;
+
             if (flag & Material.TextureDirtyFlag) {
-                this._markAllSubMeshesAsTexturesDirty();
+                Material._DirtyCallbackArray.push(Material._TextureDirtyCallBack);
             }
 
             if (flag & Material.LightDirtyFlag) {
-                this._markAllSubMeshesAsLightsDirty();
+                Material._DirtyCallbackArray.push(Material._LightsDirtyCallBack);
             }
 
             if (flag & Material.FresnelDirtyFlag) {
-                this._markAllSubMeshesAsFresnelDirty();
+                Material._DirtyCallbackArray.push(Material._FresnelDirtyCallBack);
             }
 
             if (flag & Material.AttributesDirtyFlag) {
-                this._markAllSubMeshesAsAttributesDirty();
+                Material._DirtyCallbackArray.push(Material._AttributeDirtyCallBack);
             }
 
             if (flag & Material.MiscDirtyFlag) {
-                this._markAllSubMeshesAsMiscDirty();
+                Material._DirtyCallbackArray.push(Material._MiscDirtyCallBack);
+            }
+
+            if (Material._DirtyCallbackArray.length) {
+                this._markAllSubMeshesAsDirty(Material._RunDirtyCallBacks);
             }
 
             this.getScene().resetCachedMaterial();
@@ -1187,7 +1221,12 @@ module BABYLON {
          * @param func defines a function which checks material defines against the submeshes
          */
         protected _markAllSubMeshesAsDirty(func: (defines: MaterialDefines) => void) {
-            for (var mesh of this.getScene().meshes) {
+            if (this.getScene().blockMaterialDirtyMechanism) {
+                return;
+            }
+
+            const meshes = this.getScene().meshes;
+            for (var mesh of meshes) {
                 if (!mesh.subMeshes) {
                     continue;
                 }
@@ -1209,62 +1248,56 @@ module BABYLON {
          * Indicates that image processing needs to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsImageProcessingDirty() {
-            this._markAllSubMeshesAsDirty((defines) => defines.markAsImageProcessingDirty());
+            this._markAllSubMeshesAsDirty(Material._ImageProcessingDirtyCallBack);
         }
 
         /**
          * Indicates that textures need to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsTexturesDirty() {
-            this._markAllSubMeshesAsDirty((defines) => defines.markAsTexturesDirty());
+            this._markAllSubMeshesAsDirty(Material._TextureDirtyCallBack);
         }
 
         /**
          * Indicates that fresnel needs to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsFresnelDirty() {
-            this._markAllSubMeshesAsDirty((defines) => defines.markAsFresnelDirty());
+            this._markAllSubMeshesAsDirty(Material._FresnelDirtyCallBack);
         }
 
         /**
          * Indicates that fresnel and misc need to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsFresnelAndMiscDirty() {
-            this._markAllSubMeshesAsDirty((defines) => {
-                defines.markAsFresnelDirty();
-                defines.markAsMiscDirty();
-            });
+            this._markAllSubMeshesAsDirty(Material._FresnelAndMiscDirtyCallBack);
         }
 
         /**
          * Indicates that lights need to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsLightsDirty() {
-            this._markAllSubMeshesAsDirty((defines) => defines.markAsLightDirty());
+            this._markAllSubMeshesAsDirty(Material._LightsDirtyCallBack);
         }
 
         /**
          * Indicates that attributes need to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsAttributesDirty() {
-            this._markAllSubMeshesAsDirty((defines) => defines.markAsAttributesDirty());
+            this._markAllSubMeshesAsDirty(Material._AttributeDirtyCallBack);
         }
 
         /**
          * Indicates that misc needs to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsMiscDirty() {
-            this._markAllSubMeshesAsDirty((defines) => defines.markAsMiscDirty());
+            this._markAllSubMeshesAsDirty(Material._MiscDirtyCallBack);
         }
 
         /**
          * Indicates that textures and misc need to be re-calculated for all submeshes
          */
         protected _markAllSubMeshesAsTexturesAndMiscDirty() {
-            this._markAllSubMeshesAsDirty((defines) => {
-                defines.markAsTexturesDirty();
-                defines.markAsMiscDirty();
-            });
+            this._markAllSubMeshesAsDirty(Material._TextureAndMiscDirtyCallBack);
         }
 
         /**