Crafter.Graphics/examples/RTVolume/anyhit.wgsl
catbot 5dd1086f08 docs(webgpu-rt): add RTVolume example (procedural spheres + any-hit cut-out)
A 3x3x3 grid of AABB-geometry spheres rendered through an analytic
ray-sphere intersection shader, with an any-hit spherical-checkerboard
cut-out so the background shows through. Exercises both features end to
end on the WebGPU wavefront tracer.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 22:09:30 +00:00

24 lines
1.1 KiB
WebGPU Shading Language

// RTVolume any-hit shader (runs in TRACE on every candidate sphere hit,
// because the geometry is registered non-opaque). Punches a spherical
// checkerboard of holes: for half the cells it returns RT_ANYHIT_IGNORE,
// so the ray passes straight through and the background / spheres behind
// show through. Returning RT_ANYHIT_ACCEPT keeps the hit. This is the
// visible proof the any-hit path runs — with it the spheres are perforated,
// without it they would be solid.
fn anyhit_main(ray: RayDesc, hit: HitInfo, payload: ptr<function, Payload>) -> u32 {
// Object-space hit point on the unit sphere → its normal/direction.
let posObj = hit.objectRayOrigin + hit.objectRayDirection * hit.t;
let n = normalize(posObj);
let PI = 3.14159265;
let longitude = atan2(n.z, n.x); // [-PI, PI]
let latitude = asin(clamp(n.y, -1.0, 1.0)); // [-PI/2, PI/2]
let cu = i32(floor((longitude + PI) / PI * 6.0));
let cv = i32(floor((latitude + PI * 0.5) / PI * 6.0));
if (((cu + cv) & 1) == 0) {
return RT_ANYHIT_IGNORE; // cut-out cell — see through
}
return RT_ANYHIT_ACCEPT;
}