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
82
83
84
85
86
87
88
89
90
91
92
93
94
use rfw_backend::*;
use rfw_math::*;

mod quad;
pub use quad::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Mesh2D {
    pub vertices: Vec<Vertex2D>,
    pub tex_id: Option<usize>,
}

impl Default for Mesh2D {
    fn default() -> Self {
        Self {
            vertices: Vec::new(),
            tex_id: None,
        }
    }
}

impl Mesh2D {
    pub fn new(
        vertices: Vec<[f32; 3]>,
        uvs: Vec<[f32; 2]>,
        tex_id: Option<usize>,
        color: [f32; 4],
    ) -> Self {
        let uvs = if !uvs.is_empty() {
            assert_eq!(vertices.len(), uvs.len());
            uvs
        } else {
            vec![[0.0; 2]; vertices.len()]
        };

        let tex = if let Some(id) = tex_id { id as u32 } else { 0 };
        let vertices = vertices
            .iter()
            .zip(uvs.iter())
            .map(|(v, t)| Vertex2D {
                vertex: *v,
                tex,
                uv: *t,
                color,
            })
            .collect();

        Self { vertices, tex_id }
    }

    pub fn set_tex_id(&mut self, id: u32) {
        self.vertices.iter_mut().for_each(|v| {
            v.tex = id;
        });
    }

    pub fn set_color(&mut self, color: [f32; 4]) {
        self.vertices.iter_mut().for_each(|v| {
            v.color = color;
        });
    }
}

impl From<Vec<Vertex2D>> for Mesh2D {
    fn from(vec: Vec<Vertex2D>) -> Self {
        Self {
            vertices: vec,
            tex_id: None,
        }
    }
}

impl From<&[Vertex2D]> for Mesh2D {
    fn from(vec: &[Vertex2D]) -> Self {
        Self {
            vertices: vec.to_vec(),
            tex_id: None,
        }
    }
}

pub trait ToMesh2D {
    fn into_mesh_2d(self) -> Mesh2D;
}

impl ToMesh2D for Mesh2D {
    fn into_mesh_2d(self) -> Mesh2D {
        self
    }
}