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.

BVH Basics

BVH Hit tests

Ray Tracing Tutorial

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

Ray Tracing

Ray Tracing with Raycore

View Factors Analysis

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

View Factors

View Factors and More

Overview

Raycore.RayIntersectionSessionType
RayIntersectionSession{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 trace
  • bvh::BVHAccel: BVH acceleration structure to intersect against
  • hit_function::F: Function to use for intersection testing (e.g., closest_hit or any_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
end
source
Raycore.any_hitFunction
any_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 found
  • hit_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
source
Raycore.closest_hitMethod
closest_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 found
  • hit_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
source
Raycore.hit_countMethod
hit_count(session::RayIntersectionSession)

Count the number of rays that hit geometry in the session.

source
Raycore.hit_distancesMethod
hit_distances(session::RayIntersectionSession)

Extract all hit distances from a RayIntersectionSession.

Returns a vector of Float32 containing distances for all rays that intersected geometry.

source
Raycore.hit_pointsMethod
hit_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.

source
Raycore.miss_countMethod
miss_count(session::RayIntersectionSession)

Count the number of rays that missed all geometry in the session.

source

Private Functions

Raycore.cos_θMethod

The 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.

source
Raycore.generate_ray_gridMethod
generate_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.

source
Raycore.maximum_extentMethod

Return index of the longest axis. Useful for deciding which axis to subdivide, when building ray-tracing acceleration structures.

1 - x, 2 - y, 3 - z.

source
Raycore.traverse_bvhMethod
traverse_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 structure
  • ray: The ray to test for intersections
  • hit_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
source