BVH Hit tests
BVH Hit Testing:
closest_hit vs
any_hit
This document tests and visualizes the difference between
closest_hit and
any_hit functions in the BVH implementation using the new
RayIntersectionSession API.
Test Setup
Test 1: Single Ray Through Center
Test a ray through the center that passes through all three spheres.
Test 2: Multiple Rays from Different Positions
Test multiple rays to ensure both functions work correctly.
Visualization: Multiple Rays
Test 4: Difference Between any hit and closesthit
Demonstrate that
any_hit can return different results than
closest_hit.
Key Findings:
-
any_hitexits on the first intersection during BVH traversal (usesintersect, doesn't update ray) -
closest_hitcontinues searching and updates ray'st_max(usesintersect_p!) -
In complex scenes with overlapping geometry,
any_hitcan return hits that are significantly farther -
Both always agree on whether a hit occurred (hit vs miss)
-
The difference appears when BVH traversal order differs from spatial distance order
Performance Comparison
Compare the performance of
closest_hit vs
any_hit.
| Method | Time_μs |
|---|---|
| closest_hit |
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
Range (min … max): 10.920 μs … 29.164 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 11.001 μs ┊ GC (median): 0.00%
Time (mean ± σ): 11.105 μs ± 849.468 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█
█▅▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▂ ▂
10.9 μs Histogram: frequency by time 18.1 μs <
Memory estimate: 0 bytes, allocs estimate: 0.
|
| any_hit |
BenchmarkTools.Trial: 10000 samples with 1 evaluation per sample.
Range (min … max): 14.326 μs … 35.617 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 14.427 μs ┊ GC (median): 0.00%
Time (mean ± σ): 14.566 μs ± 964.957 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█▇▂ ▁
███▆▅▅▇█▅▁▁▄▄▁▄▄▅▅▃▁▁▁▁▁▄▁▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▇▇ █
14.3 μs Histogram: log(frequency) by time 21.8 μs <
Memory estimate: 0 bytes, allocs estimate: 0.
|
Summary
This document demonstrated:
-
RayIntersectionSession- A convenient struct for managing ray tracing sessions-
Bundles rays, BVH, hit function, and results together
-
Provides helper functions:
hit_count(),miss_count(),hit_points(),hit_distances()
-
-
Makie visualization recipe - Automatic visualization via
plot(session)-
Automatically renders BVH geometry, rays, and hit points
-
Customizable colors, transparency, markers, and labels
-
Works with any Makie backend (GLMakie, WGLMakie, CairoMakie)
-
-
closest_hitcorrectly identifies the nearest intersection among multiple overlapping primitives-
Returns:
(hit_found::Bool, hit_primitive::Triangle, distance::Float32, barycentric_coords::Point3f) -
distanceis the distance from ray origin to the hit point -
Use
Raycore.sum_mul(bary_coords, primitive.vertices)to convert to world-space hit point
-
-
any_hitefficiently determines if any intersection exists, exiting early-
Returns: Same format as
closest_hit:(hit_found::Bool, hit_primitive::Triangle, distance::Float32, barycentric_coords::Point3f) -
Can exit early on first hit found, making it faster for occlusion testing
-
-
Both functions handle miss cases correctly (returning
hit_found=false) -
any_hitis typically faster thanclosest_hitdue to early termination
All tests passed! ✓