Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unexpected problem in finalcolor modifier

Discussion in 'Shaders' started by acs123123, Nov 7, 2014.

  1. acs123123

    acs123123

    Joined:
    Jun 28, 2013
    Posts:
    4
    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
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    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.
     
    acs123123 likes this.
  3. acs123123

    acs123123

    Joined:
    Jun 28, 2013
    Posts:
    4
    Thanks! This helped lead me to an article about how forward rendering works which has helped me get past the issue.