Pārlūkot izejas kodu

ThinEngine Cube Texture

sebavan 5 gadi atpakaļ
vecāks
revīzija
bd2cbf0e29

+ 7 - 7
src/Engines/Extensions/engine.rawTexture.ts

@@ -105,7 +105,7 @@ declare module "../../Engines/thinEngine" {
          * @param onError defines a callback called if there is an error
          * @returns the cube texture as an InternalTexture
          */
-        createRawCubeTextureFromUrl(url: string, scene: Scene, size: number, format: number, type: number, noMipmap: boolean,
+        createRawCubeTextureFromUrl(url: string, scene: Nullable<Scene>, size: number, format: number, type: number, noMipmap: boolean,
             callback: (ArrayBuffer: ArrayBuffer) => Nullable<ArrayBufferView[]>,
             mipmapGenerator: Nullable<((faces: ArrayBufferView[]) => ArrayBufferView[][])>,
             onLoad: Nullable<() => void>,
@@ -127,7 +127,7 @@ declare module "../../Engines/thinEngine" {
          * @param invertY defines if data must be stored with Y axis inverted
          * @returns the cube texture as an InternalTexture
          */
-        createRawCubeTextureFromUrl(url: string, scene: Scene, size: number, format: number, type: number, noMipmap: boolean,
+        createRawCubeTextureFromUrl(url: string, scene: Nullable<Scene>, size: number, format: number, type: number, noMipmap: boolean,
             callback: (ArrayBuffer: ArrayBuffer) => Nullable<ArrayBufferView[]>,
             mipmapGenerator: Nullable<((faces: ArrayBufferView[]) => ArrayBufferView[][])>,
             onLoad: Nullable<() => void>,
@@ -410,7 +410,7 @@ ThinEngine.prototype.updateRawCubeTexture = function(texture: InternalTexture, d
     texture.isReady = true;
 };
 
-ThinEngine.prototype.createRawCubeTextureFromUrl = function(url: string, scene: Scene, size: number, format: number, type: number, noMipmap: boolean,
+ThinEngine.prototype.createRawCubeTextureFromUrl = function(url: string, scene: Nullable<Scene>, size: number, format: number, type: number, noMipmap: boolean,
     callback: (ArrayBuffer: ArrayBuffer) => Nullable<ArrayBufferView[]>,
     mipmapGenerator: Nullable<((faces: ArrayBufferView[]) => ArrayBufferView[][])>,
     onLoad: Nullable<() => void> = null,
@@ -420,12 +420,12 @@ ThinEngine.prototype.createRawCubeTextureFromUrl = function(url: string, scene:
 
     var gl = this._gl;
     var texture = this.createRawCubeTexture(null, size, format, type, !noMipmap, invertY, samplingMode, null);
-    scene._addPendingData(texture);
+    scene?._addPendingData(texture);
     texture.url = url;
     this._internalTexturesCache.push(texture);
 
     var onerror = (request?: IWebRequest, exception?: any) => {
-        scene._removePendingData(texture);
+        scene?._removePendingData(texture);
         if (onError && request) {
             onError(request.status + " " + request.statusText, exception);
         }
@@ -474,7 +474,7 @@ ThinEngine.prototype.createRawCubeTextureFromUrl = function(url: string, scene:
 
         texture.isReady = true;
         // this.resetTextureCache();
-        scene._removePendingData(texture);
+        scene?._removePendingData(texture);
 
         if (onLoad) {
             onLoad();
@@ -483,7 +483,7 @@ ThinEngine.prototype.createRawCubeTextureFromUrl = function(url: string, scene:
 
     this._loadFile(url, (data) => {
         internalCallback(data);
-    }, undefined, scene.offlineProvider, true, onerror);
+    }, undefined, scene?.offlineProvider, true, onerror);
 
     return texture;
 };

+ 12 - 1
src/Materials/Textures/baseTexture.ts

@@ -393,12 +393,23 @@ export class BaseTexture implements IAnimatable {
      * @param scene Define the scene the texture blongs to
      */
     constructor(scene: Nullable<Scene>) {
+        this._setScene(scene);
+        this._uid = null;
+    }
+
+    /**
+     * @param hidden
+     */
+    public _setScene(scene: Nullable<Scene>): void {
+        if (this._scene) {
+            this._scene.removeTexture(this);
+        }
+
         this._scene = scene || EngineStore.LastCreatedScene;
         if (this._scene) {
             this.uniqueId = this._scene.getUniqueId();
             this._scene.addTexture(this);
         }
-        this._uid = null;
     }
 
     /**

+ 26 - 20
src/Materials/Textures/cubeTexture.ts

@@ -7,6 +7,7 @@ import { BaseTexture } from "../../Materials/Textures/baseTexture";
 import { Texture } from "../../Materials/Textures/texture";
 import { Constants } from "../../Engines/constants";
 import { _TypeStore } from '../../Misc/typeStore';
+import { ThinEngine } from '../../Engines/thinEngine';
 
 import "../../Engines/Extensions/engine.cubeTexture";
 import { StringTools } from '../../Misc/stringTools';
@@ -102,6 +103,7 @@ export class CubeTexture extends BaseTexture {
 
     private _format: number;
     private _createPolynomials: boolean;
+    private _engine: ThinEngine;
 
     /** @hidden */
     public _prefiltered: boolean = false;
@@ -158,11 +160,19 @@ export class CubeTexture extends BaseTexture {
      * @param lodOffset defines the offset applied to environment texture. This manages first LOD level used for IBL according to the roughness
      * @return the cube texture
      */
-    constructor(rootUrl: string, scene: Scene, extensions: Nullable<string[]> = null, noMipmap: boolean = false, files: Nullable<string[]> = null,
+    constructor(rootUrl: string, sceneOrEngine: Scene | ThinEngine, extensions: Nullable<string[]> = null, noMipmap: boolean = false, files: Nullable<string[]> = null,
         onLoad: Nullable<() => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null, format: number = Constants.TEXTUREFORMAT_RGBA, prefiltered = false,
         forcedExtension: any = null, createPolynomials: boolean = false,
         lodScale: number = 0.8, lodOffset: number = 0) {
-        super(scene);
+        super(null);
+        if (CubeTexture._isScene(sceneOrEngine)) {
+            this._setScene(sceneOrEngine);
+            this._engine = sceneOrEngine.getEngine();
+        }
+        else {
+            this._setScene(null);
+            this._engine = sceneOrEngine;
+        }
 
         this.name = rootUrl;
         this.url = rootUrl;
@@ -227,12 +237,13 @@ export class CubeTexture extends BaseTexture {
         };
 
         if (!this._texture) {
-            if (!scene.useDelayedTextureLoading) {
+            const scene = this.getScene();
+            if (!scene?.useDelayedTextureLoading) {
                 if (prefiltered) {
-                    this._texture = scene.getEngine().createPrefilteredCubeTexture(rootUrl, scene, lodScale, lodOffset, onLoad, onError, format, forcedExtension, this._createPolynomials);
+                    this._texture = this._engine.createPrefilteredCubeTexture(rootUrl, scene, lodScale, lodOffset, onLoad, onError, format, forcedExtension, this._createPolynomials);
                 }
                 else {
-                    this._texture = scene.getEngine().createCubeTexture(rootUrl, scene, files, noMipmap, onLoad, onError, this._format, forcedExtension, false, lodScale, lodOffset);
+                    this._texture = this._engine.createCubeTexture(rootUrl, scene, files, noMipmap, onLoad, onError, this._format, forcedExtension, false, lodScale, lodOffset);
                 }
                 this._texture?.onLoadedObservable.add(() => this.onLoadObservable.notifyObservers(this));
 
@@ -273,7 +284,7 @@ export class CubeTexture extends BaseTexture {
     public updateURL(url: string, forcedExtension?: string, onLoad?: () => void, prefiltered: boolean = false): void {
         if (this.url) {
             this.releaseInternalTexture();
-            this.getScene()!.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
+            this.getScene()?.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag);
         }
 
         if (!this.name || StringTools.StartsWith(this.name, "data:")) {
@@ -304,20 +315,16 @@ export class CubeTexture extends BaseTexture {
             return;
         }
 
-        let scene = this.getScene();
-
-        if (!scene) {
-            return;
-        }
         this.delayLoadState = Constants.DELAYLOADSTATE_LOADED;
         this._texture = this._getFromCache(this.url, this._noMipmap);
 
         if (!this._texture) {
+            const scene = this.getScene();
             if (this._prefiltered) {
-                this._texture = scene.getEngine().createPrefilteredCubeTexture(this.url, scene, 0.8, 0, this._delayedOnLoad, undefined, this._format, undefined, this._createPolynomials);
+                this._texture = this._engine.createPrefilteredCubeTexture(this.url, scene, 0.8, 0, this._delayedOnLoad, undefined, this._format, undefined, this._createPolynomials);
             }
             else {
-                this._texture = scene.getEngine().createCubeTexture(this.url, scene, this._files, this._noMipmap, this._delayedOnLoad, null, this._format, forcedExtension);
+                this._texture = this._engine.createCubeTexture(this.url, scene, this._files, this._noMipmap, this._delayedOnLoad, null, this._format, forcedExtension);
             }
 
             this._texture?.onLoadedObservable.add(() => this.onLoadObservable.notifyObservers(this));
@@ -342,7 +349,7 @@ export class CubeTexture extends BaseTexture {
         }
 
         if (value.isIdentity() !== this._textureMatrix.isIdentity()) {
-            this.getScene()!.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag, (mat) => mat.getActiveTextures().indexOf(this) !== -1);
+            this.getScene()?.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag, (mat) => mat.getActiveTextures().indexOf(this) !== -1);
         }
 
         this._textureMatrix = value;
@@ -391,15 +398,10 @@ export class CubeTexture extends BaseTexture {
      * @returns a new cube texture
      */
     public clone(): CubeTexture {
-        let scene = this.getScene();
         let uniqueId = 0;
 
         let newCubeTexture = SerializationHelper.Clone(() => {
-
-            if (!scene) {
-                return this;
-            }
-            const cubeTexture = new CubeTexture(this.url, scene, this._extensions, this._noMipmap, this._files);
+            const cubeTexture = new CubeTexture(this.url, this.getScene() || this._engine, this._extensions, this._noMipmap, this._files);
             uniqueId = cubeTexture.uniqueId;
 
             return cubeTexture;
@@ -409,6 +411,10 @@ export class CubeTexture extends BaseTexture {
 
         return newCubeTexture;
     }
+
+    private static _isScene(sceneOrEngine: Scene | ThinEngine): sceneOrEngine is Scene {
+        return sceneOrEngine.getClassName() === "Scene";
+    }
 }
 
 Texture._CubeTextureParser = CubeTexture.Parse;

+ 26 - 25
src/Materials/Textures/hdrCubeTexture.ts

@@ -9,6 +9,7 @@ import { CubeMapToSphericalPolynomialTools } from "../../Misc/HighDynamicRange/c
 import { _TypeStore } from '../../Misc/typeStore';
 import { Tools } from '../../Misc/tools';
 import { ToGammaSpace } from '../../Maths/math.constants';
+import { ThinEngine } from '../../Engines/thinEngine';
 
 import "../../Engines/Extensions/engine.rawTexture";
 import "../../Materials/Textures/baseTexture.polynomial";
@@ -36,6 +37,7 @@ export class HDRCubeTexture extends BaseTexture {
     private _size: number;
     private _onLoad: Nullable<() => void> = null;
     private _onError: Nullable<() => void> = null;
+    private _engine: ThinEngine;
 
     /**
      * The texture URL.
@@ -115,8 +117,16 @@ export class HDRCubeTexture extends BaseTexture {
      * @param gammaSpace Specifies if the texture will be use in gamma or linear space (the PBR material requires those texture in linear space, but the standard material would require them in Gamma space)
      * @param reserved Reserved flag for internal use.
      */
-    constructor(url: string, scene: Scene, size: number, noMipmap = false, generateHarmonics = true, gammaSpace = false, reserved = false, onLoad: Nullable<() => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null) {
-        super(scene);
+    constructor(url: string, sceneOrEngine: Scene | ThinEngine, size: number, noMipmap = false, generateHarmonics = true, gammaSpace = false, reserved = false, onLoad: Nullable<() => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null) {
+        super(null);
+        if (HDRCubeTexture._isScene(sceneOrEngine)) {
+            this._setScene(sceneOrEngine);
+            this._engine = sceneOrEngine.getEngine();
+        }
+        else {
+            this._setScene(null);
+            this._engine = sceneOrEngine;
+        }
 
         if (!url) {
             return;
@@ -138,7 +148,7 @@ export class HDRCubeTexture extends BaseTexture {
         this._texture = this._getFromCache(url, this._noMipmap);
 
         if (!this._texture) {
-            if (!scene.useDelayedTextureLoading) {
+            if (!this.getScene()?.useDelayedTextureLoading) {
                 this.loadTexture();
             } else {
                 this.delayLoadState = Constants.DELAYLOADSTATE_NOTLOADED;
@@ -169,11 +179,6 @@ export class HDRCubeTexture extends BaseTexture {
             this.lodGenerationOffset = 0.0;
             this.lodGenerationScale = 0.8;
 
-            let scene = this.getScene();
-
-            if (!scene) {
-                return null;
-            }
             // Extract the raw linear data.
             var data = HDRTools.GetCubeMapTextureData(buffer, this._size);
 
@@ -190,7 +195,7 @@ export class HDRCubeTexture extends BaseTexture {
             for (var j = 0; j < 6; j++) {
 
                 // Create uintarray fallback.
-                if (!scene.getEngine().getCaps().textureFloat) {
+                if (!this._engine.getCaps().textureFloat) {
                     // 3 channels of 1 bytes per pixel in bytes.
                     var byteBuffer = new ArrayBuffer(this._size * this._size * 3);
                     byteArray = new Uint8Array(byteBuffer);
@@ -241,24 +246,16 @@ export class HDRCubeTexture extends BaseTexture {
             return results;
         };
 
-        let scene = this.getScene();
-        if (scene) {
-            this._texture = scene.getEngine().createRawCubeTextureFromUrl(this.url, scene, this._size,
-                Constants.TEXTUREFORMAT_RGB,
-                scene.getEngine().getCaps().textureFloat ? Constants.TEXTURETYPE_FLOAT : Constants.TEXTURETYPE_UNSIGNED_INT,
-                this._noMipmap,
-                callback,
-                null, this._onLoad, this._onError);
-        }
+        this._texture = this._engine.createRawCubeTextureFromUrl(this.url, this.getScene(), this._size,
+            Constants.TEXTUREFORMAT_RGB,
+            this._engine.getCaps().textureFloat ? Constants.TEXTURETYPE_FLOAT : Constants.TEXTURETYPE_UNSIGNED_INT,
+            this._noMipmap,
+            callback,
+            null, this._onLoad, this._onError);
     }
 
     public clone(): HDRCubeTexture {
-        let scene = this.getScene();
-        if (!scene) {
-            return this;
-        }
-
-        var newTexture = new HDRCubeTexture(this.url, scene, this._size, this._noMipmap, this._generateHarmonics,
+        var newTexture = new HDRCubeTexture(this.url, this.getScene() || this._engine, this._size, this._noMipmap, this._generateHarmonics,
             this.gammaSpace);
 
         // Base texture
@@ -305,7 +302,7 @@ export class HDRCubeTexture extends BaseTexture {
         }
 
         if (value.isIdentity() !== this._textureMatrix.isIdentity()) {
-            this.getScene()!.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag, (mat) => mat.getActiveTextures().indexOf(this) !== -1);
+            this.getScene()?.markAllMaterialsAsDirty(Constants.MATERIAL_TextureDirtyFlag, (mat) => mat.getActiveTextures().indexOf(this) !== -1);
         }
     }
 
@@ -362,6 +359,10 @@ export class HDRCubeTexture extends BaseTexture {
 
         return serializationObject;
     }
+
+    private static _isScene(sceneOrEngine: Scene | ThinEngine): sceneOrEngine is Scene {
+        return sceneOrEngine.getClassName() === "Scene";
+    }
 }
 
 _TypeStore.RegisteredTypes["BABYLON.HDRCubeTexture"] = HDRCubeTexture;