spitfire_draw/
primitives.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use crate::{
    context::DrawContext,
    sprite::SpriteTexture,
    utils::{Drawable, ShaderRef, Vertex},
};
use smallvec::SmallVec;
use spitfire_core::{Triangle, VertexStream};
use spitfire_glow::{
    graphics::{Graphics, GraphicsBatch},
    renderer::{GlowBlending, GlowUniformValue},
};
use std::{
    borrow::Cow,
    cell::RefCell,
    collections::HashMap,
    f32::consts::{PI, TAU},
};
use vek::{Mat4, Rect, Rgba, Vec2};

#[derive(Debug, Default, Clone)]
pub struct PrimitivesEmitter {
    pub shader: Option<ShaderRef>,
    pub textures: SmallVec<[SpriteTexture; 4]>,
    pub uniforms: HashMap<Cow<'static, str>, GlowUniformValue>,
    pub blending: Option<GlowBlending>,
    pub screen_space: bool,
}

impl PrimitivesEmitter {
    pub fn single(texture: SpriteTexture) -> Self {
        Self {
            textures: vec![texture].into(),
            ..Default::default()
        }
    }

    pub fn shader(mut self, value: ShaderRef) -> Self {
        self.shader = Some(value);
        self
    }

    pub fn texture(mut self, value: SpriteTexture) -> Self {
        self.textures.push(value);
        self
    }

    pub fn uniform(mut self, key: Cow<'static, str>, value: GlowUniformValue) -> Self {
        self.uniforms.insert(key, value);
        self
    }

    pub fn blending(mut self, value: GlowBlending) -> Self {
        self.blending = Some(value);
        self
    }

    pub fn screen_space(mut self, value: bool) -> Self {
        self.screen_space = value;
        self
    }

    pub fn emit_lines<I: IntoIterator<Item = Vec2<f32>>>(&self, vertices: I) -> LinesDraw<I> {
        LinesDraw {
            emitter: self,
            vertices: RefCell::new(Some(vertices)),
            region: Rect {
                x: 0.0,
                y: 0.0,
                w: 1.0,
                h: 1.0,
            },
            page: 0.0,
            tint: Rgba::white(),
            thickness: 1.0,
            looped: false,
        }
    }

    pub fn emit_brush<I: IntoIterator<Item = (Vec2<f32>, f32, Rgba<f32>)>>(
        &self,
        vertices: I,
    ) -> BrushDraw<I> {
        BrushDraw {
            emitter: self,
            vertices: RefCell::new(Some(vertices)),
            region: Rect {
                x: 0.0,
                y: 0.0,
                w: 1.0,
                h: 1.0,
            },
            page: 0.0,
        }
    }

    pub fn emit_triangles<I: IntoIterator<Item = [Vertex; 3]>>(
        &self,
        vertices: I,
    ) -> TrianglesDraw<I> {
        TrianglesDraw {
            emitter: self,
            vertices: RefCell::new(Some(vertices)),
            tint: Rgba::white(),
        }
    }

    pub fn emit_triangle_fan<I: IntoIterator<Item = Vertex>>(
        &self,
        vertices: I,
    ) -> TriangleFanDraw<I> {
        TriangleFanDraw {
            emitter: self,
            vertices: RefCell::new(Some(vertices)),
        }
    }

    pub fn emit_triangle_strip<I: IntoIterator<Item = Vertex>>(
        &self,
        vertices: I,
    ) -> TriangleStripDraw<I> {
        TriangleStripDraw {
            emitter: self,
            vertices: RefCell::new(Some(vertices)),
        }
    }

    pub fn emit_regular_polygon(
        &self,
        vertices: usize,
        position: Vec2<f32>,
        radius: f32,
    ) -> RegularPolygonDraw {
        RegularPolygonDraw {
            emitter: self,
            vertices,
            position,
            radius,
            region: Rect {
                x: 0.0,
                y: 0.0,
                w: 1.0,
                h: 1.0,
            },
            page: 0.0,
            tint: Rgba::white(),
        }
    }

