Selaa lähdekoodia

Cleaned up diffuse lighting shader code.

Lasse Öörni 13 vuotta sitten
vanhempi
sitoutus
196c2158b8

+ 2 - 3
Bin/CoreData/Shaders/GLSL/DeferredLight.frag

@@ -54,11 +54,10 @@ void main()
     float diff;
     float diff;
 
 
     #ifdef DIRLIGHT
     #ifdef DIRLIGHT
-        lightDir = cLightDirPS;
-        diff = GetDiffuseDir(normal, lightDir);
+        diff = GetDiffuse(normal, cLightDirPS, lightDir);
     #else
     #else
         vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
         vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
-        diff = GetDiffusePointOrSpot(normal, lightVec, lightDir);
+        diff = GetDiffuse(normal, lightVec, lightDir);
     #endif
     #endif
 
 
     #ifdef SHADOW
     #ifdef SHADOW

+ 21 - 20
Bin/CoreData/Shaders/GLSL/Lighting.frag

@@ -1,29 +1,30 @@
-float GetDiffuseDir(vec3 normal, vec3 lightDir)
+float GetDiffuse(vec3 normal, vec3 lightVec, out vec3 lightDir)
 {
 {
-    return max(dot(normal, lightDir), 0.0);
-}
-
-float GetDiffuseDirVolumetric()
-{
-    return 1.0;
-}
+    #ifdef DIRLIGHT
+        #ifdef NORMALMAP
+            // In normal mapped forward lighting, the tangent space light vector needs renormalization
+            lightDir = normalize(lightVec);
+        #else
+            lightDir = lightVec;
+        #endif
 
 
-#if defined(POINTLIGHT) || defined(SPOTLIGHT)
-float GetDiffusePointOrSpot(vec3 normal, vec3 lightVec, out vec3 lightDir)
-{
-    float lightDist = length(lightVec);
-    lightDir = lightVec / lightDist;
-    return max(dot(normal, lightDir), 0.0) * texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
+        return max(dot(normal, lightDir), 0.0);
+    #else
+        float lightDist = length(lightVec);
+        lightDir = lightVec / lightDist;
+        return max(dot(normal, lightDir), 0.0) * texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
+    #endif
 }
 }
-#endif
 
 
-#if defined(POINTLIGHT) || defined(SPOTLIGHT)
-float GetDiffusePointOrSpotVolumetric(vec3 lightVec)
+float GetDiffuseVolumetric(vec3 lightVec)
 {
 {
-    float lightDist = length(lightVec);
-    return texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
+    #ifdef DIRLIGHT
+        return 1.0;
+    #else
+        float lightDist = length(lightVec);
+        return texture2D(sLightRampMap, vec2(lightDist, 0.0)).r;
+    #endif
 }
 }
