1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use rfw_backend::Vertex2D;
use rfw_math::*;

use crate::{Mesh2D, ToMesh2D};

#[derive(Debug, Clone)]
pub struct Quad2D {
    pub bottom_left: Vec2,
    pub top_right: Vec2,
    pub layer: f32,
    pub texture: Option<usize>,
    pub color: Vec4,
    pub bottom_left_uv: Vec2,
    pub top_right_uv: Vec2,
}

impl Default for Quad2D {
    fn default() -> Self {
        Self {
            bottom_left: Vec2::new(-0.5, -0.5),
            top_right: Vec2::new(0.5, 0.5),
            layer: 0.0,
            texture: None,
            color: Vec4::ONE,
            bottom_left_uv: Vec2::ZERO,
            top_right_uv: Vec2::ONE,
        }
    }
}

impl ToMesh2D for Quad2D {
    fn into_mesh_2d(self) -> crate::Mesh2D {
        let mut vertices = Vec::with_capacity(6);
        let tex = self.texture.unwrap_or(0) as u32;
        vertices.push(Vertex2D {
            vertex: self.bottom_left.extend(self.layer).into(),
            tex,
            uv: self.bottom_left_uv.into(),
            color: self.color.into(),
        });
        vertices.push(Vertex2D {
            vertex: Vec2::new(self.top_right.x, self.bottom_left.y)
                .extend(self.layer)
                .into(),
            tex,
            uv: Vec2::new(self.top_right_uv.x, self.bottom_left_uv.y).into(),
            color: self.color.into(),
        });
        vertices.push(Vertex2D {
            vertex: self.top_right.extend(self.layer).into(),
            tex,
            uv: self.top_right_uv.into(),
            color: self.color.into(),
        });
        vertices.push(Vertex2D {
            vertex: self.bottom_left.extend(self.layer).into(),
            tex,
            uv: self.bottom_left_uv.into(),
            color: self.color.into(),
        });
        vertices.push(Vertex2D {
            vertex: self.top_right.extend(self.layer).into(),
            tex,
            uv: self.top_right_uv.into(),
            color: self.color.into(),
        });
        vertices.push(Vertex2D {
            vertex: Vec2::new(self.bottom_left.x, self.top_right.y)
                .extend(self.layer)
                .into(),
            tex,
            uv: Vec2::new(self.bottom_left_uv.x, self.top_right_uv.y).into(),
            color: self.color.into(),
        });

        Mesh2D {
            vertices,
            tex_id: self.texture,
        }
    }
}