(I’m putting together a PR to clarify the reflection/refraction docs based on answers to my earlier question on reflection textures, but there’s a few things I can’t quite figure out…)
(1) What’s the real difference between PLANAR_MODE and CUBIC_MODE?
I read deltakosh’s post and studied the code and experimented with flat textures (playground) and cube textures (playground) but they actually seem very similar. I found these differences:
- CUBIC_MODE has an evil twin INVCUBIC_MODE (just inverts y for texture lookup) but there’s no INVPLANAR_MODE
- PLANAR_MODE uses
vec3(reflectionMatrix * vec4(coords, 1))
but CUBIC_MODE usesvec3(reflectionMatrix * vec4(coords, 0))
. I think1
is the right W-value here, not sure? This is a subtle effect (and no effect if no reflectionMatrix is used)
Otherwise they both implement the simple law of reflection (wikipedia). Both seem designed for cubemap (3D) textures, not flat textures, despite the name (they both produce (-1,-1,-1)-(+1,+1,+1) UVW, rather than (0,0,0)-(1,1,0) UV).
(2) What is SPHERICAL_MODE really, anyway?
This mode is an odd duck. It changes based on camera direction in an unusual way. Looking at reflectionFunction.fx:
vec3 viewDir = normalize(vec3(view * worldPos));
vec3 viewNormal = normalize(vec3(view * vec4(worldNormal, 0.0)));
These are the surface point and surface normal in screen (camera) coordinates (screen x/y, depth z), normalized (length 1). So it’s the direction from the eye to the point and the point normal, but in screen coordinates.
vec3 r = reflect(viewDir, viewNormal);
r = vec3(reflectionMatrix * vec4(r, 0));
This follows the law of reflection (very ordinary) in screen coordinates (hmm?).
r.z = r.z - 1.0;
float m = 2.0 * length(r);
return vec3(r.x / m + 0.5, 1.0 - r.y / m - 0.5, 0);
Finally, normalize again and map to (0,0)-(1,1) UV (centered at 0.5,0.5) for texture lookup. That part makes some sense. But I cannot figure out what the previous code is trying to accomplish, though I’m sure it is very clever and/or standard (I am still very noob).
Correct my confusion? Once clarified I can incorporate into a doc PR as best I can.