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

use itertools::Itertools;

use bevy::{
    prelude::*,
    reflect::TypeUuid,
    render::render_resource::{AsBindGroup, ShaderRef},
};
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)
        .add_plugin(MaterialPlugin::<EnemyMaterial>::default())
        .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 base_materials: ResMut<Assets<StandardMaterial>>,
    mut enemy_materials: ResMut<Assets<EnemyMaterial>>,
) {
    let depth = 1;
    let sm = ShapeMap::new("T", "skad", 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(MaterialMeshBundle {
            mesh: meshes.add(Mesh::from(s.clone())),
            material: enemy_materials.add(EnemyMaterial {
                color: Color::rgba(1.0, 1.0, 1.0, 1.0),
                alpha_mode: AlphaMode::Opaque,
            }),
            transform: Transform::from_translation(pos),
            ..default()
        });
        x += 1;
        if x > 5 {
            x = 0;
            y += 1;
        }
    }

    // reference cube
    commands.spawn(MaterialMeshBundle {
        mesh: meshes.add(Mesh::from(shape::Cube::default())),
        material: base_materials.add(Color::BLUE.into()),
        transform: Transform::from_translation(Vec3::new(-2.0, 0.0, 0.0)),
        ..default()
    });

    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.),
        ));
}

impl Material for EnemyMaterial {
    fn fragment_shader() -> ShaderRef {
        "shaders/enemy_material.wgsl".into()
    }

    fn alpha_mode(&self) -> AlphaMode {
        self.alpha_mode
    }
}

#[derive(AsBindGroup, TypeUuid, Debug, Clone)]
#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]
pub struct EnemyMaterial {
    #[uniform(0)]
    color: Color,
    alpha_mode: AlphaMode,
}