Kaynağa Gözat

Lighting shader code cleanup.
Automatically set black fog color for additive blending, so that shaders do not necessarily have to take it into account themselves.

Lasse Öörni 14 yıl önce
ebeveyn
işleme
bef427937c

+ 5 - 2
Engine/Graphics/Batch.cpp

@@ -160,8 +160,11 @@ void Batch::Prepare(Graphics* graphics, Renderer* renderer, bool setModelTransfo
         if (graphics->NeedParameterUpdate(PSP_AMBIENTCOLOR, zone_))
             graphics->SetShaderParameter(PSP_AMBIENTCOLOR, zone_->GetAmbientColor().ToVector4());
         
-        if (graphics->NeedParameterUpdate(PSP_FOGCOLOR, zone_))
-            graphics->SetShaderParameter(PSP_FOGCOLOR, zone_->GetFogColor().ToVector4());
+        // If the pass is additive, override fog color to black so that shaders do not need a separate additive path
+        BlendMode blend = pass_->GetBlendMode();
+        Zone* fogColorZone = (blend == BLEND_ADD || blend == BLEND_ADDALPHA) ? renderer->GetDefaultZone() : zone_;
+        if (graphics->NeedParameterUpdate(PSP_FOGCOLOR, fogColorZone))
+            graphics->SetShaderParameter(PSP_FOGCOLOR, fogColorZone->GetFogColor().ToVector4());
         
         if (graphics->NeedParameterUpdate(PSP_FOGPARAMS, zone_))
         {

+ 16 - 25
SourceAssets/GLSLShaders/Forward.frag

@@ -42,6 +42,8 @@ void main()
         diffColor *= vColor;
     #endif
 
+    vec3 finalColor = 0.0;
+    
     #if !defined(VOLUMETRIC) && defined(LIGHT)
 
         vec3 lightColor;
@@ -91,16 +93,9 @@ void main()
                 float specStrength = cMatSpecProperties.x;
             #endif
             float spec = GetSpecular(normal, vEyeVec, lightDir, cMatSpecProperties.y);
-            vec3 finalColor = diff * lightColor * (diffColor.rgb + spec * specStrength * cLightColor.a);
+            finalColor = diff * lightColor * (diffColor.rgb + spec * specStrength * cLightColor.a);
         #else
-            vec3 finalColor = diff * lightColor * diffColor.rgb;
-        #endif
-
-        #ifdef AMBIENT
-            finalColor += cAmbientColor * diffColor.rgb;
-            gl_FragColor = vec4(GetFog(finalColor, vLightVec.w), diffColor.a);
-        #else
-            gl_FragColor = vec4(GetLitFog(finalColor, vLightVec.w), diffColor.a);
+            finalColor = diff * lightColor * diffColor.rgb;
         #endif
 
     #elif defined(VOLUMETRIC) && defined(LIGHT)
@@ -123,25 +118,21 @@ void main()
             lightColor = cLightColor.rgb;
         #endif
 
-        vec3 finalColor = diff * lightColor * diffColor.rgb;
+        finalColor = diff * lightColor * diffColor.rgb;
 
-        #ifdef AMBIENT
-            finalColor += cAmbientColor * diffColor.rgb;
-            gl_FragColor = vec4(GetFog(finalColor, vLightVec.w), diffColor.a);
-        #else
-            gl_FragColor = vec4(GetLitFog(finalColor, vLightVec.w), diffColor.a);
-        #endif
+    #elif defined(UNLIT)
 
-    #else
+        finalColor = diffColor.rgb;
 
-        #if defined(UNLIT)
-            gl_FragColor = vec4(GetFog(diffColor.rgb, vLightVec.w), diffColor.a);
-        #elif defined(ADDITIVE)
-            gl_FragColor = vec4(GetLitFog(diffColor.rgb, vLightVec.w), diffColor.a);
-        #elif defined(AMBIENT)
-            vec3 finalColor = cAmbientColor * diffColor.rgb;
-            gl_FragColor = vec4(GetFog(finalColor, vLightVec.w), diffColor.a);
-        #endif
+    #endif
+    
+    #ifdef AMBIENT
+        finalColor += cAmbientColor * diffColor.rgb;
+    #endif
 
+    #if defined(LIGHT) && !defined(AMBIENT)
+        gl_FragColor = vec4(GetLitFog(finalColor, iLightVec.w), diffColor.a);
+    #else
+        gl_FragColor = vec4(GetFog(finalColor, iLightVec.w), diffColor.a);
     #endif
 }

+ 0 - 2
SourceAssets/GLSLShaders/Forward.xml

@@ -32,7 +32,6 @@
         <option name="SpecMap" define="SPECMAP" require="LIGHT" />
         <option name="VCol" define="VERTEXCOLOR" />
         <option name="Volume" define="VOLUMETRIC">
-            <exclude name="Additive" />
             <exclude name="Normal" />
             <exclude name="Shadow" />
             <exclude name="Spec" />
@@ -40,7 +39,6 @@
             <exclude name="Unlit" />
         </option>
         <variation name="Unlit" define="UNLIT" />
-        <variation name="Additive" define="ADDITIVE" />
         <variation name="Ambient" define="AMBIENT" />
         <variation name="Dir">
             <define name="DIRLIGHT" />

+ 17 - 26
SourceAssets/HLSLShaders/Forward.hlsl

@@ -89,7 +89,7 @@ void VS(float4 iPos : POSITION,
         #ifdef POINTLIGHT
             oCubeMaskVec = mul(oLightVec.xyz, cLightVecRot);
         #endif
-    
+
         #ifdef NORMALMAP
             oTangent = GetWorldTangent(modelMatrix);
             oBitangent = cross(oTangent, oNormal) * iTangent.w;
@@ -153,6 +153,8 @@ void PS(float2 iTexCoord : TEXCOORD0,
         diffColor *= iColor;
     #endif
 
+    float3 finalColor = 0.0;
+
     #if !defined(VOLUMETRIC) && defined(LIGHT)
 
         float3 lightColor;
@@ -202,16 +204,9 @@ void PS(float2 iTexCoord : TEXCOORD0,
                 float specStrength = cMatSpecProperties.x;
             #endif
             float spec = GetSpecular(normal, iEyeVec, lightDir, cMatSpecProperties.y);
-            float3 finalColor = diff * lightColor * (diffColor.rgb + spec * specStrength * cLightColor.a);
+            finalColor = diff * lightColor * (diffColor.rgb + spec * specStrength * cLightColor.a);
         #else
-            float3 finalColor = diff * lightColor * diffColor.rgb;
-        #endif
-
-        #ifdef AMBIENT
-            finalColor += cAmbientColor * diffColor.rgb;
-            oColor = float4(GetFog(finalColor, iLightVec.w), diffColor.a);
-        #else
-            oColor = float4(GetLitFog(finalColor, iLightVec.w), diffColor.a);
+            finalColor = diff * lightColor * diffColor.rgb;
         #endif
 
     #elif defined(VOLUMETRIC) && defined(LIGHT)
@@ -233,25 +228,21 @@ void PS(float2 iTexCoord : TEXCOORD0,
             lightColor = cLightColor.rgb;
         #endif
 
-        float3 finalColor = diff * lightColor * diffColor.rgb;
+        finalColor = diff * lightColor * diffColor.rgb;
 
-        #ifdef AMBIENT
-            finalColor += cAmbientColor * diffColor.rgb;
-            oColor = float4(GetFog(finalColor, iLightVec.w), diffColor.a);
-        #else
-            oColor = float4(GetLitFog(finalColor, iLightVec.w), diffColor.a);
-        #endif
+    #elif defined(UNLIT)
 
-    #else
+        finalColor = diffColor.rgb;
 
-        #if defined(UNLIT)
-            oColor = float4(GetFog(diffColor.rgb, iLightVec.w), diffColor.a);
-        #elif defined(ADDITIVE)
-            oColor = float4(GetLitFog(diffColor.rgb, iLightVec.w), diffColor.a);
-        #elif defined(AMBIENT)
-            float3 finalColor = cAmbientColor * diffColor.rgb;
-            oColor = float4(GetFog(finalColor, iLightVec.w), diffColor.a);
-        #endif
+    #endif
 
+    #ifdef AMBIENT
+        finalColor += cAmbientColor * diffColor.rgb;
+    #endif
+
+    #if defined(LIGHT) && !defined(AMBIENT)
+        oColor = float4(GetLitFog(finalColor, iLightVec.w), diffColor.a);
+    #else
+        oColor = float4(GetFog(finalColor, iLightVec.w), diffColor.a);
     #endif
 }

+ 0 - 2
SourceAssets/HLSLShaders/Forward.xml

@@ -33,7 +33,6 @@
         <option name="SpecMap" define="SPECMAP" require="LIGHT" />
         <option name="VCol" define="VERTEXCOLOR" />
         <option name="Volume" define="VOLUMETRIC">
-            <exclude name="Additive" />
             <exclude name="Normal" />
             <exclude name="Shadow" />
             <exclude name="Spec" />
@@ -41,7 +40,6 @@
             <exclude name="Unlit" />
         </option>
         <variation name="Unlit" define="UNLIT" />
-        <variation name="Additive" define="ADDITIVE" />
         <variation name="Ambient" define="AMBIENT" />
         <variation name="Dir">
             <define name="DIRLIGHT" />