Skip to content

Commit

Permalink
Merge pull request #57 from davids91/optimize/bitmaps
Browse files Browse the repository at this point in the history
Resolves #54 - Restructure of occupancy bitmaps
  • Loading branch information
davids91 authored Nov 5, 2024
2 parents 276820f + cd7bcf6 commit 4c7d89f
Show file tree
Hide file tree
Showing 22 changed files with 1,387 additions and 1,283 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shocovox-rs"
version = "0.4.0"
version = "0.4.1"
edition = "2021"
authors = ["Dávid Tóth <[email protected]>"]
license = "MIT OR Apache-2.0"
Expand Down
310 changes: 105 additions & 205 deletions assets/shaders/viewport_render.wgsl

Large diffs are not rendered by default.

108 changes: 56 additions & 52 deletions examples/dot_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ use shocovox_rs::octree::{
raytracing::{
bevy::create_viewing_glass, ShocoVoxRenderPlugin, ShocoVoxViewingGlass, Viewport,
},
Albedo, Octree, V3c,
Albedo, V3c,
};

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

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

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

#[cfg(feature = "bevy_wgpu")]
fn main() {
Expand Down Expand Up @@ -53,56 +53,60 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
);

// fill octree with data
let tree_path = "example_junk_dotcube";
let mut tree;
if std::path::Path::new(tree_path).exists() {
tree = Octree::<Albedo, BRICK_DIMENSION>::load(&tree_path)
.ok()
.unwrap();
} else {
tree = shocovox_rs::octree::Octree::<Albedo, BRICK_DIMENSION>::new(TREE_SIZE)
.ok()
.unwrap();

tree.insert(&V3c::new(1, 3, 3), Albedo::from(0x66FFFF))
.ok()
.unwrap();
for x in 0..TREE_SIZE {
for y in 0..TREE_SIZE {
for z in 0..TREE_SIZE {
if ((x < (TREE_SIZE / 4) || y < (TREE_SIZE / 4) || z < (TREE_SIZE / 4))
&& (0 == x % 2 && 0 == y % 4 && 0 == z % 2))
|| ((TREE_SIZE / 2) <= x && (TREE_SIZE / 2) <= y && (TREE_SIZE / 2) <= z)
{
let r = if 0 == x % (TREE_SIZE / 4) {
(x as f32 / TREE_SIZE as f32 * 255.) as u32
} else {
128
};
let g = if 0 == y % (TREE_SIZE / 4) {
(y as f32 / TREE_SIZE as f32 * 255.) as u32
} else {
128
};
let b = if 0 == z % (TREE_SIZE / 4) {
(z as f32 / TREE_SIZE as f32 * 255.) as u32
} else {
128
};
tree.insert(
&V3c::new(x, y, z),
Albedo::default()
.with_red(r as u8)
.with_green(g as u8)
.with_blue(b as u8),
)
.ok()
.unwrap();
}
let mut tree = shocovox_rs::octree::Octree::<Albedo, BRICK_DIMENSION>::new(TREE_SIZE)
.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 {
if ((x < (TREE_SIZE / 4) || y < (TREE_SIZE / 4) || z < (TREE_SIZE / 4))
&& (0 == x % 2 && 0 == y % 4 && 0 == z % 2))
|| ((TREE_SIZE / 2) <= x && (TREE_SIZE / 2) <= y && (TREE_SIZE / 2) <= z)
{
let r = if 0 == x % (TREE_SIZE / 4) {
(x as f32 / TREE_SIZE as f32 * 255.) as u32
} else {
128
};
let g = if 0 == y % (TREE_SIZE / 4) {
(y as f32 / TREE_SIZE as f32 * 255.) as u32
} else {
128
};
let b = if 0 == z % (TREE_SIZE / 4) {
(z as f32 / TREE_SIZE as f32 * 255.) as u32
} else {
128
};
// println!("Inserting at: {:?}", (x, y, z));
tree.insert(
&V3c::new(x, y, z),
Albedo::default()
.with_red(r as u8)
.with_green(g as u8)
.with_blue(b as u8)
.with_alpha(255),
)
.ok()
.unwrap();
assert!(tree.get(&V3c::new(x, y, z)).is_some());
}
}
}
tree.save(&tree_path).ok().unwrap();
}

let render_data = tree.create_bevy_view();
Expand Down Expand Up @@ -162,7 +166,7 @@ struct DomePosition {

#[cfg(feature = "bevy_wgpu")]
fn rotate_camera(
mut angles_query: Query<&mut DomePosition>,
angles_query: Query<&mut DomePosition>,
mut viewing_glass: ResMut<ShocoVoxViewingGlass>,
) {
let (yaw, roll) = (angles_query.single().yaw, angles_query.single().roll);
Expand All @@ -183,7 +187,7 @@ fn handle_zoom(
mut viewing_glass: ResMut<ShocoVoxViewingGlass>,
mut angles_query: Query<&mut DomePosition>,
) {
const ADDITION: f32 = 0.005;
const ADDITION: f32 = 0.02;
let angle_update_fn = |angle, delta| -> f32 {
let new_angle = angle + delta;
if new_angle < 360. {
Expand Down
39 changes: 6 additions & 33 deletions src/octree/convert/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ where
match self {
BrickData::Empty => encoder.emit_str("#b"),
BrickData::Solid(voxel) => encoder.emit_list(|e| {
e.emit_str("##b")?;
e.emit_str("#b#")?;
Self::encode_single(voxel, e)
}),
BrickData::Parted(brick) => encoder.emit_list(|e| {
e.emit_str("###b")?;
e.emit_str("##b#")?;
for z in 0..DIM {
for y in 0..DIM {
for x in 0..DIM {
Expand Down Expand Up @@ -61,8 +61,8 @@ where
.unwrap_or("".to_string())
.as_str()
{
"##b" => Ok(true), // The content is a single voxel
"###b" => Ok(false), // The content is a brick of voxels
"#b#" => Ok(true), // The content is a single voxel
"##b#" => Ok(false), // The content is a brick of voxels
misc => Err(bendy::decoding::Error::unexpected_token(
"A NodeContent Identifier string, which is either # or ##",
"The string ".to_owned() + misc,
Expand Down Expand Up @@ -225,15 +225,15 @@ where
if !is_leaf && !is_uniform {
let occupied_bits;
match list.next_object()?.unwrap() {
Object::Integer(i) => occupied_bits = i.parse::<u32>().ok().unwrap(),
Object::Integer(i) => occupied_bits = i.parse::<u64>().ok().unwrap(),
_ => {
return Err(bendy::decoding::Error::unexpected_token(
"int field for Internal Node Occupancy bitmap",
"Something else",
))
}
};
return Ok(NodeContent::Internal(occupied_bits as u8));
return Ok(NodeContent::Internal(occupied_bits));
}

if is_leaf && !is_uniform {
Expand Down Expand Up @@ -305,17 +305,6 @@ impl ToBencode for NodeChildren<u32> {
e.emit_str("##b##")?;
e.emit(map)
}),
NodeChildrenArray::OccupancyBitmaps(maps) => encoder.emit_list(|e| {
e.emit_str("##bs##")?;
e.emit(maps[0])?;
e.emit(maps[1])?;
e.emit(maps[2])?;
e.emit(maps[3])?;
e.emit(maps[4])?;
e.emit(maps[5])?;
e.emit(maps[6])?;
e.emit(maps[7])
}),
}
}
}
Expand Down Expand Up @@ -347,22 +336,6 @@ impl FromBencode for NodeChildren<u32> {
list.next_object()?.unwrap(),
)?),
}),
"##bs##" => {
let mut c = Vec::new();
for _ in 0..8 {
c.push(
u64::decode_bencode_object(list.next_object()?.unwrap())
.ok()
.unwrap(),
);
}
Ok(NodeChildren {
empty_marker: empty_marker(),
content: NodeChildrenArray::OccupancyBitmaps(
c.try_into().ok().unwrap(),
),
})
}
s => Err(bendy::decoding::Error::unexpected_token(
"A NodeChildren marker, either ##b##, ##bs## or ##c##",
s,
Expand Down
Loading

0 comments on commit 4c7d89f

Please sign in to comment.