ambient_sky 0.2.0

Ambient sky. Host-only.
Documentation
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
use std::{
    array,
    f32::consts::{FRAC_1_SQRT_2, PI},
    iter::Flatten,
    mem,
    sync::Arc,
};

use ambient_gizmos::{Cuboid, Gizmo, GizmoPrimitive, DEFAULT_RADIUS};
use ambient_gpu::typed_buffer::TypedBuffer;
use ambient_std::{color::Color, shapes::Ray};
use bytemuck::{Pod, Zeroable};
use derive_more::Deref;
use glam::{vec3, Vec3};
use noise::{NoiseFn, Perlin};
use ordered_float::NotNan;

/// Describes how to generate the terrain
pub trait Generator: Send + Sync + 'static {
    fn get(&self, point: Vec3) -> f32;
}

impl<F: NoiseFn<[f64; 3]> + Send + Sync + 'static> Generator for F {
    fn get(&self, p: Vec3) -> f32 {
        self.get([p.x as _, p.y as _, p.z as _]) as f32
    }
}

// 0 is a sentinel
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Pod, Zeroable, Deref)]
#[repr(transparent)]
pub struct NodeIndex(u32);

#[derive(Debug, Default, Clone, Copy, PartialEq, Pod, Zeroable)]
#[repr(C)]
pub struct Node {
    density: f32,
    origin: Vec3,
    half_size: f32,
    children: [NodeIndex; 8],
    pad: [f32; 3],
}

#[derive(Clone)]
pub struct OctreeInfo {
    pub origin: Vec3,
    pub max_depth: u32,
    pub half_size: f32,
    pub generator: Arc<dyn Generator>,
    pub scale: f32,
}

impl OctreeInfo {
    pub fn build(self) -> Octree {
        Octree {
            nodes: vec![Node { density: 0., origin: self.origin, half_size: self.half_size, ..Default::default() }],
            max_depth: self.max_depth,
            free: NodeIndex::default(),
            count: 1,
            generator: self.generator,
            scale: self.scale,
        }
    }
}

impl Default for OctreeInfo {
    fn default() -> Self {
        Self { max_depth: 10, half_size: 100., generator: Arc::new(Perlin::new()), origin: Vec3::ZERO, scale: 0.1 }
    }
}

#[derive(Clone)]
/// Tree containing clouds in an octree
/// The root node is 0.
/// An "null" node reference is also 0 (can't use Option due to Pod).
/// Since the tree is acyclic the root is never referenced.
/// The root can also never be removed.
///
/// An internal node's density is always the maximum of any child or subchild.
pub struct Octree {
    nodes: Vec<Node>,
    max_depth: u32,
    free: NodeIndex,
    count: u32,
    generator: Arc<dyn Generator>,
    scale: f32,
}

impl Octree {
    // TODO make a free chain of children
    pub fn remove(&mut self, idx: NodeIndex) -> Node {
        let mut node = mem::replace(&mut self.nodes[*idx as usize], Node::free(self.free));
        self.free = idx;

        if !node.is_leaf() {
            for c in mem::take(&mut node.children) {
                assert_ne!(c, NodeIndex::root());
                self.remove(c);
            }
        }

        self.count -= 1;
        node
    }

    pub fn merge(&mut self, index: NodeIndex) -> u32 {
        let node = self.node_mut(index);
        if node.is_leaf() {
            0
        } else {
            let children = mem::take(&mut node.children);
            for c in children {
                self.remove(c);
            }
            1
        }
    }

    pub fn _write_buffer(&self, buf: &mut TypedBuffer<Node>, on_resize: &mut impl FnMut(&TypedBuffer<Node>)) {
        buf.fill(&self.nodes, on_resize)
    }

    /// Split a node
    /// Does nothing for an internal node
    pub fn split(&mut self, index: NodeIndex) -> ([NodeIndex; 8], f32, u32) {
        let node = self.node(index);
        if !node.is_leaf() {
            (node.children, node.density, 0)
        } else {
            let size = node.half_size;
            let new_size = size / 2.;

            let mut children = [NodeIndex::default(); 8];
            let mut max_d = node.density;
            let leftmost = node.origin - Vec3::splat(new_size);

            for (i, c) in children.iter_mut().enumerate() {
                let off = vec3((i & 1 != 0) as u32 as f32 * size, (i & 2 != 0) as u32 as f32 * size, (i & 4 != 0) as u32 as f32 * size);
                let pos = leftmost + off;

                let density = self.generator.get(pos * self.scale);
                max_d = max_d.max(density);
                let node = Node { density, origin: pos, half_size: new_size, ..Default::default() };

                *c = self.insert(node);
            }

            let node = self.node_mut(index);
            node.children = children;
            node.density = max_d;
            (children, max_d, 1)
        }
    }

