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:
Tags {"LightMode" = "ForwardBase"}
...
#pragma multi_compile_fwdbase
...
struct v2f
{
float4 pos : SV_POSITION;
float3 posWorld : TEXCOORD0;
#ifdef VERTEXLIGHT_ON
float3 vertexLighting : TEXCOORD1;
#endif
};
v2f vert(myappdata input)
{
v2f output;
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
output.posWorld = mul(_Object2World, input.vertex).xyz;
#ifdef VERTEXLIGHT_ON
output.vertexLighting = Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0, output.posWorld, output.normalWorld);
#endif
return output;
}
float4 frag(v2f input) : COLOR
{
//some lighting calc
float3 o.totalLight = blah blah
#ifdef VERTEXLIGHT_ON
o.totalLight += input.vertexLighting, 1;
#endif
return float4(o,1);
}
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?