Skip to content

Commit

Permalink
Minor updates, optimizations, version updates
Browse files Browse the repository at this point in the history
  • Loading branch information
davids91 committed Dec 9, 2024
1 parent e03ad55 commit b1cca99
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 113 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shocovox-rs"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
authors = ["Dávid Tóth <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand All @@ -26,8 +26,9 @@ image = { version = "0.25.1", optional = true }
show-image = { version = "0.14.0", optional = true }

# for example bevy_wgpu
bevy = { version = "0.14.0", features = [], optional = true}
iyes_perf_ui = { version = "0.3.0", features = [], optional = true}
bevy = { version = "0.15.0", features = [], optional = true}
#iyes_perf_ui = { version = "0.3.0", features = [], optional = true}
iyes_perf_ui = { git = "https://github.com/IyesGames/iyes_perf_ui.git", features = [], optional = true}

# debugging
#linker = "/usr/bin/clang"
Expand Down
70 changes: 35 additions & 35 deletions assets/shaders/viewport_render.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -301,28 +301,6 @@ fn request_node(node_meta_index: u32, child_octant: u32) -> bool {
return true;
}

//crate::spatial::math::step_octant
fn step_octant(octant: u32, step: ptr<function, vec3f>) -> u32 {
return (
(
OCTANT_STEP_RESULT_LUT[u32(sign((*step).x) + 1)][u32(sign((*step).y) + 1)][u32(sign((*step).z) + 1)]
& (0x0Fu << (4 * octant))
) >> (4 * octant)
) & 0x0Fu;
}

//crate::spatial::math::hash_direction
fn hash_direction(direction: vec3f) -> u32 {
return hash_region(vec3f(1.) + normalize(direction), 1.);
}

// Functionality-wise this function is more generic, than its counterpart
// and is used in voxel brick mapping too
//crate::spatial::math::flat_projection
fn flat_projection(i: vec3u, dimensions: vec2u) -> u32 {
return (i.x + (i.y * dimensions.y) + (i.z * dimensions.x * dimensions.y));
}

struct BrickHit{
hit: bool,
index: vec3u,
Expand Down Expand Up @@ -377,7 +355,11 @@ fn traverse_brick(

var mapped_index = (
brick_start_index * u32(dimension * dimension * dimension)
+ flat_projection(vec3u(current_index), vec2u(u32(dimension), u32(dimension)))
+ u32( //crate::spatial::math::flat_projection
current_index.x
+ (current_index.y * dimension)
+ (current_index.z * dimension * dimension)
)
);
if mapped_index >= arrayLength(&voxels)
{
Expand Down Expand Up @@ -537,7 +519,9 @@ fn traverse_node_for_ocbits(

fn get_by_ray(ray: ptr<function, Line>) -> OctreeRayIntersection {
var ray_scale_factors = get_dda_scale_factors(ray); // Should be const, but then it can't be passed as ptr
let direction_lut_index = hash_direction((*ray).direction);
let direction_lut_index = ( //crate::spatial::math::hash_direction
hash_region(vec3f(1.) + normalize((*ray).direction), 1.)
);

var node_stack: array<u32, NODE_STACK_SIZE>;
var node_stack_meta: u32 = 0;
Expand Down Expand Up @@ -714,18 +698,27 @@ fn get_by_ray(ray: ptr<function, Line>) -> OctreeRayIntersection {
if(EMPTY_MARKER != node_stack_last(node_stack_meta)){
current_node_key = node_stack[node_stack_last(node_stack_meta)];
current_node_meta = metadata[current_node_key];
target_octant = step_octant(
hash_region( // parent current target octant
// current bound center
current_bounds.min_position + vec3f(round(current_bounds.size / 2.))
- ( // parent bound min position
current_bounds.min_position
- (current_bounds.min_position % (current_bounds.size * 2.))
),
current_bounds.size

// target octant updated to hold the target octant of the parent node temporarily
target_octant = hash_region(
// current bound center
current_bounds.min_position + vec3f(round(current_bounds.size / 2.))
- ( // parent bound min position
current_bounds.min_position
- (current_bounds.min_position % (current_bounds.size * 2.))
),
&step_vec
current_bounds.size
);

// ..which is then stepped forward as if the parent was advancing
target_octant = ( //crate::spatial::math::step_octant
(
OCTANT_STEP_RESULT_LUT[u32(sign(step_vec.x) + 1)]
[u32(sign(step_vec.y) + 1)]
[u32(sign(step_vec.z) + 1)]
& (0x0Fu << (4 * target_octant))
) >> (4 * target_octant)
) & 0x0Fu;
current_bounds.size = round(current_bounds.size * 2.);
current_bounds.min_position -= current_bounds.min_position % current_bounds.size;
}
Expand Down Expand Up @@ -785,7 +778,14 @@ fn get_by_ray(ray: ptr<function, Line>) -> OctreeRayIntersection {
&target_bounds,
&ray_scale_factors
));
target_octant = step_octant(target_octant, &step_vec);
target_octant = ( //crate::spatial::math::step_octant
(
OCTANT_STEP_RESULT_LUT[u32(sign(step_vec.x) + 1)]
[u32(sign(step_vec.y) + 1)]
[u32(sign(step_vec.z) + 1)]
& (0x0Fu << (4 * target_octant))
) >> (4 * target_octant)
) & 0x0Fu;
if OOB_OCTANT != target_octant {
target_bounds = child_bounds_for(&current_bounds, target_octant);
target_child_key = node_children[(current_node_key * 8) + target_octant];
Expand Down
45 changes: 16 additions & 29 deletions examples/dot_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ use iyes_perf_ui::{

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

#[cfg(feature = "bevy_wgpu")]
const DISPLAY_RESOLUTION: [u32; 2] = [1024, 768];

#[cfg(feature = "bevy_wgpu")]
const BRICK_DIMENSION: usize = 2;
const BRICK_DIMENSION: usize = 16;

#[cfg(feature = "bevy_wgpu")]
const TREE_SIZE: u32 = 16;
const TREE_SIZE: u32 = 128;

#[cfg(feature = "bevy_wgpu")]
fn main() {
Expand Down Expand Up @@ -102,7 +102,7 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
let mut views = SvxViewSet::default();
let output_texture = host.create_new_view(
&mut views,
40,
45,
Viewport {
origin,
direction: (V3c::new(0., 0., 0.) - origin).normalized(),
Expand All @@ -113,15 +113,8 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
);
commands.insert_resource(host);
commands.insert_resource(views);
commands.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(1024., 768.)),
..default()
},
texture: output_texture,
..default()
});
commands.spawn(Camera2dBundle::default());
commands.spawn(Sprite::from_image(output_texture));
commands.spawn(Camera2d::default());
commands.spawn((
PerfUiRoot::default(),
PerfUiEntryFPS {
Expand All @@ -148,11 +141,12 @@ struct DomePosition {
yaw: f32,
roll: f32,
}

#[cfg(feature = "bevy_wgpu")]
fn rotate_camera(angles_query: Query<&mut DomePosition>, mut tree_view: ResMut<OctreeGPUView>) {
fn rotate_camera(angles_query: Query<&mut DomePosition>, view_set: ResMut<SvxViewSet>) {
let (yaw, roll) = (angles_query.single().yaw, angles_query.single().roll);
let radius = angles_query.single().radius;

let mut tree_view = view_set.views[0].lock().unwrap();
tree_view.spyglass.viewport.origin = V3c::new(
radius / 2. + yaw.sin() * radius,
radius + roll.sin() * radius * 2.,
Expand All @@ -165,10 +159,11 @@ fn rotate_camera(angles_query: Query<&mut DomePosition>, mut tree_view: ResMut<O
#[cfg(feature = "bevy_wgpu")]
fn handle_zoom(
keys: Res<ButtonInput<KeyCode>>,
mut tree_view: ResMut<OctreeGPUView>,
tree: ResMut<OctreeGPUHost<Albedo, BRICK_DIMENSION>>,
view_set: ResMut<SvxViewSet>,
mut angles_query: Query<&mut DomePosition>,
tree: Res<OctreeGPUHost<Albedo, BRICK_DIMENSION>>,
) {
let mut tree_view = view_set.views[0].lock().unwrap();
const ADDITION: f32 = 0.05;
let angle_update_fn = |angle, delta| -> f32 {
let new_angle = angle + delta;
Expand Down Expand Up @@ -240,12 +235,6 @@ fn handle_zoom(
img.save("example_junk_cpu_render.png").ok().unwrap();
}

let multiplier = if keys.pressed(KeyCode::ShiftLeft) {
10.0 // Doesn't have any effect?!
} else {
1.0
};

if keys.pressed(KeyCode::ArrowUp) {
angles_query.single_mut().roll = angle_update_fn(angles_query.single().roll, ADDITION);
}
Expand All @@ -259,18 +248,16 @@ fn handle_zoom(
angles_query.single_mut().yaw = angle_update_fn(angles_query.single().yaw, -ADDITION);
}
if keys.pressed(KeyCode::PageUp) {
angles_query.single_mut().radius *= 1. - 0.02 * multiplier;
angles_query.single_mut().radius *= 1. - 0.02;
}
if keys.pressed(KeyCode::PageDown) {
angles_query.single_mut().radius *= 1. + 0.02 * multiplier;
angles_query.single_mut().radius *= 1. + 0.02;
}
if keys.pressed(KeyCode::Home) {
tree_view.spyglass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.z *= 1. + 0.09;
}
if keys.pressed(KeyCode::End) {
tree_view.spyglass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.z *= 1. - 0.09;
}
}

Expand Down
27 changes: 6 additions & 21 deletions examples/minecraft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,8 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
);
commands.insert_resource(host);
commands.insert_resource(views);
commands.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(1024., 768.)),
..default()
},
texture: output_texture,
..default()
});
commands.spawn(Camera2dBundle::default());
commands.spawn(Sprite::from_image(output_texture));
commands.spawn(Camera2d::default());
commands.spawn((
PerfUiRoot::default(),
PerfUiEntryFPS {
Expand Down Expand Up @@ -222,12 +215,6 @@ fn handle_zoom(
img.save("example_junk_cpu_render.png").ok().unwrap();
}

let multiplier = if keys.pressed(KeyCode::ShiftLeft) {
10.0 // Doesn't have any effect?!
} else {
1.0
};

if keys.pressed(KeyCode::ArrowUp) {
angles_query.single_mut().roll = angle_update_fn(angles_query.single().roll, ADDITION);
}
Expand All @@ -241,18 +228,16 @@ fn handle_zoom(
angles_query.single_mut().yaw = angle_update_fn(angles_query.single().yaw, -ADDITION);
}
if keys.pressed(KeyCode::PageUp) {
angles_query.single_mut().radius *= 1. - 0.02 * multiplier;
angles_query.single_mut().radius *= 1. - 0.02;
}
if keys.pressed(KeyCode::PageDown) {
angles_query.single_mut().radius *= 1. + 0.02 * multiplier;
angles_query.single_mut().radius *= 1. + 0.02;
}
if keys.pressed(KeyCode::Home) {
tree_view.spyglass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.z *= 1. + 0.09;
}
if keys.pressed(KeyCode::End) {
tree_view.spyglass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
tree_view.spyglass.viewport.w_h_fov.z *= 1. - 0.09;
}
}

Expand Down
Loading

0 comments on commit b1cca99

Please sign in to comment.