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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use std::{f32::consts::FRAC_PI_2, ops::Range};

use bevy::{
    asset::RenderAssetUsages,
    input::mouse::{AccumulatedMouseMotion, AccumulatedMouseScroll},
    prelude::*,
    render::mesh::{PrimitiveTopology, VertexAttributeValues},
};
use bevy_egui::{egui, EguiContexts, EguiPlugin};
use cytogon::*;
use rand_chacha::ChaCha8Rng;
use rand_core::SeedableRng as _;

#[derive(Clone, Copy, PartialEq)]
struct Config2d {
    pub size: UVec2,
}

impl Default for Config2d {
    fn default() -> Self {
        Self {
            size: UVec2::new(128, 64),
        }
    }
}

#[derive(Clone, Copy, PartialEq)]
struct Config3d {
    pub size: UVec3,
    pub rule: Rule3,
}

impl Default for Config3d {
    fn default() -> Self {
        Self {
            size: UVec3::new(32, 32, 32),
            // 13-26/13-14,17-19/2/M
            rule: Rule3 {
                birth: RuleBitset3::from(13u8..=14u8) | (17u8..=19u8).into(),
                survive: (13u8..=26u8).into(),
            },
        }
    }
}

#[derive(Clone, Copy, PartialEq)]
enum ConfigNd {
    D2(Config2d),
    D3(Config3d),
}

impl Default for ConfigNd {
    fn default() -> Self {
        Self::D3(default())
    }
}

#[derive(Resource, Clone, PartialEq)]
struct Config {
    pub seed: u64,
    pub auto_regenerate: bool,
    pub fill_rate: f32,
    pub smooth_iter: i32,
    pub config_nd: ConfigNd,
    pub mesh: Handle<Mesh>,
    pub material: Handle<StandardMaterial>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            seed: 42,
            auto_regenerate: true,
            fill_rate: 0.70,
            smooth_iter: 4,
            config_nd: default(),
            mesh: default(),
            material: default(),
        }
    }
}

impl Config {
    pub fn default_2d() -> Self {
        Self {
            fill_rate: 0.45,
            smooth_iter: 1,
            config_nd: ConfigNd::D2(default()),
            ..default()
        }
    }

    pub fn default_3d() -> Self {
        Self {
            fill_rate: 0.70,
            smooth_iter: 4,
            config_nd: ConfigNd::D3(default()),
            ..default()
        }
    }
}

#[derive(Debug, Resource)]
struct CameraSettings {
    pub distance_speed: f32,
    pub pitch_speed: f32,
    pub pitch_range: Range<f32>,
    pub yaw_speed: f32,
}

impl Default for CameraSettings {
    fn default() -> Self {
        // Limiting pitch stops some unexpected rotation past 90° up or down.
        let pitch_limit = FRAC_PI_2 - 0.01;
        Self {
            distance_speed: 6.0,
            pitch_speed: 0.003,
            pitch_range: -pitch_limit..pitch_limit,
            yaw_speed: 0.004,
        }
    }
}

