summaryrefslogtreecommitdiffstats
path: root/wgpu/src/layer/mesh.rs
blob: 5ed7c6544cb1601104271c0cba1f5625b2adf8d2 (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
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
95
96
97
//! A collection of triangle primitives.
use crate::core::{Rectangle, Transformation};
use crate::graphics::mesh;

/// A mesh of triangles.
#[derive(Debug, Clone, Copy)]
pub enum Mesh<'a> {
    /// A mesh of triangles with a solid color.
    Solid {
        /// The [`Transformation`] for the vertices of the [`Mesh`].
        transformation: Transformation,

        /// The vertex and index buffers of the [`Mesh`].
        buffers: &'a mesh::Indexed<mesh::SolidVertex2D>,

        /// The clipping bounds of the [`Mesh`].
        clip_bounds: Rectangle<f32>,
    },
    /// A mesh of triangles with a gradient color.
    Gradient {
        /// The [`Transformation`] for the vertices of the [`Mesh`].
        transformation: Transformation,

        /// The vertex and index buffers of the [`Mesh`].
        buffers: &'a mesh::Indexed<mesh::GradientVertex2D>,

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

impl Mesh<'_> {
    /// Returns the origin of the [`Mesh`].
    pub fn transformation(&self) -> Transformation {
        match self {
            Self::Solid { transformation, .. }
            | Self::Gradient { transformation, .. } => *transformation,
        }
    }

    /// Returns the indices of the [`Mesh`].
    pub fn indices(&self) -> &[u32] {
        match self {
            Self::Solid { buffers, .. } => &buffers.indices,
            Self::Gradient { buffers, .. } => &buffers.indices,
        }
    }

    /// Returns the clip bounds of the [`Mesh`].
    pub fn clip_bounds(&self) -> Rectangle<f32> {
        match self {
            Self::Solid { clip_bounds, .. }
            | Self::Gradient { clip_bounds, .. } => *clip_bounds,
        }
    }
}

/// The result of counting the attributes of a set of meshes.
#[derive(Debug, Clone, Copy, Default)]
pub struct AttributeCount {
    /// The total amount of solid vertices.
    pub solid_vertices: usize,

    /// The total amount of solid meshes.
    pub solids: usize,

    /// The total amount of gradient vertices.
    pub gradient_vertices: usize,

    /// The total amount of gradient meshes.
    pub gradients: usize,

    /// The total amount of indices.
    pub indices: usize,
}

/// Returns the number of total vertices & total indices of all [`Mesh`]es.
pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> AttributeCount {
    meshes
        .iter()
        .fold(AttributeCount::default(), |mut count, mesh| {
            match mesh {
                Mesh::Solid { buffers, .. } => {
                    count.solids += 1;
                    count.solid_vertices += buffers.vertices.len();
                    count.indices += buffers.indices.len();
                }
                Mesh::Gradient { buffers, .. } => {
                    count.gradients += 1;
                    count.gradient_vertices += buffers.vertices.len();
                    count.indices += buffers.indices.len();
                }
            }

            count
        })
}