rootvg_mesh/primitive/
solid.rs1use bytemuck::{Pod, Zeroable};
3use std::rc::Rc;
4
5use rootvg_core::color::PackedSrgb;
6use rootvg_core::math::{Angle, Point, Transform, Vector};
7
8use super::{Indexed, MeshUniforms};
9
10#[derive(Debug, Clone, PartialEq)]
12pub struct SolidMesh {
13 pub buffers: Indexed<SolidVertex2D>,
15}
16
17impl SolidMesh {
18 pub fn new() -> Self {
19 Self {
20 buffers: Indexed::new(),
21 }
22 }
23}
24
25#[derive(Copy, Clone, Debug, PartialEq, Zeroable, Pod)]
27#[repr(C)]
28pub struct SolidVertex2D {
29 pub position: [f32; 2],
31
32 pub color: PackedSrgb,
34}
35
36impl SolidVertex2D {
37 pub fn new(position: impl Into<[f32; 2]>, color: impl Into<PackedSrgb>) -> Self {
38 Self {
39 position: position.into(),
40 color: color.into(),
41 }
42 }
43}
44
45#[derive(Debug)]
46pub struct SolidMeshPrimitive {
47 pub mesh: Rc<SolidMesh>,
48 pub uniform: MeshUniforms,
49}
50
51impl SolidMeshPrimitive {
52 pub fn new(mesh: &Rc<SolidMesh>) -> Self {
53 Self {
54 mesh: Rc::clone(mesh),
55 uniform: MeshUniforms::default(),
56 }
57 }
58
59 pub fn new_with_offset(mesh: &Rc<SolidMesh>, offset: Point) -> Self {
60 Self {
61 mesh: Rc::clone(mesh),
62 uniform: MeshUniforms {
63 offset: offset.into(),
64 ..Default::default()
65 },
66 }
67 }
68
69 pub fn new_with_rotation(
70 mesh: &Rc<SolidMesh>,
71 angle: Angle,
72 rotation_origin: Point,
73 offset: Point,
74 ) -> Self {
75 let transform = Transform::translation(-rotation_origin.x, -rotation_origin.y)
76 .then_rotate(angle)
77 .then_translate(Vector::new(rotation_origin.x, rotation_origin.y));
78
79 Self::new_with_transform(mesh, offset, transform)
80 }
81
82 pub fn new_with_transform(mesh: &Rc<SolidMesh>, offset: Point, transform: Transform) -> Self {
83 Self {
84 mesh: Rc::clone(mesh),
85 uniform: MeshUniforms::new(offset, Some(transform)),
86 }
87 }
88}
89
90impl Clone for SolidMeshPrimitive {
91 fn clone(&self) -> Self {
92 Self {
93 mesh: Rc::clone(&self.mesh),
94 uniform: self.uniform,
95 }
96 }
97}
98
99impl PartialEq for SolidMeshPrimitive {
100 fn eq(&self, other: &Self) -> bool {
101 Rc::ptr_eq(&self.mesh, &other.mesh) && self.uniform == other.uniform
102 }
103}