summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjacopograndi <jacopo.grandi@outlook.it>2023-05-13 16:13:32 +0200
committerjacopograndi <jacopo.grandi@outlook.it>2023-05-13 16:13:32 +0200
commitf2019713decb9dd1b486e4576eccb1bbac2c1d61 (patch)
tree47e5e5326130adbe290f07d7f4d1281089d37a86 /src
shape generation
Diffstat (limited to 'src')
-rw-r--r--src/main.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..24b706f
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,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.),
+ ));
+}