diff options
| author | skal <pascal.massimino@gmail.com> | 2026-03-22 20:28:48 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-03-22 20:28:48 +0100 |
| commit | ce22f79c55e68f9fa496a47a528a6978b89e1261 (patch) | |
| tree | 04f6539b17757c20f965e5f53fe039c90d998f89 /src/shaders | |
| parent | ff46656233cd2676f1befef9ecc82722d962a89c (diff) | |
feat(shaders): add ray_sphere snippet, use in gbuf_raster impostor
Diffstat (limited to 'src/shaders')
| -rw-r--r-- | src/shaders/ray_sphere.wgsl | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/shaders/ray_sphere.wgsl b/src/shaders/ray_sphere.wgsl new file mode 100644 index 0000000..659e144 --- /dev/null +++ b/src/shaders/ray_sphere.wgsl @@ -0,0 +1,21 @@ +// Ray-sphere intersection. +// ro: ray origin, rd: ray direction (must be normalized). +// center: sphere center, radius: sphere radius. +// Returns t of the nearest positive intersection, or -1.0 if no hit. + +struct RaySphereHit { + t: f32, // distance along ray to nearest hit (negative = miss) + hit: bool, +}; + +fn ray_sphere_intersection(ro: vec3f, rd: vec3f, + center: vec3f, radius: f32) -> RaySphereHit { + let oc = ro - center; + let b = dot(oc, rd); + let c = dot(oc, oc) - radius * radius; + let disc = b * b - c; + if (disc < 0.0) { return RaySphereHit(-1.0, false); } + let t = -b - sqrt(disc); + if (t < 0.0) { return RaySphereHit(-1.0, false); } + return RaySphereHit(t, true); +} |
