chunk gen now tries nodes first, also messed around a lot w rendering
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use bevy_ecs::system::IntoSystem;
|
||||
use nalgebra::Vector3;
|
||||
use ndarray::ArrayView3;
|
||||
|
||||
@@ -20,13 +19,13 @@ impl OctNode {
|
||||
pub const fn is_leaf(&self) -> bool {
|
||||
self.0 >= LEAF_BIT
|
||||
}
|
||||
pub fn is_node(&self) -> bool {
|
||||
pub const fn is_node(&self) -> bool {
|
||||
self.0 < LEAF_BIT
|
||||
}
|
||||
pub fn node_data(&self) -> u32 {
|
||||
pub const fn node_data(&self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
pub fn leaf_data(&self) -> u32 {
|
||||
pub const fn leaf_data(&self) -> u32 {
|
||||
self.0 & !LEAF_BIT
|
||||
}
|
||||
}
|
||||
@@ -57,17 +56,22 @@ impl OctTree {
|
||||
levels,
|
||||
}
|
||||
}
|
||||
pub fn from_fn_rec(f: &mut impl FnMut(Vector3<usize>) -> u32, levels: u32) -> OctTree {
|
||||
Self::from_fn_offset(f, levels, Vector3::from_element(0))
|
||||
pub fn from_fn_rec(
|
||||
f_leaf: &mut impl FnMut(Vector3<usize>) -> u32,
|
||||
f_node: &mut impl FnMut(Vector3<usize>, u32) -> Option<u32>,
|
||||
levels: u32,
|
||||
) -> OctTree {
|
||||
Self::from_fn_offset(f_leaf, f_node, levels, Vector3::from_element(0))
|
||||
}
|
||||
pub fn from_fn_offset(
|
||||
f: &mut impl FnMut(Vector3<usize>) -> u32,
|
||||
f_leaf: &mut impl FnMut(Vector3<usize>) -> u32,
|
||||
f_node: &mut impl FnMut(Vector3<usize>, u32) -> Option<u32>,
|
||||
levels: u32,
|
||||
offset: Vector3<usize>,
|
||||
) -> Self {
|
||||
let mut data = Vec::new();
|
||||
data.push(OctNode::new_node(0));
|
||||
Self::from_fn_offset_inner(f, &mut data, levels, offset);
|
||||
Self::from_fn_offset_inner(f_leaf, f_node, &mut data, levels, offset);
|
||||
if data.len() == 2 {
|
||||
data.remove(0);
|
||||
}
|
||||
@@ -78,17 +82,18 @@ impl OctTree {
|
||||
}
|
||||
}
|
||||
fn from_fn_offset_inner(
|
||||
f: &mut impl FnMut(Vector3<usize>) -> u32,
|
||||
f_leaf: &mut impl FnMut(Vector3<usize>) -> u32,
|
||||
f_node: &mut impl FnMut(Vector3<usize>, u32) -> Option<u32>,
|
||||
accumulator: &mut Vec<OctNode>,
|
||||
level: u32,
|
||||
offset: Vector3<usize>,
|
||||
) {
|
||||
if level == 0 {
|
||||
accumulator.push(OctNode::new_leaf(f(offset)));
|
||||
accumulator.push(OctNode::new_leaf(f_leaf(offset)));
|
||||
return;
|
||||
} else if level == 1 {
|
||||
let leaves: [OctNode; 8] =
|
||||
core::array::from_fn(|i| OctNode::new_leaf(f(offset + CORNERS[i])));
|
||||
core::array::from_fn(|i| OctNode::new_leaf(f_leaf(offset + CORNERS[i])));
|
||||
if leaves[1..].iter().all(|l| *l == leaves[0]) {
|
||||
accumulator.push(leaves[0]);
|
||||
} else {
|
||||
@@ -100,20 +105,21 @@ impl OctTree {
|
||||
accumulator.resize(i + 8, OctNode::new_node(0));
|
||||
let mut data_start = 0;
|
||||
for (j, corner_offset) in CORNERS.iter().enumerate() {
|
||||
let sub_start = accumulator.len();
|
||||
Self::from_fn_offset_inner(
|
||||
f,
|
||||
accumulator,
|
||||
level - 1,
|
||||
offset + corner_offset * 2usize.pow(level - 1),
|
||||
);
|
||||
let len = accumulator.len() - sub_start;
|
||||
if len == 1 {
|
||||
accumulator[i + j] = accumulator[sub_start];
|
||||
accumulator.pop();
|
||||
let lvl = level - 1;
|
||||
let pos = offset + corner_offset * 2usize.pow(lvl);
|
||||
if let Some(node) = f_node(pos, lvl) {
|
||||
accumulator[i + j] = OctNode::new_leaf(node);
|
||||
} else {
|
||||
accumulator[i + j] = OctNode::new_node(data_start as u32);
|
||||
data_start += len;
|
||||
let sub_start = accumulator.len();
|
||||
Self::from_fn_offset_inner(f_leaf, f_node, accumulator, lvl, pos);
|
||||
let len = accumulator.len() - sub_start;
|
||||
if len == 1 {
|
||||
accumulator[i + j] = accumulator[sub_start];
|
||||
accumulator.pop();
|
||||
} else {
|
||||
accumulator[i + j] = OctNode::new_node(data_start as u32);
|
||||
data_start += len;
|
||||
}
|
||||
}
|
||||
}
|
||||
if data_start == 0 {
|
||||
@@ -125,10 +131,7 @@ impl OctTree {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_fn_iter(
|
||||
f: &mut impl FnMut(Vector3<usize>) -> u32,
|
||||
levels: u32,
|
||||
) -> Self {
|
||||
pub fn from_fn_iter(f: &mut impl FnMut(Vector3<usize>) -> u32, levels: u32) -> Self {
|
||||
let mut data = vec![OctNode::new_node(0)];
|
||||
let mut level: usize = 1;
|
||||
let mut children = Vec::new();
|
||||
@@ -173,7 +176,7 @@ impl OctTree {
|
||||
}
|
||||
|
||||
pub fn from_arr(arr: ArrayView3<u32>, levels: u32) -> Self {
|
||||
Self::from_fn_rec(&mut |p| arr[(p.x, p.y, p.z)], levels)
|
||||
Self::from_fn_rec(&mut |p| arr[(p.x, p.y, p.z)], &mut |_, _| None, levels)
|
||||
}
|
||||
pub fn get(&self, mut pos: Vector3<usize>) -> u32 {
|
||||
let mut data_start = 1;
|
||||
@@ -199,7 +202,6 @@ impl OctTree {
|
||||
pub struct OctTreeIter<'a> {
|
||||
queue: Vec<OctNode>,
|
||||
levels: Vec<u32>,
|
||||
pos: usize,
|
||||
cur: u32,
|
||||
run: usize,
|
||||
data: &'a [OctNode],
|
||||
@@ -218,9 +220,8 @@ impl<'a> Iterator for OctTreeIter<'a> {
|
||||
self.run = 8usize.pow(level);
|
||||
self.cur = node.leaf_data();
|
||||
} else {
|
||||
let pos = 0;
|
||||
let add = &self.data[pos..pos + 8];
|
||||
self.data = &self.data[pos + DATA_OFFSET..];
|
||||
let add = &self.data[..8];
|
||||
self.data = &self.data[DATA_OFFSET..];
|
||||
self.queue.extend(add.iter().rev());
|
||||
self.levels.resize(self.levels.len() + 8, level - 1);
|
||||
}
|
||||
@@ -234,7 +235,6 @@ impl<'a> IntoIterator for &'a OctTree {
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
OctTreeIter {
|
||||
data: &self.data[1..],
|
||||
pos: 0,
|
||||
cur: 0,
|
||||
levels: vec![self.levels],
|
||||
run: 0,
|
||||
|
||||
Reference in New Issue
Block a user