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
use std::{cmp::Ordering, ops::Range};

use bevy::{
    asset::Asset,
    pbr::MaterialExtension,
    reflect::TypePath,
    render::render_resource::{AsBindGroup, ShaderRef, ShaderType},
};
use glam::{Mat3, Vec2};

pub(crate) const UV_SCALE: f32 = 16.;
// * Keep this in sync with terrain.wgsl.
// * Keep this smaller or equal to de_types::objects::PLAYER_MAX_UNITS.
pub(crate) const CIRCLE_CAPACITY: usize = 127;
// * Keep this in sync with terrain.wgsl.
// * Keep this smaller or equal to de_types::objects::PLAYER_MAX_BUILDINGS.
pub(crate) const RECTANGLE_CAPACITY: usize = 31;

#[derive(Asset, AsBindGroup, TypePath, Debug, Clone)]
pub struct TerrainMaterial {
    #[uniform(100)]
    uv_scale: f32,
    #[uniform(101)]
    circles: KdTree,
    #[uniform(102)]
    rectangles: Rectangles,
}

impl TerrainMaterial {
    pub(crate) fn new(uv_scale: f32) -> Self {
        Self {
            uv_scale,
            circles: KdTree::empty(),
            rectangles: Rectangles::default(),
        }
    }

    pub(crate) fn set_circle_markers(&mut self, circles: Vec<Circle>) {
        self.circles.rebuild(circles);
    }

    pub(crate) fn set_rectangle_markers(&mut self, rectangles: Vec<Rectangle>) {
        self.rectangles.set_rectangles(rectangles);
    }
}

impl MaterialExtension for TerrainMaterial {
    fn fragment_shader() -> ShaderRef {
        "shaders/terrain.wgsl".into()
    }
}

#[derive(ShaderType, Debug, Clone, Copy, Default)]
pub(crate) struct Circle {
    #[align(16)]
    center: Vec2,
    radius: f32,
}

impl Circle {
    /// Creates a new circle.
    ///
    /// # Panics
    ///
    /// * If `center` is not finite.
    /// * If radius is non finite or is smaller or equal to zero.
    pub(crate) fn new(center: Vec2, radius: f32) -> Self {
        if !center.is_finite() {
            panic!("Circle center is not finite: {center:?}");
        }
        if !radius.is_finite() {
            panic!("Circle radius is not finite: {radius:?}");
        }
        if radius <= 0. {
            panic!("Circle radius is smaller or equal to 0: {radius:?}");
        }

        Self { center, radius }
    }

    fn coord(&self, axis: Axis) -> f32 {
        match axis {
            Axis::X => self.center.x,
            Axis::Y => self.center.y,
        }
    }
}

#[derive(Copy, Clone)]
enum Axis {
    X,
    Y,
}

impl Axis {
    fn toggle(self) -> Self {
        match self {
            Self::X => Self::Y,
            Self::Y => Self::X,
        }
    }
}

#[derive(ShaderType, Debug, Clone)]
struct KdTree {
    /// Nodes of the KD tree. Given a node at `index` its left child is at `2 *
    /// index + 1` and right child is at `2 * index + 2`.
    #[align(16)]
    nodes: [Circle; CIRCLE_CAPACITY],
    count: u32,
}

impl KdTree {
    fn empty() -> Self {
        Self {
            nodes: [Circle::default(); CIRCLE_CAPACITY],
            count: 0,
        }
    }

    /// Rebuids the KD tree from a vector of circles.
    ///
    /// # Panics
    ///
    /// Panics if the number of circles is larger than maximum allowed
    /// capacity.
    fn rebuild(&mut self, mut circles: Vec<Circle>) {
        if circles.len() > CIRCLE_CAPACITY {
            panic!(
                "Number of circles {} is greater than shader capacity {}.",
                circles.len(),
                CIRCLE_CAPACITY
            );
        }

        self.count = circles.len() as u32;
        if self.count == 0 {
            return;
        }

        struct StackItem {
            index: u16,
            axis: Axis,
            range: Range<usize>,
        }

        let mut stack: Vec<StackItem> = vec![StackItem {
            index: 0,
            axis: Axis::X,
            range: 0..circles.len(),
        }];

        while let Some(item) = stack.pop() {
            let subtree = &mut circles[item.range.clone()];

            subtree.sort_by(|a, b| {
                let a = a.coord(item.axis);
                let b = b.coord(item.axis);

                if a < b {
                    Ordering::Less
                } else if a > b {
                    Ordering::Greater
                } else {
                    Ordering::Equal
                }
            });

            // A median cannot be used because it is desired that that the
            // three occupies continuous range in tree.nodes (from 0 to
            // circles.len()).
            //
            // The following computation guarantees that that the tree is
            // balanced (the max diff between tree depth is 1) and that it each
            // level/floor is filled from "left to right".
            let num_bits = 31 - (subtree.len() as u32).leading_zeros();
            let max_subtree_size = (1 << num_bits) - 1;
            let min_subtree_size = max_subtree_size >> 1;
            let split_index = (subtree.len() - min_subtree_size - 1).min(max_subtree_size);
            self.nodes[item.index as usize] = subtree[split_index];

            if split_index < (subtree.len() - 1) {
                stack.push(StackItem {
                    index: 2 * item.index + 2,
                    axis: item.axis.toggle(),
                    range: (item.range.start + split_index + 1)..item.range.end,
                });
            }
            if split_index > 0 {
                stack.push(StackItem {
                    index: 2 * item.index + 1,
                    axis: item.axis.toggle(),
                    range: item.range.start..(item.range.start + split_index),
                });
            }
        }
    }
}

