瀏覽代碼

Merge pull request #6901 from sebavan/master

Texture and ThinEngine
David Catuhe 5 年之前
父節點
當前提交
97c8a42bdf

+ 0 - 6
src/Engines/engine.ts

@@ -22,7 +22,6 @@ import { PerfCounter } from '../Misc/perfCounter';
 
 declare type Material = import("../Materials/material").Material;
 declare type PostProcess = import("../PostProcesses/postProcess").PostProcess;
-declare type Texture = import("../Materials/Textures/texture").Texture;
 
 /**
  * Defines the interface used by display changed events
@@ -370,11 +369,6 @@ export class Engine extends ThinEngine {
     public onCanvasPointerOutObservable = new Observable<PointerEvent>();
 
     /**
-     * Observable event triggered before each texture is initialized
-     */
-    public onBeforeTextureInitObservable = new Observable<Texture>();
-
-    /**
      * Observable raised when the engine begins a new frame
      */
     public onBeginFrameObservable = new Observable<Engine>();

+ 11 - 0
src/Engines/thinEngine.ts

@@ -33,6 +33,7 @@ import { IWebRequest } from '../Misc/interfaces/iWebRequest';
 declare type Observer<T> = import("../Misc/observable").Observer<T>;
 declare type VideoTexture = import("../Materials/Textures/videoTexture").VideoTexture;
 declare type RenderTargetTexture = import("../Materials/Textures/renderTargetTexture").RenderTargetTexture;
+declare type Texture = import("../Materials/Textures/texture").Texture;
 
 /**
  * Defines the interface used by objects working like Scene
@@ -432,6 +433,11 @@ export class ThinEngine {
     public readonly premultipliedAlpha: boolean = true;
 
     /**
+     * Observable event triggered before each texture is initialized
+     */
+    public onBeforeTextureInitObservable = new Observable<Texture>();
+
+    /**
      * Creates a new engine
      * @param canvasOrContext defines the canvas or WebGL context to use for rendering. If you provide a WebGL context, Babylon.js will not hook events on the canvas (like pointers, keyboards, etc...) so no event observables will be available. This is mostly used when Babylon.js is used as a plugin on a system which alreay used the WebGL context
      * @param antialias defines enable antialiasing (default: false)
@@ -3774,6 +3780,11 @@ export class ThinEngine {
     public dispose(): void {
         this.stopRenderLoop();
 
+        // Clear observables
+        if (this.onBeforeTextureInitObservable) {
+            this.onBeforeTextureInitObservable.clear();
+        }
+
         // Empty texture
         if (this._emptyTexture) {
             this._releaseTexture(this._emptyTexture);

+ 1 - 0
src/Materials/PBR/pbrBaseMaterial.ts

@@ -35,6 +35,7 @@ import { MaterialFlags } from "../materialFlags";
 import { Constants } from "../../Engines/constants";
 import { IAnimatable } from '../../Animations/animatable.interface';
 
+import "../../Materials/Textures/baseTexture.polynomial";
 import "../../Shaders/pbr.fragment";
 import "../../Shaders/pbr.vertex";
 import { EffectFallbacks } from '../effectFallbacks';

+ 38 - 0
src/Materials/Textures/baseTexture.polynomial.ts

@@ -0,0 +1,38 @@
+import { Nullable } from "../../types";
+import { CubeMapToSphericalPolynomialTools } from "../../Misc/HighDynamicRange/cubemapToSphericalPolynomial";
+import { SphericalPolynomial } from "../../Maths/sphericalPolynomial";
+import { _TimeToken } from "../../Instrumentation/timeToken";
+import { BaseTexture } from "./baseTexture";
+
+declare module "./baseTexture" {
+    export interface BaseTexture {
+        /**
+         * Get the polynomial representation of the texture data.
+         * This is mainly use as a fast way to recover IBL Diffuse irradiance data.
+         * @see https://learnopengl.com/PBR/IBL/Diffuse-irradiance
+         */
+        sphericalPolynomial: Nullable<SphericalPolynomial>;
+    }
+}
+
+Object.defineProperty(BaseTexture.prototype, "sphericalPolynomial", {
+    get: function(this: BaseTexture) {
+        if (!this._texture || !CubeMapToSphericalPolynomialTools || !this.isReady()) {
+            return null;
+        }
+
+        if (!this._texture._sphericalPolynomial) {
+            this._texture._sphericalPolynomial =
+                CubeMapToSphericalPolynomialTools.ConvertCubeMapTextureToSphericalPolynomial(this);
+        }
+
+        return this._texture._sphericalPolynomial;
+    },
+    set: function(this: BaseTexture, value: Nullable<SphericalPolynomial>) {
+        if (this._texture) {
+            this._texture._sphericalPolynomial = value;
+        }
+    },
+    enumerable: true,
+    configurable: true
+});

