ソースを参照

fixing dispose bug with postprocess and updating variable location

Benjamin Guignabert 5 年 前
コミット
0095710844

+ 8 - 7
src/PostProcesses/postProcess.ts

@@ -657,16 +657,17 @@ export class PostProcess {
 
         this._disposeTextures();
 
+        let index;
         if (this._scene) {
-            let index = this._scene.postProcesses.indexOf(this);
+            index = this._scene.postProcesses.indexOf(this);
             if (index !== -1) {
                 this._scene.postProcesses.splice(index, 1);
             }
-        } else {
-            let index = this._engine.postProcesses.indexOf(this);
-            if (index !== -1) {
-                this._engine.postProcesses.splice(index, 1);
-            }
+        }
+
+        index = this._engine.postProcesses.indexOf(this);
+        if (index !== -1) {
+            this._engine.postProcesses.splice(index, 1);
         }
 
         if (!camera) {
@@ -674,7 +675,7 @@ export class PostProcess {
         }
         camera.detachPostProcess(this);
 
-        var index = camera._postProcesses.indexOf(this);
+        index = camera._postProcesses.indexOf(this);
         if (index === 0 && camera._postProcesses.length > 0) {
             var firstPostProcess = this._camera._getFirstPostProcess();
             if (firstPostProcess) {

+ 1 - 1
src/Rendering/prePassRenderer.ts

@@ -96,7 +96,7 @@ export class PrePassRenderer {
 
         PrePassRenderer._SceneComponentInitialization(this._scene);
 
-        this.subSurfaceConfiguration = new SubSurfaceConfiguration(this._scene);
+        this.subSurfaceConfiguration = new SubSurfaceConfiguration();
     }
 
     private _initializeAttachments() {

+ 24 - 27
src/Rendering/prePassRendererSceneComponent.ts

@@ -6,6 +6,20 @@ import { AbstractScene } from "../abstractScene";
 import { Color3 } from "../Maths/math.color";
 import { Logger } from "../Misc/logger";
 
+// Adds the parser to the scene parsers.
+AbstractScene.AddParser(SceneComponentConstants.NAME_PREPASSRENDERER, (parsedData: any, scene: Scene) => {
+    // Diffusion profiles
+    if (parsedData.ssDiffusionProfileColors !== undefined && parsedData.ssDiffusionProfileColors !== null) {
+        scene.enablePrePassRenderer();
+        if (scene.prePassRenderer) {
+            for (var index = 0, cache = parsedData.ssDiffusionProfileColors.length; index < cache; index++) {
+                var color = parsedData.ssDiffusionProfileColors[index];
+                scene.prePassRenderer.subSurfaceConfiguration.addDiffusionProfile(new Color3(color.r, color.g, color.b));
+            }
+        }
+    }
+});
+
 declare module "../abstractScene" {
     export interface AbstractScene {
         /** @hidden (Backing field) */
@@ -26,14 +40,6 @@ declare module "../abstractScene" {
          * Disables the prepass associated with the scene
          */
         disablePrePassRenderer(): void;
-
-        /**
-         * Diffusion profile colors for subsurface scattering
-         * You can add one diffusion color using `addDiffusionProfile` on `scene.prePassRenderer`
-         * See ...
-         * Note that you can only store up to 5 of them
-         */
-        ssDiffusionProfileColors: Color3[];
     }
 }
 
@@ -97,8 +103,6 @@ export class PrePassRendererSceneComponent implements ISceneSerializableComponen
      */
     constructor(scene: Scene) {
         this.scene = scene;
-
-        scene.ssDiffusionProfileColors = [];
     }
 
     /**
@@ -133,13 +137,17 @@ export class PrePassRendererSceneComponent implements ISceneSerializableComponen
      * @param serializationObject The object to serialize to
      */
     public serialize(serializationObject: any): void {
-        const ssDiffusionProfileColors = this.scene.ssDiffusionProfileColors;
+        if (!this.scene.prePassRenderer) {
+            return;
+        }
+
+        const ssDiffusionProfileColors = this.scene.prePassRenderer.subSurfaceConfiguration.ssDiffusionProfileColors;
         serializationObject.ssDiffusionProfileColors = [];
 
         for (let i = 0; i < ssDiffusionProfileColors.length; i++) {
-            serializationObject.ssDiffusionProfileColors.push(ssDiffusionProfileColors[i].r,
-                                                              ssDiffusionProfileColors[i].g,
-                                                              ssDiffusionProfileColors[i].b);
+            serializationObject.ssDiffusionProfileColors.push({ r: ssDiffusionProfileColors[i].r,
+                                                                g: ssDiffusionProfileColors[i].g,
+                                                                b: ssDiffusionProfileColors[i].b });
         }
     }
 
@@ -148,15 +156,7 @@ export class PrePassRendererSceneComponent implements ISceneSerializableComponen
      * @param container the container holding the elements
      */
     public addFromContainer(container: AbstractScene): void {
-        if (!container.ssDiffusionProfileColors) {
-            return;
-        }
-
-        if (this.scene.prePassRenderer) {
-            container.ssDiffusionProfileColors.forEach((color) => {
-                this.scene.prePassRenderer!.subSurfaceConfiguration.addDiffusionProfile(color);
-            });
-        }
+        // Nothing to do
     }
 
     /**
@@ -165,10 +165,7 @@ export class PrePassRendererSceneComponent implements ISceneSerializableComponen
      * @param dispose if the removed element should be disposed (default: false)
      */
     public removeFromContainer(container: AbstractScene, dispose?: boolean): void {
-        if (!container.ssDiffusionProfileColors) {
-            return;
-        }
-
+        // Make sure nothing will be serialized
         if (this.scene.prePassRenderer) {
             this.scene.prePassRenderer.subSurfaceConfiguration.clearAllDiffusionProfiles();
         }

+ 11 - 7
src/Rendering/subSurfaceConfiguration.ts

@@ -1,14 +1,11 @@
 import { Logger } from "../Misc/logger";
 import { Color3 } from "../Maths/math.color";
-import { Scene } from "../scene";
 
 /**
  * Contains all parameters needed for the prepass to perform
  * screen space subsurface scattering
  */
 export class SubSurfaceConfiguration {
-    private _scene: Scene;
-
     private _ssDiffusionS: number[] = [];
     private _ssFilterRadii: number[] = [];
     private _ssDiffusionD: number[] = [];
@@ -35,6 +32,14 @@ export class SubSurfaceConfiguration {
     }
 
     /**
+     * Diffusion profile colors for subsurface scattering
+     * You can add one diffusion color using `addDiffusionProfile` on `scene.prePassRenderer`
+     * See ...
+     * Note that you can only store up to 5 of them
+     */
+    public ssDiffusionProfileColors: Color3[] = [];
+
+    /**
      * Defines the ratio real world => scene units.
      * Used for subsurface scattering
      */
@@ -44,9 +49,8 @@ export class SubSurfaceConfiguration {
      * Builds a subsurface configuration object
      * @param scene The scene
      */
-    constructor(scene: Scene) {
+    constructor() {
         // Adding default diffusion profile
-        this._scene = scene;
         this.addDiffusionProfile(new Color3(1, 1, 1));
     }
 
@@ -75,7 +79,7 @@ export class SubSurfaceConfiguration {
         this._ssDiffusionS.push(color.r, color.b, color.g);
         this._ssDiffusionD.push(Math.max(Math.max(color.r, color.b), color.g));
         this._ssFilterRadii.push(this.getDiffusionProfileParameters(color));
-        this._scene.ssDiffusionProfileColors.push(color);
+        this.ssDiffusionProfileColors.push(color);
 
         return this._ssDiffusionD.length - 1;
     }
@@ -88,7 +92,7 @@ export class SubSurfaceConfiguration {
         this._ssDiffusionD = [];
         this._ssDiffusionS = [];
         this._ssFilterRadii = [];
-        this._scene.ssDiffusionProfileColors = [];
+        this.ssDiffusionProfileColors = [];
     }
 
     /**