- Home /
What kind of values come out of _WorldSpaceLightPos0?
Hi,
I am writting a shader were I need to format the X rotation value of my directional light (_WorldSpaceLightPos0.x) in a certain way.
The table bellow shows how I need the value to behave at certain points (ie.: above 90degree i need negative value until 180 degree):
Input X Rotation Required float value
0 degrees 1
45 degrees 0.2929
90 degrees 0
135 degrees -0.2929
180 degrees -1
Here is my current code:
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float LightDirAdjust = 1;
float v; // the float value I need
if (_WorldSpaceLightPos0.x > 90 && _WorldSpaceLightPos0.x < 180)
LightDirAdjust = -1;
v = ((lightDirection.y * -1) + 1) * LightDirAdjust;
In the last line I multiply by -1 to reverse the value then add 1 so when X Rotation=0 my float = 1. It also works fine for X45 = float 0.2929 and X90=float 0.
Whe I go above 90 degrees for some reason the values stays positive, I tried a lot of things to make the value work as I expect and i just cant get it.
I think I am lost somewere in bettwen quaternions, euler angles and simply not understanding what comes out of _WorldSpaceLightPos0.xyz in relation to the actual rotation values in the inspecter. I also smell a potential issue with worldspace vs localspace values... im a bit lost.
Would someone please turn on a search light for me?
Thank you!
Answer by tcz8 · Feb 13, 2017 at 04:18 AM
Here is my own answer:
Since _WorldSpaceLightPos0 is a value being fed by Unity to the shader, you can inspect it's values by using the frame debugger.
Here is how:
Go to Window > Frame Debugger
Click ENABLE on the top left
Expand the Camera.Render all the way down to the "Draw Dynamic" entries
Select them one by one until you identify (by looking at the game view) the draw pass were you are using _WorldSpaceLightPos0
Once you found the right entry, click on "shader properties" in the right pane
That's it, _WorldSpaceLightPos0 and its values will be listed right there under "Vectors". You can dynamicly move and rotate your directional light and see the X,Y,Z values change in realtime.
In my case I realized I was using X where I should have been using the Z value... With that figured out, it was easy to fix my code. Damn you worldspace!
Please note that on a few occations, the values being shown in the frame debugger either stopped refreshing or showed values that stopped making sense. Closing and reopening (also maybe disabling/re enabling) the debugger fixes the problem.
Good luck and thanks for stopping by!