Crafter.Graphics/examples/RTVolume/anyhit.wgsl

24 lines
1.1 KiB
WebGPU Shading Language
Raw Permalink Normal View History

// 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;
}