Skip to content

Commit

Permalink
Refactor of bevy module, Clarified architecture and API
Browse files Browse the repository at this point in the history
  • Loading branch information
davids91 committed Nov 23, 2024
1 parent a07b479 commit 4bd70ef
Show file tree
Hide file tree
Showing 11 changed files with 370 additions and 518 deletions.
82 changes: 28 additions & 54 deletions examples/dot_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iyes_perf_ui::{

#[cfg(feature = "bevy_wgpu")]
use shocovox_rs::octree::{
raytracing::{OctreeGPUView, Ray, SvxRenderPlugin, Viewport},
raytracing::{OctreeGPUHost, OctreeGPUView, Ray, SvxRenderPlugin, Viewport},
Albedo, Octree, V3c, VoxelData,
};

Expand All @@ -22,15 +22,6 @@ const BRICK_DIMENSION: usize = 2;
#[cfg(feature = "bevy_wgpu")]
const TREE_SIZE: u32 = 16;

#[cfg(feature = "bevy_wgpu")]
#[derive(Resource)]
struct TreeResource<T, const DIM: usize>
where
T: Default + Clone + PartialEq + VoxelData,
{
tree: Octree<T, DIM>,
}

#[cfg(feature = "bevy_wgpu")]
fn main() {
App::new()
Expand All @@ -51,8 +42,6 @@ fn main() {

#[cfg(feature = "bevy_wgpu")]
fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
// use shocovox_rs::octree::raytracing::bevy::create_viewing_glass;

let origin = V3c::new(
TREE_SIZE as f32 * 2.,
TREE_SIZE as f32 / 2.,
Expand All @@ -64,19 +53,6 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
.ok()
.unwrap();

// +++ DEBUG +++
// tree.insert(&V3c::new(0, 0, 0), Albedo::from(0x66FFFF))
// .ok()
// .unwrap();
// tree.insert(&V3c::new(3, 3, 3), Albedo::from(0x66FFFF))
// .ok()
// .unwrap();
// assert!(tree.get(&V3c::new(3, 3, 3)).is_some());
// tree.insert_at_lod(&V3c::new(0, 0, 0), 128, Albedo::from(0x66FFFF))
// .ok()
// .unwrap();

// ---DEBUG ---
for x in 0..TREE_SIZE {
for y in 0..TREE_SIZE {
for z in 0..TREE_SIZE {
Expand Down Expand Up @@ -116,8 +92,15 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
}
}

let output_texture = tree.create_new_gpu_view(
42, //TODO: decide actual number
commands.spawn(DomePosition {
yaw: 0.,
roll: 0.,
radius: tree.get_size() as f32 * 2.2,
});

let host = OctreeGPUHost::new(tree);
let output_texture = host.create_new_view(
40, //TODO: decide actual number
Viewport {
origin,
direction: (V3c::new(0., 0., 0.) - origin).normalized(),
Expand All @@ -127,12 +110,7 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
&mut commands,
images,
);

commands.spawn(DomePosition {
yaw: 0.,
roll: 0.,
radius: tree.get_size() as f32 * 2.2,
});
commands.insert_resource(host);
commands.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(1024., 768.)),
Expand All @@ -142,7 +120,6 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
..default()
});
commands.spawn(Camera2dBundle::default());
commands.insert_resource(TreeResource { tree });
commands.spawn((
PerfUiRoot::default(),
PerfUiEntryFPS {
Expand Down Expand Up @@ -174,21 +151,21 @@ fn rotate_camera(angles_query: Query<&mut DomePosition>, mut tree_view: ResMut<O
let (yaw, roll) = (angles_query.single().yaw, angles_query.single().roll);
let radius = angles_query.single().radius;

tree_view.viewing_glass.viewport.origin = V3c::new(
tree_view.spyglass.viewport.origin = V3c::new(
radius / 2. + yaw.sin() * radius,
radius + roll.sin() * radius * 2.,
radius / 2. + yaw.cos() * radius,
);
tree_view.viewing_glass.viewport.direction =
(V3c::unit(radius / 2.) - tree_view.viewing_glass.viewport.origin).normalized();
tree_view.spyglass.viewport.direction =
(V3c::unit(radius / 2.) - tree_view.spyglass.viewport.origin).normalized();
}

#[cfg(feature = "bevy_wgpu")]
fn handle_zoom(
keys: Res<ButtonInput<KeyCode>>,
mut tree_view: ResMut<OctreeGPUView>,
mut angles_query: Query<&mut DomePosition>,
tree: Res<TreeResource<Albedo, BRICK_DIMENSION>>,
tree: Res<OctreeGPUHost<Albedo, BRICK_DIMENSION>>,
) {
if keys.pressed(KeyCode::Delete) {
tree_view.do_the_thing = true;
Expand All @@ -209,17 +186,16 @@ fn handle_zoom(
// Render the current view with CPU
let viewport_up_direction = V3c::new(0., 1., 0.);
let viewport_right_direction = viewport_up_direction
.cross(tree_view.viewing_glass.viewport.direction)
.cross(tree_view.spyglass.viewport.direction)
.normalized();
let pixel_width =
tree_view.viewing_glass.viewport.w_h_fov.x as f32 / DISPLAY_RESOLUTION[0] as f32;
tree_view.spyglass.viewport.w_h_fov.x as f32 / DISPLAY_RESOLUTION[0] as f32;
let pixel_height =
tree_view.viewing_glass.viewport.w_h_fov.y as f32 / DISPLAY_RESOLUTION[1] as f32;
let viewport_bottom_left = tree_view.viewing_glass.viewport.origin
+ (tree_view.viewing_glass.viewport.direction
* tree_view.viewing_glass.viewport.w_h_fov.z)
- (viewport_up_direction * (tree_view.viewing_glass.viewport.w_h_fov.y / 2.))
- (viewport_right_direction * (tree_view.viewing_glass.viewport.w_h_fov.x / 2.));
tree_view.spyglass.viewport.w_h_fov.y as f32 / DISPLAY_RESOLUTION[1] as f32;
let viewport_bottom_left = tree_view.spyglass.viewport.origin
+ (tree_view.spyglass.viewport.direction * tree_view.spyglass.viewport.w_h_fov.z)
- (viewport_up_direction * (tree_view.spyglass.viewport.w_h_fov.y / 2.))
- (viewport_right_direction * (tree_view.spyglass.viewport.w_h_fov.x / 2.));

// define light
let diffuse_light_normal = V3c::new(0., -1., 1.).normalized();
Expand All @@ -237,8 +213,8 @@ fn handle_zoom(
+ viewport_right_direction * x as f32 * pixel_width
+ viewport_up_direction * y as f32 * pixel_height;
let ray = Ray {
origin: tree_view.viewing_glass.viewport.origin,
direction: (glass_point - tree_view.viewing_glass.viewport.origin).normalized(),
origin: tree_view.spyglass.viewport.origin,
direction: (glass_point - tree_view.spyglass.viewport.origin).normalized(),
};

use std::io::Write;
Expand Down Expand Up @@ -282,11 +258,9 @@ fn handle_zoom(
}
if keys.pressed(KeyCode::ArrowLeft) {
angles_query.single_mut().yaw = angle_update_fn(angles_query.single().yaw, ADDITION);
// println!("viewport: {:?}", viewing_glass.viewport);
}
if keys.pressed(KeyCode::ArrowRight) {
angles_query.single_mut().yaw = angle_update_fn(angles_query.single().yaw, -ADDITION);
// println!("viewport: {:?}", viewing_glass.viewport);
}
if keys.pressed(KeyCode::PageUp) {
angles_query.single_mut().radius *= 1. - 0.02 * multiplier;
Expand All @@ -295,12 +269,12 @@ fn handle_zoom(
angles_query.single_mut().radius *= 1. + 0.02 * multiplier;
}
if keys.pressed(KeyCode::Home) {
tree_view.viewing_glass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
tree_view.viewing_glass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
}
if keys.pressed(KeyCode::End) {
tree_view.viewing_glass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
tree_view.viewing_glass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
}
}

Expand Down
55 changes: 21 additions & 34 deletions examples/minecraft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bevy::{prelude::*, window::WindowPlugin};

#[cfg(feature = "bevy_wgpu")]
use shocovox_rs::octree::{
raytracing::{OctreeGPUView, Ray, SvxRenderPlugin, Viewport},
raytracing::{OctreeGPUHost, OctreeGPUView, Ray, SvxRenderPlugin, Viewport},
Albedo, Octree, V3c, VoxelData,
};

Expand All @@ -20,15 +20,6 @@ const DISPLAY_RESOLUTION: [u32; 2] = [1024, 768];
#[cfg(feature = "bevy_wgpu")]
const BRICK_DIMENSION: usize = 32;

#[cfg(feature = "bevy_wgpu")]
#[derive(Resource)]
struct TreeResource<T, const DIM: usize>
where
T: Default + Clone + PartialEq + VoxelData,
{
tree: Octree<T, DIM>,
}

#[cfg(feature = "bevy_wgpu")]
fn main() {
App::new()
Expand Down Expand Up @@ -79,7 +70,8 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
radius: tree.get_size() as f32 * 2.2,
});

let output_texture = tree.create_new_gpu_view(
let host = OctreeGPUHost::new(tree);
let output_texture = host.create_new_view(
20,
Viewport {
origin: V3c {
Expand All @@ -98,7 +90,7 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
&mut commands,
images,
);

commands.insert_resource(host);
commands.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(1024., 768.)),
Expand All @@ -108,8 +100,6 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
..default()
});
commands.spawn(Camera2dBundle::default());
commands.insert_resource(TreeResource { tree });

commands.spawn((
PerfUiRoot::default(),
PerfUiEntryFPS {
Expand Down Expand Up @@ -142,21 +132,21 @@ fn rotate_camera(angles_query: Query<&mut DomePosition>, mut tree_view: ResMut<O
let (yaw, roll) = (angles_query.single().yaw, angles_query.single().roll);
let radius = angles_query.single().radius;

tree_view.viewing_glass.viewport.origin = V3c::new(
tree_view.spyglass.viewport.origin = V3c::new(
radius / 2. + yaw.sin() * radius,
radius + roll.sin() * radius * 2.,
radius / 2. + yaw.cos() * radius,
);
tree_view.viewing_glass.viewport.direction =
(V3c::unit(radius / 2.) - tree_view.viewing_glass.viewport.origin).normalized();
tree_view.spyglass.viewport.direction =
(V3c::unit(radius / 2.) - tree_view.spyglass.viewport.origin).normalized();
}

#[cfg(feature = "bevy_wgpu")]
fn handle_zoom(
keys: Res<ButtonInput<KeyCode>>,
mut tree_view: ResMut<OctreeGPUView>,
mut angles_query: Query<&mut DomePosition>,
tree: Res<TreeResource<Albedo, BRICK_DIMENSION>>,
tree: Res<OctreeGPUHost<Albedo, BRICK_DIMENSION>>,
) {
if keys.pressed(KeyCode::Delete) {
tree_view.do_the_thing = true;
Expand All @@ -177,17 +167,16 @@ fn handle_zoom(
// Render the current view with CPU
let viewport_up_direction = V3c::new(0., 1., 0.);
let viewport_right_direction = viewport_up_direction
.cross(tree_view.viewing_glass.viewport.direction)
.cross(tree_view.spyglass.viewport.direction)
.normalized();
let pixel_width =
tree_view.viewing_glass.viewport.w_h_fov.x as f32 / DISPLAY_RESOLUTION[0] as f32;
tree_view.spyglass.viewport.w_h_fov.x as f32 / DISPLAY_RESOLUTION[0] as f32;
let pixel_height =
tree_view.viewing_glass.viewport.w_h_fov.y as f32 / DISPLAY_RESOLUTION[1] as f32;
let viewport_bottom_left = tree_view.viewing_glass.viewport.origin
+ (tree_view.viewing_glass.viewport.direction
* tree_view.viewing_glass.viewport.w_h_fov.z)
- (viewport_up_direction * (tree_view.viewing_glass.viewport.w_h_fov.y / 2.))
- (viewport_right_direction * (tree_view.viewing_glass.viewport.w_h_fov.x / 2.));
tree_view.spyglass.viewport.w_h_fov.y as f32 / DISPLAY_RESOLUTION[1] as f32;
let viewport_bottom_left = tree_view.spyglass.viewport.origin
+ (tree_view.spyglass.viewport.direction * tree_view.spyglass.viewport.w_h_fov.z)
- (viewport_up_direction * (tree_view.spyglass.viewport.w_h_fov.y / 2.))
- (viewport_right_direction * (tree_view.spyglass.viewport.w_h_fov.x / 2.));

// define light
let diffuse_light_normal = V3c::new(0., -1., 1.).normalized();
Expand All @@ -205,8 +194,8 @@ fn handle_zoom(
+ viewport_right_direction * x as f32 * pixel_width
+ viewport_up_direction * y as f32 * pixel_height;
let ray = Ray {
origin: tree_view.viewing_glass.viewport.origin,
direction: (glass_point - tree_view.viewing_glass.viewport.origin).normalized(),
origin: tree_view.spyglass.viewport.origin,
direction: (glass_point - tree_view.spyglass.viewport.origin).normalized(),
};

use std::io::Write;
Expand Down Expand Up @@ -250,11 +239,9 @@ fn handle_zoom(
}
if keys.pressed(KeyCode::ArrowLeft) {
angles_query.single_mut().yaw = angle_update_fn(angles_query.single().yaw, ADDITION);
// println!("viewport: {:?}", viewing_glass.viewport);
}
if keys.pressed(KeyCode::ArrowRight) {
angles_query.single_mut().yaw = angle_update_fn(angles_query.single().yaw, -ADDITION);
// println!("viewport: {:?}", viewing_glass.viewport);
}
if keys.pressed(KeyCode::PageUp) {
angles_query.single_mut().radius *= 1. - 0.02 * multiplier;
Expand All @@ -263,12 +250,12 @@ fn handle_zoom(
angles_query.single_mut().radius *= 1. + 0.02 * multiplier;
}
if keys.pressed(KeyCode::Home) {
tree_view.viewing_glass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
tree_view.viewing_glass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
}
if keys.pressed(KeyCode::End) {
tree_view.viewing_glass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
tree_view.viewing_glass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/octree/convert/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,6 @@ where
)?;
let node_children = Vec::decode_bencode_object(list.next_object()?.unwrap())?;
Ok(Self {
#[cfg(feature = "bevy_wgpu")]
views: Vec::new(),
auto_simplify,
octree_size: root_size,
nodes,
Expand Down
2 changes: 0 additions & 2 deletions src/octree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ where
let root_node_key = nodes.push(NodeContent::Nothing); // The first element is the root Node
assert!(root_node_key == 0);
Ok(Self {
#[cfg(feature = "bevy_wgpu")]
views: Vec::new(),
auto_simplify: true,
octree_size: size,
nodes,
Expand Down
6 changes: 3 additions & 3 deletions src/octree/raytracing/bevy/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::object_pool::empty_marker;
use crate::spatial::math::flat_projection;
use crate::{
octree::{
raytracing::bevy::types::{SvxRenderData, Voxelement},
raytracing::bevy::types::{OctreeRenderData, Voxelement},
types::{NodeChildrenArray, NodeContent},
BrickData, Octree, VoxelData,
},
Expand Down Expand Up @@ -64,7 +64,7 @@ impl VictimPointer {
/// Provides the first available index in the metadata buffer which can be overwritten
/// with node related information. It never returns with 0, because that is reserved for the root node,
/// which should not be overwritten.
fn first_available_node(&mut self, render_data: &mut SvxRenderData) -> usize {
fn first_available_node(&mut self, render_data: &mut OctreeRenderData) -> usize {
// If there is space left in the cache, use it all up
if !self.is_full() {
render_data.metadata[self.stored_items] |= 0x01;
Expand Down Expand Up @@ -131,7 +131,7 @@ impl VictimPointer {
}

/// Finds the first available brick, and marks it as used
fn first_available_brick(&mut self, render_data: &mut SvxRenderData) -> usize {
fn first_available_brick(&mut self, render_data: &mut OctreeRenderData) -> usize {
let max_brick_count = render_data.metadata.len() * 8;

// If there is space left in the cache, use it all up
Expand Down
Loading

0 comments on commit 4bd70ef

Please sign in to comment.