    pub fn emit_circle(
        &self,
        position: Vec2<f32>,
        radius: f32,
        maximum_error: f32,
    ) -> RegularPolygonDraw {
        RegularPolygonDraw {
            emitter: self,
            vertices: (PI / (1.0 - maximum_error / radius).acos()).ceil() as _,
            position,
            radius,
            region: Rect {
                x: 0.0,
                y: 0.0,
                w: 1.0,
                h: 1.0,
            },
            page: 0.0,
            tint: Rgba::white(),
        }
    }

    fn stream_transformed(
        &self,
        context: &mut DrawContext,
        graphics: &mut Graphics<Vertex>,
        f: impl FnMut(&mut VertexStream<Vertex, GraphicsBatch>),
    ) {
        let batch = GraphicsBatch {
            shader: context.shader(self.shader.as_ref()),
            uniforms: self
                .uniforms
                .iter()
                .map(|(k, v)| (k.clone(), v.to_owned()))
                .chain(std::iter::once((
                    "u_projection_view".into(),
                    GlowUniformValue::M4(
                        if self.screen_space {
                            graphics.main_camera.screen_matrix()
                        } else {
                            graphics.main_camera.world_matrix()
                        }
                        .into_col_array(),
                    ),
                )))
                .chain(self.textures.iter().enumerate().map(|(index, texture)| {
                    (texture.sampler.clone(), GlowUniformValue::I1(index as _))
                }))
                .collect(),
            textures: self
                .textures
                .iter()
                .filter_map(|texture| {
                    Some((context.texture(Some(&texture.texture))?, texture.filtering))
                })
                .collect(),
            blending: self.blending.unwrap_or_else(|| context.top_blending()),
            scissor: None,
        };
        graphics.stream.batch_optimized(batch);
        let transform = Mat4::from(context.top_transform());
        graphics.stream.transformed(f, |vertex| {
            let point = transform.mul_point(Vec2::from(vertex.position));
            vertex.position[0] = point.x;
            vertex.position[1] = point.y;
        });
    }
}

pub struct LinesDraw<'a, I: IntoIterator<Item = Vec2<f32>>> {
    emitter: &'a PrimitivesEmitter,
    vertices: RefCell<Option<I>>,
    pub region: Rect<f32, f32>,
    pub page: f32,
    pub tint: Rgba<f32>,
    pub thickness: f32,
    pub looped: bool,
}

impl<'a, I: IntoIterator<Item = Vec2<f32>>> LinesDraw<'a, I> {
    pub fn region_page(mut self, region: Rect<f32, f32>, page: f32) -> Self {
        self.region = region;
        self.page = page;
        self
    }

    pub fn tint(mut self, value: Rgba<f32>) -> Self {
        self.tint = value;
        self
    }

    pub fn thickness(mut self, value: f32) -> Self {
        self.thickness = value;
        self
    }

    pub fn looped(mut self, value: bool) -> Self {
        self.looped = value;
        self
    }
}

impl<'a, I: IntoIterator<Item = Vec2<f32>>> Drawable for LinesDraw<'a, I> {
    fn draw(&self, context: &mut DrawContext, graphics: &mut Graphics<Vertex>) {
        fn push(
            stream: &mut VertexStream<Vertex, GraphicsBatch>,
            region: Rect<f32, f32>,
            page: f32,
            color: [f32; 4],
            prev: Vec2<f32>,
            next: Vec2<f32>,
            normal: Vec2<f32>,
        ) {
            stream.extend(
                [
                    Vertex {
                        position: (prev - normal).into_array(),
                        uv: [region.x, region.y, page],
                        color,
                    },
                    Vertex {
                        position: (prev + normal).into_array(),
                        uv: [region.x + region.w, region.y, page],
                        color,
                    },
                    Vertex {
                        position: (next + normal).into_array(),
                        uv: [region.x + region.w, region.y + region.h, page],
                        color,
                    },
                    Vertex {
                        position: (next - normal).into_array(),
                        uv: [region.x, region.y + region.h, page],
                        color,
                    },
                ],
                [Triangle { a: 0, b: 1, c: 2 }, Triangle { a: 2, b: 3, c: 0 }],
            );
        }

        self.emitter
            .stream_transformed(context, graphics, |stream| {
                if let Some(vertices) = self.vertices.borrow_mut().take() {
                    let mut vertices = vertices.into_iter();
                    let Some(mut prev) = vertices.next() else {
                        return;
                    };
                    let start = prev;
                    let color = self.tint.into_array();
                    for next in vertices {
                        let tangent = next - prev;
                        let normal = Vec2 {
                            x: tangent.y,
                            y: -tangent.x,
                        }
                        .try_normalized()
                        .unwrap_or_default()
                            * self.thickness;
                        push(stream, self.region, self.page, color, prev, next, normal);
                        prev = next;
                    }
                    if self.looped {
                        let tangent = start - prev;
                        let normal = Vec2 {
                            x: tangent.y,
                            y: -tangent.x,
                        }
                        .try_normalized()
                        .unwrap_or_default()
                            * self.thickness;
                        push(stream, self.region, self.page, color, prev, start, normal);
                    }
                }
            });
    }
}

