- Home /
_WorldSpaceLightPos0.xyz always 0
The following line works perfectly when using Directional Light, yet when I switch to Pointlight, the value returned is always 0.0 .
float4 frag(vertexOutput i) : COLOR
{
lightDirection = normalize(_WorldSpaceLightPos0.xyz);
return float4(lightDirection, 1.0);
}
Actually, I'd like to use _WorldSpaceLightPos0 to get the vector between it and the world coords (which are being calculated in the vertex function), which does not work, since _WorldSpaceLightPos0 is always zero when using Point Light. Using Tags{ "RenderType" = "ForwardBase" } . What am I missing?
I just stumbled upon the builtin helper functions of UnityCG.cginc, sadly with the same results.
vertexOutput vert(vertexInput v)
{
vertexOutput o;
// more stuff here....
o.posWorld = mul(_Object2World, v.vertex);
return o;
}
float4 frag(vertexOutput i) : COLOR
{
// more stuff here... not important to question
return float4(WorldSpaceLightDir(i.posWorld), 1.0);
}
Spits out colors, as expected, when using Directional Light, yet it remains black using Point Light.
Furthermore, I now came accross unity_LightPosition[0].xyz which simply returns (0, 0, 1), regardless of the lights position.
Answer by Eno-Khaon · Jan 03, 2016 at 06:14 PM
The "ForwardBase" pass is used to apply all of a scene's generic, presumably single-use lights. This generally means ambient lighting and a single, primary directional light are applied at this time, all in one pass.
Point Lights (and other non-directional lights, for that matter) are assumed not to be primary light sources. As a result, none of them will be handled in the ForwardBase pass, but instead in the ForwardAdd pass.
Because the lights aren't handled in ForwardBase, there's no information being sent to the shader about them. Therefore, the position data defaults to a zero vector.
Regarding the unity_LightPosition array, that is used for vertex lighting. The values are applied in view space, so you're seeing fallback values of (0, 0) across the screen and 1 depth.
Answer by glupsk · Jan 20 at 09:38 PM
"RenderType" = "ForwardBase"
is wrong. It should be "LightMode" = "ForwardBase"
. So your SubShader pass should something like this:
Shader "Shader name here"
{
SubShader
{
Tags { "RenderType" = "Opaque" "Queue" = "Geometry" }
Pass
{
Name "FORWARD"
Tags { "LightMode" = "ForwardBase" }
// Shader code here
}
}
}
Your answer
Follow this Question
Related Questions
ShaderLab builtin lighting properties are not correct? 0 Answers
Is it more qualitative to calculate light direction manually or using WorldSpaceLightDir(vertex)? 1 Answer
How to add shadows to surface shader? 1 Answer
what would you choose to write unity shaders GLSL, HLSL/CG or Surface lang? 1 Answer
How do I initialise a TEX2DARRAY in shader with RenderTextures from C# script 0 Answers