displace: Parallax Occlusion Mapping Function
Heightmap Surface Displacement with Ray Intersection
The displace function implements parallax occlusion mapping through iterative raymarching. Given a heightmap texture and viewing parameters, it computes surface intersection points by stepping along ray directions until depth discontinuities are detected.
Mathematical Foundation
For a ray originating at position with direction , the displacement algorithm samples heightmap values along the parametric line:
The intersection condition is satisfied when:
where is the displacement depth scale factor.
Function Variants
Function | Input | Output | Purpose |
---|---|---|---|
displace(tex, ro, rd) | texture, vec3, vec3 | vec3 | Ray-based displacement |
displaceUV(tex, ro, uv) | texture, vec3, vec2 | vec3 | UV-based displacement |
Implementation Details
The function performs binary refinement when depth intersection occurs. The final position interpolates between the last two sample points using distance-based weighting:
where and represent signed distances to the heightmap surface.
ライブエディター
const fragment = () => { const heightTex = uniform('https://avatars.githubusercontent.com/u/40712342') const viewPos = vec3(iTime.cos(), iTime.sin(), 0.5) const surface = displaceUV(heightTex, viewPos, uv) return vec4(surface, 1) }