3d_cube_volume_noise/
3d-cube-volume-noise.rs

1use blinksy::{
2    layout::{Layout3d, Shape3d, Vec3},
3    patterns::noise::{noise_fns, Noise3d, NoiseParams},
4    util::map_range,
5    ControlBuilder,
6};
7use blinksy_desktop::{
8    driver::{Desktop, DesktopError},
9    time::elapsed_in_ms,
10};
11use std::{iter, thread::sleep, time::Duration};
12
13struct CubeVolumeLayout;
14
15impl Layout3d for CubeVolumeLayout {
16    const PIXEL_COUNT: usize = 5 * 5 * 5;
17
18    fn shapes() -> impl Iterator<Item = Shape3d> {
19        let mut index: usize = 0;
20
21        fn map(n: usize) -> f32 {
22            map_range(n as f32, 0., 4., -1., 1.)
23        }
24
25        iter::from_fn(move || {
26            if index >= 5 * 5 * 5 {
27                return None;
28            }
29
30            let x = map(index % 5);
31            let z = map(index / 5 % 5);
32            let y = map(index / 5 / 5);
33
34            index += 1;
35
36            Some(Shape3d::Point(Vec3::new(x, y, z)))
37        })
38    }
39}
40
41fn main() {
42    Desktop::new_3d::<CubeVolumeLayout>().start(|driver| {
43        let mut control = ControlBuilder::new_3d()
44            .with_layout::<CubeVolumeLayout, { CubeVolumeLayout::PIXEL_COUNT }>()
45            .with_pattern::<Noise3d<noise_fns::Perlin>>(NoiseParams {
46                time_scalar: 0.25 / 1e3,
47                position_scalar: 0.25,
48            })
49            .with_driver(driver)
50            .with_frame_buffer_size::<{ CubeVolumeLayout::PIXEL_COUNT }>()
51            .build();
52
53        loop {
54            if let Err(DesktopError::WindowClosed) = control.tick(elapsed_in_ms()) {
55                break;
56            }
57
58            sleep(Duration::from_millis(16));
59        }
60    });
61}