Reflection and environment map explorer
A reflection map looks a cube map up in the direction a view ray would take after bouncing off the surface. This page combines the five textbook samples (reflectionMap, reflectionMap1, reflectionMap2, reflectingCube, reflectingSphere) and extends them with richer environments, materials, a dynamic environment, and a split-screen compare that draws the same object twice with one setting changed.
Notes on what the samples do and what is approximated
- Eye space vs world space.
reflectingCube.jsandreflectingSphere.jsorbit the camera but look the cube map up with a reflection vector in eye coordinates. For a camera at the origin looking down −z (the fixed camera ofreflectionMap.js) that is harmless; once the camera moves, the centre of a reflecting sphere always reflects (0, 0, 1), the +Z face, whatever the camera does, so the environment is glued to the camera. Rotating the vector into world coordinates (uViewRotationInverse) fixes it. - The normal matrix of reflectionMap2.js. The sample uploads
transpose(normalMatrix(modelView)). The normal matrix of a rotation is the rotation itself, so the transpose is its inverse: normals rotate the wrong way, 19° off at 10° of rotation and 57° off at 30°. The other samples use the correct matrix. - Raw vs normalized eye vector. Reflection is linear in the eye vector, so on a flat face interpolating the per-vertex vectors computed from the raw eye vector (
reflectionMap1) is exactly the per-pixel answer. Normalizing first (reflectionMap2) breaks the linearity and makes it approximate. - Per vertex vs per fragment. On a smooth surface, per-vertex vectors cut corners: on a sphere the mean error against the per-pixel vector is about 116° at subdivision level 0, 9° at level 2 and 0.1° at level 5.
- Orthographic projection. The samples draw through
ortho()but compute the reflection for an eye at a point. This page defaults to perspective, which the maths assumes. - Cube-map conventions. Faces and texel orientation follow the OpenGL ES 3.0 cube-map selection table. Painted environments are drawn as seen from inside and resampled into texel order, and the unfolded preview shows them the same way. The dynamic environment renders each face with the usual look-at and up vectors, which match the table.
- Glossy reflections read a coarser mip level of the cube map, which averages the environment. That is a plausible blur, not a prefiltered specular map, so it does not respond correctly to the viewing angle.
- Glass refracts once, at the front surface; a real glass ball bends the light again leaving it, and absorbs and internally reflects along the way. Dispersion shifts the index of each colour channel separately.
- The dynamic environment is re-rendered from the centre of the object, so it is exact only there: reflections of nearby objects are slightly wrong at the object's surface. It costs six extra renders of the scene per frame.