浏览代码

Split the reflection block into sub functions for easier reuse in other pbr blocks

Popov72 5 年之前
父节点
当前提交
312d595f34
共有 1 个文件被更改,包括 116 次插入42 次删除
  1. 116 42
      src/Shaders/ShadersInclude/pbrBlockReflection.fx

+ 116 - 42
src/Shaders/ShadersInclude/pbrBlockReflection.fx

@@ -17,47 +17,19 @@
     #endif
     };
 
-    void reflectionBlock(
+    void createReflectionCoords(
         const in vec3 vPositionW,
         const in vec3 normalW,
-        const in float alphaG,
-        const in vec3 vReflectionMicrosurfaceInfos,
-        const in vec2 vReflectionInfos,
     #ifdef ANISOTROPIC
         const in anisotropicOutParams anisotropicOut,
     #endif
-    #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
-        const in float NdotVUnclamped,
-    #endif
-    #ifdef LINEARSPECULARREFLECTION
-        const in float roughness,
-    #endif
     #ifdef REFLECTIONMAP_3D
-        const in samplerCube reflectionSampler,
+        out vec3 reflectionCoords
     #else
-        const in sampler2D reflectionSampler,
-    #endif
-    #if defined(NORMAL) && defined(USESPHERICALINVERTEX)
-        const in vec3 vEnvironmentIrradiance,
+        out vec2 reflectionCoords
     #endif
-    #ifdef USESPHERICALFROMREFLECTIONMAP
-        #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX)
-            const in mat4 reflectionMatrix,
-        #endif
-    #endif
-    #ifdef USEIRRADIANCEMAP
-        #ifdef REFLECTIONMAP_3D
-            const in samplerCube irradianceSampler,
-        #else
-            const in sampler2D irradianceSampler,
-        #endif
-    #endif
-        out reflectionOutParams outParams
     )
     {
-        vec4 environmentRadiance = vec4(0., 0., 0., 0.);
-        vec3 environmentIrradiance = vec3(0., 0., 0.);
-
         #ifdef ANISOTROPIC
             vec3 reflectionVector = computeReflectionCoords(vec4(vPositionW, 1.0), anisotropicOut.anisotropicNormal);
         #else
@@ -70,15 +42,37 @@
 
         // _____________________________ 2D vs 3D Maps ________________________________
         #ifdef REFLECTIONMAP_3D
-            vec3 reflectionCoords = reflectionVector;
+            reflectionCoords = reflectionVector;
         #else
-            vec2 reflectionCoords = reflectionVector.xy;
+            reflectionCoords = reflectionVector.xy;
             #ifdef REFLECTIONMAP_PROJECTION
                 reflectionCoords /= reflectionVector.z;
             #endif
             reflectionCoords.y = 1.0 - reflectionCoords.y;
         #endif
+    }
 
+    void sampleReflectionTexture(
+        const in float alphaG,
+        const in vec3 vReflectionMicrosurfaceInfos,
+        const in vec2 vReflectionInfos,
+    #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
+        const in float NdotVUnclamped,
+    #endif
+    #ifdef LINEARSPECULARREFLECTION
+        const in float roughness,
+    #endif
+    #ifdef REFLECTIONMAP_3D
+        const in samplerCube reflectionSampler,
+        const vec3 reflectionCoords,
+    #else
+        const in sampler2D reflectionSampler,
+        const vec2 reflectionCoords,
+    #endif
+        out vec4 environmentRadiance
+    )
+    {
+        // _____________________________ 2D vs 3D Maps ________________________________
         #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
             float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG, NdotVUnclamped);
         #elif defined(LINEARSPECULARREFLECTION)
@@ -108,21 +102,21 @@
                 float requestedReflectionLOD = reflectionLOD;
             #endif
 
-            environmentRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, requestedReflectionLOD);
+            environmentRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, reflectionLOD);
         #else
             float lodReflectionNormalized = saturate(reflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
             float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
 
-            vec4 environmentSpecularMid = sampleReflection(reflectionSampler, reflectionCoords);
-            if(lodReflectionNormalizedDoubled < 1.0){
+            vec4 environmentMid = sampleReflection(reflectionSampler, reflectionCoords);
+            if (lodReflectionNormalizedDoubled < 1.0){
                 environmentRadiance = mix(
                     sampleReflection(reflectionSamplerHigh, reflectionCoords),
-                    environmentSpecularMid,
+                    environmentMid,
                     lodReflectionNormalizedDoubled
                 );
-            }else{
+            } else {
                 environmentRadiance = mix(
-                    environmentSpecularMid,
+                    environmentMid,
                     sampleReflection(reflectionSamplerLow, reflectionCoords),
                     lodReflectionNormalizedDoubled - 1.0
                 );
@@ -137,7 +131,90 @@
             environmentRadiance.rgb = toLinearSpace(environmentRadiance.rgb);
         #endif
 
+        // _____________________________ Levels _____________________________________
+        environmentRadiance.rgb *= vReflectionInfos.x;
+        environmentRadiance.rgb *= vReflectionColor.rgb;
+    }
+
+    void reflectionBlock(
+        const in vec3 vPositionW,
+        const in vec3 normalW,
+        const in float alphaG,
+        const in vec3 vReflectionMicrosurfaceInfos,
+        const in vec2 vReflectionInfos,
+    #ifdef ANISOTROPIC
+        const in anisotropicOutParams anisotropicOut,
+    #endif
+    #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
+        const in float NdotVUnclamped,
+    #endif
+    #ifdef LINEARSPECULARREFLECTION
+        const in float roughness,
+    #endif
+    #ifdef REFLECTIONMAP_3D
+        const in samplerCube reflectionSampler,
+    #else
+        const in sampler2D reflectionSampler,
+    #endif
+    #if defined(NORMAL) && defined(USESPHERICALINVERTEX)
+        const in vec3 vEnvironmentIrradiance,
+    #endif
+    #ifdef USESPHERICALFROMREFLECTIONMAP
+        #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX)
+            const in mat4 reflectionMatrix,
+        #endif
+    #endif
+    #ifdef USEIRRADIANCEMAP
+        #ifdef REFLECTIONMAP_3D
+            const in samplerCube irradianceSampler,
+        #else
+            const in sampler2D irradianceSampler,
+        #endif
+    #endif
+        out reflectionOutParams outParams
+    )
+    {
+        // _____________________________ Radiance ________________________________
+        vec4 environmentRadiance = vec4(0., 0., 0., 0.);
+
+        #ifdef REFLECTIONMAP_3D
+            vec3 reflectionCoords = vec3(0.);
+        #else
+            vec2 reflectionCoords = vec2(0.);
+        #endif
+
+        createReflectionCoords(
+            vPositionW,
+            normalW,
+        #ifdef ANISOTROPIC
+            anisotropicOut,
+        #endif
+            reflectionCoords
+        );
+
+        sampleReflectionTexture(
+            alphaG,
+            vReflectionMicrosurfaceInfos,
+            vReflectionInfos,
+        #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
+            NdotVUnclamped,
+        #endif
+        #ifdef LINEARSPECULARREFLECTION
+            roughness,
+        #endif
+        #ifdef REFLECTIONMAP_3D
+            reflectionSampler,
+            reflectionCoords,
+        #else
+            reflectionSampler,
+            reflectionCoords,
+        #endif
+            environmentRadiance
+        );
+
         // _____________________________ Irradiance ________________________________
+        vec3 environmentIrradiance = vec3(0., 0., 0.);
+
         #ifdef USESPHERICALFROMREFLECTIONMAP
             #if defined(NORMAL) && defined(USESPHERICALINVERTEX)
                 environmentIrradiance = vEnvironmentIrradiance;
@@ -170,9 +247,6 @@
             #endif
         #endif
 
-        // _____________________________ Levels _____________________________________
-        environmentRadiance.rgb *= vReflectionInfos.x;
-        environmentRadiance.rgb *= vReflectionColor.rgb;
         environmentIrradiance *= vReflectionColor.rgb;
 
         outParams.environmentRadiance = environmentRadiance;