I'm trying to write a fairly basic shader but I keep running into lighting issues with Unity. My first problem was trying to figure out which variable stored the light's position in world space. (I'm only working with one light, at least at the moment).
I finally found something that responds to movement of my light source: unity_LightPosition[0]
. However the shader still didn't do what I wanted it to do, so I did some debugging:
I have a basic vertex and fragment shader and what I'm currently doing in the fragment shader is:
float4 c = float4(0, 0, 0, 1);
float3 lp = normalize(unity_LightPosition[0]);
c.rgb = lp;
return c;
This should just set the color of my object to the normalized position of the light source, correct? What I see in my scene is my object changes colors when I move my camera around it (I'm not moving the light source at all, it is a separate entity). The color also seems to increase in strength as I zoom in.
To me, it seems as if unity_LightPosition[0] is related to my camera's position, as well as orientation and zoom level, yet the documentation I've found says it should represent the position of the light in world space.
I tried switching to using the following:
float3 lp = float4(
unity_4LightPosX0[0],
unity_4LightPosY0[0],
unity_4LightPosZ0[0],
1.0
);
...but that only colors my object red regardless of the light's actual position.
Additionally, I've set the LightMode
tag to Vertex
.
It seems like simply getting the world position of the light source shouldn't be this difficult. What am I not doing correctly?
EDIT:
I've been playing around with the camera and light position and I think that unity_LightPosition[0]
actually puts out position in the projection space. If I place the light source in the lower-left corner of the screen, the object is black. Upper-left the object is green (0, 1), upper-right is orange (1, 1), and lower-right is red (1, 0). When I place it in the lower-left (0, 0) and move the camera in front of it, it becomes blue. This is consistent with the depth value behavior. (0, 0, >0) would appear as blue.
Now, how to get it from projection back to world?...or should I use a different variable?