Browse Source

Get scene from sharedData instead of Engine.LastCreatedScene

Popov72 5 năm trước cách đây
mục cha
commit
89ebffc43b

+ 12 - 9
src/Materials/Node/Blocks/Fragment/PBR/pbrMetallicRoughnessBlock.ts

@@ -21,22 +21,21 @@ import { AmbientOcclusionBlock } from './ambientOcclusionBlock';
 import { SheenBlock } from './sheenBlock';
 import { ReflectivityBlock } from './reflectivityBlock';
 import { BaseTexture } from '../../../../Textures/baseTexture';
-import { Engine } from '../../../../../Engines/engine';
 import { BRDFTextureTools } from '../../../../../Misc/brdfTextureTools';
 import { MaterialFlags } from '../../../../materialFlags';
 import { AnisotropyBlock } from './anisotropyBlock';
 import { ReflectionBlock } from './reflectionBlock';
 
 export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
-    private _lightId: number;
-
     /**
      * Gets or sets the light associated with this block
      */
     public light: Nullable<Light>;
 
-    protected _environmentBRDFTexture: Nullable<BaseTexture> = null;
-    protected _environmentBrdfSamplerName: string;
+    private _lightId: number;
+    private _scene: Scene;
+    private _environmentBRDFTexture: Nullable<BaseTexture> = null;
+    private _environmentBrdfSamplerName: string;
 
     public constructor(name: string) {
         super(name, NodeMaterialBlockTargets.VertexAndFragment);
@@ -71,8 +70,6 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         this.registerOutput("refraction", NodeMaterialBlockConnectionPointTypes.Color3, NodeMaterialBlockTargets.Fragment);
         this.registerOutput("lighting", NodeMaterialBlockConnectionPointTypes.Color4, NodeMaterialBlockTargets.Fragment);
         this.registerOutput("shadow", NodeMaterialBlockConnectionPointTypes.Float, NodeMaterialBlockTargets.Fragment);
-
-        this._environmentBRDFTexture = BRDFTextureTools.GetEnvironmentBRDFTexture(Engine.LastCreatedScene!);
     }
 
     @editableInPropertyPage("Alpha from albedo", PropertyTypeForEdition.Boolean, "TRANSPARENCY", { "notifiers": { "update": true }})
@@ -378,7 +375,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         // Rendering
         defines.setValue("RADIANCEOVERALPHA", this.useRadianceOverAlpha);
         defines.setValue("SPECULAROVERALPHA", this.useSpecularOverAlpha);
-        defines.setValue("SPECULARAA", Engine.LastCreatedScene!.getEngine().getCaps().standardDerivatives && this.enableSpecularAntiAliasing);
+        defines.setValue("SPECULARAA", this._scene.getEngine().getCaps().standardDerivatives && this.enableSpecularAntiAliasing);
 
         // Advanced
         defines.setValue("BRDF_V_HEIGHT_CORRELATED", true);
@@ -452,7 +449,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
 
         effect.setFloat2("vDebugMode", this.debugLimit, this.debugFactor);
 
-        const ambientScene = Engine.LastCreatedScene?.ambientColor;
+        const ambientScene = this._scene.ambientColor;
 
         if (ambientScene) {
             effect.setColor3("ambientFromScene", ambientScene);
@@ -543,6 +540,12 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
     protected _buildBlock(state: NodeMaterialBuildState) {
         super._buildBlock(state);
 
+        this._scene = state.sharedData.scene;
+
+        if (!this._environmentBRDFTexture) {
+            this._environmentBRDFTexture = BRDFTextureTools.GetEnvironmentBRDFTexture(this._scene);
+        }
+
         const reflectionBlock = this.reflectionParams.isConnected ? this.reflectionParams.connectedPoint?.ownerBlock as ReflectionBlock : null;
 
         if (reflectionBlock) {

+ 7 - 5
src/Materials/Node/Blocks/Fragment/PBR/reflectionBlock.ts

@@ -7,7 +7,6 @@ import { _TypeStore } from '../../../../../Misc/typeStore';
 import { NodeMaterialConnectionPointCustomObject } from "../../../nodeMaterialConnectionPointCustomObject";
 import { ReflectionTextureBaseBlock } from '../../Dual/reflectionTextureBaseBlock';
 import { AbstractMesh } from '../../../../../Meshes/abstractMesh';
-import { Engine } from '../../../../../Engines/engine';
 import { Nullable } from '../../../../../types';
 import { Texture } from '../../../../Textures/texture';
 import { BaseTexture } from '../../../../Textures/baseTexture';
@@ -24,6 +23,7 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock {
     private _defineLODBasedMicroSurface: string;
     private _vEnvironmentIrradianceName: string;
     private _vReflectionMicrosurfaceInfosName: string;
+    private _scene: Scene;
 
     public worldPositionConnectionPoint: NodeMaterialConnectionPoint;
     public worldNormalConnectionPoint: NodeMaterialConnectionPoint;
@@ -107,7 +107,7 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock {
     }
 
     public get hasTexture(): boolean {
-        return this._getTexture() !== null;
+        return !!this._getTexture();
     }
 
     protected _getTexture(): Nullable<BaseTexture> {
@@ -115,7 +115,7 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock {
             return this.texture;
         }
 
-        return Engine.LastCreatedScene?.environmentTexture ?? null;
+        return this._scene.environmentTexture;
     }
 
     public prepareDefines(mesh: AbstractMesh, nodeMaterial: NodeMaterial, defines: NodeMaterialDefines) {
@@ -132,7 +132,7 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock {
 
         defines.setValue(this._defineLODReflectionAlpha, reflectionTexture!.lodLevelInAlpha);
         defines.setValue(this._defineLinearSpecularReflection, reflectionTexture!.linearSpecularLOD);
-        defines.setValue(this._defineLODBasedMicroSurface, Engine.LastCreatedScene?.getEngine()?.getCaps().textureLOD ?? false);
+        defines.setValue(this._defineLODBasedMicroSurface, this._scene.getEngine().getCaps().textureLOD);
 
         defines.setValue("SPHERICAL_HARMONICS", this.useSphericalHarmonics);
 
@@ -140,7 +140,7 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock {
             if (reflectionTexture.isCube) {
                 defines.setValue("USESPHERICALFROMREFLECTIONMAP", true);
                 defines.setValue("USEIRRADIANCEMAP", false);
-                if (this.forceIrradianceInFragment || Engine.LastCreatedScene!.getEngine().getCaps().maxVaryingVectors <= 8) {
+                if (this.forceIrradianceInFragment || this._scene.getEngine().getCaps().maxVaryingVectors <= 8) {
                     defines.setValue("USESPHERICALINVERTEX", false);
                 }
                 else {
@@ -357,6 +357,8 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock {
     }
 
     protected _buildBlock(state: NodeMaterialBuildState) {
+        this._scene = state.sharedData.scene;
+
         if (state.target !== NodeMaterialBlockTargets.Fragment) {
             this._defineLODReflectionAlpha = state._getFreeDefineName("LODINREFLECTIONALPHA");
             this._defineLinearSpecularReflection = state._getFreeDefineName("LINEARSPECULARREFLECTION");