kas_wgpu/
draw_shaded.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Drawing APIs — shaded drawing
7
8use kas::draw::color::Rgba;
9use kas::draw::{DrawIface, DrawImpl, DrawSharedImpl, PassId};
10use kas::geom::Quad;
11
12/// Extension trait providing shaded drawing for [`DrawIface`]
13///
14/// All methods draw some feature.
15///
16/// Methods are parameterised via a pair of normals, `(inner, outer)`, which
17/// specify the surface normal direction at inner and outer edges of the feature
18/// respectively (with interpolation between these edges). These have values
19/// from the closed range `[-1, 1]`, where -1 points towards the inside of the
20/// feature, 1 points away from the feature, and 0 is perpendicular to the
21/// screen towards the viewer.
22pub trait DrawShaded {
23    /// Add a shaded square to the draw buffer
24    ///
25    /// For shading purposes, the mid-point is considered the inner edge.
26    fn shaded_square(&mut self, rect: Quad, norm: (f32, f32), col: Rgba);
27
28    /// Add a shaded circle to the draw buffer
29    ///
30    /// For shading purposes, the mid-point is considered the inner edge.
31    fn shaded_circle(&mut self, rect: Quad, norm: (f32, f32), col: Rgba);
32
33    /// Add a shaded frame with square corners to the draw buffer
34    fn shaded_square_frame(
35        &mut self,
36        outer: Quad,
37        inner: Quad,
38        norm: (f32, f32),
39        outer_col: Rgba,
40        inner_col: Rgba,
41    );
42
43    /// Add a shaded frame with rounded corners to the draw buffer
44    fn shaded_round_frame(&mut self, outer: Quad, inner: Quad, norm: (f32, f32), col: Rgba);
45}
46
47impl<'a, DS: DrawSharedImpl> DrawShaded for DrawIface<'a, DS>
48where
49    DS::Draw: DrawShadedImpl,
50{
51    fn shaded_square(&mut self, rect: Quad, norm: (f32, f32), col: Rgba) {
52        self.draw.shaded_square(self.pass, rect, norm, col);
53    }
54
55    fn shaded_circle(&mut self, rect: Quad, norm: (f32, f32), col: Rgba) {
56        self.draw.shaded_circle(self.pass, rect, norm, col);
57    }
58
59    fn shaded_square_frame(
60        &mut self,
61        outer: Quad,
62        inner: Quad,
63        norm: (f32, f32),
64        outer_col: Rgba,
65        inner_col: Rgba,
66    ) {
67        self.draw
68            .shaded_square_frame(self.pass, outer, inner, norm, outer_col, inner_col);
69    }
70
71    fn shaded_round_frame(&mut self, outer: Quad, inner: Quad, norm: (f32, f32), col: Rgba) {
72        self.draw
73            .shaded_round_frame(self.pass, outer, inner, norm, col);
74    }
75}
76
77/// Extended draw interface for [`DrawIface`] providing shaded drawing
78///
79/// This trait is an extension over [`DrawImpl`] providing solid shaded shapes.
80///
81/// Some drawing primitives (the "round" ones) are partially transparent.
82/// If the implementation buffers draw commands, it should draw these
83/// primitives after solid primitives.
84///
85/// Methods are parameterised via a pair of normals, `(inner, outer)`. These may
86/// have values from the closed range `[-1, 1]`, where -1 points inwards,
87/// 0 is perpendicular to the screen towards the viewer, and 1 points outwards.
88pub trait DrawShadedImpl: DrawImpl {
89    /// Add a shaded square to the draw buffer
90    fn shaded_square(&mut self, pass: PassId, rect: Quad, norm: (f32, f32), col: Rgba);
91
92    /// Add a shaded circle to the draw buffer
93    fn shaded_circle(&mut self, pass: PassId, rect: Quad, norm: (f32, f32), col: Rgba);
94
95    /// Add a square shaded frame to the draw buffer.
96    fn shaded_square_frame(
97        &mut self,
98        pass: PassId,
99        outer: Quad,
100        inner: Quad,
101        norm: (f32, f32),
102        outer_col: Rgba,
103        inner_col: Rgba,
104    );
105
106    /// Add a rounded shaded frame to the draw buffer.
107    fn shaded_round_frame(
108        &mut self,
109        pass: PassId,
110        outer: Quad,
111        inner: Quad,
112        norm: (f32, f32),
113        col: Rgba,
114    );
115}