use crate::core::{BufferAttribute, BufferGeometry};
use crate::math::{Color, Matrix4, Vector3};
use std::collections::HashMap;
#[derive(Debug, Clone, Default)]
pub struct MeshBuilder {
positions: Vec<Vector3>,
normals: Vec<Option<Vector3>>,
uvs: Vec<[f32; 2]>,
colors: Vec<Option<[f32; 3]>>,
triangles: Vec<[u32; 3]>,
next_normal: Option<Vector3>,
next_uv: [f32; 2],
next_color: Option<[f32; 3]>,
side: Option<Vector3>,
}
impl MeshBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn normal(&mut self, normal: Vector3) -> &mut Self {
self.next_normal = Some(normal.normalize());
self
}
pub fn auto_normal(&mut self) -> &mut Self {
self.next_normal = None;
self
}
pub fn uv(&mut self, u: f32, v: f32) -> &mut Self {
self.next_uv = [u, v];
self
}
pub fn color(&mut self, color: Color) -> &mut Self {
self.next_color = Some([color.r, color.g, color.b]);
self
}
pub fn facing(&mut self, side: Vector3) -> &mut Self {
self.side = Some(side);
self
}
pub fn as_wound(&mut self) -> &mut Self {
self.side = None;
self
}
pub fn vertex(&mut self, position: Vector3) -> u32 {
self.positions.push(position);
self.normals.push(self.next_normal);
self.uvs.push(self.next_uv);
self.colors.push(self.next_color);
self.positions.len() as u32 - 1
}
pub fn vertex_count(&self) -> usize {
self.positions.len()
}
pub fn triangle_count(&self) -> usize {
self.triangles.len()
}
pub fn triangle(&mut self, a: u32, b: u32, c: u32) -> &mut Self {
let count = self.positions.len() as u32;
for index in [a, b, c] {
assert!(
index < count,
"vertex {index} does not exist: there are {count}"
);
}
let (p, q, r) = (
self.positions[a as usize],
self.positions[b as usize],
self.positions[c as usize],
);
let backwards = self
.side
.is_some_and(|side| (q - p).cross(r - p).dot(side) < 0.0);
self.triangles
.push(if backwards { [a, c, b] } else { [a, b, c] });
self
}
pub fn quad(&mut self, a: u32, b: u32, c: u32, d: u32) -> &mut Self {
self.triangle(a, b, c).triangle(a, c, d)
}
pub fn ribbon(
&mut self,
left: &[Vector3],
right: &[Vector3],
side: Vector3,
closed: bool,
) -> &mut Self {
assert_eq!(left.len(), right.len(), "a ribbon's edges must pair up");
let keep = self.side;
self.facing(side);
let mut along = 0.0;
let first = self.positions.len() as u32;
for (i, (l, r)) in left.iter().zip(right).enumerate() {
if i > 0 {
along += l.distance_to(left[i - 1]);
}
self.uv(0.0, along);
self.vertex(*l);
self.uv(1.0, along);
self.vertex(*r);
}
let count = left.len() as u32;
for i in 0..count.saturating_sub(1) {
let at = first + i * 2;
self.quad(at, at + 1, at + 3, at + 2);
}
if closed && count > 2 {
let last = first + (count - 1) * 2;
self.quad(last, last + 1, first + 1, first);
}
self.side = keep;
self
}
pub fn grid(
&mut self,
width: f32,
depth: f32,
columns: u32,
rows: u32,
mut height: impl FnMut(f32, f32) -> f32,
) -> &mut Self {
let keep = self.side;
self.facing(Vector3::UP);
let (columns, rows) = (columns.max(1), rows.max(1));
let first = self.positions.len() as u32;
for row in 0..=rows {
for column in 0..=columns {
let (u, v) = (column as f32 / columns as f32, row as f32 / rows as f32);
let (x, z) = ((u - 0.5) * width, (v - 0.5) * depth);
self.uv(u, v);
self.vertex(Vector3::new(x, height(x, z), z));
}
}
let stride = columns + 1;
for row in 0..rows {
for column in 0..columns {
let at = first + row * stride + column;
self.quad(at, at + 1, at + stride + 1, at + stride);
}
}
self.side = keep;
self
}
pub fn append(&mut self, geometry: &BufferGeometry, transform: &Matrix4) -> &mut Self {
let Some(positions) = geometry.get_attribute("position") else {
return self;
};
let normals = geometry.get_attribute("normal");
let uvs = geometry.get_attribute("uv");
let colors = geometry.get_attribute("color");
let e = &transform.elements;
let cofactor = |x: Vector3| {
let (c0, c1, c2) = (
Vector3::new(e[0], e[1], e[2]),
Vector3::new(e[4], e[5], e[6]),
Vector3::new(e[8], e[9], e[10]),
);
(c1.cross(c2) * x.x + c2.cross(c0) * x.y + c0.cross(c1) * x.z).normalize()
};
let mirrored = {
let (c0, c1, c2) = (
Vector3::new(e[0], e[1], e[2]),
Vector3::new(e[4], e[5], e[6]),
Vector3::new(e[8], e[9], e[10]),
);
c0.cross(c1).dot(c2) < 0.0
};
let sign = if mirrored { -1.0 } else { 1.0 };
let first = self.positions.len() as u32;
let read = |attribute: &BufferAttribute, i: usize, size: usize| -> [f32; 3] {
let at = i * attribute.item_size;
let mut out = [0.0; 3];
out[..size].copy_from_slice(&attribute.array[at..at + size]);
out
};
for i in 0..positions.count() {
let [x, y, z] = read(positions, i, 3);
self.positions
.push(Vector3::new(x, y, z).apply_matrix4(transform));
self.normals.push(normals.map(|n| {
let [x, y, z] = read(n, i, 3);
cofactor(Vector3::new(x, y, z)) * sign
}));
self.uvs.push(uvs.map_or([0.0; 2], |uv| {
let [u, v, _] = read(uv, i, 2);
[u, v]
}));
self.colors
.push(colors.map(|c| read(c, i, 3)).or(self.next_color));
}
for t in 0..geometry.triangle_count() {
let [a, b, c] = geometry.triangle(t).map(|index| index + first);
self.triangles
.push(if mirrored { [a, c, b] } else { [a, b, c] });
}
self
}
pub fn build(&self) -> BufferGeometry {
let mut geometry = self.assemble(&self.smooth_normals());
geometry.compute_bounding_box();
geometry.compute_bounding_sphere();
geometry
}
pub fn build_flat(&self) -> BufferGeometry {
let mut flat = MeshBuilder::new();
for [a, b, c] in &self.triangles {
let [a, b, c] = [*a as usize, *b as usize, *c as usize];
let (p, q, r) = (self.positions[a], self.positions[b], self.positions[c]);
let normal = (q - p).cross(r - p).normalize();
for i in [a, b, c] {
flat.positions.push(self.positions[i]);
flat.normals.push(Some(normal));
flat.uvs.push(self.uvs[i]);
flat.colors.push(self.colors[i]);
}
let at = flat.positions.len() as u32;
flat.triangles.push([at - 3, at - 2, at - 1]);
}
flat.build()
}
fn smooth_normals(&self) -> Vec<Vector3> {
let mut sums = vec![Vector3::ZERO; self.positions.len()];
for &[a, b, c] in &self.triangles {
let [a, b, c] = [a as usize, b as usize, c as usize];
let face = (self.positions[b] - self.positions[a])
.cross(self.positions[c] - self.positions[a]);
sums[a] = sums[a] + face;
sums[b] = sums[b] + face;
sums[c] = sums[c] + face;
}
sums.iter()
.zip(&self.normals)
.map(|(sum, set)| match set {
Some(normal) => *normal,
None if sum.length() > 0.0 => sum.normalize(),
None => Vector3::UP,
})
.collect()
}
fn assemble(&self, normals: &[Vector3]) -> BufferGeometry {
let flatten = |vectors: &[Vector3]| {
vectors
.iter()
.flat_map(|v| [v.x, v.y, v.z])
.collect::<Vec<f32>>()
};
let mut geometry = BufferGeometry::new();
geometry.set_attribute(
"position",
BufferAttribute::new(flatten(&self.positions), 3),
);
geometry.set_attribute("normal", BufferAttribute::new(flatten(normals), 3));
let uvs = self.uvs.iter().flatten().copied().collect();
geometry.set_attribute("uv", BufferAttribute::new(uvs, 2));
if self.colors.iter().any(Option::is_some) {
let colors = self
.colors
.iter()
.flat_map(|c| c.unwrap_or([1.0; 3]))
.collect();
geometry.set_attribute("color", BufferAttribute::new(colors, 3));
}
geometry.set_index(self.triangles.iter().flatten().copied().collect());
geometry
}
}
impl From<MeshBuilder> for BufferGeometry {
fn from(builder: MeshBuilder) -> Self {
builder.build()
}
}
impl From<&MeshBuilder> for BufferGeometry {
fn from(builder: &MeshBuilder) -> Self {
builder.build()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct MeshReport {
pub triangles: usize,
pub degenerate: usize,
pub open_edges: usize,
pub flipped_edges: usize,
pub crowded_edges: usize,
}
impl MeshReport {
pub fn of(geometry: &BufferGeometry) -> Self {
let mut report = Self {
triangles: geometry.triangle_count(),
..Self::default()
};
let Some(positions) = geometry.positions() else {
return report;
};
let mut ids = HashMap::new();
let mut welded = Vec::new();
let mut points = Vec::new();
for p in positions {
let key = [p.x.to_bits(), p.y.to_bits(), p.z.to_bits()];
let next = ids.len() as u32;
welded.push(*ids.entry(key).or_insert(next));
points.push(p);
}
let mut edges: HashMap<(u32, u32), (u32, u32)> = HashMap::new();
for t in 0..geometry.triangle_count() {
let [a, b, c] = geometry.triangle(t);
let area = (points[b as usize] - points[a as usize])
.cross(points[c as usize] - points[a as usize])
.length();
if area <= f32::EPSILON {
report.degenerate += 1;
continue;
}
let [a, b, c] = [a, b, c].map(|i| welded[i as usize]);
for (from, to) in [(a, b), (b, c), (c, a)] {
let counts = edges.entry((from.min(to), from.max(to))).or_default();
if from < to {
counts.0 += 1;
} else {
counts.1 += 1;
}
}
}
for &(forward, back) in edges.values() {
match forward + back {
1 => report.open_edges += 1,
2 if forward == 2 || back == 2 => report.flipped_edges += 1,
2 => {}
_ => report.crowded_edges += 1,
}
}
report
}
pub fn is_solid(&self) -> bool {
self.triangles > 0
&& self.degenerate == 0
&& self.open_edges == 0
&& self.flipped_edges == 0
&& self.crowded_edges == 0
}
}