pub struct BrushDraw<'a, I: IntoIterator<Item = (Vec2<f32>, f32, Rgba<f32>)>> {
    emitter: &'a PrimitivesEmitter,
    vertices: RefCell<Option<I>>,
    pub region: Rect<f32, f32>,
    pub page: f32,
}

impl<'a, I: IntoIterator<Item = (Vec2<f32>, f32, Rgba<f32>)>> BrushDraw<'a, I> {
    pub fn region_page(mut self, region: Rect<f32, f32>, page: f32) -> Self {
        self.region = region;
        self.page = page;
        self
    }
}

impl<'a, I: IntoIterator<Item = (Vec2<f32>, f32, Rgba<f32>)>> Drawable for BrushDraw<'a, I> {
    fn draw(&self, context: &mut DrawContext, graphics: &mut Graphics<Vertex>) {
        fn push(
            stream: &mut VertexStream<Vertex, GraphicsBatch>,
            region: Rect<f32, f32>,
            page: f32,
            prev: (Vec2<f32>, f32, Rgba<f32>),
            next: (Vec2<f32>, f32, Rgba<f32>),
            normal_prev: Vec2<f32>,
            normal_next: Vec2<f32>,
        ) {
            stream.extend(
                [
                    Vertex {
                        position: ((prev.0 + next.0) * 0.5).into_array(),
                        uv: [region.x, region.y, page],
                        color: ((prev.2 + next.2) * 0.5).into_array(),
                    },
                    Vertex {
                        position: (prev.0 - normal_prev * prev.1).into_array(),
                        uv: [region.x, region.y, page],
                        color: prev.2.into_array(),
                    },
                    Vertex {
                        position: (prev.0 + normal_prev * prev.1).into_array(),
                        uv: [region.x + region.w, region.y, page],
                        color: prev.2.into_array(),
                    },
                    Vertex {
                        position: (next.0 + normal_next * next.1).into_array(),
                        uv: [region.x + region.w, region.y + region.h, page],
                        color: next.2.into_array(),
                    },
                    Vertex {
                        position: (next.0 - normal_next * next.1).into_array(),
                        uv: [region.x, region.y + region.h, page],
                        color: next.2.into_array(),
                    },
                ],
                [
                    Triangle { a: 0, b: 1, c: 2 },
                    Triangle { a: 0, b: 2, c: 3 },
                    Triangle { a: 0, b: 3, c: 4 },
                    Triangle { a: 0, b: 4, c: 1 },
                ],
            );
        }

        self.emitter
            .stream_transformed(context, graphics, |stream| {
                if let Some(vertices) = self.vertices.borrow_mut().take() {
                    let mut vertices = vertices.into_iter().peekable();
                    let Some(mut prev) = vertices.next() else {
                        return;
                    };
                    let mut prev_tangent = Option::<Vec2<f32>>::None;
                    while let Some(curr) = vertices.next() {
                        let next = vertices.peek().copied();
                        let curr_tangent = (curr.0 - prev.0).try_normalized().unwrap_or_default();
                        let tangent = prev_tangent
                            .replace(curr_tangent)
                            .and_then(|tangent| (curr_tangent + tangent).try_normalized())
                            .unwrap_or(curr_tangent);
                        let next_tangent = next
                            .and_then(|next| (next.0 - curr.0).try_normalized())
                            .and_then(|tangent| (curr_tangent + tangent).try_normalized())
                            .unwrap_or(curr_tangent);
                        let normal_prev = Vec2 {
                            x: tangent.y,
                            y: -tangent.x,
                        }
                        .try_normalized()
                        .unwrap_or_default();
                        let normal_next = Vec2 {
                            x: next_tangent.y,
                            y: -next_tangent.x,
                        }
                        .try_normalized()
                        .unwrap_or_default();
                        push(
                            stream,
                            self.region,
                            self.page,
                            prev,
                            curr,
                            normal_prev,
                            normal_next,
                        );
                        prev = curr;
                    }
                }
            });
    }
}

