September 19, 2011

box projected cube environment mapping

I have been quite busy moving to another country, so nothing new this time. Oh, yeah, I added donate button, hehe.

BPCEM or box projected cube environment mapping is a cube environment mapping technique made by Bartosz Czuba I thank him for helping me get this to work in BGE.

Regular Cube Environment Mapping is a very common technique of making fast fake reflections. It works best in outdoors, when reflecting distant skies and stuff that reaches to infinity, but looks very wrong in indoors, especially on flat surfaces, like walls and floor. It is caused by cube-map coordinates that reaches to infinity.

What BPCEM basically does, it takes the cube-map coordinates and clamps them to the size of the room. This technique only looks best in simple box shaped rooms. The original thread about BPCEM is HERE.

comparison stills (regular cube mapping vs BPCEM):
regular cubemap still looks fine on spheres, but wrong on flat surfaces.

All the difference makes these few lines of code:

vec3 bpcem (in vec3 v, vec3 eMax, vec3 eMin, vec3 ePos)
vec3 nrdir = normalize(v);
vec3 rbmax = (eMax - pos)/nrdir;
vec3 rbmin = (eMin - pos)/nrdir;

vec3 rbminmax;
rbminmax.x = (nrdir.x>0.0)?rbmax.x:rbmin.x;
rbminmax.y = (nrdir.y>0.0)?rbmax.y:rbmin.y;
rbminmax.z = (nrdir.z>0.0)?rbmax.z:rbmin.z;
float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
vec3 posonbox = pos + nrdir * fa;
return posonbox - ePos;

here is GLSL code:

and a .blend file:


  1. I never get tired of looking at this file. Great working Martinsh.

    At some point during Siggraph I even thought the envmap were not pre-baked (I was spoiled by a horse power computer, true :).

    For sure one of the great hits at the Blender Booth!

  2. Seeing path-tracing engines like Octane and Cycles to render stuff at interactive speeds, in near future games you will not need to fake the effects with these shaders anymore ;)

  3. But for the time being, we need to fake...(sight). I was thinking, this BPCEM is great! Can you combine it with the VideoToTexture BGE Module to update the environement map once the camera is out of the 'room's' range? The cube map has 6 faces right, you get 6 different textures from 6 cameras and then render the result from another, then replace the map with the result, and refresh it!
    Et voila, quasi realtime reflections!

    1. there is actually another very nice method for accurate real time reflections I am trying out. It is called "Real time Local Reflections" It is now widely used in many newest titles. I will post the results soon.

  4. thanks for the code was very helpfull
    Why do you reconstruct you normal like this, normaly you just get it from the texture.
    vec3 nTangent = normalize(vec3(n.xy*bumpCoeff,sqrt( 1.0 - n.y * n.y - n.x * n.x)));

  5. I'm a few years late here. :) This is an beautiful demo. The metal sphere, the refractive sphere, plastic wrap on the crates, awesome! and it has source code, thanks.