summaryrefslogtreecommitdiffstats
path: root/graphics/src/triangle.rs
blob: 8b41bfc4af139c1d98ff23e8d8f5da192fc9d550 (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
//! Draw geometry using meshes of triangles.
use crate::Color;
#[cfg(not(target_arch = "wasm32"))]
use crate::Gradient;

use bytemuck::{Pod, Zeroable};

/// 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.
    ///
    /// Therefore, this list should always have a length that is a multiple of 3.
    pub indices: Vec<u32>,
}

/// A two-dimensional vertex.
#[derive(Copy, Clone, Debug, Zeroable, Pod)]
#[repr(C)]
pub struct Vertex2D {
    /// The vertex position in 2D space.
    pub position: [f32; 2],
}

#[derive(Debug, Clone, PartialEq)]
/// Supported shaders for triangle primitives.
pub enum Style {
    /// Fill a primitive with a solid color.
    Solid(Color),
    #[cfg(not(target_arch = "wasm32"))]
    /// Fill a primitive with an interpolated color.
    Gradient(Gradient),
}

impl From<Color> for Style {
    fn from(color: Color) -> Self {
        Self::Solid(color)
    }
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Gradient> for Style {
    fn from(gradient: Gradient) -> Self {
        Self::Gradient(gradient)
    }
}