Raycore.jl
High-performance ray-triangle intersection engine with BVH acceleration for CPU and GPU.
Features
- Fast BVH acceleration for ray-triangle intersection
- CPU and GPU support via KernelAbstractions.jl
- Analysis tools: centroid calculation, illumination analysis, view factors for radiosity
- Makie integration for visualization
Interactive Examples
BVH Hit Tests & Basics
Learn the basics of ray-triangle intersection, BVH construction, and visualization.

Ray Tracing Tutorial
Build a complete ray tracer from scratch with shadows, materials, reflections, and tone mapping.

View Factors Analysis
Calculate view factors, illumination, and centroids for radiosity and thermal analysis.

Overview
Raycore.RayIntersectionSession — TypeRayIntersectionSession{F}Represents a ray tracing session containing rays, a BVH structure, a hit function, and the computed intersection results.
Fields
rays::Vector{<:AbstractRay}: Array of rays to tracebvh::BVHAccel: BVH acceleration structure to intersect againsthit_function::F: Function to use for intersection testing (e.g.,closest_hitorany_hit)hits::Vector{Tuple{Bool, Triangle, Float32, Point3f}}: Results of hit_function applied to each ray
Example
using Raycore, GeometryBasics
# Create BVH from geometry
sphere = Tesselation(Sphere(Point3f(0, 0, 1), 1.0f0), 20)
bvh = Raycore.BVHAccel([sphere])
# Create rays
rays = [
Raycore.Ray(Point3f(0, 0, -5), Vec3f(0, 0, 1)),
Raycore.Ray(Point3f(1, 0, -5), Vec3f(0, 0, 1)),
]
# Create session
session = RayIntersectionSession(rays, bvh, Raycore.closest_hit)
# Access results
for (i, hit) in enumerate(session.hits)
hit_found, primitive, distance, bary_coords = hit
if hit_found
println("Ray $i hit at distance $distance")
end
endRaycore.any_hit — Functionany_hit(bvh::BVHAccel, ray::AbstractRay)Test if a ray intersects any primitive in the BVH (without finding the closest hit). This function stops at the first intersection found, making it faster than closest_hit when you only need to know if there's an intersection.
Returns:
hit_found: Boolean indicating if any intersection was foundhit_primitive: The primitive that was hit (if any)distance: Distance along the ray to the hit point (hit_point = ray.o + ray.d * distance)barycentric_coords: Barycentric coordinates of the hit point
Raycore.closest_hit — Methodclosest_hit(bvh::BVHAccel{P}, ray::AbstractRay) where {P}Find the closest intersection between a ray and the primitives stored in a BVH.
Returns:
hit_found: Boolean indicating if an intersection was foundhit_primitive: The primitive that was hit (if any)distance: Distance along the ray to the hit point (hit_point = ray.o + ray.d * distance)barycentric_coords: Barycentric coordinates of the hit point
Raycore.hit_count — Methodhit_count(session::RayIntersectionSession)Count the number of rays that hit geometry in the session.
Raycore.hit_distances — Methodhit_distances(session::RayIntersectionSession)Extract all hit distances from a RayIntersectionSession.
Returns a vector of Float32 containing distances for all rays that intersected geometry.
Raycore.hit_points — Methodhit_points(session::RayIntersectionSession)Extract all valid hit points from a RayIntersectionSession.
Returns a vector of Point3f containing the world-space hit points for all rays that intersected geometry.
Raycore.miss_count — Methodmiss_count(session::RayIntersectionSession)Count the number of rays that missed all geometry in the session.
Raycore.reflect — MethodReflect wo about n.
Private Functions
Raycore.cos_θ — MethodThe shading coordinate system gives a frame for expressing directions in spherical coordinates (θ, ϕ). The angle θ is measured from the given direction to the z-axis and ϕ is the angle formed with the x-axis after projection of the direction onto xy-plane.
Since normal is (0, 0, 1) → cos_θ = n · w = (0, 0, 1) ⋅ w = w.z.
Raycore.face_forward — MethodFlip normal n so that it lies in the same hemisphere as v.
Raycore.generate_ray_grid — Methodgenerate_ray_grid(bvh::BVHAccel, ray_direction::Vec3f, grid_size::Int)Generate a grid of ray origins based on the BVH bounding box and a given ray direction.
Raycore.intersect_p — Methoddirisnegative: 1 – false, 2 – true
Raycore.maximum_extent — MethodReturn index of the longest axis. Useful for deciding which axis to subdivide, when building ray-tracing acceleration structures.
1 - x, 2 - y, 3 - z.
Raycore.offset — MethodGet offset of a point from the minimum point of the bounds.
Raycore.traverse_bvh — Methodtraverse_bvh(bvh::BVHAccel{P}, ray::AbstractRay, hit_callback::F) where {P, F<:Function}Internal function that traverses the BVH to find ray-primitive intersections. Uses a callback pattern to handle different intersection behaviors.
Arguments:
bvh: The BVH acceleration structureray: The ray to test for intersectionshit_callback: Function called when primitive is tested. Signature: hitcallback(primitive, ray) -> (continuetraversal::Bool, ray::AbstractRay, results::Any)
Returns:
- The final result from the hit_callback