use glam::{Mat4, Vec3};
use super::{Geometry, InstanceDesc, SceneDesc};
use crate::cull::Aabb;
use crate::mesh_pool::vertex_positions;
use crcbl_shaders::mesh::VERTEX_STRIDE;
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum SceneBoundsError {
#[error("instance {instance} names mesh {mesh}, which the scene does not have")]
MissingMesh {
instance: usize,
mesh: usize,
},
#[error("instance {instance} names mesh {mesh}, a cluster DAG with no levels")]
NoLevels {
instance: usize,
mesh: usize,
},
#[error("instance {instance} names mesh {mesh}, whose vertex bytes end in a partial vertex")]
PartialVertex {
instance: usize,
mesh: usize,
},
#[error("instance {instance}'s vertex {vertex} is not finite once placed")]
NonFinitePosition {
instance: usize,
vertex: usize,
},
#[error("instance {instance}'s mesh has no vertices")]
EmptyPart {
instance: usize,
},
#[error("the instances have no vertices to bound")]
Empty,
}
impl SceneDesc<'_> {
pub fn instance_bounds(
&self,
instances: &[InstanceDesc],
root: Mat4,
) -> Result<Aabb, SceneBoundsError> {
let mut bounds: Option<Aabb> = None;
for (index, instance) in instances.iter().enumerate() {
if let Some(placed) = self.placed_bounds(index, instance, root)? {
bounds = Some(match bounds {
Some(so_far) => Aabb {
min: so_far.min.min(placed.min),
max: so_far.max.max(placed.max),
},
None => placed,
});
}
}
bounds.ok_or(SceneBoundsError::Empty)
}
pub fn instance_parts(
&self,
instances: &[InstanceDesc],
) -> Result<Vec<Aabb>, SceneBoundsError> {
instances
.iter()
.enumerate()
.map(|(index, instance)| {
self.placed_bounds(index, instance, Mat4::IDENTITY)?
.ok_or(SceneBoundsError::EmptyPart { instance: index })
})
.collect()
}
fn placed_bounds(
&self,
index: usize,
instance: &InstanceDesc,
root: Mat4,
) -> Result<Option<Aabb>, SceneBoundsError> {
let mesh = instance.mesh;
let geometry = &self
.meshes
.get(mesh)
.ok_or(SceneBoundsError::MissingMesh {
instance: index,
mesh,
})?
.geometry;
let vertices = match geometry {
Geometry::Flat { vertices, .. } => vertices.as_ref(),
Geometry::Dag { levels, .. } => levels
.first()
.ok_or(SceneBoundsError::NoLevels {
instance: index,
mesh,
})?
.as_ref(),
};
if vertices.len() % VERTEX_STRIDE != 0 {
return Err(SceneBoundsError::PartialVertex {
instance: index,
mesh,
});
}
let transform = root * instance.transform;
let mut bounds: Option<Aabb> = None;
for (vertex, position) in vertex_positions(vertices).enumerate() {
let point: Vec3 = transform.transform_point3(position);
if !point.is_finite() {
return Err(SceneBoundsError::NonFinitePosition {
instance: index,
vertex,
});
}
bounds = Some(match bounds {
Some(so_far) => Aabb {
min: so_far.min.min(point),
max: so_far.max.max(point),
},
None => Aabb {
min: point,
max: point,
},
});
}
Ok(bounds)
}
}
#[cfg(test)]
mod tests {
use std::borrow::Cow;
use std::f32::consts::{FRAC_1_SQRT_2, FRAC_PI_2, FRAC_PI_4};
use crcbl_phys::{AabbCompound, Ray, Transform};
use crcbl_shaders::mesh::{self, MeshVertex};
use crcbl_shaders::meshlet::MeshClusters;
use crcbl_shaders::vertex::UvRange;
use glam::{DQuat, DVec3};
use super::*;
use crate::scene::{DEMO_CUBE, MeshDesc, demo};
fn cube(transform: Mat4) -> InstanceDesc {
InstanceDesc {
mesh: DEMO_CUBE,
material: 0,
transform,
}
}
fn with_mesh(positions: &[[f32; 3]]) -> (SceneDesc<'static>, usize) {
let vertices: Vec<MeshVertex> = positions
.iter()
.map(|&position| {
MeshVertex::from_normal(
position,
[0.0, 1.0, 0.0],
[1.0; 4],
[0.0; 2],
&UvRange::default(),
)
})
.collect();
let mut scene = demo();
scene.meshes.push(MeshDesc {
label: Cow::Borrowed("test mesh"),
geometry: Geometry::Flat {
vertices: Cow::Owned(mesh::vertex_bytes(&vertices)),
uv_range: UvRange::default(),
indices: Cow::Owned(Vec::new()),
clusters: MeshClusters::default(),
flags: 0,
},
});
let index = scene.meshes.len() - 1;
(scene, index)
}
fn flat_vertices<'s>(scene: &'s mut SceneDesc<'_>, mesh: usize) -> &'s mut Vec<u8> {
let Geometry::Flat { vertices, .. } = &mut scene.meshes[mesh].geometry else {
panic!("mesh {mesh} is flat geometry");
};
vertices.to_mut()
}
fn assert_close(actual: Vec3, expected: Vec3) {
assert!(
actual.abs_diff_eq(expected, 1e-6),
"{actual} is not {expected}"
);
}
#[test]
fn a_turned_instance_is_bounded_by_its_vertices() {
let (scene, octahedron) = with_mesh(&[
[1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, -1.0],
]);
let place = Mat4::from_translation(Vec3::new(1.0, 2.0, 3.0));
let instance = InstanceDesc {
mesh: octahedron,
material: 0,
transform: Mat4::from_rotation_z(FRAC_PI_4),
};
let bounds = scene
.instance_bounds(&[instance], place)
.expect("finite geometry");
assert_close(
bounds.min,
Vec3::new(1.0 - FRAC_1_SQRT_2, 2.0 - FRAC_1_SQRT_2, 2.0),
);
assert_close(
bounds.max,
Vec3::new(1.0 + FRAC_1_SQRT_2, 2.0 + FRAC_1_SQRT_2, 4.0),
);
let loose = Aabb {
min: Vec3::splat(-1.0),
max: Vec3::ONE,
}
.transformed(place * instance.transform);
assert!(
loose.max.x > bounds.max.x + 0.5,
"the turned box of the mesh's box is {loose:?}, not the vertex bound"
);
}
#[test]
fn several_instances_are_bounded_together() {
let scene = demo();
let bounds = scene
.instance_bounds(
&[
cube(Mat4::from_translation(Vec3::new(-2.0, 0.0, 0.0))),
cube(
Mat4::from_translation(Vec3::new(3.0, 4.0, 5.0))
* Mat4::from_scale(Vec3::new(2.0, 1.0, 3.0)),
),
],
Mat4::IDENTITY,
)
.expect("two cubes");
assert_eq!(bounds.min, Vec3::new(-2.5, -0.5, -0.5));
assert_eq!(bounds.max, Vec3::new(4.0, 4.5, 6.5));
}
#[test]
fn a_nan_vertex_is_refused_and_names_its_instance() {
let (mut scene, poisoned) = with_mesh(&[[0.0; 3], [1.0; 3], [2.0; 3]]);
let bytes = flat_vertices(&mut scene, poisoned);
bytes[2 * mesh::VERTEX_STRIDE..2 * mesh::VERTEX_STRIDE + 4]
.copy_from_slice(&f32::NAN.to_le_bytes());
let instances = [
cube(Mat4::IDENTITY),
InstanceDesc {
mesh: poisoned,
material: 0,
transform: Mat4::IDENTITY,
},
];
let expected = SceneBoundsError::NonFinitePosition {
instance: 1,
vertex: 2,
};
assert_eq!(
scene.instance_bounds(&instances, Mat4::IDENTITY),
Err(expected)
);
assert_eq!(scene.instance_parts(&instances), Err(expected));
assert_eq!(
expected.to_string(),
"instance 1's vertex 2 is not finite once placed"
);
}
#[test]
fn a_non_finite_transform_is_refused() {
let scene = demo();
let nan = Mat4::from_translation(Vec3::new(f32::NAN, 0.0, 0.0));
let expected = SceneBoundsError::NonFinitePosition {
instance: 0,
vertex: 0,
};
assert_eq!(
scene.instance_bounds(&[cube(nan)], Mat4::IDENTITY),
Err(expected)
);
assert_eq!(
scene.instance_bounds(&[cube(Mat4::IDENTITY)], nan),
Err(expected)
);
assert_eq!(scene.instance_parts(&[cube(nan)]), Err(expected));
let huge = Mat4::from_scale(Vec3::splat(f32::MAX));
assert_eq!(
scene.instance_bounds(&[cube(huge)], Mat4::from_scale(Vec3::splat(4.0))),
Err(expected)
);
}
#[test]
fn an_empty_set_has_no_bounds_and_no_parts() {
let scene = demo();
assert_eq!(
scene.instance_bounds(&[], Mat4::IDENTITY),
Err(SceneBoundsError::Empty)
);
assert_eq!(scene.instance_parts(&[]), Ok(Vec::new()));
}
#[test]
fn an_empty_mesh_contributes_nothing_and_is_no_part() {
let (scene, empty) = with_mesh(&[]);
let hollow = InstanceDesc {
mesh: empty,
material: 0,
transform: Mat4::IDENTITY,
};
assert_eq!(
scene.instance_bounds(&[hollow], Mat4::IDENTITY),
Err(SceneBoundsError::Empty)
);
assert_eq!(
scene.instance_bounds(&[hollow, cube(Mat4::IDENTITY)], Mat4::IDENTITY),
Ok(Aabb {
min: Vec3::splat(-0.5),
max: Vec3::splat(0.5),
})
);
assert_eq!(
scene.instance_parts(&[cube(Mat4::IDENTITY), hollow]),
Err(SceneBoundsError::EmptyPart { instance: 1 })
);
}
#[test]
fn the_parts_build_a_compound_a_ray_strikes() {
let scene = demo();
let parts = scene
.instance_parts(&[
cube(Mat4::IDENTITY),
cube(Mat4::from_translation(Vec3::new(3.0, 0.0, 0.0))),
])
.expect("two cubes");
let widened: Vec<crcbl_phys::Aabb> = parts
.iter()
.map(|part| crcbl_phys::Aabb::new(part.min.as_dvec3(), part.max.as_dvec3()))
.collect();
let compound = AabbCompound::new(&widened).expect("finite, non-inverted parts");
let pose = Transform::new(
DVec3::new(0.0, 10.0, 0.0),
DQuat::from_rotation_z(core::f64::consts::FRAC_PI_2),
);
let hit = compound
.ray_cast(
&pose,
&Ray::new(DVec3::new(0.0, 11.75, 0.0), DVec3::new(0.0, 1.0, 0.0)),
)
.expect("the ray starts between the parts and runs into the second");
assert_eq!(hit.part, 1);
assert!((hit.hit.point - DVec3::new(0.0, 12.5, 0.0)).length() < 1e-9);
assert!((hit.hit.t - 0.75).abs() < 1e-9);
}
#[test]
fn missing_or_malformed_mesh_geometry_is_refused() {
let scene = demo();
let missing = InstanceDesc {
mesh: scene.meshes.len(),
..cube(Mat4::IDENTITY)
};
assert_eq!(
scene.instance_bounds(&[missing], Mat4::IDENTITY),
Err(SceneBoundsError::MissingMesh {
instance: 0,
mesh: scene.meshes.len(),
})
);
let mut incomplete = demo();
flat_vertices(&mut incomplete, DEMO_CUBE).pop();
assert_eq!(
incomplete.instance_bounds(&[cube(Mat4::IDENTITY)], Mat4::IDENTITY),
Err(SceneBoundsError::PartialVertex {
instance: 0,
mesh: DEMO_CUBE,
})
);
let mut nonfinite = demo();
flat_vertices(&mut nonfinite, DEMO_CUBE)[..4].copy_from_slice(&f32::NAN.to_le_bytes());
assert_eq!(
nonfinite.instance_bounds(&[cube(Mat4::IDENTITY)], Mat4::IDENTITY),
Err(SceneBoundsError::NonFinitePosition {
instance: 0,
vertex: 0,
})
);
}
#[test]
fn a_mesh_with_no_vertices_alone_has_no_bounds() {
let mut scene = demo();
flat_vertices(&mut scene, DEMO_CUBE).clear();
assert_eq!(
scene.instance_bounds(&[cube(Mat4::IDENTITY)], Mat4::IDENTITY),
Err(SceneBoundsError::Empty)
);
}
#[test]
fn a_dag_is_bounded_by_its_finest_level_and_transforms_compose() {
let mut dag_scene = demo();
let dag_mesh = dag_scene
.meshes
.iter()
.position(|mesh| matches!(mesh.geometry, Geometry::Dag { .. }))
.expect("the demo includes a DAG mesh");
let instance = InstanceDesc {
mesh: dag_mesh,
material: 0,
transform: Mat4::IDENTITY,
};
let expected = dag_scene
.instance_bounds(&[instance], Mat4::IDENTITY)
.expect("the dunes have vertices");
{
let Geometry::Dag { levels, .. } = &mut dag_scene.meshes[dag_mesh].geometry else {
panic!("the selected demo mesh is a DAG");
};
assert!(levels.len() > 1);
for vertex in levels[1]
.to_mut()
.as_chunks_mut::<{ mesh::VERTEX_STRIDE }>()
.0
{
vertex[..12].copy_from_slice(
&[10_000.0_f32, 10_000.0, 10_000.0]
.map(f32::to_le_bytes)
.concat(),
);
}
}
assert_eq!(
dag_scene.instance_bounds(&[instance], Mat4::IDENTITY),
Ok(expected)
);
let Geometry::Dag { levels, .. } = &mut dag_scene.meshes[dag_mesh].geometry else {
panic!("the selected demo mesh is a DAG");
};
levels.clear();
assert_eq!(
dag_scene.instance_bounds(&[instance], Mat4::IDENTITY),
Err(SceneBoundsError::NoLevels {
instance: 0,
mesh: dag_mesh,
})
);
let scene = demo();
let root =
Mat4::from_translation(Vec3::new(10.0, 20.0, 30.0)) * Mat4::from_rotation_z(FRAC_PI_2);
let bounds = scene
.instance_bounds(&[cube(Mat4::from_translation(Vec3::X * 2.0))], root)
.expect("a cube");
assert_eq!(bounds.min, Vec3::new(9.5, 21.5, 29.5));
assert_eq!(bounds.max, Vec3::new(10.5, 22.5, 30.5));
}
#[test]
fn parts_follow_each_instance_transform() {
let scene = demo();
let large_coordinate = f32::MAX * 0.75;
let parts = scene
.instance_parts(&[
cube(Mat4::from_translation(Vec3::new(-2.0, 0.0, 0.0))),
cube(
Mat4::from_translation(Vec3::new(3.0, 4.0, 5.0))
* Mat4::from_scale(Vec3::new(2.0, 1.0, 3.0)),
),
cube(Mat4::from_translation(Vec3::splat(large_coordinate))),
cube(
Mat4::from_translation(Vec3::new(7.0, 8.0, 9.0)) * Mat4::from_scale(Vec3::ZERO),
),
])
.expect("four finite cubes");
assert_eq!(
parts,
vec![
Aabb {
min: Vec3::new(-2.5, -0.5, -0.5),
max: Vec3::new(-1.5, 0.5, 0.5),
},
Aabb {
min: Vec3::new(2.0, 3.5, 3.5),
max: Vec3::new(4.0, 4.5, 6.5),
},
Aabb {
min: Vec3::splat(large_coordinate),
max: Vec3::splat(large_coordinate),
},
Aabb {
min: Vec3::new(7.0, 8.0, 9.0),
max: Vec3::new(7.0, 8.0, 9.0),
},
]
);
}
}