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(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
40impl PartialEq for Vertex {
41    fn eq(&self, other: &Self) -> bool {
42        self.position == other.position
43            && self.color == other.color
44            && self.texcoord == other.texcoord
45    }
46}
47
48impl Eq for Vertex {}
49
50impl From<((f32, f32, f32), (f32, f32, f32, f32), (f32, f32))> for Vertex {
51    fn from(data: ((f32, f32, f32), (f32, f32, f32, f32), (f32, f32))) -> Self {
52        Self {
53            position: Vector3::new(data.0.0, data.0.1, data.0.2),
54            color: Color::new_const(data.1.0, data.1.1, data.1.2, data.1.3),
55            texcoord: Vector2::new(data.2.0, data.2.1),
56        }
57    }
58}
59
60impl From<(Vector3, Color, Vector2)> for Vertex {
61    fn from(data: (Vector3, Color, Vector2)) -> Self {
62        Self {
63            position: data.0,
64            color: data.1,
65            texcoord: data.2,
66        }
67    }
68}
69
70impl From<((f32, f32), (f32, f32, f32, f32), (f32, f32))> for Vertex {
71    fn from(data: ((f32, f32), (f32, f32, f32, f32), (f32, f32))) -> Self {
72        Self {
73            position: Vector3::new(data.0.0, data.0.1, 0.0),
74            color: Color::new_const(data.1.0, data.1.1, data.1.2, data.1.3),
75            texcoord: Vector2::new(data.2.0, data.2.1),
76        }
77    }
78}
79
80impl From<(Vector2, Color, Vector2)> for Vertex {
81    fn from(data: (Vector2, Color, Vector2)) -> Self {
82        Self {
83            position: data.0.into(),
84            color: data.1,
85            texcoord: data.2,
86        }
87    }
88}
89
90impl From<[f32; 8]> for Vertex {
91    fn from(data: [f32; 8]) -> Self {
92        Self {
93            position: Vector3::new(data[0], data[1], data[2]),
94            color: Color::new_const(data[3], data[4], data[5], data[6]),
95            texcoord: Vector2::new(data[7], 0.0),
96        }
97    }
98}
99
100impl From<[f32; 6]> for Vertex {
101    fn from(data: [f32; 6]) -> Self {
102        Self {
103            position: Vector3::new(data[0], data[1], 0.0),
104            color: Color::new_const(data[2], data[3], data[4], 1.0),
105            texcoord: Vector2::new(data[5], 0.0),
106        }
107    }
108}