est_render/math/
vertex.rs

1use bytemuck::{Pod, Zeroable};
2
3use super::{Color, Vector2, Vector3};
4
5/// To use this vertex struct in your shader, you need to use this WGSL code as your vertex type:
6/// ```wgsl
7/// struct VertexInput {
8///     @location(0) position: vec3<f32>,
9///     @location(1) color: vec4<f32>,
10///     @location(2) texCoord: vec2<f32>,
11/// };
12/// ```
13#[repr(C)]
14#[derive(Debug, Clone, Copy, Default, Pod, Zeroable)]
15pub struct Vertex {
16    pub position: Vector3,
17    pub color: Color,
18    pub texcoord: Vector2,
19}
20
21#[allow(dead_code)]
22impl Vertex {
23    pub fn new(position: Vector3, color: Color, texcoord: Vector2) -> Self {
24        Self {
25            position,
26            color,
27            texcoord,
28        }
29    }
30
31    pub fn new_slice(position: [f32; 3], color: [f32; 4], texcoord: [f32; 2]) -> Self {
32        Self {
33            position: Vector3::new(position[0], position[1], position[2]),
34            color: Color::new_const(color[0], color[1], color[2], color[3]),
35            texcoord: Vector2::new(texcoord[0], texcoord[1]),
36        }
37    }
38
39    pub fn new_slice_raw(arr: &[f32]) -> Self {
40        assert!(arr.len() >= 9, "Array must have at least 9 elements");
41        Self {
42            position: Vector3::new(arr[0], arr[1], arr[2]),
43            color: Color::new_const(arr[3], arr[4], arr[5], arr[6]),
44            texcoord: Vector2::new(arr[7], arr[8]),
45        }
46    }
47}
48
49impl PartialEq for Vertex {
50    fn eq(&self, other: &Self) -> bool {
51        self.position == other.position
52            && self.color == other.color
53            && self.texcoord == other.texcoord
54    }
55}
56
57impl Eq for Vertex {}
58
59impl From<((f32, f32, f32), (f32, f32, f32, f32), (f32, f32))> for Vertex {
60    fn from(data: ((f32, f32, f32), (f32, f32, f32, f32), (f32, f32))) -> Self {
61        Self {
62            position: Vector3::new(data.0.0, data.0.1, data.0.2),
63            color: Color::new_const(data.1.0, data.1.1, data.1.2, data.1.3),
64            texcoord: Vector2::new(data.2.0, data.2.1),
65        }
66    }
67}
68
69impl From<(Vector3, Color, Vector2)> for Vertex {
70    fn from(data: (Vector3, Color, Vector2)) -> Self {
71        Self {
72            position: data.0,
73            color: data.1,
74            texcoord: data.2,
75        }
76    }
77}
78
79impl From<((f32, f32), (f32, f32, f32, f32), (f32, f32))> for Vertex {
80    fn from(data: ((f32, f32), (f32, f32, f32, f32), (f32, f32))) -> Self {
81        Self {
82            position: Vector3::new(data.0.0, data.0.1, 0.0),
83            color: Color::new_const(data.1.0, data.1.1, data.1.2, data.1.3),
84            texcoord: Vector2::new(data.2.0, data.2.1),
85        }
86    }
87}
88
89impl From<(Vector2, Color, Vector2)> for Vertex {
90    fn from(data: (Vector2, Color, Vector2)) -> Self {
91        Self {
92            position: data.0.into(),
93            color: data.1,
94            texcoord: data.2,
95        }
96    }
97}
98
99impl From<[f32; 8]> for Vertex {
100    fn from(data: [f32; 8]) -> Self {
101        Self {
102            position: Vector3::new(data[0], data[1], data[2]),
103            color: Color::new_const(data[3], data[4], data[5], data[6]),
104            texcoord: Vector2::new(data[7], 0.0),
105        }
106    }
107}
108
109impl From<[f32; 6]> for Vertex {
110    fn from(data: [f32; 6]) -> Self {
111        Self {
112            position: Vector3::new(data[0], data[1], 0.0),
113            color: Color::new_const(data[2], data[3], data[4], 1.0),
114            texcoord: Vector2::new(data[5], 0.0),
115        }
116    }
117}