proof of concept auto-meshing in the ecs

This commit is contained in:
Lillian Vixe 2024-04-02 17:37:40 -07:00
parent 3c8048a51e
commit 3dce7650d5
4 changed files with 51 additions and 17 deletions

View file

@ -4,6 +4,8 @@ use bevy::window::WindowResolution;
use bevy::DefaultPlugins; use bevy::DefaultPlugins;
mod orbit_camera; mod orbit_camera;
mod vvum; mod vvum;
use vvlib::octtree;
use vvlib::OctTreePlugin;
use vvum::mesh_plugin; use vvum::mesh_plugin;
mod vvlib; mod vvlib;
@ -17,5 +19,6 @@ fn main() {
..default() ..default()
})) }))
.add_plugins(mesh_plugin::PluginInitializer) .add_plugins(mesh_plugin::PluginInitializer)
.add_plugins(OctTreePlugin {})
.run(); .run();
} }

View file

@ -3,6 +3,8 @@
use bevy::app::App; use bevy::app::App;
use bevy::app::Last; use bevy::app::Last;
use bevy::app::Plugin; use bevy::app::Plugin;
use bevy::app::Update;
use bevy::asset::Asset;
use bevy::asset::AssetServer; use bevy::asset::AssetServer;
use bevy::asset::Assets; use bevy::asset::Assets;
use bevy::asset::Handle; use bevy::asset::Handle;
@ -122,14 +124,14 @@ pub mod constants {
pub struct OctTreePlugin {} pub struct OctTreePlugin {}
impl Plugin for OctTreePlugin { impl Plugin for OctTreePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Last, oct_tree_edit_updater); app.add_systems(Update, oct_tree_edit_updater);
} }
} }
#[derive(Component)] #[derive(Component)]
pub struct OctTreeModelComponent { pub struct OctTreeModelComponent {
model: octtree::OctTree<Color>, pub model: octtree::OctTree<Color>,
handle: Option<Handle<Mesh>>, pub handle: Option<Handle<Mesh>>,
//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 //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() { } else if model.model.needs_update && model.handle.is_some() {
// //
let meshed = model.render_as_mesh(); 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() { if in_place.is_some() {
let mut in_place = in_place.unwrap(); let mut in_place = in_place.unwrap();
@ -183,14 +187,29 @@ pub fn oct_tree_edit_updater(
} else { } else {
panic!("no indices on newly generated mesh?"); 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 //TODO: event that takes finished remeshing events and emits an event to be caught with the relevant mesh to update the meshdata
impl OctTreeModelComponent { impl OctTreeModelComponent {
pub fn new(
model: octtree::OctTree<Color>,
mut mesh_assets: Option<ResMut<Assets<Mesh>>>,
) -> 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 { pub fn render_as_mesh(&self) -> Mesh {
let mut vertices = Vec::new(); let mut vertices = Vec::new();
let mut normals = Vec::new(); let mut normals = Vec::new();

View file

@ -120,7 +120,7 @@ impl<T: Clone> OctTree<T> {
size: 1, size: 1,
center: npos, center: npos,
root: TreeNode::Leaf(value), root: TreeNode::Leaf(value),
needs_update: false, needs_update: true,
} }
} }

View file

@ -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)] #[derive(Event)]
pub struct SendMeshEvent { pub struct SendMeshEvent {
@ -17,8 +20,7 @@ pub mod mesh_plugin {
impl Plugin for PluginInitializer { impl Plugin for PluginInitializer {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_systems(Update, (pan_orbit_camera, listen_send_mesh_event)); app.add_systems(Update, pan_orbit_camera);
app.add_event::<SendMeshEvent>();
} }
} }
@ -229,13 +231,23 @@ pub mod mesh_plugin {
} }
} }
fn setup(mut ev_sender: EventWriter<SendMeshEvent>, mut commands: Commands) { fn setup(mesh_assets: ResMut<Assets<Mesh>>, mut commands: Commands) {
ev_sender.send(SendMeshEvent { let oct = OctTree::new(Vec3::ZERO, Color::RED);
location: Vec3::new(0., 0., 0.), //commands.spawn(oct);
}); let mut model_oct = OctTreeModelComponent::new(oct, Some(mesh_assets));
ev_sender.send(SendMeshEvent {
location: Vec3::new(1., 0., 0.), 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 = let camera_and_light_transform =
Transform::from_xyz(1.8, 1.8, 1.8).looking_at(Vec3::ZERO, Vec3::Y); Transform::from_xyz(1.8, 1.8, 1.8).looking_at(Vec3::ZERO, Vec3::Y);