    /// Update topotracingy by splitting or merging nodes
    /// `desired_size` desired radial size of the voxel
    pub fn update_topo(&mut self, index: NodeIndex, depth: u32, desired_size: f32, fov: f32, pos: Vec3) -> (f32, u32) {
        let max_depth = self.max_depth;
        let node = self.node_mut(index);

        let dist = pos.distance(node.origin) - node.half_size;
        let cur_size = node.half_size * FRAC_1_SQRT_2 / (PI * (dist) * fov);
        let mut d = node.density;

        let (children, u) = if (cur_size < 0. || cur_size > desired_size) && depth < max_depth {
            let (children, _, u) = self.split(index);
            (Some(children), u)
        }
        // Merge if node is less than half the desired size to give some leeway
        else if 4. * cur_size < desired_size {
            let d = node.density;
            let u = self.merge(index);
            return (d, u);
        } else {
            (node.children(), 0)
        };

        // We know that no child will update a parent
        let u = children.iter().flatten().fold(u, |u, &c| {
            assert_ne!(c, NodeIndex::root());
            let (new_d, new_n) = self.update_topo(c, depth + 1, desired_size, fov, pos);
            d = d.max(new_d);
            u + new_n
        });

        // Update density reading
        self.node_mut(index).density = d;
        (d, u)
    }

    pub fn node(&self, index: NodeIndex) -> &Node {
        &self.nodes[index.0 as usize]
    }

    pub fn node_mut(&mut self, index: NodeIndex) -> &mut Node {
        &mut self.nodes[index.0 as usize]
    }

    pub fn len(&self) -> u32 {
        self.count
    }

    pub fn query<F>(&self, accept: F) -> TreeQuery<F>
    where
        F: Fn(NodeIndex, &Node) -> bool,
    {
        TreeQuery::new(self, accept)
    }

    pub fn raycast(&self, ray: &Ray, d_threshold: f32) -> Option<RayIntersect> {
        let distance = self.raycast_inner(NodeIndex::root(), ray, d_threshold)?;
        Some(RayIntersect { point: ray.origin + ray.dir * distance, distance })
    }

    fn raycast_inner(&self, index: NodeIndex, ray: &Ray, d_threshold: f32) -> Option<f32> {
        let node = self.node(index);
        let int = node.ray_intersect(ray, d_threshold)?;
        // If leaf, only check with self and return it
        if node.is_leaf() {
            Some(int)
        }
        // Internal node:
        // Density is the max(children); if self passes, some child density
        // should pass as well.
        //
        // iotw: if self succeeds, so do its parents
        else {
            node.children()
                .unwrap()
                .iter()
                .flat_map(|c| Some((c, self.raycast_inner(*c, ray, d_threshold).and_then(|v| NotNan::new(v).ok())?)))
                .min_by_key(|(_, v)| *v)
                .map(|(_, v)| *v)
        }
    }

    pub fn insert(&mut self, node: Node) -> NodeIndex {
        self.count += 1;
        if self.free.is_valid() {
            // Init old node
            let idx = self.free;
            self.free = self.nodes[*idx as usize].next_free();
            self.nodes[*idx as usize] = node;
            idx
        } else {
            let idx = NodeIndex(self.nodes.len().try_into().unwrap());
            self.nodes.push(node);
            idx
        }
    }

    /// Get a reference to the octree's nodes.
    #[must_use]
    pub fn nodes(&self) -> &[Node] {
        self.nodes.as_ref()
    }

    fn _root(&self) -> &Node {
        self.node(NodeIndex::root())
    }

    pub fn gizmos(&self, d_threshold: f32) -> Gizmos {
        Gizmos { query: self.query(|_, _| true), d_threshold }
    }
}

