use std::{error::Error, fmt, sync::Arc};
use crate::{Color, Vec3};
mod attributes;
mod environment;
mod material;
mod style;
#[cfg(test)]
mod tests;
mod uv;
pub use attributes::Mesh3dAttributes;
pub use environment::{
AmbientLight3d, DirectionalLight3d, Fog3d, Fog3dError, Lighting3d, Lighting3dError,
SurfaceLighting3d,
};
pub use material::{SurfaceAlphaMode3d, SurfaceSidedness3d, SurfaceStyle3d};
pub use style::{Mesh3dStyleError, MeshStyle3d, WireframeStyle3d};
pub use uv::{TextureAddressMode3d, TextureUvTransform3d, TextureUvTransformError};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct MeshEdge3d {
start: u32,
end: u32,
}
impl MeshEdge3d {
pub fn new(start: u32, end: u32) -> Result<Self, Mesh3dError> {
if start == end {
return Err(Mesh3dError::DegenerateDisplayEdge { start, end });
}
Ok(Self { start, end })
}
pub const fn start(self) -> u32 {
self.start
}
pub const fn end(self) -> u32 {
self.end
}
fn canonical(self) -> (u32, u32) {
if self.start < self.end {
(self.start, self.end)
} else {
(self.end, self.start)
}
}
}
#[derive(Debug, PartialEq)]
struct Mesh3dStorage {
vertices: Vec<Vec3>,
triangle_indices: Vec<u32>,
display_edges: Vec<MeshEdge3d>,
texture_coordinates: Vec<TextureCoordinate2d>,
vertex_colors: Vec<Color>,
normals: Vec<Vec3>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextureCoordinate2d {
u: f32,
v: f32,
}
impl TextureCoordinate2d {
pub fn new(u: f32, v: f32) -> Result<Self, Mesh3dError> {
if !u.is_finite()
|| !v.is_finite()
|| !(0.0..=1.0).contains(&u)
|| !(0.0..=1.0).contains(&v)
{
return Err(Mesh3dError::InvalidTextureCoordinate);
}
Ok(Self { u, v })
}
pub const fn u(self) -> f32 {
self.u
}
pub const fn v(self) -> f32 {
self.v
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Mesh3d {
storage: Arc<Mesh3dStorage>,
bounds_min: Vec3,
bounds_max: Vec3,
minimum_nonzero_components: [f32; 3],
}
impl Mesh3d {
pub fn new(vertices: Vec<Vec3>, triangle_indices: Vec<u32>) -> Result<Self, Mesh3dError> {
Self::with_display_edges(vertices, triangle_indices, Vec::new())
}
pub fn with_display_edges(
vertices: Vec<Vec3>,
triangle_indices: Vec<u32>,
display_edges: Vec<MeshEdge3d>,
) -> Result<Self, Mesh3dError> {
Self::with_attributes(
vertices,
triangle_indices,
display_edges,
Mesh3dAttributes::new(),
)
}
pub fn textured(
vertices: Vec<Vec3>,
texture_coordinates: Vec<TextureCoordinate2d>,
triangle_indices: Vec<u32>,
display_edges: Vec<MeshEdge3d>,
) -> Result<Self, Mesh3dError> {
if texture_coordinates.len() != vertices.len() {
return Err(Mesh3dError::TextureCoordinateCountMismatch {
vertex_count: vertices.len(),
coordinate_count: texture_coordinates.len(),
});
}
Self::with_attributes(
vertices,
triangle_indices,
display_edges,
Mesh3dAttributes::new().with_texture_coordinates(texture_coordinates),
)
}
pub fn with_attributes(
vertices: Vec<Vec3>,
triangle_indices: Vec<u32>,
display_edges: Vec<MeshEdge3d>,
attributes: Mesh3dAttributes,
) -> Result<Self, Mesh3dError> {
if let Some(coordinates) = &attributes.texture_coordinates
&& coordinates.len() != vertices.len()
{
return Err(Mesh3dError::TextureCoordinateCountMismatch {
vertex_count: vertices.len(),
coordinate_count: coordinates.len(),
});
}
if let Some(colors) = &attributes.vertex_colors
&& colors.len() != vertices.len()
{
return Err(Mesh3dError::VertexColorCountMismatch {
vertex_count: vertices.len(),
color_count: colors.len(),
});
}
if let Some(normals) = &attributes.normals
&& normals.len() != vertices.len()
{
return Err(Mesh3dError::NormalCountMismatch {
vertex_count: vertices.len(),
normal_count: normals.len(),
});
}
if vertices.is_empty() {
return Err(Mesh3dError::EmptyVertices);
}
if triangle_indices.is_empty() && display_edges.is_empty() {
return Err(Mesh3dError::EmptyGeometry);
}
if !triangle_indices.len().is_multiple_of(3) {
return Err(Mesh3dError::InvalidTriangleIndexCount {
index_count: triangle_indices.len(),
});
}
for (triangle, indices) in triangle_indices.chunks_exact(3).enumerate() {
let [first, second, third] = [indices[0], indices[1], indices[2]];
validate_index(first, vertices.len())?;
validate_index(second, vertices.len())?;
validate_index(third, vertices.len())?;
if first == second
|| second == third
|| first == third
|| triangle_is_degenerate(
vertices[first as usize],
vertices[second as usize],
vertices[third as usize],
)
{
return Err(Mesh3dError::DegenerateTriangle { triangle });
}
}
for edge in &display_edges {
validate_index(edge.start, vertices.len())?;
validate_index(edge.end, vertices.len())?;
if vertices[edge.start as usize] == vertices[edge.end as usize] {
return Err(Mesh3dError::DegenerateDisplayEdge {
start: edge.start,
end: edge.end,
});
}
}
let canonical_bytes = display_edges
.len()
.checked_mul(std::mem::size_of::<((u32, u32), usize)>())
.ok_or(Mesh3dError::AllocationFailed {
requested_bytes: usize::MAX,
})?;
let mut canonical_edges = Vec::new();
canonical_edges
.try_reserve_exact(display_edges.len())
.map_err(|_| Mesh3dError::AllocationFailed {
requested_bytes: canonical_bytes,
})?;
for (index, edge) in display_edges.iter().enumerate() {
canonical_edges.push((edge.canonical(), index));
}
canonical_edges.sort_unstable_by_key(|entry| entry.0);
if let Some(pair) = canonical_edges
.windows(2)
.find(|pair| pair[0].0 == pair[1].0)
{
let duplicate = display_edges[pair[0].1.max(pair[1].1)];
return Err(Mesh3dError::DuplicateDisplayEdge {
start: duplicate.start,
end: duplicate.end,
});
}
let (bounds_min, bounds_max, minimum_nonzero_components) = mesh_bounds(&vertices);
Ok(Self {
storage: Arc::new(Mesh3dStorage {
vertices,
triangle_indices,
display_edges,
texture_coordinates: attributes.texture_coordinates.unwrap_or_default(),
vertex_colors: attributes.vertex_colors.unwrap_or_default(),
normals: attributes.normals.unwrap_or_default(),
}),
bounds_min,
bounds_max,
minimum_nonzero_components,
})
}
pub fn vertices(&self) -> &[Vec3] {
&self.storage.vertices
}
pub fn triangle_indices(&self) -> &[u32] {
&self.storage.triangle_indices
}
pub fn display_edges(&self) -> &[MeshEdge3d] {
&self.storage.display_edges
}
pub fn texture_coordinates(&self) -> &[TextureCoordinate2d] {
&self.storage.texture_coordinates
}
pub fn vertex_colors(&self) -> &[Color] {
&self.storage.vertex_colors
}
pub fn normals(&self) -> &[Vec3] {
&self.storage.normals
}
pub fn triangle_count(&self) -> usize {
self.storage.triangle_indices.len() / 3
}
pub const fn bounds_min(&self) -> Vec3 {
self.bounds_min
}
pub const fn bounds_max(&self) -> Vec3 {
self.bounds_max
}
#[cfg(feature = "wgpu")]
pub(crate) const fn minimum_nonzero_components(&self) -> [f32; 3] {
self.minimum_nonzero_components
}
pub fn recovery_memory_bytes(&self) -> usize {
self.storage
.vertices
.capacity()
.saturating_mul(std::mem::size_of::<Vec3>())
.saturating_add(
self.storage
.triangle_indices
.capacity()
.saturating_mul(std::mem::size_of::<u32>()),
)
.saturating_add(
self.storage
.display_edges
.capacity()
.saturating_mul(std::mem::size_of::<MeshEdge3d>()),
)
.saturating_add(
self.storage
.texture_coordinates
.capacity()
.saturating_mul(std::mem::size_of::<TextureCoordinate2d>()),
)
.saturating_add(
self.storage
.vertex_colors
.capacity()
.saturating_mul(std::mem::size_of::<Color>()),
)
.saturating_add(
self.storage
.normals
.capacity()
.saturating_mul(std::mem::size_of::<Vec3>()),
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mesh3dError {
InvalidVertexNormal {
vertex_index: usize,
},
NormalCountMismatch {
vertex_count: usize,
normal_count: usize,
},
InvalidVertexColor {
vertex_index: usize,
},
VertexColorCountMismatch {
vertex_count: usize,
color_count: usize,
},
InvalidTextureCoordinate,
TextureCoordinateCountMismatch {
vertex_count: usize,
coordinate_count: usize,
},
EmptyVertices,
EmptyGeometry,
InvalidTriangleIndexCount {
index_count: usize,
},
IndexOutOfBounds {
index: u32,
vertex_count: usize,
},
DegenerateTriangle {
triangle: usize,
},
DegenerateDisplayEdge {
start: u32,
end: u32,
},
DuplicateDisplayEdge {
start: u32,
end: u32,
},
AllocationFailed {
requested_bytes: usize,
},
}
impl fmt::Display for Mesh3dError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidVertexNormal { vertex_index } => write!(
formatter,
"3D vertex {vertex_index} normal must be a nonzero finite direction"
),
Self::NormalCountMismatch {
vertex_count,
normal_count,
} => write!(
formatter,
"{vertex_count} mesh vertices require matching normals, got {normal_count}"
),
Self::InvalidVertexColor { vertex_index } => write!(
formatter,
"3D vertex {vertex_index} color must be finite straight-linear RGBA in 0..=1"
),
Self::VertexColorCountMismatch {
vertex_count,
color_count,
} => write!(
formatter,
"{vertex_count} mesh vertices require matching colors, got {color_count}"
),
Self::InvalidTextureCoordinate => {
write!(formatter, "texture coordinates must be finite in 0..=1")
}
Self::TextureCoordinateCountMismatch {
vertex_count,
coordinate_count,
} => write!(
formatter,
"{vertex_count} mesh vertices require matching UVs, got {coordinate_count}"
),
Self::EmptyVertices => write!(formatter, "3D mesh requires at least one vertex"),
Self::EmptyGeometry => {
write!(formatter, "3D mesh requires triangles or display edges")
}
Self::InvalidTriangleIndexCount { index_count } => write!(
formatter,
"3D mesh triangle index count, when present, must be divisible by three, got {index_count}"
),
Self::IndexOutOfBounds {
index,
vertex_count,
} => write!(
formatter,
"3D mesh index {index} is outside {vertex_count} vertices"
),
Self::DegenerateTriangle { triangle } => {
write!(formatter, "3D mesh triangle {triangle} has zero area")
}
Self::DegenerateDisplayEdge { start, end } => {
write!(formatter, "3D display edge {start}-{end} has zero length")
}
Self::DuplicateDisplayEdge { start, end } => {
write!(formatter, "3D display edge {start}-{end} is duplicated")
}
Self::AllocationFailed { requested_bytes } => write!(
formatter,
"could not reserve {requested_bytes} bytes for 3D mesh validation"
),
}
}
}
impl Error for Mesh3dError {}
fn validate_index(index: u32, vertex_count: usize) -> Result<(), Mesh3dError> {
((index as u64) < vertex_count as u64)
.then_some(())
.ok_or(Mesh3dError::IndexOutOfBounds {
index,
vertex_count,
})
}
fn triangle_is_degenerate(first: Vec3, second: Vec3, third: Vec3) -> bool {
let ab = (
second.x() as f64 - first.x() as f64,
second.y() as f64 - first.y() as f64,
second.z() as f64 - first.z() as f64,
);
let ac = (
third.x() as f64 - first.x() as f64,
third.y() as f64 - first.y() as f64,
third.z() as f64 - first.z() as f64,
);
let cross = (
ab.1 * ac.2 - ab.2 * ac.1,
ab.2 * ac.0 - ab.0 * ac.2,
ab.0 * ac.1 - ab.1 * ac.0,
);
cross.0 == 0.0 && cross.1 == 0.0 && cross.2 == 0.0
}
fn mesh_bounds(vertices: &[Vec3]) -> (Vec3, Vec3, [f32; 3]) {
let first = vertices[0];
let mut minimum = [first.x(), first.y(), first.z()];
let mut maximum = minimum;
let mut minimum_nonzero = [f32::INFINITY; 3];
for vertex in vertices {
let components = [vertex.x(), vertex.y(), vertex.z()];
for axis in 0..3 {
minimum[axis] = minimum[axis].min(components[axis]);
maximum[axis] = maximum[axis].max(components[axis]);
if components[axis] != 0.0 {
minimum_nonzero[axis] = minimum_nonzero[axis].min(components[axis].abs());
}
}
}
(
Vec3::new_unchecked(minimum[0], minimum[1], minimum[2]),
Vec3::new_unchecked(maximum[0], maximum[1], maximum[2]),
minimum_nonzero,
)
}