armory3d / armory

3D Engine with Blender Integration
https://armory3d.org
zlib License
3.05k stars 317 forks source link

[$100 Bounty] Box-Project Cube Environment Mapping #2613

Open Naxela opened 1 year ago

Naxela commented 1 year ago

A working implementation of Box Projected Cubemap Environment Mapping (BPCEM) will release the bounty of $100 on Bountysource.

Sidenote: It doesn't have to be a realtime solution.

Implementation material (Inspiration):

Bountysource Link: https://app.bountysource.com/issues/111900128-100-bounty-box-project-cube-environment-mapping

gitgudyyao commented 1 year ago

Here is an example of a working implementation of BPCEM in GLSL, the shading language used in OpenGL:

// uniform variables uniform samplerCube cubemap; // cubemap texture uniform mat4 viewMatrix; // view matrix uniform mat4 projectionMatrix; // projection matrix uniform vec3 boxMin; // minimum corner of the box uniform vec3 boxMax; // maximum corner of the box

// varyings varying vec3 vNormal; // surface normal varying vec3 vPosition; // surface position

// projection function vec3 boxProject(vec3 point) { vec3 result = (point - boxMin) / (boxMax - boxMin); result = result * 2.0 - 1.0; return result; }

// main function void main() { // project the surface position onto the box vec3 projPos = boxProject(vPosition);

// calculate the reflection vector
vec3 R = reflect(projPos, vNormal);

// transform the reflection vector into view space
vec3 V = normalize((viewMatrix * vec4(R, 0)).xyz);

// sample the cubemap using the reflection vector
vec3 color = texture(cubemap, V).rgb;

// output the final color
gl_FragColor = vec4(color, 1.0);

}

This implementation assumes that the following inputs are provided as vertex attributes:

vNormal: the surface normal of the vertex vPosition: the position of the vertex in world space And that the following uniforms are provided:

cubemap: a cubemap texture containing the environment map viewMatrix: the view matrix of the camera projectionMatrix: the projection matrix of the camera boxMin: the minimum corner of the bounding box in world space boxMax: the maximum corner of the bounding box in world space This implementation also uses the reflect function, which calculates the reflection vector of an incident vector I around a surface normal N.