2022-10-05 10:49:58 -07:00
|
|
|
//! A collection of triangle primitives.
|
|
|
|
|
use crate::gradient::Gradient;
|
2022-11-03 04:33:54 +01:00
|
|
|
use crate::{triangle, Color, Point, Rectangle};
|
2022-10-05 10:49:58 -07:00
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 04:33:54 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2022-10-05 10:49:58 -07:00
|
|
|
/// Supported shaders for primitives.
|
|
|
|
|
pub enum Style {
|
|
|
|
|
/// Fill a primitive with a solid color.
|
|
|
|
|
Solid(Color),
|
|
|
|
|
/// Fill a primitive with an interpolated color.
|
2022-11-03 04:33:54 +01:00
|
|
|
Gradient(Gradient),
|
2022-10-05 10:49:58 -07:00
|
|
|
}
|
|
|
|
|
|
2022-11-03 04:53:27 +01:00
|
|
|
impl From<Gradient> for Style {
|
|
|
|
|
fn from(gradient: Gradient) -> Self {
|
|
|
|
|
Self::Gradient(gradient)
|
2022-10-05 10:49:58 -07:00
|
|
|
}
|
2022-10-06 07:28:05 -07:00
|
|
|
}
|
2022-10-07 13:21:32 -07:00
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
|
})
|
|
|
|
|
}
|