pub struct TrianglesDraw<'a, I: IntoIterator<Item = [Vertex; 3]>> {
    emitter: &'a PrimitivesEmitter,
    vertices: RefCell<Option<I>>,
    pub tint: Rgba<f32>,
}

impl<'a, I: IntoIterator<Item = [Vertex; 3]>> Drawable for TrianglesDraw<'a, I> {
    fn draw(&self, context: &mut DrawContext, graphics: &mut Graphics<Vertex>) {
        self.emitter
            .stream_transformed(context, graphics, |stream| {
                if let Some(vertices) = self.vertices.borrow_mut().take() {
                    unsafe {
                        let start = stream.vertices().len();
                        stream.extend_vertices(vertices.into_iter().flatten());
                        let end = stream.vertices().len();
                        stream.extend_triangles(
                            false,
                            (start..end).step_by(3).map(|index| Triangle {
                                a: index as u32,
                                b: index as u32 + 1,
                                c: index as u32 + 2,
                            }),
                        );
                    }
                }
            });
    }
}

pub struct TriangleFanDraw<'a, I: IntoIterator<Item = Vertex>> {
    emitter: &'a PrimitivesEmitter,
    vertices: RefCell<Option<I>>,
}

impl<'a, I: IntoIterator<Item = Vertex>> Drawable for TriangleFanDraw<'a, I> {
    fn draw(&self, context: &mut DrawContext, graphics: &mut Graphics<Vertex>) {
        self.emitter
            .stream_transformed(context, graphics, |stream| {
                if let Some(vertices) = self.vertices.borrow_mut().take() {
                    stream.triangle_fan(vertices);
                }
            });
    }
}

pub struct TriangleStripDraw<'a, I: IntoIterator<Item = Vertex>> {
    emitter: &'a PrimitivesEmitter,
    vertices: RefCell<Option<I>>,
}

impl<'a, I: IntoIterator<Item = Vertex>> Drawable for TriangleStripDraw<'a, I> {
    fn draw(&self, context: &mut DrawContext, graphics: &mut Graphics<Vertex>) {
        self.emitter
            .stream_transformed(context, graphics, |stream| {
                if let Some(vertices) = self.vertices.borrow_mut().take() {
                    stream.triangle_strip(vertices);
                }
            });
    }
}

pub struct RegularPolygonDraw<'a> {
    emitter: &'a PrimitivesEmitter,
    vertices: usize,
    position: Vec2<f32>,
    radius: f32,
    pub region: Rect<f32, f32>,
    pub page: f32,
    pub tint: Rgba<f32>,
}

impl<'a> RegularPolygonDraw<'a> {
    pub fn region_page(mut self, region: Rect<f32, f32>, page: f32) -> Self {
        self.region = region;
        self.page = page;
        self
    }

    pub fn tint(mut self, value: Rgba<f32>) -> Self {
        self.tint = value;
        self
    }
}

impl<'a> Drawable for RegularPolygonDraw<'a> {
    fn draw(&self, context: &mut DrawContext, graphics: &mut Graphics<Vertex>) {
        let color = self.tint.into_array();
        self.emitter
            .stream_transformed(context, graphics, |stream| {
                stream.triangle_fan((0..=self.vertices).map(|index| {
                    let angle = TAU / self.vertices as f32 * index as f32;
                    let (y, x) = angle.sin_cos();
                    let u = (x + 1.0) * 0.5;
                    let v = (y + 1.0) * 0.5;
                    Vertex {
                        position: [
                            self.position.x + x * self.radius,
                            self.position.y + y * self.radius,
                        ],
                        uv: [
                            self.region.x + self.region.w * u,
                            self.region.y + self.region.h * v,
                            self.page,
                        ],
                        color,
                    }
                }));
            });
    }
}