Merge branch 'master' into non-uniform-border-radius-for-quads

This commit is contained in:
Héctor Ramón Jiménez 2022-12-02 18:53:21 +01:00
commit 4029a1cdaa
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
147 changed files with 4828 additions and 2184 deletions

View file

@ -1,31 +1,93 @@
//! A collection of triangle primitives.
use crate::triangle;
use crate::{Point, Rectangle};
use crate::{Gradient, 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,
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`].
pub buffers: &'a triangle::Mesh2D,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a triangle::Mesh2D<triangle::ColoredVertex2D>,
/// The clipping bounds of the [`Mesh`].
pub clip_bounds: Rectangle<f32>,
/// 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 shader of the [`Mesh`].
pub style: &'a triangle::Style,
/// The vertex and index buffers of the [`Mesh`].
buffers: &'a triangle::Mesh2D<triangle::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>]) -> (usize, usize) {
pub fn attribute_count_of<'a>(meshes: &'a [Mesh<'a>]) -> AttributeCount {
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)
.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
})
}