# [noob-ish] Reflection texture coordinate modes ðŸªž

(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 uses `vec3(reflectionMatrix * vec4(coords, 0))`. I think `1` 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.

CUBIC mode is mostly for reflection texture where we need a vector (and not a position) hence the w set to 0. Planar is about evaluating a position (hence w === 1). Planar can work with 2d textures but work better with cube textures

Spherical mode comes from 3dsmax (or I bet Maya). The idea is to project a texture onto a spherical target

If itâ€™s not too much trouble, can you explain why the W=0 vs W=1 difference matters at all? Both CUBIC_MODE and PLANAR_MODE seem to act nearly identically with both flat and cube textures. The W=0 vs W=1 would only make a different if reflectionMatrix is non-identity, and even then not usually.

(Neither one seems to work very well with flat textures, they end up with a projection of the X/Y texture onto the unit circle, also the texture is upside down, also the texture is repeated 4 times with seams at the zeroes, all of this is because itâ€™s (-1,-1,-1)-(+1,+1,+1) X/Y/Z vectors.)

Do you have an example of SPHERICAL_MODE doing anything but just looking trippy and random and swirling around a lot? I canâ€™t make any, and as currently coded Iâ€™m not sure it mathematically corresponds to anything sensible. (If people want to wrap a texture around a sphere, the equirectangular coordinate modes are a fine way to do thatâ€¦)

It makes a difference as soon as the reflectionMatrix contains a translation

I do agree that both work better with cube:)

The other big difference is in the code itself as we provide different textures for each case:
Babylon.js/texture.ts at master Â· BabylonJS/Babylon.js (github.com)

The spherical mode here is great to simulate sky reflection

Thanks! My attempt to capture the meaning of the modes: Revise the "diving deeper" (howto) page on reflections & refractions by egnor Â· Pull Request #230 Â· BabylonJS/Documentation Â· GitHub