2 releases
new 0.1.0-beta.1 | Dec 20, 2024 |
---|---|
0.1.0-beta.0 | Dec 15, 2024 |
#204 in Graphics APIs
104 downloads per month
55KB
340 lines
tinybvh-rs
Rust wrapper for tinybvh.
Features
Provides BVH (Bounding Volume Hierarchy) construction and intersection:
For more information about each layout: tinybvh.
Examples
BVH Wald
use tinybvh_rs::{BVH, Intersector, Ray};
let primitives = vec![
[-2.0, 1.0, -1.0, 0.0], //
[-1.0, 1.0, -1.0, 0.0], // Left triangle
[-2.0, 0.0, -1.0, 0.0], //
[2.0, 1.0, -1.0, 0.0], //
[2.0, 0.0, -1.0, 0.0], // Right triangle
[1.0, 0.0, -1.0, 0.0], //
];
let bvh = BVH::new(&primitives);
// No intersection, ray pass between the primitives
let mut ray = Ray::new([0.0, 0.0, 0.0], [0.0, 0.0, -1.0]);
bvh.intersect(&mut ray);
println!("Hit distance: {}", ray.hit.t); // 1e30
// Intersects left primitive
let mut ray = Ray::new([-1.5, 0.5, 0.0], [0.0, 0.0, -1.0]);
bvh.intersect(&mut ray);
println!("Hit distance & primtive: {} / {}", ray.hit.t, ray.hit.prim); // 1.0 / 0
// Intersects right primitive
let mut ray = Ray::new([1.5, 0.45, 0.0], [0.0, 0.0, -1.0]);
bvh.intersect(&mut ray);
println!("Hit distance & primtive: {} / {}", ray.hit.t, ray.hit.prim); // 1.0 / 1
Strided
If the vertices position are strided (located in a Vertex
struct for instance),
you can enable the strided
feature and use:
use tinybvh_rs::{BVH, Intersector, Ray};
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
position: [f32; 4],
normal: [f32; 3],
}
let vertices = [
Vertex {
position: [-1.0, 1.0, -1.0, 0.0],
normal: [0.0, 0.0, 1.0]
},
Vertex {
position: [-0.5, 1.0, -1.0, 0.0],
normal: [0.0, 0.0, 1.0]
},
Vertex {
position: [-1.0, 0.0, -1.0, 0.0],
normal: [0.0, 0.0, 1.0]
},
];
let positions = pas::slice_attr!(vertices, [0].position);
let bvh = BVH::new_strided(&positions);
Dependencies
~0.7–2.3MB
~36K SLoC