-#endif
 
 
 float GetSpecular(vec3 normal, vec3 eyeVec, vec3 lightDir, float specularPower)
 float GetSpecular(vec3 normal, vec3 eyeVec, vec3 lightDir, float specularPower)
 {
 {

+ 2 - 6
Bin/CoreData/Shaders/GLSL/LitParticle.frag

@@ -43,12 +43,8 @@ void main()
         vec3 finalColor;
         vec3 finalColor;
         float diff;
         float diff;
     
     
-        #ifdef DIRLIGHT
-            diff = GetDiffuseDirVolumetric();
-        #else
-            diff = GetDiffusePointOrSpotVolumetric(vLightVec.xyz);
-        #endif
-    
+        diff = GetDiffuseVolumetric(vLightVec.xyz);
+
         #if defined(SPOTLIGHT)
         #if defined(SPOTLIGHT)
             lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
             lightColor = vSpotPos.w > 0.0 ? texture2DProj(sLightSpotMap, vSpotPos).rgb * cLightColor.rgb : vec3(0.0, 0.0, 0.0);
         #elif defined(CUBEMASK)
         #elif defined(CUBEMASK)

+ 1 - 10
Bin/CoreData/Shaders/GLSL/LitSolid.frag

@@ -66,16 +66,7 @@ void main()
             vec3 normal = normalize(vNormal);
             vec3 normal = normalize(vNormal);
         #endif
         #endif
     
     
-        #ifdef DIRLIGHT
-            #ifdef NORMALMAP
-                lightDir = normalize(vLightVec.xyz);
-            #else
-                lightDir = vLightVec.xyz;
-            #endif
-            diff = GetDiffuseDir(normal, lightDir);
-        #else
-            diff = GetDiffusePointOrSpot(normal, vLightVec.xyz, lightDir);
-        #endif
+        diff = GetDiffuse(normal, vLightVec.xyz, lightDir);
     
     
         #ifdef SHADOW
         #ifdef SHADOW
             diff *= GetShadow(vShadowPos, vLightVec.w);
             diff *= GetShadow(vShadowPos, vLightVec.w);

+ 2 - 3
Bin/CoreData/Shaders/GLSL/PrepassLight.frag

@@ -53,11 +53,10 @@ void main()
 
 
     // Accumulate light at half intensity to allow 2x "overburn"
     // Accumulate light at half intensity to allow 2x "overburn"
     #ifdef DIRLIGHT
     #ifdef DIRLIGHT
-        lightDir = cLightDirPS;
-        diff = 0.5 * GetDiffuseDir(normal, lightDir);
+        diff = 0.5 * GetDiffuse(normal, cLightDirPS, lightDir);
     #else
     #else
         vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
         vec3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
-        diff = 0.5 * GetDiffusePointOrSpot(normal, lightVec, lightDir);
+        diff = 0.5 * GetDiffuse(normal, lightVec, lightDir);
     #endif
     #endif
 
 
     #ifdef SHADOW
     #ifdef SHADOW

+ 1 - 6
Bin/CoreData/Shaders/GLSL/TerrainBlend.frag

@@ -54,12 +54,7 @@ void main()
 
 
         vec3 normal = normalize(vNormal);
         vec3 normal = normalize(vNormal);
 
 
-        #ifdef DIRLIGHT
-            lightDir = vLightVec.xyz;
-            diff = GetDiffuseDir(normal, lightDir);
-        #else
-            diff = GetDiffusePointOrSpot(normal, vLightVec.xyz, lightDir);
-        #endif
+        diff = GetDiffuse(normal, vLightVec.xyz, lightDir);
 
 
         #ifdef SHADOW
         #ifdef SHADOW
             diff *= GetShadow(vShadowPos, vLightVec.w);
             diff *= GetShadow(vShadowPos, vLightVec.w);

+ 2 - 3
Bin/CoreData/Shaders/HLSL/DeferredLight.hlsl

@@ -91,11 +91,10 @@ void PS(
     float diff;
     float diff;
 
 
     #ifdef DIRLIGHT
     #ifdef DIRLIGHT
-        lightDir = cLightDirPS;
-        diff = GetDiffuseDir(normal, lightDir);
+        diff = GetDiffuse(normal, cLightDirPS, lightDir);
     #else
     #else
         float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
         float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
-        diff = GetDiffusePointOrSpot(normal, lightVec, lightDir);
+        diff = GetDiffuse(normal, lightVec, lightDir);
     #endif
     #endif
 
 
     #ifdef SHADOW
     #ifdef SHADOW

+ 21 - 16
Bin/CoreData/Shaders/HLSL/Lighting.hlsl

@@ -1,26 +1,31 @@
 #pragma warning(disable:3571)
 #pragma warning(disable:3571)
 
 
-float GetDiffuseDir(float3 normal, float3 lightDir)
+float GetDiffuse(float3 normal, float3 lightVec, out float3 lightDir)
 {
 {
-    return saturate(dot(normal, lightDir));
-}
-
-float GetDiffusePointOrSpot(float3 normal, float3 lightVec, out float3 lightDir)
-{
-    float lightDist = length(lightVec);
-    lightDir = lightVec / lightDist;
-    return saturate(dot(normal, lightDir)) * tex1D(sLightRampMap, lightDist).r;
-}
+    #ifdef DIRLIGHT
+        #ifdef NORMALMAP
+            // In normal mapped forward lighting, the tangent space light vector needs renormalization
+            lightDir = normalize(lightVec);
+        #else
+            lightDir = lightVec;
+        #endif
 
 
-float GetDiffuseDirVolumetric()
-{
-    return 1.0;
+        return saturate(dot(normal, lightDir));
+    #else
+        float lightDist = length(lightVec);
+        lightDir = lightVec / lightDist;
+        return saturate(dot(normal, lightDir)) * tex1D(sLightRampMap, lightDist).r;
+    #endif
 }
 }
 
 
-float GetDiffusePointOrSpotVolumetric(float3 lightVec)
+float GetDiffuseVolumetric(float3 lightVec)
 {
 {
-    float lightDist = length(lightVec);
-    return tex1D(sLightRampMap, lightDist).r;
+    #ifdef DIRLIGHT
+        return 1.0;
+    #else
+        float lightDist = length(lightVec);
+        return tex1D(sLightRampMap, lightDist).r;
+    #endif
 }
 }
 
 
 float GetSpecular(float3 normal, float3 eyeVec, float3 lightDir, float specularPower)
 float GetSpecular(float3 normal, float3 eyeVec, float3 lightDir, float specularPower)

+ 2 - 6
Bin/CoreData/Shaders/HLSL/LitParticle.hlsl

@@ -114,12 +114,8 @@ void PS(float2 iTexCoord : TEXCOORD0,
         float3 finalColor;
         float3 finalColor;
         float diff;
         float diff;
     
     
-        #ifdef DIRLIGHT
-            diff = GetDiffuseDirVolumetric();
-        #else
-            diff = GetDiffusePointOrSpotVolumetric(iLightVec.xyz);
-        #endif
-    
+        diff = GetDiffuseVolumetric(iLightVec.xyz);
+
         #if defined(SPOTLIGHT)
         #if defined(SPOTLIGHT)
             lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
             lightColor = iSpotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, iSpotPos).rgb * cLightColor.rgb : 0.0;
         #elif defined(CUBEMASK)
         #elif defined(CUBEMASK)

+ 2 - 11
Bin/CoreData/Shaders/HLSL/LitSolid.hlsl

@@ -188,17 +188,8 @@ void PS(float2 iTexCoord : TEXCOORD0,
             float3 normal = normalize(iNormal);
             float3 normal = normalize(iNormal);
         #endif
         #endif
 
 
-        #ifdef DIRLIGHT
-            #ifdef NORMALMAP
-                lightDir = normalize(iLightVec.xyz);
-            #else
-                lightDir = iLightVec.xyz;
-            #endif
-            diff = GetDiffuseDir(normal, lightDir);
-        #else
-            diff = GetDiffusePointOrSpot(normal, iLightVec.xyz, lightDir);
-        #endif
-    
+        diff = GetDiffuse(normal, iLightVec.xyz, lightDir);
+
         #ifdef SHADOW
         #ifdef SHADOW
             diff *= GetShadow(iShadowPos, iLightVec.w);
             diff *= GetShadow(iShadowPos, iLightVec.w);
         #endif
         #endif

+ 2 - 3
Bin/CoreData/Shaders/HLSL/PrepassLight.hlsl

@@ -83,11 +83,10 @@ void PS(
 
 
     // Accumulate light at half intensity to allow 2x "overburn"
     // Accumulate light at half intensity to allow 2x "overburn"
     #ifdef DIRLIGHT
     #ifdef DIRLIGHT
-        lightDir = cLightDirPS;
-        diff = 0.5 * GetDiffuseDir(normal, lightDir);
+        diff = 0.5 * GetDiffuse(normal, cLightDirPS, lightDir);
     #else
     #else
         float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
         float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
-        diff = 0.5 * GetDiffusePointOrSpot(normal, lightVec, lightDir);
+        diff = 0.5 * GetDiffuse(normal, lightVec, lightDir);
     #endif
     #endif
 
 
     #ifdef SHADOW
     #ifdef SHADOW

+ 2 - 7
Bin/CoreData/Shaders/HLSL/TerrainBlend.hlsl

@@ -148,13 +148,8 @@ void PS(float2 iTexCoord : TEXCOORD0,
         float diff;
         float diff;
 
 
         float3 normal = normalize(iNormal);
         float3 normal = normalize(iNormal);
-
-        #ifdef DIRLIGHT
-            lightDir = iLightVec.xyz;
-            diff = GetDiffuseDir(normal, lightDir);
-        #else
-            diff = GetDiffusePointOrSpot(normal, iLightVec.xyz, lightDir);
-        #endif
+        
+        diff = GetDiffuse(normal, iLightVec.xyz, lightDir);
     
     
         #ifdef SHADOW
         #ifdef SHADOW
             diff *= GetShadow(iShadowPos, iLightVec.w);
             diff *= GetShadow(iShadowPos, iLightVec.w);