use crate::camera64::DVec3;
use crate::geometry3d::TEXTURED_VERTEX_STRIDE;
use crate::scene3d::Geometry3D;
#[derive(Debug, Clone, Default)]
pub struct MeshBuilder {
vertices: Vec<f32>,
indices: Vec<u32>,
origin: DVec3,
}
impl MeshBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(vertices: usize, triangles: usize) -> Self {
Self {
vertices: Vec::with_capacity(vertices * TEXTURED_VERTEX_STRIDE),
indices: Vec::with_capacity(triangles * 3),
origin: DVec3::ZERO,
}
}
pub fn set_origin(&mut self, origin: DVec3) {
self.origin = origin;
}
pub fn clear(&mut self) {
self.vertices.clear();
self.indices.clear();
}
pub fn vertex_capacity(&self) -> usize {
self.vertices.capacity()
}
pub fn index_capacity(&self) -> usize {
self.indices.capacity()
}
pub fn vertex_count(&self) -> usize {
self.vertices.len() / TEXTURED_VERTEX_STRIDE
}
pub fn triangle_count(&self) -> usize {
self.indices.len() / 3
}
pub fn index_count(&self) -> usize {
self.indices.len()
}
pub fn is_empty(&self) -> bool {
self.indices.is_empty()
}
pub fn push_vertex(&mut self, position: DVec3, normal: [f32; 3], uv: (f32, f32)) -> u32 {
let local = position.sub(self.origin);
self.vertices.push(local.x as f32);
self.vertices.push(local.y as f32);
self.vertices.push(local.z as f32);
self.vertices.extend_from_slice(&normal);
self.vertices.push(uv.0);
self.vertices.push(uv.1);
(self.vertex_count() - 1) as u32
}
pub fn push_triangle(&mut self, a: u32, b: u32, c: u32) {
if a == b || b == c || a == c {
return;
}
self.indices.extend_from_slice(&[a, b, c]);
}
pub fn push_quad(&mut self, a: u32, b: u32, c: u32, d: u32) {
self.push_triangle(a, b, c);
self.push_triangle(a, c, d);
}
pub fn push_grid(
&mut self,
heights: &[DVec3],
cols: usize,
rows: usize,
uv_rect: (f32, f32, f32, f32),
) {
if cols < 2 || rows < 2 || heights.len() != cols * rows {
return;
}
let (u0, v0, u1, v1) = uv_rect;
let base = self.vertex_count() as u32;
for row in 0..rows {
let v = v0 + (v1 - v0) * (row as f32 / (rows - 1) as f32);
for col in 0..cols {
let u = u0 + (u1 - u0) * (col as f32 / (cols - 1) as f32);
let position = heights[row * cols + col];
self.push_vertex(position, [0.0, 0.0, 1.0], (u, v));
}
}
for row in 0..rows - 1 {
for col in 0..cols - 1 {
let i = base + (row * cols + col) as u32;
let right = i + 1;
let down = i + cols as u32;
let down_right = down + 1;
self.push_quad(i, right, down_right, down);
}
}
}
pub fn push_grid_with_normals(
&mut self,
positions: &[DVec3],
normals: &[[f32; 3]],
cols: usize,
rows: usize,
uv_rect: (f32, f32, f32, f32),
) {
if cols < 2 || rows < 2 || positions.len() != cols * rows || normals.len() != cols * rows {
return;
}
let (u0, v0, u1, v1) = uv_rect;
let base = self.vertex_count() as u32;
for row in 0..rows {
let v = v0 + (v1 - v0) * (row as f32 / (rows - 1) as f32);
for col in 0..cols {
let u = u0 + (u1 - u0) * (col as f32 / (cols - 1) as f32);
let index = row * cols + col;
self.push_vertex(positions[index], normals[index], (u, v));
}
}
for row in 0..rows - 1 {
for col in 0..cols - 1 {
let i = base + (row * cols + col) as u32;
let right = i + 1;
let down = i + cols as u32;
let down_right = down + 1;
self.push_quad(i, right, down_right, down);
}
}
}
pub fn push_wall(&mut self, top: &[DVec3], bottom: &[DVec3], uv_rect: (f32, f32, f32, f32)) {
if top.len() < 2 || top.len() != bottom.len() {
return;
}
let (u0, v0, u1, v1) = uv_rect;
let segments = top.len() - 1;
for segment in 0..segments {
let t0 = segment as f32 / segments as f32;
let t1 = (segment + 1) as f32 / segments as f32;
let ua = u0 + (u1 - u0) * t0;
let ub = u0 + (u1 - u0) * t1;
let a = self.push_vertex(top[segment], [0.0, 0.0, 1.0], (ua, v1));
let b = self.push_vertex(top[segment + 1], [0.0, 0.0, 1.0], (ub, v1));
let c = self.push_vertex(bottom[segment + 1], [0.0, 0.0, 1.0], (ub, v0));
let d = self.push_vertex(bottom[segment], [0.0, 0.0, 1.0], (ua, v0));
self.push_quad(a, b, c, d);
}
}
pub fn push_skirt(&mut self, top: &[DVec3], bottom: &[DVec3], uvs: &[(f32, f32)]) {
if top.len() < 2 || top.len() != bottom.len() || top.len() != uvs.len() {
return;
}
let segments = top.len() - 1;
for i in 0..segments {
let edge = top[i + 1].sub(top[i]);
let down = bottom[i].sub(top[i]);
let cross = down.cross(edge);
let normal = if cross.length() > 1e-9 {
cross.normalize().as_f32()
} else {
[0.0, 0.0, 1.0]
};
let a = self.push_vertex(top[i], normal, uvs[i]);
let b = self.push_vertex(bottom[i], normal, uvs[i]);
let c = self.push_vertex(bottom[i + 1], normal, uvs[i + 1]);
let d = self.push_vertex(top[i + 1], normal, uvs[i + 1]);
self.push_quad(a, b, c, d);
}
}
#[allow(clippy::needless_range_loop)]
pub fn push_grid_perimeter_skirts(
&mut self,
grid_positions: &[DVec3],
cols: usize,
rows: usize,
skirt_depth: f64,
uv_rect: (f32, f32, f32, f32),
) {
if cols < 2 || rows < 2 || grid_positions.len() != cols * rows || skirt_depth <= 0.0 {
return;
}
let (u0, v0, u1, v1) = uv_rect;
let get_down = |pos: DVec3| -> DVec3 {
let len = pos.length();
if len > 1000.0 {
pos.scale(-skirt_depth / len)
} else {
DVec3::new(0.0, 0.0, -skirt_depth)
}
};
let mut south_top = Vec::with_capacity(cols);
let mut south_bot = Vec::with_capacity(cols);
let mut south_uvs = Vec::with_capacity(cols);
for col in 0..cols {
let pos = grid_positions[col];
let u = u0 + (u1 - u0) * (col as f32 / (cols - 1) as f32);
south_top.push(pos);
south_bot.push(pos.add(get_down(pos)));
south_uvs.push((u, v0));
}
self.push_skirt(&south_top, &south_bot, &south_uvs);
let mut east_top = Vec::with_capacity(rows);
let mut east_bot = Vec::with_capacity(rows);
let mut east_uvs = Vec::with_capacity(rows);
for row in 0..rows {
let pos = grid_positions[row * cols + (cols - 1)];
let v = v0 + (v1 - v0) * (row as f32 / (rows - 1) as f32);
east_top.push(pos);
east_bot.push(pos.add(get_down(pos)));
east_uvs.push((u1, v));
}
self.push_skirt(&east_top, &east_bot, &east_uvs);
let mut north_top = Vec::with_capacity(cols);
let mut north_bot = Vec::with_capacity(cols);
let mut north_uvs = Vec::with_capacity(cols);
for col in (0..cols).rev() {
let pos = grid_positions[(rows - 1) * cols + col];
let u = u0 + (u1 - u0) * (col as f32 / (cols - 1) as f32);
north_top.push(pos);
north_bot.push(pos.add(get_down(pos)));
north_uvs.push((u, v1));
}
self.push_skirt(&north_top, &north_bot, &north_uvs);
let mut west_top = Vec::with_capacity(rows);
let mut west_bot = Vec::with_capacity(rows);
let mut west_uvs = Vec::with_capacity(rows);
for row in (0..rows).rev() {
let pos = grid_positions[row * cols];
let v = v0 + (v1 - v0) * (row as f32 / (rows - 1) as f32);
west_top.push(pos);
west_bot.push(pos.add(get_down(pos)));
west_uvs.push((u0, v));
}
self.push_skirt(&west_top, &west_bot, &west_uvs);
}
pub fn push_building(
&mut self,
base: DVec3,
half: (f64, f64),
height: f64,
facade_uv: (f32, f32, f32, f32),
roof_uv: (f32, f32, f32, f32),
) {
if half.0 <= 0.0 || half.1 <= 0.0 || height <= 0.0 {
return;
}
let (hx, hz) = half;
let corners = [
base.add(DVec3::new(-hx, -hz, 0.0)),
base.add(DVec3::new(hx, -hz, 0.0)),
base.add(DVec3::new(hx, hz, 0.0)),
base.add(DVec3::new(-hx, hz, 0.0)),
];
let top: Vec<DVec3> = corners
.iter()
.map(|c| c.add(DVec3::new(0.0, 0.0, height)))
.collect();
let (u0, v0, u1, v1) = facade_uv;
let normals = [
[0.0, -1.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[-1.0, 0.0, 0.0],
];
for side in 0..4 {
let next = (side + 1) % 4;
let normal = normals[side];
let a = self.push_vertex(corners[side], normal, (u0, v0));
let b = self.push_vertex(corners[next], normal, (u1, v0));
let c = self.push_vertex(top[next], normal, (u1, v1));
let d = self.push_vertex(top[side], normal, (u0, v1));
self.push_quad(a, b, c, d);
}
let (ru0, rv0, ru1, rv1) = roof_uv;
let r0 = self.push_vertex(top[0], [0.0, 0.0, 1.0], (ru0, rv1));
let r1 = self.push_vertex(top[1], [0.0, 0.0, 1.0], (ru1, rv1));
let r2 = self.push_vertex(top[2], [0.0, 0.0, 1.0], (ru1, rv0));
let r3 = self.push_vertex(top[3], [0.0, 0.0, 1.0], (ru0, rv0));
self.push_quad(r0, r1, r2, r3);
}
pub fn finish(&self) -> Option<Geometry3D> {
if self.indices.is_empty() {
return None;
}
Some(Geometry3D::CustomTextured {
vertices: self.vertices.clone(),
indices: self.indices.clone(),
})
}
pub fn push_rte_vertex(
&mut self,
position: DVec3,
normal: [f32; 3],
uv: (f32, f32),
) -> (u32, [f32; 3], [f32; 3]) {
let (high, low) = position.split_high_low();
let idx = self.push_vertex(position, normal, uv);
(idx, high, low)
}
}
#[derive(Debug, Clone, Default)]
pub struct RteMeshBuilder {
vertices: Vec<f32>,
indices: Vec<u32>,
}
impl RteMeshBuilder {
pub const STRIDE: usize = 11;
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(vertex_count: usize, triangle_count: usize) -> Self {
Self {
vertices: Vec::with_capacity(vertex_count * Self::STRIDE),
indices: Vec::with_capacity(triangle_count * 3),
}
}
pub fn clear(&mut self) {
self.vertices.clear();
self.indices.clear();
}
pub fn vertex_count(&self) -> usize {
self.vertices.len() / Self::STRIDE
}
pub fn triangle_count(&self) -> usize {
self.indices.len() / 3
}
pub fn push_vertex(&mut self, position: DVec3, normal: [f32; 3], uv: (f32, f32)) -> u32 {
let (high, low) = position.split_high_low();
self.vertices.extend_from_slice(&high);
self.vertices.extend_from_slice(&low);
self.vertices.extend_from_slice(&normal);
self.vertices.push(uv.0);
self.vertices.push(uv.1);
(self.vertex_count() - 1) as u32
}
pub fn push_triangle(&mut self, a: u32, b: u32, c: u32) {
if a == b || b == c || a == c {
return;
}
self.indices.extend_from_slice(&[a, b, c]);
}
pub fn push_quad(&mut self, a: u32, b: u32, c: u32, d: u32) {
self.push_triangle(a, b, c);
self.push_triangle(a, c, d);
}
pub fn vertices(&self) -> &[f32] {
&self.vertices
}
pub fn indices(&self) -> &[u32] {
&self.indices
}
}