summaryrefslogtreecommitdiffstats
path: root/graphics/src/layer/mesh.rs
blob: a2232a1f23775639852548a77d2bbd92af8095d5 (plain) (blame)
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
//! A collection of triangle primitives.

use crate::gradient::Gradient;
use crate::{triangle, Color, Point, Rectangle};

/// A mesh of triangles.
#[derive(Debug, Clone, Copy)]
pub struct Mesh<'a> {
    /// The origin of the vertices of the [`Mesh`].
    pub origin: Point,

    /// The vertex and index buffers of the [`Mesh`].
    pub buffers: &'a triangle::Mesh2D,

    /// The clipping bounds of the [`Mesh`].
    pub clip_bounds: Rectangle<f32>,

    /// The shader of the [`Mesh`].
    pub style: &'a Style,
}

#[derive(Debug, Clone, PartialEq)]
/// Supported shaders for primitives.
pub enum Style {
    /// Fill a primitive with a solid color.
    Solid(Color),
    /// Fill a primitive with an interpolated color.
    Gradient(Gradient),
}

impl<'a> Into<Style> for Gradient {
    fn into(self) -> Style {
        match self {
            Gradient::Linear(linear) => {
                Style::Gradient(Gradient::Linear(linear))
            }
        }
    }
}

/// Returns the number of total vertices & total indices of all [`Mesh`]es.
pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> (usize, usize) {
    meshes
        .iter()
        .map(|Mesh { buffers, .. }| {
            (buffers.vertices.len(), buffers.indices.len())
        })
        .fold((0, 0), |(total_v, total_i), (v, i)| {
            (total_v + v, total_i + i)
        })
}