+ 0 - 26
src/Materials/Textures/baseTexture.ts

@@ -1,10 +1,8 @@
 import { serialize, SerializationHelper, serializeAsTexture, expandToProperty } from "../../Misc/decorators";
 import { Observer, Observable } from "../../Misc/observable";
-import { CubeMapToSphericalPolynomialTools } from "../../Misc/HighDynamicRange/cubemapToSphericalPolynomial";
 import { Nullable } from "../../types";
 import { Scene } from "../../scene";
 import { Matrix } from "../../Maths/math.vector";
-import { SphericalPolynomial } from "../../Maths/sphericalPolynomial";
 import { EngineStore } from "../../Engines/engineStore";
 import { InternalTexture } from "../../Materials/Textures/internalTexture";
 import { _TimeToken } from "../../Instrumentation/timeToken";
@@ -658,30 +656,6 @@ export class BaseTexture implements IAnimatable {
         }
     }
 
-    /**
-     * Get the polynomial representation of the texture data.
-     * This is mainly use as a fast way to recover IBL Diffuse irradiance data.
-     * @see https://learnopengl.com/PBR/IBL/Diffuse-irradiance
-     */
-    public get sphericalPolynomial(): Nullable<SphericalPolynomial> {
-        if (!this._texture || !CubeMapToSphericalPolynomialTools || !this.isReady()) {
-            return null;
-        }
-
-        if (!this._texture._sphericalPolynomial) {
-            this._texture._sphericalPolynomial =
-                CubeMapToSphericalPolynomialTools.ConvertCubeMapTextureToSphericalPolynomial(this);
-        }
-
-        return this._texture._sphericalPolynomial;
-    }
-
-    public set sphericalPolynomial(value: Nullable<SphericalPolynomial>) {
-        if (this._texture) {
-            this._texture._sphericalPolynomial = value;
-        }
-    }
-
     /** @hidden */
     public get _lodTextureHigh(): Nullable<BaseTexture> {
         if (this._texture) {

+ 3 - 1
src/Materials/Textures/hdrCubeTexture.ts

@@ -9,9 +9,11 @@ import { HDRTools } from "../../Misc/HighDynamicRange/hdr";
 import { CubeMapToSphericalPolynomialTools } from "../../Misc/HighDynamicRange/cubemapToSphericalPolynomial";
 import { _TypeStore } from '../../Misc/typeStore';
 import { Tools } from '../../Misc/tools';
-import "../../Engines/Extensions/engine.rawTexture";
 import { ToGammaSpace } from '../../Maths/math.constants';
 
+import "../../Engines/Extensions/engine.rawTexture";
+import "../../Materials/Textures/baseTexture.polynomial";
+
 /**
  * This represents a texture coming from an HDR input.
  *

+ 1 - 0
src/Materials/Textures/index.ts

@@ -1,4 +1,5 @@
 export * from "./baseTexture";
+export * from "./baseTexture.polynomial";
 export * from "./colorGradingTexture";
 export * from "./cubeTexture";
 export * from "./dynamicTexture";

+ 1 - 1
src/Materials/Textures/internalTexture.ts

@@ -1,6 +1,5 @@
 import { Observable } from "../../Misc/observable";
 import { Nullable, int } from "../../types";
-import { SphericalPolynomial } from "../../Maths/sphericalPolynomial";
 import { RenderTargetCreationOptions } from "../../Materials/Textures/renderTargetCreationOptions";
 import { _TimeToken } from "../../Instrumentation/timeToken";
 import { Constants } from "../../Engines/constants";
@@ -9,6 +8,7 @@ import { Engine } from '../../Engines/engine';
 
 declare type ThinEngine = import("../../Engines/thinEngine").ThinEngine;
 declare type BaseTexture = import("../../Materials/Textures/baseTexture").BaseTexture;
+declare type SphericalPolynomial = import("../../Maths/sphericalPolynomial").SphericalPolynomial;
 
 /**
  * Defines the source of the internal texture

+ 5 - 4
src/Materials/Textures/texture.ts

@@ -1,14 +1,13 @@
 import { serialize, SerializationHelper } from "../../Misc/decorators";
 import { Observable } from "../../Misc/observable";
 import { Nullable } from "../../types";
-import { Scene } from "../../scene";
 import { Matrix, Vector3 } from "../../Maths/math.vector";
 import { BaseTexture } from "../../Materials/Textures/baseTexture";
 import { Constants } from "../../Engines/constants";
 import { _TypeStore } from '../../Misc/typeStore';
 import { _DevTools } from '../../Misc/devTools';
 import { IInspectable } from '../../Misc/iInspectable';
-import { Engine } from '../../Engines/engine';
+import { ThinEngine } from '../../Engines/thinEngine';
 import { TimingTools } from '../../Misc/timingTools';
 import { InstantiationTools } from '../../Misc/instantiationTools';
 import { Plane } from '../../Maths/math.plane';
@@ -17,6 +16,7 @@ import { StringTools } from '../../Misc/stringTools';
 declare type CubeTexture = import("../../Materials/Textures/cubeTexture").CubeTexture;
 declare type MirrorTexture = import("../../Materials/Textures/mirrorTexture").MirrorTexture;
 declare type RenderTargetTexture = import("../../Materials/Textures/renderTargetTexture").RenderTargetTexture;
+declare type Scene = import("../../scene").Scene;
 
 /**
  * This represents a texture in babylon. It can be easily loaded from a network, base64 or html input.
@@ -275,7 +275,7 @@ export class Texture extends BaseTexture {
      * @param deleteBuffer define if the buffer we are loading the texture from should be deleted after load
      * @param format define the format of the texture we are trying to load (Engine.TEXTUREFORMAT_RGBA...)
      */
-    constructor(url: Nullable<string>, sceneOrEngine: Nullable<Scene | Engine>, noMipmap: boolean = false, invertY: boolean = true, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: Nullable<() => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null, buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob> = null, deleteBuffer: boolean = false, format?: number) {
+    constructor(url: Nullable<string>, sceneOrEngine: Nullable<Scene | ThinEngine>, noMipmap: boolean = false, invertY: boolean = true, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: Nullable<() => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null, buffer: Nullable<string | ArrayBuffer | ArrayBufferView | HTMLImageElement | Blob> = null, deleteBuffer: boolean = false, format?: number) {
         super((sceneOrEngine && sceneOrEngine.getClassName() === "Scene") ? (sceneOrEngine as Scene) : null);
 
         this.name = url || "";
@@ -290,11 +290,12 @@ export class Texture extends BaseTexture {
         }
 
         var scene = this.getScene();
-        var engine = (sceneOrEngine && (sceneOrEngine as Engine).getCaps) ? (sceneOrEngine as Engine) : (scene ? scene.getEngine() : null);
+        var engine = (sceneOrEngine && (sceneOrEngine as ThinEngine).getCaps) ? (sceneOrEngine as ThinEngine) : (scene ? scene.getEngine() : null);
 
         if (!engine) {
             return;
         }
+
         engine.onBeforeTextureInitObservable.notifyObservers(this);
 
         let load = () => {

+ 1 - 0
src/Misc/environmentTextureTools.ts

@@ -12,6 +12,7 @@ import { PostProcess } from "../PostProcesses/postProcess";
 import { Logger } from "../Misc/logger";
 
 import "../Engines/Extensions/engine.renderTargetCube";
+import "../Materials/Textures/baseTexture.polynomial";
 
 import "../Shaders/rgbdEncode.fragment";
 import "../Shaders/rgbdDecode.fragment";