Skip to main content

emblema_core/
vertices.rs

1//! A triangle mesh a caller supplies directly.
2//!
3//! Everything else this canvas draws is a shape it tessellates itself, which
4//! means the triangles are always well formed and the question of what a
5//! caller might hand over does not arise. Here it does, so the checking
6//! happens once at construction rather than at every draw: a mesh that exists
7//! is a mesh that can be drawn.
8
9use crate::Color;
10use emblema_hal::{Error, Result};
11use glam::{Affine2, Vec2};
12
13/// How positions are grouped into triangles.
14///
15/// The two compact forms exist because a strip or a fan states a triangle in
16/// one vertex where a list states it in three, and a caller who has that shape
17/// already should not have to expand it. They are expanded here rather than
18/// carried to a backend, since the difference is an index buffer and both
19/// backends draw indexed triangles.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
21pub enum VertexMode {
22    /// Every three positions are one triangle.
23    #[default]
24    Triangles,
25    /// Each position after the second closes a triangle with the two before
26    /// it, alternating winding so the strip is consistently wound.
27    TriangleStrip,
28    /// Each position after the second closes a triangle with the first and the
29    /// one before it.
30    TriangleFan,
31}
32
33/// A mesh of triangles in user space, with optional texture coordinates.
34///
35/// Texture coordinates run from zero to one across the image and are read per
36/// vertex, which is the point of supplying them: every other way of drawing an
37/// image here maps it by position, and a mesh that wanted that mapping would
38/// not need coordinates at all.
39#[derive(Debug, Clone, PartialEq, Default)]
40pub struct Vertices {
41    positions: Vec<Vec2>,
42    texture_coords: Vec<Vec2>,
43    colors: Vec<Color>,
44    indices: Vec<u32>,
45}
46
47impl Vertices {
48    /// A mesh from positions alone, filled by the paint.
49    ///
50    /// The paint is sampled by position, as it is for any shape: a gradient
51    /// runs across the mesh the way it would across a path covering the same
52    /// area.
53    pub fn new(mode: VertexMode, positions: Vec<Vec2>) -> Result<Self> {
54        Self::build(mode, positions, Vec::new(), Vec::new(), None)
55    }
56
57    /// A mesh whose vertices name where in the paint they read.
58    ///
59    /// Any shader reads there, which is what `dart:ui` means by a color source
60    /// at a mesh's coordinates: an image samples its texture, a gradient
61    /// measures its ramp, and a caller's program receives them as `uv` beside
62    /// the clip position and chooses. This used to say "requires an image
63    /// paint", which was true of neither the last two.
64    pub fn textured(
65        mode: VertexMode,
66        positions: Vec<Vec2>,
67        texture_coords: Vec<Vec2>,
68    ) -> Result<Self> {
69        Self::build(mode, positions, texture_coords, Vec::new(), None)
70    }
71
72    /// A mesh whose vertices each carry a color.
73    ///
74    /// The color multiplies whatever the paint produced, so a white paint
75    /// leaves the mesh's own colors, and a gradient paint is shaded by them.
76    /// Multiplying is the only combination offered: `dart:ui` takes a blend
77    /// mode here, and every other mode either discards one of the two inputs
78    /// or is not expressible without a second value per fragment.
79    pub fn colored(mode: VertexMode, positions: Vec<Vec2>, colors: Vec<Color>) -> Result<Self> {
80        Self::build(mode, positions, Vec::new(), colors, None)
81    }
82
83    /// The same, with the triangles named by index rather than by order.
84    pub fn indexed(
85        mode: VertexMode,
86        positions: Vec<Vec2>,
87        texture_coords: Vec<Vec2>,
88        indices: Vec<u32>,
89    ) -> Result<Self> {
90        Self::build(mode, positions, texture_coords, Vec::new(), Some(indices))
91    }
92
93    /// Everything at once, for a caller who has all of it.
94    pub fn full(
95        mode: VertexMode,
96        positions: Vec<Vec2>,
97        texture_coords: Vec<Vec2>,
98        colors: Vec<Color>,
99        indices: Vec<u32>,
100    ) -> Result<Self> {
101        Self::build(mode, positions, texture_coords, colors, Some(indices))
102    }
103
104    fn build(
105        mode: VertexMode,
106        positions: Vec<Vec2>,
107        texture_coords: Vec<Vec2>,
108        colors: Vec<Color>,
109        indices: Option<Vec<u32>>,
110    ) -> Result<Self> {
111        if !texture_coords.is_empty() && texture_coords.len() != positions.len() {
112            return Err(Error::Unsupported(
113                "a mesh with texture coordinates needs one per position",
114            ));
115        }
116        if !colors.is_empty() && colors.len() != positions.len() {
117            return Err(Error::Unsupported(
118                "a mesh with colors needs one per position",
119            ));
120        }
121        // Checked here rather than clamped at draw time. An index past the end
122        // reads whatever follows the buffer, which on one backend is a
123        // validation error and on the other is a triangle drawn from
124        // uninitialized floats -- so this is the difference between a refusal
125        // and a picture that differs between backends for no visible reason.
126        if let Some(indices) = &indices {
127            if let Some(bad) = indices.iter().find(|i| **i as usize >= positions.len()) {
128                let _ = bad;
129                return Err(Error::Unsupported(
130                    "a mesh index names a position the mesh does not have",
131                ));
132            }
133        }
134        if !positions.iter().all(|p| p.is_finite()) || !texture_coords.iter().all(|c| c.is_finite())
135        {
136            return Err(Error::Unsupported(
137                "a mesh position or texture coordinate is not a finite number",
138            ));
139        }
140
141        // The order the triangles are actually drawn in, which is where the
142        // mode stops mattering: a strip and a fan are two ways of writing an
143        // index buffer, and writing it here means nothing below this point has
144        // to know which was used.
145        let order: Vec<u32> = match indices {
146            Some(indices) => indices,
147            None => (0..positions.len() as u32).collect(),
148        };
149        let indices = expand(mode, &order);
150        Ok(Self {
151            positions,
152            texture_coords,
153            colors,
154            indices,
155        })
156    }
157
158    pub fn positions(&self) -> &[Vec2] {
159        &self.positions
160    }
161
162    /// Texture coordinates, or empty where the mesh carries none.
163    pub fn texture_coords(&self) -> &[Vec2] {
164        &self.texture_coords
165    }
166
167    /// Per-vertex colors, or empty where the mesh carries none.
168    pub fn colors(&self) -> &[Color] {
169        &self.colors
170    }
171
172    /// Triangle indices, three per triangle, whatever mode built them.
173    pub fn indices(&self) -> &[u32] {
174        &self.indices
175    }
176
177    pub fn is_empty(&self) -> bool {
178        self.indices.is_empty()
179    }
180}
181
182/// One piece of a sprite sheet, and where it goes.
183///
184/// The unit a batched sprite draw is built from. `source` is in texels of the
185/// sheet, because that is how a sheet's layout is known -- a packer emits
186/// pixel rectangles, and normalizing them at every call site is how one of
187/// them eventually gets normalized twice. The quad is that rectangle's size in
188/// user units before the transform, so a sprite drawn with the identity lands
189/// at its own size with its top-left corner at the origin.
190#[derive(Debug, Clone, Copy, PartialEq)]
191pub struct Sprite {
192    /// The part of the sheet to draw, in texels.
193    pub source: SourceRect,
194    /// A color combined with this sprite alone, by the paint's
195    /// [`Paint::tint_blend`](crate::paint::Paint::tint_blend): the sprite the
196    /// source and the color the destination, as `drawAtlas` takes them.
197    /// Under the default, `Modulate`, it is multiplied in.
198    ///
199    /// White changes nothing under that default, and is what [`Sprite::new`]
200    /// and [`Sprite::at`] give. This is what makes one sheet serve a whole palette -- and it is
201    /// per sprite rather than per batch, which is the difference between one
202    /// draw and one draw per color.
203    pub color: Color,
204    /// Where it goes, applied to a quad running from the origin to the
205    /// source's size.
206    ///
207    /// A full affine rather than the rotation-scale-translation that
208    /// `dart:ui` restricts this to. That restriction buys a smaller per-sprite
209    /// payload for a shader that applies the transform itself; these are
210    /// applied here, where a general transform costs exactly the same and a
211    /// caller who wants to skew a sprite is not told they may not.
212    pub transform: Affine2,
213}
214
215/// A rectangle in texels, as a sprite's source.
216///
217/// Deliberately not the canvas `Rect`, which is in user space: a sheet
218/// rectangle and a destination rectangle are different things, and the one
219/// mistake this call invites is passing one where the other belongs.
220#[derive(Debug, Clone, Copy, PartialEq)]
221pub struct SourceRect {
222    pub x: f32,
223    pub y: f32,
224    pub width: f32,
225    pub height: f32,
226}
227
228impl SourceRect {
229    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
230        Self {
231            x,
232            y,
233            width,
234            height,
235        }
236    }
237
238    pub fn is_finite(&self) -> bool {
239        self.x.is_finite()
240            && self.y.is_finite()
241            && self.width.is_finite()
242            && self.height.is_finite()
243    }
244}
245
246impl Sprite {
247    pub fn new(source: SourceRect, transform: Affine2) -> Self {
248        Self {
249            source,
250            color: Color::WHITE,
251            transform,
252        }
253    }
254
255    /// The same sprite, tinted.
256    pub fn with_color(mut self, color: Color) -> Self {
257        self.color = color;
258        self
259    }
260
261    /// A sprite placed without rotation or scaling.
262    pub fn at(source: SourceRect, position: Vec2) -> Self {
263        Self::new(source, Affine2::from_translation(position))
264    }
265}
266
267/// Rewrite an order of vertices as a list of triangles.
268///
269/// A degenerate tail -- one or two vertices left over from a list, or fewer
270/// than three altogether -- produces no triangle rather than an error, which
271/// is what a mesh with nothing in it should do. An error would be a different
272/// claim: that the caller made a mistake, when a mesh built from a loop that
273/// found nothing is not one.
274fn expand(mode: VertexMode, order: &[u32]) -> Vec<u32> {
275    let mut out = Vec::new();
276    match mode {
277        VertexMode::Triangles => {
278            for triangle in order.chunks_exact(3) {
279                out.extend_from_slice(triangle);
280            }
281        }
282        VertexMode::TriangleStrip => {
283            for (i, window) in order.windows(3).enumerate() {
284                // Every other triangle has the opposite winding, so two of its
285                // vertices swap. Emitting them in strip order instead would
286                // alternate front and back faces, which matters the moment
287                // anything culls -- and a mesh whose triangles disagree about
288                // which side they face is wrong even where nothing does.
289                if i % 2 == 0 {
290                    out.extend_from_slice(&[window[0], window[1], window[2]]);
291                } else {
292                    out.extend_from_slice(&[window[1], window[0], window[2]]);
293                }
294            }
295        }
296        VertexMode::TriangleFan => {
297            for window in order.windows(2).skip(1) {
298                out.extend_from_slice(&[order[0], window[0], window[1]]);
299            }
300        }
301    }
302    out
303}
304
305#[cfg(test)]
306mod tests {
307    use super::*;
308
309    /// The signed area of a triangle, whose sign is its winding.
310    fn winding(a: Vec2, b: Vec2, c: Vec2) -> f32 {
311        (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y)
312    }
313
314    #[test]
315    fn a_strip_comes_out_consistently_wound() {
316        // The claim the alternation exists to make, and the only one that says
317        // why it is there: emitted in strip order the triangles would alternate
318        // front and back faces, and a mesh whose triangles disagree about which
319        // way they face is wrong the moment anything culls -- and wrong in a way
320        // that draws correctly until something does.
321        //
322        // Beside the test that pins the index sequence rather than instead of
323        // it. That one builds its strip from `points`, which are collinear, so
324        // every triangle in it encloses nothing and has no winding to be
325        // consistent about: it fixes the ordering, and this fixes what the
326        // ordering is for.
327        //
328        // A zigzag rather than a straight run, because a degenerate strip has no
329        // winding to be consistent about: three collinear points enclose nothing
330        // and their signed area is zero, which agrees with everything.
331        let strip: Vec<Vec2> = (0..6)
332            .map(|i| Vec2::new(i as f32 * 10.0, if i % 2 == 0 { 0.0 } else { 12.0 }))
333            .collect();
334        let indices = expand(VertexMode::TriangleStrip, &[0, 1, 2, 3, 4, 5]);
335        assert_eq!(indices.len(), 4 * 3, "six points make four triangles");
336
337        let signs: Vec<f32> = indices
338            .chunks_exact(3)
339            .map(|t| {
340                winding(
341                    strip[t[0] as usize],
342                    strip[t[1] as usize],
343                    strip[t[2] as usize],
344                )
345                .signum()
346            })
347            .collect();
348        assert!(
349            signs.iter().all(|s| *s == signs[0]),
350            "every triangle in a strip should wind the same way, got {signs:?}"
351        );
352
353        // And the alternation is a reordering rather than a different set: each
354        // triangle still covers the three points the strip says it does.
355        for (i, t) in indices.chunks_exact(3).enumerate() {
356            let mut got = [t[0], t[1], t[2]];
357            got.sort_unstable();
358            let want = [i as u32, i as u32 + 1, i as u32 + 2];
359            assert_eq!(got, want, "triangle {i} covers the wrong points");
360        }
361    }
362
363    fn points(n: usize) -> Vec<Vec2> {
364        (0..n).map(|i| Vec2::new(i as f32, 0.0)).collect()
365    }
366
367    #[test]
368    fn a_strip_alternates_winding_so_every_triangle_faces_the_same_way() {
369        let mesh = Vertices::new(VertexMode::TriangleStrip, points(4)).expect("strip");
370        assert_eq!(mesh.indices(), &[0, 1, 2, 2, 1, 3]);
371    }
372
373    #[test]
374    fn a_fan_shares_its_first_vertex_with_every_triangle() {
375        let mesh = Vertices::new(VertexMode::TriangleFan, points(4)).expect("fan");
376        assert_eq!(mesh.indices(), &[0, 1, 2, 0, 2, 3]);
377    }
378
379    #[test]
380    fn a_list_drops_a_partial_triangle_rather_than_inventing_a_vertex() {
381        let mesh = Vertices::new(VertexMode::Triangles, points(5)).expect("list");
382        assert_eq!(mesh.indices(), &[0, 1, 2]);
383    }
384
385    #[test]
386    fn too_few_positions_for_any_triangle_is_an_empty_mesh_rather_than_an_error() {
387        for mode in [
388            VertexMode::Triangles,
389            VertexMode::TriangleStrip,
390            VertexMode::TriangleFan,
391        ] {
392            let mesh = Vertices::new(mode, points(2)).expect("two points");
393            assert!(mesh.is_empty(), "{mode:?} made a triangle from two points");
394        }
395    }
396
397    #[test]
398    fn an_index_past_the_end_is_refused_where_it_is_written() {
399        let error = Vertices::indexed(VertexMode::Triangles, points(3), Vec::new(), vec![0, 1, 3]);
400        assert!(error.is_err(), "an out-of-range index was accepted");
401    }
402
403    #[test]
404    fn texture_coordinates_have_to_match_the_positions_they_belong_to() {
405        let error = Vertices::textured(VertexMode::Triangles, points(3), points(2));
406        assert!(error.is_err(), "a short coordinate list was accepted");
407    }
408}