#[derive(ShaderType, Debug, Clone, Copy, Default)]
pub(crate) struct Rectangle {
    pub(crate) inverse_transform: Mat3,
    pub(crate) half_size: Vec2,
}

impl Rectangle {
    pub(crate) fn new(inverse_transform: Mat3, half_size: Vec2) -> Self {
        Self {
            inverse_transform,
            half_size,
        }
    }
}

#[derive(ShaderType, Debug, Clone)]
struct Rectangles {
    items: [Rectangle; RECTANGLE_CAPACITY],
    count: u32,
}

impl Rectangles {
    /// Sets the rectangles held in the list.
    ///
    /// # Panics
    ///
    /// Panics if the number of rectangles is larger than maximum allowed
    /// capacity.
    fn set_rectangles(&mut self, rectangles: Vec<Rectangle>) {
        if rectangles.len() > RECTANGLE_CAPACITY {
            panic!(
                "Number of rectangles {} is greater than shader capacity {}.",
                rectangles.len(),
                RECTANGLE_CAPACITY
            );
        }

        self.items[..rectangles.len()].copy_from_slice(&rectangles);
        self.count = rectangles.len() as u32;
    }
}

impl Default for Rectangles {
    fn default() -> Self {
        Self {
            items: [Rectangle::default(); RECTANGLE_CAPACITY],
            count: 0,
        }
    }
}

#[cfg(test)]
mod tests {
    use itertools::Itertools;

    use super::*;

    #[test]
    fn test_kd_tree_build_many() {
        let mut circles = vec![
            Circle::new(Vec2::new(1., -4.), 1.),
            Circle::new(Vec2::new(-2., 1.), 1.),
            Circle::new(Vec2::new(-1.5, -3.), 1.),
            Circle::new(Vec2::new(2., 1.), 1.),
        ];

        let mut tree = KdTree::empty();

        for permutation in (0..circles.len()).permutations(circles.len()) {
            let version: Vec<Circle> = permutation.iter().map(|index| circles[*index]).collect();
            tree.rebuild(version);

            assert_eq!(tree.count, 4);
            assert_eq!(tree.nodes[0].center, Vec2::new(1., -4.));
            assert_eq!(tree.nodes[1].center, Vec2::new(-2., 1.));
            assert_eq!(tree.nodes[2].center, Vec2::new(2., 1.));
            assert_eq!(tree.nodes[3].center, Vec2::new(-1.5, -3.));
        }

        circles.push(Circle::new(Vec2::new(-8., 2.), 1.));
        for permutation in (0..circles.len()).permutations(circles.len()) {
            let version: Vec<Circle> = permutation.iter().map(|index| circles[*index]).collect();
            tree.rebuild(version);

            assert_eq!(tree.count, 5);
            assert_eq!(tree.nodes[0].center, Vec2::new(1., -4.));
            assert_eq!(tree.nodes[1].center, Vec2::new(-2., 1.));
            assert_eq!(tree.nodes[2].center, Vec2::new(2., 1.));
            assert_eq!(tree.nodes[3].center, Vec2::new(-1.5, -3.));
            assert_eq!(tree.nodes[4].center, Vec2::new(-8., 2.));
        }

        circles.push(Circle::new(Vec2::new(1.5, 0.), 1.));
        for permutation in (0..circles.len()).permutations(circles.len()) {
            let version: Vec<Circle> = permutation.iter().map(|index| circles[*index]).collect();
            tree.rebuild(version);

            assert_eq!(tree.count, 6);
            assert_eq!(tree.nodes[0].center, Vec2::new(1., -4.));
            assert_eq!(tree.nodes[1].center, Vec2::new(-2., 1.));
            assert_eq!(tree.nodes[2].center, Vec2::new(2., 1.));
            assert_eq!(tree.nodes[3].center, Vec2::new(-1.5, -3.));
            assert_eq!(tree.nodes[4].center, Vec2::new(-8., 2.));
            assert_eq!(tree.nodes[5].center, Vec2::new(1.5, 0.));
        }
    }
}