From f2019713decb9dd1b486e4576eccb1bbac2c1d61 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Sat, 13 May 2023 16:13:32 +0200 Subject: shape generation --- src/main.rs | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/main.rs (limited to 'src') 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, +} + +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::() + 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>, + mut materials: ResMut>, +) { + 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.), + )); +} -- cgit v1.2.3-54-g00ecf