lib-env.glsl
Public Functions:
envSample
envIrradiance
worldToEnvSpace
Needed for math constants
Copied to your clipboardimport lib-defines.glsl
Engine provided parameters
Copied to your clipboard//: param auto texture_environmentuniform sampler2D environment_texture;//: param auto environment_matrixuniform mat3 environment_matrix;//: param auto environment_exposureuniform float environment_exposure;//: param auto environment_irrad_mat_reduniform mat4 irrad_mat_red;//: param auto environment_irrad_mat_greenuniform mat4 irrad_mat_green;//: param auto environment_irrad_mat_blueuniform mat4 irrad_mat_blue;
Helper that allows one to sample environment. Rotation is taken into account. The environment map is a panoramic env map behind the scene, that's why there is extra computation from dir vector. Direction must be in environment space (using worldToEnvSpace function)
Copied to your clipboardvec3 envSample(vec3 dir, float lod){// WORKAROUND: Intel GLSL compiler for HD5000 is bugged on OSX:// https://bugs.chromium.org/p/chromium/issues/detail?id=308366// It is necessary to replace atan(y, -x) by atan(y, -1.0 * x) to force// the second parameter to be interpreted as a floatvec2 pos = M_INV_PI * vec2(atan(-dir.z, -1.0 * dir.x), 2.0 * asin(dir.y));pos = 0.5 * pos + vec2(0.5);return textureLod(environment_texture, pos, lod).rgb * environment_exposure;}
Transform a direction from world to environment space
Copied to your clipboardvec3 worldToEnvSpace(vec3 dirW){return environment_matrix * dirW;}
Return the irradiance for a given direction. The computation is based on environment's spherical harmonics projection. Direction in world space
Copied to your clipboardvec3 envIrradiance(vec3 dirW){vec4 shDir = vec4(worldToEnvSpace(dirW).xzy, 1.0);return max(vec3(0.0), vec3(dot(shDir, irrad_mat_red * shDir),dot(shDir, irrad_mat_green * shDir),dot(shDir, irrad_mat_blue * shDir))) * environment_exposure;}