diff options
author | 2023-03-01 21:34:26 +0100 | |
---|---|---|
committer | 2023-03-01 21:34:26 +0100 | |
commit | 5fd5d1cdf8e5354788dc40729c4565ef377d3bba (patch) | |
tree | 0921efc7dc13a3050e03482147a791f85515f1f2 /wgpu/src/layer/mesh.rs | |
parent | 3f6e28fa9b1b8d911f765c9efb5249a9e0c942d5 (diff) | |
download | iced-5fd5d1cdf8e5354788dc40729c4565ef377d3bba.tar.gz iced-5fd5d1cdf8e5354788dc40729c4565ef377d3bba.tar.bz2 iced-5fd5d1cdf8e5354788dc40729c4565ef377d3bba.zip |
Implement `Canvas` support for `iced_tiny_skia`
Diffstat (limited to 'wgpu/src/layer/mesh.rs')
-rw-r--r-- | wgpu/src/layer/mesh.rs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/wgpu/src/layer/mesh.rs b/wgpu/src/layer/mesh.rs new file mode 100644 index 00000000..5c1e41ad --- /dev/null +++ b/wgpu/src/layer/mesh.rs @@ -0,0 +1,93 @@ +//! A collection of triangle primitives. +use crate::primitive; +use crate::{Gradient, Point, Rectangle}; + +/// A mesh of triangles. +#[derive(Debug, Clone, Copy)] +pub enum Mesh<'a> { + /// A mesh of triangles with a solid color. + Solid { + /// The origin of the vertices of the [`Mesh`]. + origin: Point, + + /// The vertex and index buffers of the [`Mesh`]. + buffers: &'a primitive::Mesh2D<primitive::ColoredVertex2D>, + + /// The clipping bounds of the [`Mesh`]. + clip_bounds: Rectangle<f32>, + }, + /// A mesh of triangles with a gradient color. + Gradient { + /// The origin of the vertices of the [`Mesh`]. + origin: Point, + + /// The vertex and index buffers of the [`Mesh`]. + buffers: &'a primitive::Mesh2D<primitive::Vertex2D>, + + /// The clipping bounds of the [`Mesh`]. + clip_bounds: Rectangle<f32>, + + /// The gradient to apply to the [`Mesh`]. + gradient: &'a Gradient, + }, +} + +impl Mesh<'_> { + /// Returns the origin of the [`Mesh`]. + pub fn origin(&self) -> Point { + match self { + Self::Solid { origin, .. } | Self::Gradient { origin, .. } => { + *origin + } + } + } + + /// 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 gradient vertices. + pub gradient_vertices: 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.solid_vertices += buffers.vertices.len(); + count.indices += buffers.indices.len(); + } + Mesh::Gradient { buffers, .. } => { + count.gradient_vertices += buffers.vertices.len(); + count.indices += buffers.indices.len(); + } + } + + count + }) +} |