pub struct TreeQuery<'a, F> {
    tree: &'a Octree,
    stack: Vec<NodeIndex>,
    accept: F,
}

impl<'a, F> TreeQuery<'a, F> {
    pub fn new(tree: &'a Octree, accept: F) -> Self {
        Self { tree, stack: vec![NodeIndex::root()], accept }
    }
}

impl<'a, F> Iterator for TreeQuery<'a, F>
where
    F: Fn(NodeIndex, &Node) -> bool,
{
    type Item = (NodeIndex, &'a Node);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let index = self.stack.pop()?;

            let node = self.tree.node(index);

            if (self.accept)(index, node) {
                // Add children
                for c in node.children().iter().flatten() {
                    self.stack.push(*c)
                }

                return Some((index, node));
            }
        }
    }
}

#[doc(hidden)]
pub struct Gizmos<'a> {
    query: TreeQuery<'a, fn(NodeIndex, &Node) -> bool>,
    d_threshold: f32,
}

impl<'a> Gizmo for Gizmos<'a> {
    type Items = Flatten<Self>;

    fn into_gizmo_primitives(self) -> Self::Items {
        self.flatten()
    }
}

impl<'a> Iterator for Gizmos<'a> {
    type Item = std::array::IntoIter<GizmoPrimitive, 6>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (_, node) = self.query.next()?;
            if node.is_leaf() && node.density > self.d_threshold {
                return Some(
                    Cuboid::new(node.origin, Vec3::splat(node.half_size), Vec3::ONE * (1. - node.density), node.density).into_iter(),
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use glam::vec3;

    use super::*;
    #[test]
    fn cloud_tree() {
        let mut tree = OctreeInfo::default().build();

        tree.split(NodeIndex::root());
        assert_eq!(tree.len(), 9);

        tree.merge(NodeIndex::root());
        tree.split(NodeIndex::root());
        assert_eq!(tree.nodes.len(), 9);
    }

    #[test]
    fn cloud_topo() {
        let mut tree = OctreeInfo::default().build();
        let center = vec3(12.0, 0., 0.0);

        tree.update_topo(NodeIndex::root(), 0, 0.1, 1.0, center);

        tracing::info!("Tree len: {}", tree.len());
    }
}

impl Node {
    fn free(next: NodeIndex) -> Node {
        Self {
            children: [
                next,
                NodeIndex::root(),
                NodeIndex::root(),
                NodeIndex::root(),
                NodeIndex::root(),
                NodeIndex::root(),
                NodeIndex::root(),
                NodeIndex::root(),
            ],
            ..Self::default()
        }
    }

    fn is_leaf(&self) -> bool {
        !self.children[0].is_valid()
    }

    fn next_free(&self) -> NodeIndex {
        self.children[0]
    }

    fn ray_intersect(&self, ray: &Ray, d_threshold: f32) -> Option<f32> {
        let dir = ray.dir;
        let origin = ray.origin - self.origin;
        let inv_dir = dir.recip();

        let t1 = (-self.half_size - origin) * inv_dir;
        let t2 = (self.half_size - origin) * inv_dir;

        let tmin = t1.min(t2);
        let tmax = t1.max(t2);

        let tmin = tmin.max_element();
        let tmax = tmax.min_element();

        if self.density > d_threshold && tmax > 0. && tmax >= tmin {
            Some(tmin)
        } else {
            None
        }
    }

    #[inline]
    fn children(&self) -> Option<[NodeIndex; 8]> {
        if self.is_leaf() {
            None
        } else {
            Some(self.children)
        }
    }
}

#[derive(Debug, Clone, PartialEq, Copy)]
pub struct RayIntersect {
    pub point: Vec3,
    pub distance: f32,
}

impl IntoIterator for RayIntersect {
    type IntoIter = array::IntoIter<GizmoPrimitive, 1>;

    type Item = GizmoPrimitive;

    fn into_iter(self) -> Self::IntoIter {
        [GizmoPrimitive::sphere(self.point, DEFAULT_RADIUS).with_color(Color::hsl(self.distance / 100., 1., 0.5).into())].into_iter()
    }
}

impl NodeIndex {
    pub fn is_valid(&self) -> bool {
        self.0 != 0
    }

    pub fn root() -> NodeIndex {
        Self(0)
    }
}