Unexpected problem in finalcolor modifier

In the shader below, I am trying to render a default color when no lighting is applied to the pixel. Notice that I’m simply setting the color in the surface shader to green. However the result has the green color blended with the default color (red). _BaseColor = red.

If I comment out the line color = _BaseColor then the result is the green color expected.

Any help would be great, thanks!

SubShader {

Tags { “RenderType”=“Opaque” }
LOD 200

Cull Back
CGPROGRAM
#pragma surface surf Lambert finalcolor:finalcolor

sampler2D _MainTex;
float4 _BaseColor;

struct Input {
float2 uv_MainTex;
};

void finalcolor (Input IN, SurfaceOutput o, inout fixed4 color)
{
if (color.g == 0)// && color.g == 0.0 && color.b == 0.0)
{
color = _BaseColor;
}
}

void surf(Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
//o.Albedo = c.rgb;
//o.Alpha = c.a;

o.Albedo.r = 0;
o.Albedo.g = 1;
o.Albedo.b = 0;
o.Alpha = 1;
}
ENDCG

This is probably happening for each pass… how many lights do you have on the model?

If you’ve got a single directional light, it should work as you intend. But each light after that (or the first light, if you have no directional light) will colour any unlit pixels for that pass and it’ll add it to the result of the passes before it.

1 Like

Thanks! This helped lead me to an article about how forward rendering works which has helped me get past the issue.