World Space Light Pos node confusion

Node-based Shader Editor

World Space Light Pos node confusion

Postby mst1228 » Wed Jan 17, 2018 9:25 pm

Hey there! Thanks for the awesome plugin, it's incredibly useful!

I'm working on a shader and trying to calculate the angle between the light direction to the game object and the camera direction to the game object. I'm using the World Position node to get the coordinates of the surface, the World Space Camera Pos node to get the coordinates of the camera and the World Space Light Pos node to get the coordinates of the light. I'm calculating the direction vectors and angle between them using the values from those nodes.

World Position and World Space Camera Pos nodes seem to be giving me a 1 to 1 position vector of those objects in my scene. However, World Space Light Pos does not seem to be providing a 1 to 1 position vector of my light in my scene.

As a very simple test setup, I have a single spot light in my scene, a camera and a cube. If I put the camera at position (1, 0, 1) and use World Space Camera Pos node with the X output connected to Emission in my shader output, the cube glows white with full emission. If I connect the Y output from that node to emission, it's 0 and the cube is black. The same test works when I use World Position node with my cube at position (1, 0, 1) and connecting those outputs to emission.

When I try the same test with my light at position (1, 0, 1), the cube is always black no matter which output I connect to emission. As a side test, I connect the Type output from World Space Light Pos directly to emission (which should be 0 or 1 according to the docs, and 1 for all lights other than directional), the cube is also just black.

Am I misunderstanding the World Space Light Pos node in some way? What I really want, is the 1 to 1 position vector of the light in world space in my scene. In this possible in any other way?

Thank you very much, please let me know if I can provide more info.

Max.
mst1228
 
Posts: 2
Joined: Wed Jan 17, 2018 9:09 pm

Re: World Space Light Pos node confusion

Postby mst1228 » Wed Jan 17, 2018 9:58 pm

Hey!

I think I've just figured out that World Space Light Pos node only works to pass the actual position if I include the "LightMode" tag and set it to "ForwardAdd" in my shader. I'm getting the result that I'd hoped for now. Although I'm not exactly sure what implications it has on my shader aside from that...

I know that's another question beyond the scope of your product, but do you have any information about that?

Thanks again!

Max.
mst1228
 
Posts: 2
Joined: Wed Jan 17, 2018 9:09 pm

Re: World Space Light Pos node confusion

Postby Amplify_RnD_Rick » Thu Jan 18, 2018 6:49 pm

Hey mst1228,

No problem at all we're here to help.

You already stumbled into a possible/viable solution.

World Space Light Pos is quite a tricky node and we do apologize as it should be better explained at it's Wiki page.

This node internally uses the _WorldSpaceLightPos0 variable and it has some limitations.

It's first limitation is that it only works on the Forward rendering path.

How Unity handle lighting in Forward is rendering the object through several passes. The first pass is called Forward Base and it is used to render the object with the directional light. The influence of all other lights is rendering the object through the Forward Add pass. It is called an Add pass since it will run for each light and add its contribution to the object.

So, World Space Light Pos will only have the point and spotlight positions on that Add pass.

These passes are automatically constructed according to what you do on the surface shader, this is good for some situations but bad in others, which leads me to the second limitation.

That node does not work with the Emission port. This is because Unity see that you are calculating a value to set on the surface emission, and since emission does not depend on lighting calculations, it does not set the _WorldSpaceLightPos0 variable on the Add pass.

A way to force it into working correctly is through Custom Lighting mode, more specifically under a sub-graph which is connected to the Custom Lighting port.

So in sum World Space Light Pos only works:
- In Forward Mode
- If not connected to the Emission port on the Standard Light Model
- If connected to the Custom Lighting port on the Custom Lighting light model.

Please let us know if you have further questions. We'll gladly answer them!
Amplify_RnD_Rick
 
Posts: 40
Joined: Wed Mar 01, 2017 6:33 pm


Return to Amplify Shader Editor

Who is online

Users browsing this forum: No registered users and 0 guests