This commit is contained in:
Elias Stepanik 2025-06-08 18:27:36 +02:00
parent bef1e3d06d
commit 366381286a
2 changed files with 57 additions and 24 deletions

View File

@ -18,4 +18,5 @@ noise = "0.9.0"
itertools = "0.13.0" itertools = "0.13.0"
bitvec = "1.0.1" bitvec = "1.0.1"
smallvec = "1.14.0" smallvec = "1.14.0"
once_cell = "1.21.3" once_cell = "1.21.3"
rayon = "1.10.0"

View File

@ -1,42 +1,33 @@
use bevy::asset::RenderAssetUsages; use crate::plugins::big_space::big_space_plugin::RootGrid;
use bevy::pbr::wireframe::{Wireframe, WireframeColor}; use crate::plugins::environment::systems::voxels::structure::*;
use bevy::prelude::*; use bevy::prelude::*;
use bevy::render::mesh::*; use bevy::render::mesh::*;
use big_space::floating_origins::FloatingOrigin; use noise::{NoiseFn, Perlin};
use big_space::prelude::GridCell;
use noise::{Fbm, NoiseFn, Perlin};
use crate::plugins::big_space::big_space_plugin::RootGrid;
use crate::plugins::environment::systems::camera_system::CameraController;
use crate::plugins::environment::systems::planet_system::PlanetMaker;
use crate::plugins::environment::systems::voxels::structure::*;
pub fn setup( pub fn setup(
mut commands: Commands, mut commands: Commands,
root: Res<RootGrid>, root: Res<RootGrid>,
) { ) {
let unit_size = 1.0; let unit_size = 1.0_f32;
let octree_base_size = 64.0 * unit_size;
let octree_base_size = 64.0 * unit_size; // Octree's total size in your world space
let octree_depth = 10; let octree_depth = 10;
// 1. Create octree and wrap in Arc<Mutex<>> for thread-safe generation
let mut octree = SparseVoxelOctree::new(octree_depth, octree_base_size, false, false, false);
let mut octree = SparseVoxelOctree::new(octree_depth, octree_base_size as f32, false, false, false); // 2. Generate sphere in parallel, dropping the cloned Arc inside the function
let color = Color::rgb(0.2, 0.8, 0.2); let color = Color::rgb(0.2, 0.8, 0.2);
/*generate_voxel_rect(&mut octree,color);*/
generate_voxel_sphere(&mut octree, 100, color); generate_voxel_sphere(&mut octree, 100, color);
// 4. Spawn entity with both Transform and the real octree component
commands.entity(root.0).with_children(|parent| { commands.entity(root.0).with_children(|parent| {
parent.spawn( parent.spawn((Transform::default(), octree));
(
Transform::default(),
octree
)
);
}); });
} }
fn generate_voxel_sphere( fn generate_voxel_sphere(
octree: &mut SparseVoxelOctree, octree: &mut SparseVoxelOctree,
planet_radius: i32, planet_radius: i32,
@ -77,7 +68,6 @@ fn generate_voxel_sphere(
} }
/// Inserts a 16x256x16 "column" of voxels into the octree at (0,0,0) corner. /// Inserts a 16x256x16 "column" of voxels into the octree at (0,0,0) corner.
/// If you want it offset or centered differently, just adjust the for-loop ranges or offsets. /// If you want it offset or centered differently, just adjust the for-loop ranges or offsets.
fn generate_voxel_rect( fn generate_voxel_rect(
@ -151,3 +141,45 @@ fn generate_large_plane(
} }
} }
pub fn generate_solid_plane_with_noise(
octree: &mut SparseVoxelOctree,
width: usize,
depth: usize,
color: Color,
noise: &Perlin,
frequency: f32,
amplitude: f32,
) {
// Size of one voxel at the deepest level
let step = octree.get_spacing_at_depth(octree.max_depth);
for ix in 0..width {
let x = ix as f32;
for iz in 0..depth {
let z = iz as f32;
// Sample Perlin noise at scaled coordinates
let sample_x = x * frequency;
let sample_z = z * frequency;
let noise_val = noise.get([sample_x as f64, sample_z as f64]) as f32;
// Height in world units
let height_world = noise_val * amplitude;
// Convert height to number of voxel layers
let max_layer = (height_world / step).ceil() as usize;
// Fill from layer 0 up to max_layer
for iy in 0..=max_layer {
let position = Vec3::new(
x * step,
iy as f32 * step,
z * step,
);
let voxel = Voxel { color };
octree.insert(position, voxel);
}
}
}
}