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

VERTEXLIGHT_ON always undefined in Fragment Shader

Discussion in 'Shaders' started by GGeff, Dec 9, 2014.

  1. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    In creating a custom vertex and fragment shader, I've noticed that while VERTEXLIGHT_ON works great for optimizing the vertex shading component of your shader, it is always undefined in the fragment shading component. This makes it impossible to take full advantage of the optimizations that can be gleaned by only processing the 4 point light data when there is at least 1 light to be processed, i.e. something like this:

    Code (CSharp):
    1. Tags {"LightMode" = "ForwardBase"}
    2.  
    3. ...
    4.  
    5. #pragma multi_compile_fwdbase
    6.  
    7. ...
    8.  
    9. struct v2f
    10. {
    11.     float4 pos : SV_POSITION;
    12.     float3 posWorld : TEXCOORD0;
    13. #ifdef VERTEXLIGHT_ON
    14.         float3 vertexLighting : TEXCOORD1;
    15. #endif
    16. };
    17. v2f vert(myappdata input)
    18. {
    19.     v2f output;
    20.     output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    21.     output.posWorld = mul(_Object2World, input.vertex).xyz;
    22.  
    23. #ifdef VERTEXLIGHT_ON
    24.     output.vertexLighting = Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
    25.                     unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
    26.                     unity_4LightAtten0, output.posWorld, output.normalWorld);
    27. #endif
    28.  
    29.     return output;
    30. }
    31. float4 frag(v2f input) : COLOR
    32. {  
    33.     //some lighting calc
    34.     float3 o.totalLight = blah blah
    35.  
    36. #ifdef VERTEXLIGHT_ON
    37.         o.totalLight += input.vertexLighting, 1;
    38. #endif
    39.  
    40.     return float4(o,1);
    41. }
    This is a greatly simplified example, of course. The fragment shader could do calculations more complicated with the passed vertexLighting, but even so, minimizing the amount of data in the v2f is important, no?
     
  2. GGeff

    GGeff

    Joined:
    Nov 11, 2014
    Posts:
    40
    This problem only seems to occur when

    #pragma multi_compile_fwdbase

    is used.

    When I use

    #pragma multi_compile NOTHINGSPECIAL VERTEXLIGHT_ON

    I get the expected results of the fragment shader being properly affected by VERTEXLIGHT_ON
    I am a bit too green to try and draw any conclusions from this, but this workaround seems to be a decent enough solution for me for the time being.