hi guys
When manipulating the Cubemap reflection in Unity, why using the world View Direction as the parameter for the function: reflect ( ), but not using the world Light Direction as the parameter, cause the reflection is generated from the incoming light, isn’t it ? And why there is a minus symbol before the parameter -o.worldViewDir ?
The cubemap is the light. But instead of a single point source, it captures a panoramic view of the light. The world space view direction is the vector from the object surface to the camera. With the - in front, it becomes the vector from the camera to the surface. If you then reflect this in the surface normal, it becomes the vector from the surface to the world after being reflected. The you obtain the light at this location in the world by looking it up in the cubemap.
Actually the world space view direction is from the camera to the surface. However the reflect() function takes a vector going from the surface to the camera so it needs to be negated. Similary a dot product is positive when the two inputs are going the same direction, ie: the normal and light direction are both the directions going out from the surface.
Ah, I’m used to the hlsl version of reflect:
v = i - 2 * n * dot(i•n)
Where i is the incident vector, so camera to surface. So, looking straight on to the normal, i = -n, you get:
v = -n - 2 * n * dot(-n•n)
v = -n - 2 * n * -1
v = -n + 2 * n
v = n
I’m actually generally used to have the view vector as surface to camera, like the light vector as surface to light. So you can do dot products with the normal without having to do a negate or abs.
This is the hlsl version … and I was wrong. Your explanation is correct.
The UnityWorldSpaceViewDir() is defined as:
inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
{
return _WorldSpaceCameraPos.xyz - worldPos;
}
Which would result in the camera position relative to the vertex / surface position, ie: the surface to camera vector. My own expectation was wrong.
A quote I’ve heard from a few big name graphics programmers is “A big part of graphics programming is making sure you have an even number of sign errors”.
thank all of you for helpful explanation