environment_mapping.shaderModule
- README.md1.7 KB
- module.wgsl7.3 KB
This package holds more than this page lists. See all of its files in its world.
README.md
environment_mapping
Where a direction lands on a 2D environment image, and the screen-space gradients to sample it at. A sky, a skybox surface or a reflection that reads an environment image along a direction samples it through these, so the image reads as one continuous sky across every cube edge and all the way around the horizon.
zero_env_cube_strip(dir, ddx_dir, ddy_dir, size, levels): a vertical strip of six square cube faces, top to bottom +X, -X, +Y, -Y, +Z, -Z. The coordinate stays inside its face by the reach of the read (half the footprint its gradients span, where an anisotropic read lays its taps, plus half a texel of the coarsest mip level it touches), so a filtered read takes nothing from the face stacked beside it in the strip.zero_env_equirect(dir, ddx_dir, ddy_dir): an equirectangular (latitude / longitude) panorama,u = atan2(z, x) / 2PI + 0.5, the zenith at the top.
Both answer a ZeroEnvCoords { uv, ddx, ddy } for textureSampleGrad. The
gradients come from the direction, which turns smoothly, rather than from the
UV, which jumps where a face changes or the longitude wraps; an
implicit-derivative textureSample on that UV reads the image's smallest mips
along every jump.
The including shader takes the direction's derivatives in uniform control flow and scales them by the frame's texture LOD bias:
#include "@builtin::shaderModules.environment_mapping"
let bias = exp2(uniforms.texture_sampling.x);
let c = zero_env_cube_strip(dir, dpdx(dir) * bias, dpdy(dir) * bias,
vec2<f32>(textureDimensions(tex)), f32(textureNumLevels(tex)));
let colour = textureSampleGrad(tex, tex_sampler, c.uv, c.ddx, c.ddy);
Pure math: no bindings, no uniforms.