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
use crate::{
    material::domains::surface::SurfaceDomain,
    math::*,
    mesh::{
        geometry::{
            Geometry, GeometryPrimitives, GeometryTriangle, GeometryVertices,
            GeometryVerticesColumn,
        },
        vertex_factory::StaticVertexFactory,
        MeshError,
    },
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct SurfaceTriangle2dVertex {
    pub position: vek::Vec2<f32>,
    pub texture_coord: vek::Vec2<f32>,
    pub color: vek::Vec4<f32>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SurfaceTriangles2dFactory {
    pub triangles: Vec<[SurfaceTriangle2dVertex; 3]>,
}

impl SurfaceTriangles2dFactory {
    pub fn geometry(&self) -> Result<Geometry, MeshError> {
        Ok(Geometry::new(
            GeometryVertices::default().with_columns([
                GeometryVerticesColumn::new(
                    "position",
                    self.triangles
                        .iter()
                        .flat_map(|[a, b, c]| [a.position, b.position, c.position])
                        .collect(),
                ),
                GeometryVerticesColumn::new(
                    "textureCoord",
                    self.triangles
                        .iter()
                        .flat_map(|[a, b, c]| [a.texture_coord, b.texture_coord, c.texture_coord])
                        .collect(),
                ),
                GeometryVerticesColumn::new(
                    "color",
                    self.triangles
                        .iter()
                        .flat_map(|[a, b, c]| [a.color, b.color, c.color])
                        .collect(),
                ),
            ])?,
            GeometryPrimitives::triangles(
                (0..self.triangles.len())
                    .map(|i| {
                        let i = i * 3;
                        GeometryTriangle::new([i, i + 1, i + 2])
                    })
                    .collect::<Vec<_>>(),
            ),
        ))
    }

    pub fn factory<T>(&self) -> Result<StaticVertexFactory, MeshError>
    where
        T: SurfaceDomain,
    {
        self.geometry()?.factory::<T>()
    }
}