feat: bound scene visibility rendering and continuous native input
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
//! Immutable world-space BVH. Camera changes reuse it; it contains no VM data.
|
||||
use super::{
|
||||
Mesh,
|
||||
math::{Basis, Camera, Ray, Vec3},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Bounds {
|
||||
pub min: Vec3,
|
||||
pub max: Vec3,
|
||||
}
|
||||
impl Bounds {
|
||||
fn mesh(mesh: &Mesh) -> Option<Self> {
|
||||
let mut points = mesh
|
||||
.geometry
|
||||
.vertices
|
||||
.iter()
|
||||
.map(|point| mesh.world.point(*point));
|
||||
let first = points.next()?;
|
||||
Some(points.fold(
|
||||
Self {
|
||||
min: first,
|
||||
max: first,
|
||||
},
|
||||
|bounds, point| Self {
|
||||
min: Vec3::new(
|
||||
bounds.min.x.min(point.x),
|
||||
bounds.min.y.min(point.y),
|
||||
bounds.min.z.min(point.z),
|
||||
),
|
||||
max: Vec3::new(
|
||||
bounds.max.x.max(point.x),
|
||||
bounds.max.y.max(point.y),
|
||||
bounds.max.z.max(point.z),
|
||||
),
|
||||
},
|
||||
))
|
||||
}
|
||||
fn union(self, other: Self) -> Self {
|
||||
Self {
|
||||
min: Vec3::new(
|
||||
self.min.x.min(other.min.x),
|
||||
self.min.y.min(other.min.y),
|
||||
self.min.z.min(other.min.z),
|
||||
),
|
||||
max: Vec3::new(
|
||||
self.max.x.max(other.max.x),
|
||||
self.max.y.max(other.max.y),
|
||||
self.max.z.max(other.max.z),
|
||||
),
|
||||
}
|
||||
}
|
||||
fn center(self, axis: usize) -> f32 {
|
||||
let min = [self.min.x, self.min.y, self.min.z];
|
||||
let max = [self.max.x, self.max.y, self.max.z];
|
||||
(min[axis] + max[axis]) / 2.
|
||||
}
|
||||
fn ray(self, ray: Ray) -> Option<f32> {
|
||||
let mut near = 0f32;
|
||||
let mut far = f32::INFINITY;
|
||||
for (origin, direction, min, max) in [
|
||||
(ray.origin.x, ray.direction.x, self.min.x, self.max.x),
|
||||
(ray.origin.y, ray.direction.y, self.min.y, self.max.y),
|
||||
(ray.origin.z, ray.direction.z, self.min.z, self.max.z),
|
||||
] {
|
||||
if direction.abs() < 1e-12 {
|
||||
if origin < min || origin > max {
|
||||
return None;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let a = (min - origin) / direction;
|
||||
let b = (max - origin) / direction;
|
||||
near = near.max(a.min(b));
|
||||
far = far.min(a.max(b));
|
||||
if far < near {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
Some(near)
|
||||
}
|
||||
fn visible(self, camera: Camera, basis: Basis, planes: &[[f32; 4]; 6]) -> bool {
|
||||
let corners: [Vec3; 8] = std::array::from_fn(|index| {
|
||||
camera.view(
|
||||
basis,
|
||||
Vec3::new(
|
||||
if index & 1 == 0 {
|
||||
self.min.x
|
||||
} else {
|
||||
self.max.x
|
||||
},
|
||||
if index & 2 == 0 {
|
||||
self.min.y
|
||||
} else {
|
||||
self.max.y
|
||||
},
|
||||
if index & 4 == 0 {
|
||||
self.min.z
|
||||
} else {
|
||||
self.max.z
|
||||
},
|
||||
),
|
||||
)
|
||||
});
|
||||
!planes.iter().any(|[a, b, c, d]| {
|
||||
corners
|
||||
.iter()
|
||||
.all(|point| a * point.x + b * point.y + c * point.z + d < 0.)
|
||||
})
|
||||
}
|
||||
}
|
||||
#[derive(Debug)]
|
||||
enum Node {
|
||||
Leaf {
|
||||
bounds: Bounds,
|
||||
items: Vec<usize>,
|
||||
},
|
||||
Branch {
|
||||
bounds: Bounds,
|
||||
left: usize,
|
||||
right: usize,
|
||||
},
|
||||
}
|
||||
impl Node {
|
||||
fn bounds(&self) -> Bounds {
|
||||
match self {
|
||||
Self::Leaf { bounds, .. } | Self::Branch { bounds, .. } => *bounds,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Index {
|
||||
bounds: Vec<Option<Bounds>>,
|
||||
nodes: Vec<Node>,
|
||||
root: Option<usize>,
|
||||
}
|
||||
impl Index {
|
||||
pub fn build(meshes: &[Mesh]) -> Self {
|
||||
let bounds: Vec<_> = meshes.iter().map(Bounds::mesh).collect();
|
||||
let mut items: Vec<_> = bounds
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(index, bounds)| bounds.map(|_| index))
|
||||
.collect();
|
||||
let mut index = Self {
|
||||
bounds,
|
||||
nodes: Vec::new(),
|
||||
root: None,
|
||||
};
|
||||
if !items.is_empty() {
|
||||
index.root = Some(index.split(&mut items));
|
||||
}
|
||||
index
|
||||
}
|
||||
fn split(&mut self, items: &mut [usize]) -> usize {
|
||||
let bounds = items
|
||||
.iter()
|
||||
.map(|&index| self.bounds[index].unwrap())
|
||||
.reduce(Bounds::union)
|
||||
.unwrap();
|
||||
let index = self.nodes.len();
|
||||
self.nodes.push(Node::Leaf {
|
||||
bounds,
|
||||
items: Vec::new(),
|
||||
});
|
||||
if items.len() <= 4 {
|
||||
self.nodes[index] = Node::Leaf {
|
||||
bounds,
|
||||
items: items.to_vec(),
|
||||
};
|
||||
} else {
|
||||
let extent = bounds.max - bounds.min;
|
||||
let axis = if extent.x >= extent.y && extent.x >= extent.z {
|
||||
0
|
||||
} else if extent.y >= extent.z {
|
||||
1
|
||||
} else {
|
||||
2
|
||||
};
|
||||
items.sort_unstable_by(|&a, &b| {
|
||||
self.bounds[a]
|
||||
.unwrap()
|
||||
.center(axis)
|
||||
.total_cmp(&self.bounds[b].unwrap().center(axis))
|
||||
});
|
||||
let mid = items.len() / 2;
|
||||
let (left, right) = items.split_at_mut(mid);
|
||||
let left = self.split(left);
|
||||
let right = self.split(right);
|
||||
self.nodes[index] = Node::Branch {
|
||||
bounds,
|
||||
left,
|
||||
right,
|
||||
};
|
||||
}
|
||||
index
|
||||
}
|
||||
pub fn matches(&self, meshes: usize) -> bool {
|
||||
self.bounds.len() == meshes
|
||||
}
|
||||
pub fn ray(&self, ray: Ray) -> Vec<(f32, usize)> {
|
||||
let mut stack: Vec<_> = self.root.into_iter().collect();
|
||||
let mut output = Vec::new();
|
||||
while let Some(index) = stack.pop() {
|
||||
if self.nodes[index].bounds().ray(ray).is_none() {
|
||||
continue;
|
||||
}
|
||||
match &self.nodes[index] {
|
||||
Node::Leaf { items, .. } => {
|
||||
for &item in items {
|
||||
if let Some(distance) = self.bounds[item].unwrap().ray(ray) {
|
||||
output.push((distance, item));
|
||||
}
|
||||
}
|
||||
}
|
||||
Node::Branch { left, right, .. } => {
|
||||
stack.push(*left);
|
||||
stack.push(*right);
|
||||
}
|
||||
}
|
||||
}
|
||||
output.sort_unstable_by(|a, b| a.0.total_cmp(&b.0).then(a.1.cmp(&b.1)));
|
||||
output
|
||||
}
|
||||
pub fn visible(&self, camera: Camera, aspect: f32) -> Vec<usize> {
|
||||
let Some(basis) = camera.basis() else {
|
||||
return Vec::new();
|
||||
};
|
||||
let planes = super::frustum_planes(camera, aspect);
|
||||
let mut stack: Vec<_> = self.root.into_iter().collect();
|
||||
let mut output = Vec::new();
|
||||
while let Some(index) = stack.pop() {
|
||||
if !self.nodes[index].bounds().visible(camera, basis, &planes) {
|
||||
continue;
|
||||
}
|
||||
match &self.nodes[index] {
|
||||
Node::Leaf { items, .. } => output.extend(
|
||||
items
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|item| self.bounds[*item].unwrap().visible(camera, basis, &planes)),
|
||||
),
|
||||
Node::Branch { left, right, .. } => {
|
||||
stack.push(*left);
|
||||
stack.push(*right);
|
||||
}
|
||||
}
|
||||
}
|
||||
output.sort_unstable();
|
||||
output
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user