- Home /
Is it more qualitative to calculate light direction manually or using WorldSpaceLightDir(vertex)?
I'm wondering how to get best vizualization, and I calculate light direction in my vertex function using function WorldSpaceLightDir, and all the rest calculations are in the fragment function. However, I also could have calculated light direction in fragment function using: lightDir = normalize(_WorldSpaceLightPos0.xyz)(if we consider also point lights, code will be longer of course); . Which one gives best visual effects? Or is the difference negligible?
Comment
Answer by Bunny83 · Mar 16, 2016 at 03:48 PM
Decide yourself:
// Taken from \Unity\Editor\Data\CGIncludes\UnityCG.cginc
// Computes world space light direction, from world space position
inline float3 UnityWorldSpaceLightDir( in float3 worldPos )
{
#ifndef USING_LIGHT_MULTI_COMPILE
return _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w;
#else
#ifndef USING_DIRECTIONAL_LIGHT
return _WorldSpaceLightPos0.xyz - worldPos;
#else
return _WorldSpaceLightPos0.xyz;
#endif
#endif
}
// Computes world space light direction, from object space position
// *Legacy* Please use UnityWorldSpaceLightDir instead
inline float3 WorldSpaceLightDir( in float4 localPos )
{
float3 worldPos = mul(_Object2World, localPos).xyz;
return UnityWorldSpaceLightDir(worldPos);
}
If you only need directional lighting it's probably simpler to use _WorldSpaceLightPos0.xyz
directly. Also note that WorldSpaceLightDir has a "Legacy note" so you shouldn't use it at all ^^.