فهرست منبع

Make sure the world normal used is normalized

Popov72 5 سال پیش
والد
کامیت
ff7299753e

+ 5 - 1
src/Materials/Node/Blocks/Dual/reflectionTextureBlock.ts

@@ -142,7 +142,11 @@ export class ReflectionTextureBlock extends ReflectionTextureBaseBlock {
 
         this.handleFragmentSideInits(state);
 
-        state.compilationString += this.handleFragmentSideCodeReflectionCoords(this.worldNormal.associatedVariableName);
+        const normalWUnit = state._getFreeVariableName("normalWUnit");
+
+        state.compilationString += `vec4 ${normalWUnit} = normalize(${this.worldNormal.associatedVariableName});\r\n`;
+
+        state.compilationString += this.handleFragmentSideCodeReflectionCoords(normalWUnit);
 
         state.compilationString += this.handleFragmentSideCodeReflectionColor();
 

+ 7 - 2
src/Materials/Node/Blocks/PBR/pbrMetallicRoughnessBlock.ts

@@ -55,6 +55,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
     private _scene: Scene;
     private _environmentBRDFTexture: Nullable<BaseTexture> = null;
     private _environmentBrdfSamplerName: string;
+    private _vNormalWName: string;
 
     /**
      * Create a new ReflectionBlock
@@ -831,11 +832,15 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         state.compilationString += `vec4 vLightingIntensity = vec4(1.);\r\n`;
 
         // _____________________________ Geometry Information ____________________________
+        this._vNormalWName = state._getFreeVariableName("vNormalW");
+
+        state.compilationString += `vec4 ${this._vNormalWName} = normalize(${this.worldNormal.associatedVariableName});\r\n`;
+
         if (state._registerTempVariable("viewDirectionW")) {
             state.compilationString += `vec3 viewDirectionW = normalize(${this.cameraPosition.associatedVariableName} - ${"v_" + worldPos.associatedVariableName}.xyz);\r\n`;
         }
 
-        state.compilationString += `vec3 geometricNormalW = normalize(${this.worldNormal.associatedVariableName}.xyz);\r\n`;
+        state.compilationString += `vec3 geometricNormalW = ${this._vNormalWName}.xyz;\r\n`;
 
         state.compilationString += `vec3 normalW = ${normalShading.isConnected ? "normalize(" + normalShading.associatedVariableName + ".xyz)" : "geometricNormalW"};\r\n`;
 
@@ -982,7 +987,7 @@ export class PBRMetallicRoughnessBlock extends NodeMaterialBlock {
         // _____________________________ Generate debug code ________________________
         state.compilationString += state._emitCodeFromInclude("pbrDebug", comments, {
             replaceStrings: [
-                { search: /vNormalW/g, replace: this.worldNormal.associatedVariableName },
+                { search: /vNormalW/g, replace: this._vNormalWName },
                 { search: /vPositionW/g, replace: "v_" + this.worldPosition.associatedVariableName },
                 { search: /albedoTexture\.rgb;/g, replace: this.baseTexture.associatedVariableName + ".rgb;\r\ngl_FragColor.rgb = toGammaSpace(gl_FragColor.rgb);\r\n" },
                 { search: /opacityMap/g, replace: this.opacityTexture.associatedVariableName },

+ 1 - 1
src/Materials/Node/Blocks/PBR/reflectionBlock.ts

@@ -281,7 +281,7 @@ export class ReflectionBlock extends ReflectionTextureBaseBlock {
 
         code +=
             `#if defined(USESPHERICALFROMREFLECTIONMAP) && defined(USESPHERICALINVERTEX)
-                vec3 ${reflectionVectorName} = vec3(${this._reflectionMatrixName} * vec4(${this.worldNormal.associatedVariableName}.xyz, 0)).xyz;
+                vec3 ${reflectionVectorName} = vec3(${this._reflectionMatrixName} * vec4(normalize(${this.worldNormal.associatedVariableName}).xyz, 0)).xyz;
                 #ifdef ${this._defineOppositeZ}
                     ${reflectionVectorName}.z *= -1.0;
                 #endif