Sfoglia il codice sorgente

Avoid calling createEffect everytime

Popov72 5 anni fa
parent
commit
56def0bd9e
1 ha cambiato i file con 20 aggiunte e 15 eliminazioni
  1. 20 15
      src/Materials/shaderMaterial.ts

+ 20 - 15
src/Materials/shaderMaterial.ts

@@ -90,6 +90,7 @@ export class ShaderMaterial extends Material {
     private _cachedWorldViewProjectionMatrix = new Matrix();
     private _renderId: number;
     private _multiview: boolean = false;
+    private _cachedDefines: string;
 
     /**
      * Instantiate a new shader material.
@@ -589,23 +590,27 @@ export class ShaderMaterial extends Material {
         var previousEffect = this._effect;
         var join = defines.join("\n");
 
-        this._effect = engine.createEffect(shaderName, <IEffectCreationOptions>{
-            attributes: attribs,
-            uniformsNames: uniforms,
-            uniformBuffersNames: uniformBuffers,
-            samplers: samplers,
-            defines: join,
-            fallbacks: fallbacks,
-            onCompiled: this.onCompiled,
-            onError: this.onError
-        }, engine);
-
-        if (this._onEffectCreatedObservable) {
-            onCreatedEffectParameters.effect = this._effect;
-            this._onEffectCreatedObservable.notifyObservers(onCreatedEffectParameters);
+        if (this._cachedDefines !== join) {
+            this._cachedDefines = join;
+
+            this._effect = engine.createEffect(shaderName, <IEffectCreationOptions>{
+                attributes: attribs,
+                uniformsNames: uniforms,
+                uniformBuffersNames: uniformBuffers,
+                samplers: samplers,
+                defines: join,
+                fallbacks: fallbacks,
+                onCompiled: this.onCompiled,
+                onError: this.onError
+            }, engine);
+
+            if (this._onEffectCreatedObservable) {
+                onCreatedEffectParameters.effect = this._effect;
+                this._onEffectCreatedObservable.notifyObservers(onCreatedEffectParameters);
+            }
         }
 
-        if (!this._effect.isReady()) {
+        if (!this._effect?.isReady() ?? true) {
             return false;
         }