From 3dce7650d536ba3b5c4262fb54539506987387d9 Mon Sep 17 00:00:00 2001 From: adia redmoon Date: Tue, 2 Apr 2024 17:37:40 -0700 Subject: [PATCH] proof of concept auto-meshing in the ecs --- src/main.rs | 3 +++ src/vvlib/mod.rs | 31 +++++++++++++++++++++++++------ src/vvlib/octtree.rs | 2 +- src/vvum.rs | 32 ++++++++++++++++++++++---------- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index b897406..c64ea26 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ use bevy::window::WindowResolution; use bevy::DefaultPlugins; mod orbit_camera; mod vvum; +use vvlib::octtree; +use vvlib::OctTreePlugin; use vvum::mesh_plugin; mod vvlib; @@ -17,5 +19,6 @@ fn main() { ..default() })) .add_plugins(mesh_plugin::PluginInitializer) + .add_plugins(OctTreePlugin {}) .run(); } diff --git a/src/vvlib/mod.rs b/src/vvlib/mod.rs index 59f4346..fc52a9f 100644 --- a/src/vvlib/mod.rs +++ b/src/vvlib/mod.rs @@ -3,6 +3,8 @@ use bevy::app::App; use bevy::app::Last; use bevy::app::Plugin; +use bevy::app::Update; +use bevy::asset::Asset; use bevy::asset::AssetServer; use bevy::asset::Assets; use bevy::asset::Handle; @@ -122,14 +124,14 @@ pub mod constants { pub struct OctTreePlugin {} impl Plugin for OctTreePlugin { fn build(&self, app: &mut App) { - app.add_systems(Last, oct_tree_edit_updater); + app.add_systems(Update, oct_tree_edit_updater); } } #[derive(Component)] pub struct OctTreeModelComponent { - model: octtree::OctTree, - handle: Option>, + pub model: octtree::OctTree, + pub handle: Option>, //TODO: add Option<> handle for remeshing-event for async-computing it - so if it gets edited again we can cancel the old one and let it run a new mesher async compute } @@ -148,7 +150,9 @@ pub fn oct_tree_edit_updater( } else if model.model.needs_update && model.handle.is_some() { // let meshed = model.render_as_mesh(); - let mut in_place = mesh_assets.get_mut(model.handle.clone().unwrap()); + mesh_assets.insert(model.handle.clone().unwrap(), meshed); + model.model.needs_update = false; + /* let mut in_place = mesh_assets.get_mut(model.handle.clone().unwrap()); if in_place.is_some() { let mut in_place = in_place.unwrap(); @@ -183,14 +187,29 @@ pub fn oct_tree_edit_updater( } else { panic!("no indices on newly generated mesh?"); } - } + } */ } } } //TODO: event that takes finished remeshing events and emits an event to be caught with the relevant mesh to update the meshdata - impl OctTreeModelComponent { + pub fn new( + model: octtree::OctTree, + mut mesh_assets: Option>>, + ) -> OctTreeModelComponent { + if mesh_assets.is_some() { + return OctTreeModelComponent { + model: model, + handle: Some(mesh_assets.unwrap().reserve_handle()), + }; + } else { + return OctTreeModelComponent { + model: model, + handle: None, + }; + } + } pub fn render_as_mesh(&self) -> Mesh { let mut vertices = Vec::new(); let mut normals = Vec::new(); diff --git a/src/vvlib/octtree.rs b/src/vvlib/octtree.rs index 633435a..ebf7acd 100644 --- a/src/vvlib/octtree.rs +++ b/src/vvlib/octtree.rs @@ -120,7 +120,7 @@ impl OctTree { size: 1, center: npos, root: TreeNode::Leaf(value), - needs_update: false, + needs_update: true, } } diff --git a/src/vvum.rs b/src/vvum.rs index de36e2f..21ff591 100644 --- a/src/vvum.rs +++ b/src/vvum.rs @@ -6,7 +6,10 @@ pub mod mesh_plugin { }, }; - use crate::orbit_camera::orbit_camera::*; + use crate::{ + orbit_camera::orbit_camera::*, + vvlib::{octtree::OctTree, OctTreeModelComponent}, + }; #[derive(Event)] pub struct SendMeshEvent { @@ -17,8 +20,7 @@ pub mod mesh_plugin { impl Plugin for PluginInitializer { fn build(&self, app: &mut App) { app.add_systems(Startup, setup); - app.add_systems(Update, (pan_orbit_camera, listen_send_mesh_event)); - app.add_event::(); + app.add_systems(Update, pan_orbit_camera); } } @@ -229,13 +231,23 @@ pub mod mesh_plugin { } } - fn setup(mut ev_sender: EventWriter, mut commands: Commands) { - ev_sender.send(SendMeshEvent { - location: Vec3::new(0., 0., 0.), - }); - ev_sender.send(SendMeshEvent { - location: Vec3::new(1., 0., 0.), - }); + fn setup(mesh_assets: ResMut>, mut commands: Commands) { + let oct = OctTree::new(Vec3::ZERO, Color::RED); + //commands.spawn(oct); + let mut model_oct = OctTreeModelComponent::new(oct, Some(mesh_assets)); + + model_oct + .model + .set_voxel_at_location(Vec3::new(1., 0., 0.), Color::BLUE); + + commands.spawn(( + PbrBundle { + mesh: model_oct.handle.clone().unwrap(), + ..default() + }, + model_oct, + )); + let camera_and_light_transform = Transform::from_xyz(1.8, 1.8, 1.8).looking_at(Vec3::ZERO, Vec3::Y);