summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 24b706fbfd02874f048baaa62d7f8368d221315f (plain)
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
use std::collections::HashMap;

use itertools::Itertools;

use bevy::prelude::*;
use polyhedron_ops as p_ops;
use smooth_bevy_cameras::{
    controllers::orbit::{OrbitCameraBundle, OrbitCameraController, OrbitCameraPlugin},
    LookTransformPlugin,
};

fn main() {
    App::new()
        .insert_resource(Msaa::Sample4)
        .add_plugins(DefaultPlugins)
        .add_plugin(LookTransformPlugin)
        .add_plugin(OrbitCameraPlugin::default())
        .add_startup_system(setup)
        .add_system(bevy::window::close_on_esc)
        .run();
}

#[derive(Debug, Clone)]
struct ShapeMap {
    shapes: HashMap<String, p_ops::Polyhedron>,
}

impl ShapeMap {
    fn new(seed: &str, ops: &str, depth: u32) -> Self {
        let mut sm = ShapeMap {
            shapes: HashMap::new(),
        };

        for var in (1..=depth as usize).flat_map(|n| {
            std::iter::repeat(ops.chars())
                .take(n)
                .multi_cartesian_product()
        }) {
            let key = var.into_iter().collect::<String>() + seed;
            let mut polyhedron = p_ops::Polyhedron::try_from(key.as_ref()).unwrap();
            polyhedron.planarize(Some(100), false);
            println!("{}: {}", key, polyhedron.positions().len());
            sm.shapes.insert(key, polyhedron);
        }
        println!("{}", sm.shapes.len());
        sm
    }
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    let depth = 1;
    let sm = ShapeMap::new("T", "asdk", depth);

    let mut x = 0;
    let mut y = 0;
    for (_, s) in sm.shapes.iter() {
        let pos = Vec3::new(0.0, x as f32 * 2.0, y as f32 * 2.0);
        commands.spawn(PbrBundle {
            mesh: meshes.add(Mesh::from(s.clone())),
            material: materials.add(Color::rgb(0.4, 0.35, 0.3).into()),
            transform: Transform::from_translation(pos),
            ..default()
        });
        x += 1;
        if x > 5 {
            x = 0;
            y += 1;
        }
    }

    commands
        // light
        .spawn(DirectionalLightBundle {
            transform: Transform::from_translation(Vec3::new(4.0, 8.0, 4.0)),
            ..Default::default()
        });

    commands
        // camera
        .spawn(Camera3dBundle::default())
        .insert(OrbitCameraBundle::new(
            OrbitCameraController {
                mouse_rotate_sensitivity: Vec2::new(2.0, 2.0),
                mouse_translate_sensitivity: Vec2::new(2.0, 2.0),
                ..default()
            },
            Vec3::new(-3.0, 3.0, 5.0),
            Vec3::new(0., 0., 0.),
            Vec3::new(0., -1., 0.),
        ));
}