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;
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();
}

View file

@ -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<Color>,
handle: Option<Handle<Mesh>>,
pub model: octtree::OctTree<Color>,
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
}
@ -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<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 {
let mut vertices = Vec::new();
let mut normals = Vec::new();

View file

@ -120,7 +120,7 @@ impl<T: Clone> OctTree<T> {
size: 1,
center: npos,
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)]
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::<SendMeshEvent>();
app.add_systems(Update, pan_orbit_camera);
}
}
@ -229,13 +231,23 @@ pub mod mesh_plugin {
}
}
fn setup(mut ev_sender: EventWriter<SendMeshEvent>, 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<Assets<Mesh>>, 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);