#[derive(Component)]
struct Root;

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut config: ResMut<Config>,
) {
    // camera
    commands.spawn((
        Camera3d::default(),
        Transform::from_xyz(0.0, 48.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));

    // light
    commands.spawn((
        DirectionalLight {
            color: LinearRgba::rgb(1.0, 0.8, 0.8).into(),
            shadows_enabled: true,
            ..default()
        },
        Transform::from_xyz(-30.0, 48.0, 16.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));
    commands.spawn((
        DirectionalLight {
            color: LinearRgba::rgb(0.8, 0.8, 1.0).into(),
            shadows_enabled: true,
            ..default()
        },
        Transform::from_xyz(50.0, -32.0, -16.0).looking_at(Vec3::ZERO, Vec3::Y),
    ));

    // rendering data for cubes
    config.mesh = meshes.add(Cuboid::from_size(Vec3::ONE));
    config.material = materials.add(Color::srgb_u8(124, 144, 255));

    // object root
    //commands.spawn((Transform::IDENTITY, Visibility::Visible, Root));
    commands.spawn((
        Transform::IDENTITY,
        Mesh3d(config.mesh.clone()),
        MeshMaterial3d(config.material.clone()),
        Root,
    ));
}

/// Helper system to enable closing the example application by pressing the
/// escape key (ESC).
pub fn close_on_esc(mut ev_app_exit: EventWriter<AppExit>, input: Res<ButtonInput<KeyCode>>) {
    if input.just_pressed(KeyCode::Escape) {
        ev_app_exit.send(AppExit::Success);
    }
}

fn ui_config(mut contexts: EguiContexts, mut config: ResMut<Config>) {
    egui::Window::new("Config").show(contexts.ctx_mut(), |ui| {
        let mut old_config = config.clone();

        ui.checkbox(&mut old_config.auto_regenerate, "Auto-regenerate");
        if !old_config.auto_regenerate && ui.button("Regenerate").clicked() {
            // Just "touch" the config to mark it changed, which will trigger a regenerate
            config.set_changed();
        }

        if let ConfigNd::D3(config_3d) = &mut old_config.config_nd {
            ui.add(egui::Slider::new(&mut config_3d.size.x, 4..=128).text("Size"));
            config_3d.size = UVec3::splat(config_3d.size.x);

            ui.label("Birth");
            let mut bits = config_3d.rule.birth.to_array();
            ui.checkbox(&mut bits[0], "0");
            ui.horizontal(|ui| {
                for b in &mut bits[1..13] {
                    ui.checkbox(b, "");
                }
                ui.checkbox(&mut bits[13], "13");
            });
            ui.horizontal(|ui| {
                for b in &mut bits[14..26] {
                    ui.checkbox(b, "");
                }
                ui.checkbox(&mut bits[26], "26");
            });
            config_3d.rule.birth = bits.into();

            ui.label("Survive");
            let mut bits = config_3d.rule.survive.to_array();
            ui.checkbox(&mut bits[0], "0");
            ui.horizontal(|ui| {
                for b in &mut bits[1..13] {
                    ui.checkbox(b, "");
                }
                ui.checkbox(&mut bits[13], "13");
            });
            ui.horizontal(|ui| {
                for b in &mut bits[14..26] {
                    ui.checkbox(b, "");
                }
                ui.checkbox(&mut bits[26], "26");
            });
            config_3d.rule.survive = bits.into();
        }

        ui.separator();

        ui.label("Initial fill");
        ui.indent(1, |ui| {
            ui.add(egui::Slider::new(&mut old_config.fill_rate, 0.0..=1.0).text("Fill rate"));
            ui.add(
                egui::Slider::new(&mut old_config.smooth_iter, 0..=50).text("Smooth iterations"),
            );
        });

        // Ensure we don't trigger the Bevy change detection if nothing changed
        config.set_if_neq(old_config);
    });
}

fn generate_mesh(
    mut meshes: ResMut<Assets<Mesh>>,
    config: Res<Config>,
    q_root: Query<&Mesh3d, With<Root>>,
) {
    if !config.is_changed() {
        return;
    }

    let handle = q_root.single();
    let mesh = meshes.get_mut(handle).unwrap();

    // Spawn new cubes
    match &config.config_nd {
        ConfigNd::D3(config_3d) => {
            let cave = {
                #[cfg(feature = "trace")]
                let _span = info_span!("generate").entered();

                let mut cave = Grid3::new(config_3d.size);
                {
                    #[cfg(feature = "trace")]
                    let _span = info_span!("fill_rand").entered();

                    let mut prng = ChaCha8Rng::seed_from_u64(config.seed);
                    cave.fill_rand(config.fill_rate, &mut prng);
                }

                for _ in 0..config.smooth_iter {
                    #[cfg(feature = "trace")]
                    let _span = info_span!("smooth").entered();

                    cave.smooth(&config_3d.rule);
                }

                cave
            };

            {
                #[cfg(feature = "trace")]
                let _span = info_span!("rebuild_mesh").entered();

                //let offset = -config_3d.size.as_vec3() / 2.;
                rebuild_mesh(&cave, mesh);
            }
        }

        ConfigNd::D2(config_2d) => {
            let mut cave = Grid2::new(config_2d.size);
            let mut prng = ChaCha8Rng::seed_from_u64(config.seed);
            cave.fill_rand(config.fill_rate, &mut prng);
        }
    }
}

fn generate_cubes(mut commands: Commands, config: Res<Config>, q_root: Query<Entity, With<Root>>) {
    if !config.is_changed() || !config.auto_regenerate {
        return;
    }

    // Clear all cubes
    let mut cmd = commands.entity(q_root.single());
    cmd.despawn_descendants();

    // Spawn new cubes
    cmd.with_children(|parent| match &config.config_nd {
        ConfigNd::D3(config_3d) => {
            let mut cave = Grid3::new(config_3d.size);
            let mut prng = ChaCha8Rng::seed_from_u64(config.seed);
            cave.fill_rand(config.fill_rate, &mut prng);
            // 13-26/13-14,17-19/2/M
            let rule = Rule3 {
                birth: RuleBitset3::from(13u8..=14u8) | (17u8..=19u8).into(),
                survive: (13u8..=26u8).into(),
            };
            for _ in 0..config.smooth_iter {
                cave.smooth(&rule);
            }

            let offset = -config_3d.size.as_vec3() / 2.;
            for k in 0..config_3d.size.z {
                for j in 0..config_3d.size.y {
                    for i in 0..config_3d.size.x {
                        let pos = IVec3::new(i as i32, j as i32, k as i32);
                        if cave.cell(pos).unwrap_or(false) {
                            parent.spawn((
                                Mesh3d(config.mesh.clone()),
                                MeshMaterial3d(config.material.clone()),
                                Transform::from_translation(offset + pos.as_vec3()),
                            ));
                        }
                    }
                }
            }
        }

        ConfigNd::D2(config_2d) => {
            let mut cave = Grid2::new(config_2d.size);
            let mut prng = ChaCha8Rng::seed_from_u64(config.seed);
            cave.fill_rand(config.fill_rate, &mut prng);
        }
    });
}

fn rebuild_mesh(grid: &Grid3, mesh: &mut Mesh) {
    assert_eq!(mesh.primitive_topology(), PrimitiveTopology::TriangleList);
    assert_eq!(
        mesh.asset_usage,
        RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD
    );

    // Ensure the geometry is not indexed, otherwise compute_flat_normals() doesn't
    // work
    {
        #[cfg(feature = "trace")]
        let _span = info_span!("remove_unused").entered();

        mesh.remove_indices();
        mesh.remove_attribute(Mesh::ATTRIBUTE_UV_0);
    }

    let x = grid.size.x as usize;
    let y = grid.size.y as usize;
    let z = grid.size.z as usize;
    let capacity = ((z + 1) * x * y + (y + 1) * z * x + (x + 1) * z * y) * 6;

    {
        #[cfg(feature = "trace")]
        let _span = info_span!("build").entered();

        // Steal position array
        let values = mesh.attribute_mut(Mesh::ATTRIBUTE_POSITION).unwrap();
        let VertexAttributeValues::Float32x3(vertices) = values else {
            panic!("Mesh doesn't have Float32x3 vertices.");
        };
        let mut positions = std::mem::take(vertices);
        positions.reserve(capacity / 8); // heuristic
        positions.truncate(0);

        // Steal normal array
        let values = mesh.attribute_mut(Mesh::ATTRIBUTE_NORMAL).unwrap();
        let VertexAttributeValues::Float32x3(normals) = values else {
            panic!("Mesh doesn't have Float32x3 normals.");
        };
        let mut normals = std::mem::take(normals);
        normals.reserve(capacity / 8); // heuristic
        normals.truncate(0);

        let offset = -grid.size.as_vec3() / 2.;

        // X faces
        for i in 0..=grid.size.x {
            for k in 0..grid.size.z {
                for j in 0..grid.size.y {
                    let pos = IVec3::new(i as i32, j as i32, k as i32);
                    let cur = grid.cell(pos).unwrap_or(false);
                    let prev = if i > 0 {
                        grid.cell(pos - IVec3::X).unwrap_or(false)
                    } else {
                        false
                    };
                    if cur != prev {
                        let half_size = Vec3::new(0.5, 0.5, 0.5);
                        let min = offset + pos.as_vec3() - half_size;

                        let mut n = [0f32; 3];
                        if prev {
                            n[0] = 1.0;
                            positions.push(min.to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::Y + Vec3::Z).to_array());
                        } else {
                            n[0] = -1.0;
                            positions.push(min.to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::Y + Vec3::Z).to_array());
                        }

                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                    }
                }
            }
        }
        // Y faces
        for j in 0..=grid.size.y {
            for k in 0..grid.size.z {
                for i in 0..grid.size.x {
                    let pos = IVec3::new(i as i32, j as i32, k as i32);
                    let cur = grid.cell(pos).unwrap_or(false);
                    let prev = if j > 0 {
                        grid.cell(pos - IVec3::Y).unwrap_or(false)
                    } else {
                        false
                    };
                    if cur != prev {
                        let half_size = Vec3::new(0.5, 0.5, 0.5);
                        let min = offset + pos.as_vec3() - half_size;

                        let mut n = [0f32; 3];
                        if prev {
                            n[1] = 1.0;
                            positions.push(min.to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::X + Vec3::Z).to_array());
                        } else {
                            n[1] = -1.0;
                            positions.push(min.to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::Z).to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::X + Vec3::Z).to_array());
                        }

                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                    }
                }
            }
        }
        // Z faces
        for k in 0..=grid.size.z {
            for j in 0..grid.size.y {
                for i in 0..grid.size.x {
                    let pos = IVec3::new(i as i32, j as i32, k as i32);
                    let cur = grid.cell(pos).unwrap_or(false);
                    let prev = if k > 0 {
                        grid.cell(pos - IVec3::Z).unwrap_or(false)
                    } else {
                        false
                    };
                    if cur != prev {
                        let half_size = Vec3::new(0.5, 0.5, 0.5);
                        let min = offset + pos.as_vec3() - half_size;

                        let mut n = [0f32; 3];
                        if prev {
                            n[2] = 1.0;
                            positions.push(min.to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::X + Vec3::Y).to_array());
                        } else {
                            n[2] = -1.0;
                            positions.push(min.to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::X).to_array());
                            positions.push((min + Vec3::Y).to_array());
                            positions.push((min + Vec3::X + Vec3::Y).to_array());
                        }

                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                        normals.push(n);
                    }
                }
            }
        }

        // Restore positions array
        let values = mesh.attribute_mut(Mesh::ATTRIBUTE_POSITION).unwrap();
        let VertexAttributeValues::Float32x3(dummy) = values else {
            panic!("Mesh doesn't have Float32x3 vertices.");
        };
        std::mem::swap(dummy, &mut positions);

        // Restore normals array
        let values = mesh.attribute_mut(Mesh::ATTRIBUTE_NORMAL).unwrap();
        let VertexAttributeValues::Float32x3(dummy) = values else {
            panic!("Mesh doesn't have Float32x3 normals.");
        };
        std::mem::swap(dummy, &mut normals);
    }
}

