You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
159 lines
4.9 KiB
159 lines
4.9 KiB
use bevy::prelude::*; |
|
use noise_functions::{Noise, Simplex}; |
|
|
|
use crate::{ |
|
bloxel::{ |
|
prelude::*, |
|
storage::{ChunkedOctree, OctreeNode}, |
|
}, |
|
camera_controller::ControlledCamera, |
|
TerrainBlocks, |
|
}; |
|
|
|
pub struct WorldGenPlugin; |
|
|
|
impl Plugin for WorldGenPlugin { |
|
fn build(&self, app: &mut App) { |
|
app.init_resource::<ExistingChunks>().add_systems( |
|
Update, |
|
( |
|
create_chunks_around_camera, |
|
destroy_chunks_away_from_camera, |
|
generate_terrain, |
|
), |
|
); |
|
} |
|
} |
|
|
|
fn create_chunks_around_camera( |
|
mut commands: Commands, |
|
mut octree: ResMut<ExistingChunks>, |
|
camera: Single<&Transform, With<ControlledCamera>>, |
|
chunk_map: Single<Entity, With<ChunkMap>>, |
|
) { |
|
let block_pos = camera.translation.as_ivec3().into(); |
|
let (chunk_pos, _) = ChunkPos::from_block_pos(block_pos); |
|
|
|
let mut create_chunk = |octree: &mut ExistingChunks, pos: ChunkPos| { |
|
commands.entity(*chunk_map).with_child((Chunk, pos)); |
|
octree.update(pos, |_, children, parent| { |
|
let children = children.map(|a| a.as_slice()).unwrap_or_default(); |
|
*parent = if children.iter().all(|c| *c == Existing::All) { |
|
Existing::All |
|
} else { |
|
Existing::Some |
|
} |
|
}); |
|
}; |
|
|
|
// Create chunk at camera's position, if it's not already. This is necessary because we |
|
// need to "seed" the octree with a region so we have something to begin the search from. |
|
if octree.get(chunk_pos) == Existing::None { |
|
create_chunk(&mut octree, chunk_pos); |
|
} |
|
|
|
let to_create = octree |
|
.find(|node, exist| { |
|
(exist != Existing::All).then(|| -node.region().distance_to_squared(chunk_pos)) |
|
}) |
|
.take_while(|(_, _, neg_sqr_distance)| *neg_sqr_distance > -(12 * 12)) |
|
.map(|(chunk_pos, _, _)| chunk_pos) |
|
.take(12) // Create up to 12 chunks per system iteration. |
|
.collect::<Vec<_>>(); |
|
|
|
for chunk_pos in to_create { |
|
create_chunk(&mut octree, chunk_pos); |
|
} |
|
} |
|
|
|
fn destroy_chunks_away_from_camera( |
|
mut commands: Commands, |
|
mut octree: ResMut<ExistingChunks>, |
|
camera: Single<&Transform, With<ControlledCamera>>, |
|
chunk_map: Single<&ChunkMap>, |
|
) { |
|
let block_pos = camera.translation.as_ivec3().into(); |
|
let (chunk_pos, _) = ChunkPos::from_block_pos(block_pos); |
|
|
|
let distance = |node: OctreeNode| -> i32 { |
|
let (min, max) = node.region().into(); |
|
let a = (chunk_pos - min).length_squared(); |
|
let b = (chunk_pos - max).length_squared(); |
|
a.max(b) |
|
}; |
|
|
|
let to_destroy = octree |
|
.find(|node, exist| (exist != Existing::None).then(|| distance(node))) |
|
.take_while(|(_, _, neg_sqr_distance)| *neg_sqr_distance > 16 * 16) |
|
.map(|(chunk_pos, _, _)| chunk_pos) |
|
.collect::<Vec<_>>(); |
|
|
|
for chunk_pos in to_destroy { |
|
let chunk = *chunk_map.get(&chunk_pos).unwrap(); |
|
commands.entity(chunk).despawn(); |
|
octree.update(chunk_pos, |_, children, parent| { |
|
let children = children.map(|a| a.as_slice()).unwrap_or_default(); |
|
*parent = if children.iter().all(|c| *c == Existing::None) { |
|
Existing::None |
|
} else { |
|
Existing::Some |
|
} |
|
}); |
|
} |
|
} |
|
|
|
fn generate_terrain( |
|
mut commands: Commands, |
|
terrain_blocks: Option<Res<TerrainBlocks>>, |
|
chunks_without_data: Query<(Entity, &ChunkPos), (With<Chunk>, Without<ChunkData>)>, |
|
) { |
|
let Some(terrain) = terrain_blocks else { |
|
return; |
|
}; |
|
|
|
for (entity, chunk_pos) in chunks_without_data.iter() { |
|
let mut data = ChunkData::new(terrain.air); |
|
let size = data.size().as_ivec3(); |
|
for z in 0..size.z { |
|
for y in 0..size.y { |
|
for x in 0..size.x { |
|
let relative = IVec3::new(x, y, z); |
|
let block_pos = chunk_pos.to_block_pos(relative); |
|
let float_pos = Into::<IVec3>::into(block_pos).as_vec3(); |
|
|
|
let bias = ((float_pos.y + 32.) / 64.).clamp(-0.25, 1.); |
|
let sample = Simplex |
|
.fbm(3, 0.65, 2.0) |
|
.weighted(0.4) |
|
.frequency(0.01) |
|
.sample3(float_pos); |
|
|
|
if sample > bias { |
|
data.set(relative, terrain.rock); |
|
} |
|
} |
|
} |
|
} |
|
commands.entity(entity).insert(data); |
|
} |
|
} |
|
|
|
#[derive(Resource, Deref, DerefMut)] |
|
pub struct ExistingChunks { |
|
octree: ChunkedOctree<Existing>, |
|
} |
|
|
|
impl Default for ExistingChunks { |
|
fn default() -> Self { |
|
let octree = ChunkedOctree::new(5); |
|
Self { octree } |
|
} |
|
} |
|
|
|
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash, Debug)] |
|
pub enum Existing { |
|
#[default] |
|
None, |
|
Some, |
|
All, |
|
}
|
|
|