瀏覽代碼

Fix spotlight projection texture race condition

sebavan 4 年之前
父節點
當前提交
feab004414
共有 1 個文件被更改,包括 16 次插入3 次删除
  1. 16 3
      src/Lights/spotLight.ts

+ 16 - 3
src/Lights/spotLight.ts

@@ -9,6 +9,7 @@ import { BaseTexture } from "../Materials/Textures/baseTexture";
 import { Light } from "./light";
 import { ShadowLight } from "./shadowLight";
 import { Texture } from '../Materials/Textures/texture';
+import { ProceduralTexture } from '../Materials';
 
 Node.AddNodeConstructor("Light_Type_2", (name, scene) => {
     return () => new SpotLight(name, Vector3.Zero(), Vector3.Zero(), 0, 0, scene);
@@ -176,15 +177,27 @@ export class SpotLight extends ShadowLight {
         this._projectionTexture = value;
         this._projectionTextureDirty = true;
         if (this._projectionTexture && !this._projectionTexture.isReady()) {
-            let texture = this._projectionTexture as Texture;
-            if (texture.onLoadObservable) {
-                texture.onLoadObservable.addOnce(() => {
+            if (SpotLight._IsProceduralTexture(this._projectionTexture)) {
+                this._projectionTexture.getEffect().executeWhenCompiled(() => {
+                    this._markMeshesAsLightDirty();
+                });
+            }
+            else if (SpotLight._IsTexture(this._projectionTexture)) {
+                this._projectionTexture.onLoadObservable.addOnce(() => {
                     this._markMeshesAsLightDirty();
                 });
             }
         }
     }
 
+    private static _IsProceduralTexture(texture: BaseTexture): texture is ProceduralTexture {
+        return (texture as ProceduralTexture).onGeneratedObservable !== undefined;
+    }
+
+    private static _IsTexture(texture: BaseTexture): texture is Texture {
+        return (texture as Texture).onLoadObservable !== undefined;
+    }
+
     private _projectionTextureViewLightDirty = true;
     private _projectionTextureProjectionLightDirty = true;
     private _projectionTextureDirty = true;