fn orbit_camera(
    mut camera: Single<&mut Transform, With<Camera>>,
    camera_settings: Res<CameraSettings>,
    mouse_buttons: Res<ButtonInput<MouseButton>>,
    mouse_motion: Res<AccumulatedMouseMotion>,
    mouse_scroll: Res<AccumulatedMouseScroll>,
    time: Res<Time>,
) {
    let mut distance = camera.translation.length();
    distance -= mouse_scroll.delta.y * camera_settings.distance_speed;

    if mouse_buttons.pressed(MouseButton::Right) {
        // Mouse motion is one of the few inputs that should not be multiplied by delta
        // time, as we are already receiving the full movement since the last frame
        // was rendered. Multiplying by delta time here would make the movement
        // slower that it should be.
        let delta = -mouse_motion.delta;
        let delta_pitch = delta.y * camera_settings.pitch_speed;
        let delta_yaw = delta.x * camera_settings.yaw_speed;

        // Obtain the existing pitch, yaw, and roll values from the transform.
        let (yaw, pitch, roll) = camera.rotation.to_euler(EulerRot::YXZ);

        // Establish the new yaw and pitch, preventing the pitch value from exceeding
        // our limits.
        let pitch = (pitch + delta_pitch).clamp(
            camera_settings.pitch_range.start,
            camera_settings.pitch_range.end,
        );
        //let roll = roll + delta_roll;
        let yaw = yaw + delta_yaw;
        camera.rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, roll);
    }

    let target = Vec3::ZERO;
    camera.translation = target - camera.forward() * distance;
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                title: "🔬 μscope".into(),
                ..default()
            }),
            ..default()
        }))
        .add_plugins(EguiPlugin)
        .init_resource::<Config>()
        .init_resource::<CameraSettings>()
        .add_systems(Startup, setup)
        .add_systems(Update, close_on_esc)
        .add_systems(Update, ui_config)
        .add_systems(Update, orbit_camera)
        //.add_systems(PostUpdate, generate_cubes)
        .add_systems(PostUpdate, generate_mesh)
        .run();
}