2020-05-28 01:37:59 +02:00
|
|
|
//! Draw geometry using meshes of triangles.
|
2022-11-10 14:43:38 -08:00
|
|
|
use crate::Color;
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
|
use crate::Gradient;
|
2022-11-03 05:50:53 +01:00
|
|
|
|
2020-11-10 20:11:23 +01:00
|
|
|
use bytemuck::{Pod, Zeroable};
|
2020-05-28 01:37:59 +02:00
|
|
|
|
2020-05-19 17:15:44 +02:00
|
|
|
/// A set of [`Vertex2D`] and indices representing a list of triangles.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
pub struct Mesh2D {
|
|
|
|
|
/// The vertices of the mesh
|
|
|
|
|
pub vertices: Vec<Vertex2D>,
|
|
|
|
|
/// The list of vertex indices that defines the triangles of the mesh.
|
2022-10-18 15:18:37 -07:00
|
|
|
///
|
|
|
|
|
/// Therefore, this list should always have a length that is a multiple of 3.
|
2020-05-19 17:15:44 +02:00
|
|
|
pub indices: Vec<u32>,
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 10:52:58 -07:00
|
|
|
/// A two-dimensional vertex.
|
2020-11-10 20:11:23 +01:00
|
|
|
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
|
2020-05-19 17:15:44 +02:00
|
|
|
#[repr(C)]
|
|
|
|
|
pub struct Vertex2D {
|
2022-09-29 10:52:58 -07:00
|
|
|
/// The vertex position in 2D space.
|
2020-05-19 17:15:44 +02:00
|
|
|
pub position: [f32; 2],
|
2022-10-06 07:28:05 -07:00
|
|
|
}
|
2022-11-03 05:50:53 +01:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
/// Supported shaders for triangle primitives.
|
|
|
|
|
pub enum Style {
|
|
|
|
|
/// Fill a primitive with a solid color.
|
|
|
|
|
Solid(Color),
|
2022-11-10 14:43:38 -08:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2022-11-03 05:50:53 +01:00
|
|
|
/// Fill a primitive with an interpolated color.
|
|
|
|
|
Gradient(Gradient),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Color> for Style {
|
|
|
|
|
fn from(color: Color) -> Self {
|
|
|
|
|
Self::Solid(color)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-10 14:43:38 -08:00
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2022-11-03 05:50:53 +01:00
|
|
|
impl From<Gradient> for Style {
|
|
|
|
|
fn from(gradient: Gradient) -> Self {
|
|
|
|
|
Self::Gradient(gradient)
|
|
|
|
|
}
|
|
|
|
|
}
|