This commit is contained in:
2026-08-31 15:25:03 +02:00
parent dbda39b61a
commit 194cbf04ef
21 changed files with 1252658 additions and 325 deletions
+88
View File
@@ -0,0 +1,88 @@
use glam::Vec3;
use itertools::Itertools;
use crate::voxel::gpu::ExplicitNTreeNode;
use crate::voxel::gpu::StructurePointer;
use crate::voxel::sparse::Color;
pub struct SineGenerator<const N: usize>
{
chunk_power: usize,
}
pub fn map(x: f32, x_min: f32, x_max: f32, y_min: f32, y_max: f32) -> f32
{
((x - x_min) / (x_max - x_min)) * (y_max - y_min) + y_min
}
impl<const N: usize> SineGenerator<N>
where
[(); N * N * N]:,
{
pub fn new(chunk_power: usize) -> Self
{
SineGenerator { chunk_power }
}
pub fn produce_node(
&self,
depth: usize,
nx: usize,
ny: usize,
nz: usize,
) -> ExplicitNTreeNode<N>
{
let node_size = N.pow((self.chunk_power - depth) as u32);
let child_size = node_size / N;
let global_size = N.pow(self.chunk_power as u32);
let mut children = vec![];
let mut children_color = vec![];
let gnx = nx * node_size;
let gny = ny * node_size;
let gnz = nz * node_size;
// Iterate on children of this node
for ((cx, cy), cz) in (0..N).cartesian_product(0..N).cartesian_product(0..N)
{
let gvx = gnx + cx * child_size + (child_size / 2);
let gvy = gny + cy * child_size + (child_size / 2);
let gvz = gnz + cz * child_size + (child_size / 2);
let dist = Vec3::new(gvx as f32 - 128., gvy as f32 - 128., gvz as f32 - 128.).length();
let child_diagonal_length = (child_size as f32 / 2.) * f32::sqrt(3.);
let alpha;
if (dist - 128.).abs() <= child_diagonal_length
{
children.push(StructurePointer::new(depth <= 3, false, 0));
alpha = if dist > 128. { 0. } else { 1. };
}
else if dist > 128.
{
children.push(StructurePointer::new(false, false, 0));
alpha = 0.;
}
else
{
children.push(StructurePointer::new(false, false, 0));
alpha = 1.;
}
// let alpha = 1.;
// children.push(StructurePointer::new(depth <= 3, false, 0));
children_color.push(Color(
gvx as f32 / global_size as f32,
gvy as f32 / global_size as f32,
gvz as f32 / global_size as f32,
alpha,
));
}
ExplicitNTreeNode {
structure: std::array::from_fn(|i| children[i]),
colors: std::array::from_fn(|i| children_color[i]),
}
}
}