use crate::core::{BufferAttribute, BufferGeometry};
use crate::extras::shape_utils;
use crate::extras::Shape;
use crate::math::Vector2;
pub fn shape_geometry_default_shape() -> Shape {
Shape::from_points(&[
Vector2::new(0.0, 0.5),
Vector2::new(-0.5, -0.5),
Vector2::new(0.5, -0.5),
])
}
pub fn shape_geometry(shape: &Shape, curve_segments: usize) -> BufferGeometry {
build(std::slice::from_ref(shape), curve_segments, false)
}
pub fn shape_geometry_multi(shapes: &[Shape], curve_segments: usize) -> BufferGeometry {
build(shapes, curve_segments, true)
}
fn build(shapes: &[Shape], curve_segments: usize, groups: bool) -> BufferGeometry {
let mut geometry = BufferGeometry::new();
let mut indices: Vec<u32> = Vec::new();
let mut vertices: Vec<f32> = Vec::new();
let mut normals: Vec<f32> = Vec::new();
let mut uvs: Vec<f32> = Vec::new();
let mut group_start = 0;
let mut group_count = 0;
for (i, shape) in shapes.iter().enumerate() {
let index_offset = vertices.len() / 3;
let points = shape.extract_points(curve_segments);
let mut shape_vertices = points.shape;
let mut shape_holes = points.holes;
if !shape_utils::is_clock_wise(&shape_vertices) {
shape_vertices.reverse();
}
for shape_hole in shape_holes.iter_mut() {
if shape_utils::is_clock_wise(shape_hole) {
shape_hole.reverse();
}
}
let faces = shape_utils::triangulate_shape(&mut shape_vertices, &mut shape_holes);
for shape_hole in &shape_holes {
shape_vertices.extend_from_slice(shape_hole);
}
for vertex in &shape_vertices {
vertices.extend([vertex.x as f32, vertex.y as f32, 0.0]);
normals.extend([0.0, 0.0, 1.0]);
uvs.extend([vertex.x as f32, vertex.y as f32]); }
for face in &faces {
let a = face[0] + index_offset;
let b = face[1] + index_offset;
let c = face[2] + index_offset;
indices.extend([a as u32, b as u32, c as u32]);
group_count += 3;
}
if groups {
geometry.add_group(group_start, group_count, i); group_start += group_count;
group_count = 0;
}
}
geometry.set_index(&indices);
geometry.set_attribute("position", BufferAttribute::new(vertices, 3));
geometry.set_attribute("normal", BufferAttribute::new(normals, 3));
geometry.set_attribute("uv", BufferAttribute::new(uvs, 2));
geometry
}