This commit is contained in:
Lillian Vixe 2024-04-21 14:12:53 -07:00
parent 0f25c2660e
commit c73e66fd64
4 changed files with 67 additions and 36 deletions

View file

@ -193,16 +193,24 @@ pub fn edit_click_events(
continue;
}
let edit_octtree = &mut octtree.unwrap().model;
let collision =
octtree_ray_overlap(edit_octtree, &obb, ray.origin, ray.direction.normalize());
let collision = {
let eoct = edit_octtree.read().unwrap();
octtree_ray_overlap(&eoct, &obb, ray.origin, ray.direction.normalize())
};
if let Some(_) = paint.filter(|input| input.pressed()) {
if collision.is_some() {
let col = collision.unwrap();
edit_octtree.set_voxel_at_location(
col.voxel_index_location,
color_from(shared_ui_state.brush_color),
);
{
let mut eoct = edit_octtree.write().unwrap();
eoct.set_voxel_at_location(
col.voxel_index_location,
color_from(shared_ui_state.brush_color),
);
}
let path = shared_ui_state.name_from_handle(oct.oct_handle.clone());
if let Some(path) = path {
shared_ui_state.set_grid_data(&path, oct.oct_handle.clone(), true);
@ -211,8 +219,10 @@ pub fn edit_click_events(
} else if let Some(_) = remove.filter(|input| input.pressed()) {
if collision.is_some() {
let col = collision.unwrap();
edit_octtree.remove_voxel(col.voxel_index_location);
{
let mut eoct = edit_octtree.write().unwrap();
eoct.remove_voxel(col.voxel_index_location);
}
let path = shared_ui_state.name_from_handle(oct.oct_handle.clone());
if let Some(path) = path {
shared_ui_state.set_grid_data(&path, oct.oct_handle.clone(), true);
@ -224,10 +234,14 @@ pub fn edit_click_events(
let hitloc = col.hit_data.hitpoint.local.clone();
let hitnorm = col.hit_data.normal.local.clone();
let hitvox = hitloc + (hitnorm * 0.25);
edit_octtree.set_voxel_at_location(
hitvox.round(),
color_from(shared_ui_state.brush_color),
);
{
let mut eoct = edit_octtree.write().unwrap();
eoct.set_voxel_at_location(
hitvox.round(),
color_from(shared_ui_state.brush_color),
);
}
let path = shared_ui_state.name_from_handle(oct.oct_handle.clone());
if let Some(path) = path {
shared_ui_state.set_grid_data(&path, oct.oct_handle.clone(), true);
@ -310,6 +324,7 @@ pub fn edit_window_ui(
shared_ui_state.window = pos;
}
}
response.show(contexts.ctx_mut(), |ui: &mut egui::Ui| {
//ui.ctx().set_pixels_per_point(0.1);
let mut label = String::from("Pre-Alpha");
@ -331,7 +346,11 @@ pub fn edit_window_ui(
let handle = each.id.clone();
let asset = oct_assets.get(handle);
if asset.is_some() {
let list = asset.unwrap().model.collect_voxels();
let list = {
let eoct = asset.unwrap().model.read().unwrap();
eoct.collect_voxels()
};
let mut count: Vec<(Color, usize)> = Vec::new();
fn eq(c1: &Color, c2: &mut Color) -> bool {
c1.r() == c2.r() && c1.g() == c2.g() && c1.b() == c2.b()
@ -375,8 +394,13 @@ pub fn edit_window_ui(
let tree = oct_assets.get_mut(id);
if tree.is_some() {
let tree = tree.unwrap();
tree.model
.set_voxel_at_location(Vec3::ZERO, color_from(shared_ui_state.brush_color));
{
let mut eoct = tree.model.write().unwrap();
eoct.set_voxel_at_location(
Vec3::ZERO,
color_from(shared_ui_state.brush_color),
);
}
}
}
}

View file

@ -0,0 +1 @@

View file

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, sync::RwLock};
use bevy::{
app::{App, Plugin, Update},
@ -28,7 +28,7 @@ use super::octtree::{self, Path};
#[derive(Asset, TypePath)]
pub struct OctTreeAsset {
pub model: octtree::OctTree<Color>,
pub model: RwLock<octtree::OctTree<Color>>,
pub handle: Option<Handle<Mesh>>,
pub task: Option<Task<CommandQueue>>,
}
@ -231,14 +231,17 @@ pub mod serialization {
return Err(OctLoadError::FileDataInvalid("".to_string()));
}
Ok(OctTreeAsset {
model: oct.unwrap(),
model: oct.unwrap().into(),
handle: None,
task: None,
})
}
pub fn save(asset: &OctTreeAsset) -> Option<Vec<u8>> {
let mut data: String = "".into();
let voxels = asset.model.collect_voxels();
let voxels = {
let read = asset.model.read().unwrap();
read.collect_voxels()
};
if voxels.len() == 0 {
return None;
}
@ -424,23 +427,26 @@ pub fn oct_asset_to_mesh(
oct_asset.task = None;
}
}
if oct_asset.model.needs_update {
if oct_asset.task.is_none() {
let thread_pool = AsyncComputeTaskPool::get();
let handle = pair.mesh_handle.clone();
let pre_collect = oct_asset.model.collect_voxels();
let task = thread_pool.spawn(async move {
let meshed = deferred_render(pre_collect);
let mut command_queue = CommandQueue::default();
command_queue.push(move |world: &mut World| {
let mut mesh_assets = world.resource_mut::<Assets<Mesh>>();
mesh_assets.insert(handle, meshed);
{
let mut writable = oct_asset.model.write().unwrap();
if writable.needs_update {
if oct_asset.task.is_none() {
let thread_pool = AsyncComputeTaskPool::get();
let handle = pair.mesh_handle.clone();
//let pre_collect = oct_asset.model.collect_voxels();
let copy = writable.clone();
let task = thread_pool.spawn(async move {
let meshed = deferred_render(copy.collect_voxels());
let mut command_queue = CommandQueue::default();
command_queue.push(move |world: &mut World| {
let mut mesh_assets = world.resource_mut::<Assets<Mesh>>();
mesh_assets.insert(handle, meshed);
});
return command_queue;
});
return command_queue;
});
oct_asset.task = Some(task);
oct_asset.model.needs_update = false;
oct_asset.task = Some(task);
writable.needs_update = false;
}
}
}
}

View file

@ -18,7 +18,7 @@ pub enum TreeNode<T> {
Empty,
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct OctTree<T> {
pub size: usize,
pub center: Vec3,