euv_engine/raytracing/fn.rs
1use super::*;
2
3/// Returns the component-wise minimum of two vectors.
4///
5/// # Arguments
6///
7/// - `Vector3D` - The first vector.
8/// - `Vector3D` - The second vector.
9///
10/// # Returns
11///
12/// - `Vector3D` - The component-wise minimum.
13fn vector3d_min(a: Vector3D, b: Vector3D) -> Vector3D {
14 Vector3D::new(
15 a.get_x().min(b.get_x()),
16 a.get_y().min(b.get_y()),
17 a.get_z().min(b.get_z()),
18 )
19}
20
21/// Returns the component-wise maximum of two vectors.
22///
23/// # Arguments
24///
25/// - `Vector3D` - The first vector.
26/// - `Vector3D` - The second vector.
27///
28/// # Returns
29///
30/// - `Vector3D` - The component-wise maximum.
31fn vector3d_max(a: Vector3D, b: Vector3D) -> Vector3D {
32 Vector3D::new(
33 a.get_x().max(b.get_x()),
34 a.get_y().max(b.get_y()),
35 a.get_z().max(b.get_z()),
36 )
37}
38
39/// Returns the inclusive parameter interval of a single AABB slab along one
40/// ray axis, or `None` when the ray runs parallel to the slab outside it.
41///
42/// A parallel axis is not rejected outright: the interval becomes
43/// `(-inf, +inf)` when the origin lies inside the slab and the caller keeps
44/// only the two axes that actually constrain the ray.
45///
46/// # Arguments
47///
48/// - `f64` - The ray origin component on this axis.
49/// - `f64` - The ray direction component on this axis.
50/// - `f64` - The slab lower bound.
51/// - `f64` - The slab upper bound.
52///
53/// # Returns
54///
55/// - `Option<(f64, f64)>` - The `(enter, exit)` parameters, or `None` when
56/// the ray is parallel to the slab and outside it.
57fn slab_interval(
58 origin_component: f64,
59 dir_component: f64,
60 lower: f64,
61 upper: f64,
62) -> Option<(f64, f64)> {
63 if dir_component.abs() < RAYTRACE_TRIANGLE_EPSILON {
64 if origin_component < lower || origin_component > upper {
65 return None;
66 }
67 return Some((f64::NEG_INFINITY, f64::INFINITY));
68 }
69 let inverse: f64 = 1.0 / dir_component;
70 let t0: f64 = (lower - origin_component) * inverse;
71 let t1: f64 = (upper - origin_component) * inverse;
72 Some((t0.min(t1), t0.max(t1)))
73}
74
75/// Reports whether the ray segment `t_min`..=`t_max` can touch the AABB
76/// spanning `lower` to `upper`.
77///
78/// This is a conservative broad-phase reject: it never discards an occluder
79/// whose exact test could report a hit inside the requested range, because
80/// the AABB fully contains the occluder and every exact hit lies inside the
81/// AABB.
82///
83/// # Arguments
84///
85/// - `Vector3D` - The ray origin.
86/// - `Vector3D` - The ray direction.
87/// - `Vector3D` - The AABB minimum corner.
88/// - `Vector3D` - The AABB maximum corner.
89/// - `f64` - The minimum ray parameter considered a valid hit.
90/// - `f64` - The maximum ray parameter considered a valid hit.
91///
92/// # Returns
93///
94/// - `bool` - `true` when the segment may touch the AABB.
95fn ray_segment_overlaps_aabb(
96 origin: Vector3D,
97 dir: Vector3D,
98 lower: Vector3D,
99 upper: Vector3D,
100 t_min: f64,
101 t_max: f64,
102) -> bool {
103 let x: Option<(f64, f64)> =
104 slab_interval(origin.get_x(), dir.get_x(), lower.get_x(), upper.get_x());
105 let y: Option<(f64, f64)> =
106 slab_interval(origin.get_y(), dir.get_y(), lower.get_y(), upper.get_y());
107 let z: Option<(f64, f64)> =
108 slab_interval(origin.get_z(), dir.get_z(), lower.get_z(), upper.get_z());
109 match (x, y, z) {
110 (Some((x0, x1)), Some((y0, y1)), Some((z0, z1))) => {
111 let t_enter: f64 = x0.max(y0).max(z0);
112 let t_exit: f64 = x1.min(y1).min(z1);
113 t_enter <= t_exit && t_exit >= t_min && t_enter <= t_max
114 }
115 _ => false,
116 }
117}
118
119/// Intersects a ray with the triangle `v0`, `v1`, `v2` using the
120/// Moller-Trumbore algorithm.
121///
122/// The test is two-sided: a hit on either face counts, and the returned
123/// normal is flipped when needed so that it always faces against the ray
124/// direction, making back-face hits usable for shading. A ray parallel to
125/// the triangle plane yields `None`. No `t_min`/`t_max` filtering is
126/// applied here; use [`Ray::intersect_triangle`] for the range-clamped
127/// variant.
128///
129/// # Arguments
130///
131/// - `Vector3D` - The ray origin.
132/// - `Vector3D` - The ray direction (expected to be unit length).
133/// - `Vector3D` - The first triangle vertex.
134/// - `Vector3D` - The second triangle vertex.
135/// - `Vector3D` - The third triangle vertex.
136///
137/// # Returns
138///
139/// - `Option<(f64, Vector3D)>` - The hit distance along the ray and the
140/// unit surface normal oriented against the ray direction, or `None` on
141/// miss.
142pub fn intersect_triangle(
143 origin: Vector3D,
144 dir: Vector3D,
145 v0: Vector3D,
146 v1: Vector3D,
147 v2: Vector3D,
148) -> Option<(f64, Vector3D)> {
149 let edge1: Vector3D = v1 - v0;
150 let edge2: Vector3D = v2 - v0;
151 let pvec: Vector3D = dir.cross(edge2);
152 let det: f64 = edge1.dot(pvec);
153 if det.abs() < RAYTRACE_TRIANGLE_EPSILON {
154 return None;
155 }
156 let inverse_det: f64 = 1.0 / det;
157 let tvec: Vector3D = origin - v0;
158 let u: f64 = tvec.dot(pvec) * inverse_det;
159 if u < 0.0 || u > 1.0 {
160 return None;
161 }
162 let qvec: Vector3D = tvec.cross(edge1);
163 let v: f64 = dir.dot(qvec) * inverse_det;
164 if v < 0.0 || u + v > 1.0 {
165 return None;
166 }
167 let t: f64 = edge2.dot(qvec) * inverse_det;
168 let geometric: Vector3D = edge1.cross(edge2).normalized();
169 let normal: Vector3D = if geometric.dot(dir) > 0.0 {
170 -geometric
171 } else {
172 geometric
173 };
174 Some((t, normal))
175}
176
177/// Returns the AABB extents `(min, max)` of an [`Occluder`].
178///
179/// For AABB occluders this is `(center, extent)`. For sphere occluders
180/// the bounding box is computed from the center and the `.x` component of
181/// `extent` (the sphere radius). For triangle occluders the bounding box
182/// is the component-wise span of the three vertices.
183///
184/// # Arguments
185///
186/// - `&Occluder` - The occluder to bound.
187///
188/// # Returns
189///
190/// - `(Vector3D, Vector3D)` - The `(min, max)` corners of the AABB.
191fn occluder_aabb_extents(occluder: &Occluder) -> (Vector3D, Vector3D) {
192 let (mn, mx): (Vector3D, Vector3D) = match occluder.get_kind() {
193 OccluderKind::Aabb => (occluder.get_center(), occluder.get_extent()),
194 OccluderKind::Sphere => {
195 let center: Vector3D = occluder.get_center();
196 let radius: f64 = occluder.get_extent().get_x();
197 let r: Vector3D = Vector3D::new(radius, radius, radius);
198 (center - r, center + r)
199 }
200 OccluderKind::Triangle => {
201 let [v0, v1, v2]: [Vector3D; 3] = occluder.get_vertices();
202 let lo: Vector3D = vector3d_min(v0, vector3d_min(v1, v2));
203 let hi: Vector3D = vector3d_max(v0, vector3d_max(v1, v2));
204 (lo, hi)
205 }
206 };
207 (vector3d_min(mn, mx), vector3d_max(mn, mx))
208}
209
210/// Flattens every occluder into `(center, radius)` sphere tuples used by
211/// [`soft_shadow_factor`].
212///
213/// For sphere occluders the tuple is `(center, radius)`. For AABB
214/// occluders a conservative bounding sphere is computed from the AABB.
215pub(crate) fn collect_occluder_points(occluders: &[Occluder]) -> Vec<(Vector3D, f64)> {
216 let mut out: Vec<(Vector3D, f64)> = Vec::new();
217 for occ in occluders.iter() {
218 let (mn, mx): (Vector3D, Vector3D) = occluder_aabb_extents(occ);
219 let cx: f64 = (mn.get_x() + mx.get_x()) * 0.5;
220 let cy: f64 = (mn.get_y() + mx.get_y()) * 0.5;
221 let cz: f64 = (mn.get_z() + mx.get_z()) * 0.5;
222 let ex: f64 = (mx.get_x() - mn.get_x()) * 0.5;
223 let ey: f64 = (mx.get_y() - mn.get_y()) * 0.5;
224 let ez: f64 = (mx.get_z() - mn.get_z()) * 0.5;
225 let r: f64 = (ex * ex + ey * ey + ez * ez).sqrt();
226 out.push((Vector3D::new(cx, cy, cz), r));
227 }
228 out
229}
230
231/// Finds the closest intersection between a ray and a list of occluders
232/// without touching any [`Material`].
233///
234/// Returns the winning occluder's index alongside the hit data so callers
235/// can borrow the material directly from the occluder list instead of
236/// cloning it per candidate. The tie-breaking rule matches the historical
237/// behavior: the first occluder achieving the minimum `t` wins.
238///
239/// Every occluder is first run through a broad-phase AABB slab test
240/// against the ray's `t_min`..=`t_max` segment; occluders whose bounds the
241/// segment provably cannot touch are skipped without any exact math. The
242/// test is conservative, so it only removes candidates that could not have
243/// produced a hit, leaving the returned hit identical to a full scan.
244///
245/// # Arguments
246///
247/// - `&Ray` - The ray to test.
248/// - `&[Occluder]` - The occluders to test against.
249///
250/// # Returns
251///
252/// - `Option<(usize, f64, Vector3D, Vector3D)>` - The occluder index, the
253/// hit distance `t`, the hit position, and the surface normal, or `None`
254/// if the ray misses.
255pub(crate) fn closest_hit_indexed(
256 ray: &Ray,
257 occluders: &[Occluder],
258) -> Option<(usize, f64, Vector3D, Vector3D)> {
259 let origin: Vector3D = ray.get_origin();
260 let dir: Vector3D = ray.get_direction();
261 let t_min: f64 = ray.get_t_min();
262 let t_max: f64 = ray.get_t_max();
263 let mut best: Option<(usize, f64, Vector3D, Vector3D)> = None;
264 for (index, occ) in occluders.iter().enumerate() {
265 let (bound_min, bound_max): (Vector3D, Vector3D) = occluder_aabb_extents(occ);
266 if !ray_segment_overlaps_aabb(origin, dir, bound_min, bound_max, t_min, t_max) {
267 continue;
268 }
269 let candidate: Option<(f64, Vector3D)> = match occ.get_kind() {
270 OccluderKind::Sphere => {
271 let center: Vector3D = occ.get_center();
272 let radius: f64 = occ.get_extent().get_x();
273 match ray_sphere_intersect(origin, dir, center, radius) {
274 Some((t, n)) if t >= t_min && t <= t_max => Some((t, n)),
275 _ => None,
276 }
277 }
278 OccluderKind::Aabb => match ray_aabb_intersect(origin, dir, bound_min, bound_max) {
279 Some((t_near, _t_far, n)) if t_near >= t_min && t_near <= t_max => {
280 Some((t_near, n))
281 }
282 _ => None,
283 },
284 OccluderKind::Triangle => {
285 let [v0, v1, v2]: [Vector3D; 3] = occ.get_vertices();
286 ray.intersect_triangle(v0, v1, v2)
287 }
288 };
289 if let Some((t, n)) = candidate {
290 let keep_previous: bool = matches!(&best, Some(previous) if previous.1 <= t);
291 if !keep_previous {
292 let hit_pos: Vector3D = origin + dir.scaled(t);
293 best = Some((index, t, hit_pos, n));
294 }
295 }
296 }
297 best
298}
299
300/// Builds a reflected ray bouncing off a surface point with the given
301/// normal.
302///
303/// # Arguments
304///
305/// - `&Ray` - The incoming ray.
306/// - `Vector3D` - The world-space hit position.
307/// - `Vector3D` - The outward unit normal at the hit point.
308///
309/// # Returns
310///
311/// - `Ray` - A new ray originating at the hit point with the reflected
312/// direction, `t_min` reset to `RAYTRACE_DEFAULT_T_MIN`, `t_max` set to
313/// `RAYTRACE_DEFAULT_T_MAX`, and `depth` incremented by one.
314fn bounce_ray(ray: &Ray, position: Vector3D, normal: Vector3D) -> Ray {
315 let dir: Vector3D = ray.get_direction();
316 let dot: f64 = dir.dot(normal);
317 let reflected_dir: Vector3D = dir - normal.scaled(2.0 * dot);
318 Ray {
319 origin: position,
320 direction: reflected_dir,
321 t_min: RAYTRACE_DEFAULT_T_MIN,
322 t_max: RAYTRACE_DEFAULT_T_MAX,
323 depth: ray.get_depth() + 1,
324 }
325}
326
327/// Iteratively traces a ray against `occluders` using precomputed shadow
328/// bounding spheres, performing no heap allocation per bounce.
329///
330/// Color contributions are accumulated with a specular throughput: each
331/// bounce multiplies the throughput by the hit material's specular
332/// intensity, and a miss adds the ambient color scaled by the current
333/// throughput. The bounce loop stops when the ray misses, when `depth`
334/// reaches `max_bounces`, or when the hit material's specular intensity is
335/// not greater than [`EPSILON`], matching the behavior of the historical
336/// recursive formulation.
337///
338/// # Arguments
339///
340/// - `Ray` - The ray to trace.
341/// - `&[Occluder]` - All occluding surfaces in the scene.
342/// - `&[(Vector3D, f64)]` - Precomputed `(center, radius)` shadow bounding
343/// spheres, one per occluder.
344/// - `&LightingUniforms` - Lighting parameters used during shading.
345/// - `u32` - The maximum number of bounces allowed for this ray.
346///
347/// # Returns
348///
349/// - `Vector3D` - The final traced color.
350pub(crate) fn trace_bounces(
351 ray: Ray,
352 occluders: &[Occluder],
353 shadow_points: &[(Vector3D, f64)],
354 lights: &LightingUniforms,
355 max_bounces: u32,
356) -> Vector3D {
357 let ambient: Vector3D = lights.get_ambient();
358 let mut color: Vector3D = Vector3D::zero();
359 let mut throughput: f64 = 1.0;
360 let mut current: Ray = ray;
361 loop {
362 let (index, _t, position, normal): (usize, f64, Vector3D, Vector3D) =
363 match closest_hit_indexed(¤t, occluders) {
364 None => {
365 color += ambient.scaled(throughput);
366 break;
367 }
368 Some(hit) => hit,
369 };
370 let material: &Material = occluders[index].get_material();
371 color += lights
372 .shade(position, normal, material, shadow_points)
373 .scaled(throughput);
374 let spec: f64 = material.get_specular();
375 if current.get_depth() >= max_bounces || spec <= EPSILON {
376 break;
377 }
378 throughput *= spec;
379 current = bounce_ray(¤t, position, normal);
380 }
381 color
382}