misc
This commit is contained in:
@@ -68,6 +68,7 @@ pub mod prelude {
|
|||||||
pub first_layer_mask: bool,
|
pub first_layer_mask: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct Chunk {
|
pub struct Chunk {
|
||||||
pub heights: [f32; Chunk::SIZE * Chunk::SIZE],
|
pub heights: [f32; Chunk::SIZE * Chunk::SIZE],
|
||||||
pub textures: [[u32; 2]; Chunk::SIZE * Chunk::SIZE],
|
pub textures: [[u32; 2]; Chunk::SIZE * Chunk::SIZE],
|
||||||
@@ -80,7 +81,7 @@ pub mod prelude {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
heights: [0.; Chunk::SIZE * Chunk::SIZE],
|
heights: [0.; Chunk::SIZE * Chunk::SIZE],
|
||||||
textures: [[0;2]; Chunk::SIZE * Chunk::SIZE],
|
textures: [[0; 2]; Chunk::SIZE * Chunk::SIZE],
|
||||||
moisture: [0.; Chunk::SIZE * Chunk::SIZE],
|
moisture: [0.; Chunk::SIZE * Chunk::SIZE],
|
||||||
temperature: [0.; Chunk::SIZE * Chunk::SIZE],
|
temperature: [0.; Chunk::SIZE * Chunk::SIZE],
|
||||||
chunk_offset: Default::default(),
|
chunk_offset: Default::default(),
|
||||||
@@ -95,7 +96,7 @@ pub mod prelude {
|
|||||||
pub const WORLD_SIZE: Vec2 = Vec2::new(Chunk::WORLD_WIDTH, Chunk::WORLD_HEIGHT);
|
pub const WORLD_SIZE: Vec2 = Vec2::new(Chunk::WORLD_WIDTH, Chunk::WORLD_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource)]
|
#[derive(Resource, Clone)]
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
pub chunks: Vec<Chunk>,
|
pub chunks: Vec<Chunk>,
|
||||||
pub height: usize,
|
pub height: usize,
|
||||||
|
|||||||
@@ -1,26 +1,17 @@
|
|||||||
use std::thread;
|
|
||||||
|
|
||||||
use bevy::ecs::system::CommandQueue;
|
use bevy::ecs::system::CommandQueue;
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::tasks::futures_lite::future;
|
use bevy::tasks::futures_lite::future;
|
||||||
use bevy::tasks::*;
|
use bevy::tasks::*;
|
||||||
use bevy_rapier3d::geometry::Collider;
|
use bevy_rapier3d::geometry::Collider;
|
||||||
use bevy_rapier3d::geometry::TriMeshFlags;
|
use bevy_rapier3d::geometry::TriMeshFlags;
|
||||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
use world_generation::prelude::Map;
|
||||||
use world_generation::{
|
|
||||||
biome_painter::BiomePainterAsset,
|
|
||||||
hex_utils::SHORT_DIAGONAL,
|
|
||||||
prelude::{Chunk, Map},
|
|
||||||
tile_manager::TileAsset,
|
|
||||||
tile_mapper::TileMapperAsset,
|
|
||||||
};
|
|
||||||
|
|
||||||
|
use crate::prelude::RebuildChunk;
|
||||||
use crate::{
|
use crate::{
|
||||||
prelude::{ChunkAtlas, PhosChunk, PhosChunkRegistry},
|
prelude::{PhosChunk, PhosChunkRegistry},
|
||||||
utlis::{chunk_utils::prepare_chunk_mesh, render_distance_system::RenderDistanceVisibility},
|
utlis::chunk_utils::prepare_chunk_mesh,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::prelude::CurrentBiomePainter;
|
|
||||||
pub struct ChunkRebuildPlugin;
|
pub struct ChunkRebuildPlugin;
|
||||||
|
|
||||||
impl Plugin for ChunkRebuildPlugin {
|
impl Plugin for ChunkRebuildPlugin {
|
||||||
@@ -37,81 +28,55 @@ pub struct ChunkRebuildQueue {
|
|||||||
pub queue: Vec<usize>,
|
pub queue: Vec<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
//Todo: Re-use existing entity/collider until new collider is generated
|
|
||||||
fn chunk_rebuilder(
|
fn chunk_rebuilder(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut queue: ResMut<ChunkRebuildQueue>,
|
chunk_query: Query<(Entity, &PhosChunk), With<RebuildChunk>>,
|
||||||
mut chunks: ResMut<PhosChunkRegistry>,
|
|
||||||
atlas: Res<ChunkAtlas>,
|
|
||||||
heightmap: Res<Map>,
|
heightmap: Res<Map>,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
) {
|
) {
|
||||||
if queue.queue.len() == 0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
queue.queue.dedup();
|
|
||||||
|
|
||||||
let chunk_indices = queue.queue.clone();
|
|
||||||
let pool = AsyncComputeTaskPool::get();
|
let pool = AsyncComputeTaskPool::get();
|
||||||
|
|
||||||
for chunk_index in &queue.queue {
|
for (chunk, idx) in &chunk_query {
|
||||||
let chunk = chunks.chunks[*chunk_index];
|
#[cfg(feature = "tracing")]
|
||||||
// commands.entity(chunk).remove::<Handle<Mesh>>();
|
let _spawn_span = info_span!("Rebuild Chunk").entered();
|
||||||
commands.entity(chunk).despawn();
|
let map = heightmap.clone();
|
||||||
}
|
let chunk_index = idx.index;
|
||||||
|
|
||||||
let chunk_meshes: Vec<_> = queue
|
|
||||||
.queue
|
|
||||||
.par_iter()
|
|
||||||
.map(|idx| {
|
|
||||||
return prepare_chunk_mesh(&heightmap.chunks[*idx], &heightmap);
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
for (mesh, collider_data, pos, index) in chunk_meshes {
|
|
||||||
let mut chunk = commands.spawn((
|
|
||||||
MaterialMeshBundle {
|
|
||||||
mesh: meshes.add(mesh),
|
|
||||||
material: atlas.chunk_material_handle.clone(),
|
|
||||||
transform: Transform::from_translation(pos),
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
PhosChunk::new(index),
|
|
||||||
RenderDistanceVisibility::default().with_offset(Vec3::new(
|
|
||||||
(Chunk::SIZE / 2) as f32 * SHORT_DIAGONAL,
|
|
||||||
0.,
|
|
||||||
(Chunk::SIZE / 2) as f32 * 1.5,
|
|
||||||
)),
|
|
||||||
));
|
|
||||||
let entity = chunk.id();
|
|
||||||
let task = pool.spawn(async move {
|
let task = pool.spawn(async move {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
let _spawn_span = info_span!("Rebuild Task").entered();
|
||||||
let mut queue = CommandQueue::default();
|
let mut queue = CommandQueue::default();
|
||||||
|
let (mesh, collider_data, _, _) = prepare_chunk_mesh(&map.chunks[chunk_index], &map);
|
||||||
let c = Collider::trimesh_with_flags(
|
let c = Collider::trimesh_with_flags(
|
||||||
collider_data.0,
|
collider_data.0,
|
||||||
collider_data.1,
|
collider_data.1,
|
||||||
TriMeshFlags::DELETE_DUPLICATE_TRIANGLES,
|
TriMeshFlags::DELETE_DUPLICATE_TRIANGLES,
|
||||||
);
|
);
|
||||||
queue.push(move |world: &mut World| {
|
queue.push(move |world: &mut World| {
|
||||||
world.entity_mut(entity).insert(c).remove::<ColliderTask>();
|
world.entity_mut(chunk).insert(c).remove::<ChunkRebuildTask>();
|
||||||
});
|
});
|
||||||
|
|
||||||
return queue;
|
return (queue, mesh);
|
||||||
});
|
});
|
||||||
chunk.insert(ColliderTask { task });
|
commands
|
||||||
chunks.chunks[index] = chunk.id();
|
.entity(chunk)
|
||||||
|
.insert(ChunkRebuildTask { task })
|
||||||
|
.remove::<RebuildChunk>();
|
||||||
}
|
}
|
||||||
queue.queue.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collider_task_resolver(mut chunks: Query<&mut ColliderTask, With<PhosChunk>>, mut commands: Commands) {
|
fn collider_task_resolver(
|
||||||
for mut task in &mut chunks {
|
mut chunks: Query<(&mut ChunkRebuildTask, &Handle<Mesh>), With<PhosChunk>>,
|
||||||
if let Some(mut c) = block_on(future::poll_once(&mut task.task)) {
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
) {
|
||||||
|
for (mut task, mesh_handle) in &mut chunks {
|
||||||
|
if let Some((mut c, mesh)) = block_on(future::poll_once(&mut task.task)) {
|
||||||
commands.append(&mut c);
|
commands.append(&mut c);
|
||||||
|
meshes.insert(mesh_handle, mesh);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct ColliderTask {
|
struct ChunkRebuildTask {
|
||||||
pub task: Task<CommandQueue>,
|
pub task: Task<(CommandQueue, Mesh)>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,14 +110,7 @@ fn finalize_texture(
|
|||||||
map.regenerate = true;
|
map.regenerate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_map(
|
fn create_map(mut commands: Commands, mut cam: Query<(&mut Transform, Entity), With<PhosCamera>>) {
|
||||||
mut commands: Commands,
|
|
||||||
mut cam: Query<(&mut Transform, Entity), With<PhosCamera>>,
|
|
||||||
tile_assets: Res<Assets<TileAsset>>,
|
|
||||||
tile_mappers: Res<Assets<TileMapperAsset>>,
|
|
||||||
biome_painters: Res<Assets<BiomePainterAsset>>,
|
|
||||||
painter: Res<CurrentBiomePainter>,
|
|
||||||
) {
|
|
||||||
let config = GenerationConfig {
|
let config = GenerationConfig {
|
||||||
layers: vec![
|
layers: vec![
|
||||||
GeneratorLayer {
|
GeneratorLayer {
|
||||||
@@ -187,11 +180,7 @@ fn create_map(
|
|||||||
size: UVec2::splat(1024 / Chunk::SIZE as u32),
|
size: UVec2::splat(1024 / Chunk::SIZE as u32),
|
||||||
// size: UVec2::splat(1),
|
// size: UVec2::splat(1),
|
||||||
};
|
};
|
||||||
let mut heightmap = generate_heightmap(&config, 4);
|
let heightmap = generate_heightmap(&config, 4);
|
||||||
|
|
||||||
let b_painter = biome_painters.get(painter.handle.clone());
|
|
||||||
let cur_painter = b_painter.unwrap();
|
|
||||||
paint_map(&mut heightmap, cur_painter, &tile_assets, &tile_mappers);
|
|
||||||
|
|
||||||
let (mut cam_t, cam_entity) = cam.single_mut();
|
let (mut cam_t, cam_entity) = cam.single_mut();
|
||||||
cam_t.translation = heightmap.get_center();
|
cam_t.translation = heightmap.get_center();
|
||||||
@@ -203,16 +192,23 @@ fn create_map(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_map(
|
fn spawn_map(
|
||||||
heightmap: Res<Map>,
|
mut heightmap: ResMut<Map>,
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
atlas: Res<ChunkAtlas>,
|
atlas: Res<ChunkAtlas>,
|
||||||
mut map: ResMut<PhosMap>,
|
mut map: ResMut<PhosMap>,
|
||||||
|
tile_assets: Res<Assets<TileAsset>>,
|
||||||
|
tile_mappers: Res<Assets<TileMapperAsset>>,
|
||||||
|
biome_painters: Res<Assets<BiomePainterAsset>>,
|
||||||
|
painter: Res<CurrentBiomePainter>,
|
||||||
) {
|
) {
|
||||||
if !map.ready || !map.regenerate {
|
if !map.ready || !map.regenerate {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
map.regenerate = false;
|
map.regenerate = false;
|
||||||
|
let b_painter = biome_painters.get(painter.handle.clone());
|
||||||
|
let cur_painter = b_painter.unwrap();
|
||||||
|
paint_map(&mut heightmap, cur_painter, &tile_assets, &tile_mappers);
|
||||||
|
|
||||||
let chunk_meshes: Vec<_> = heightmap
|
let chunk_meshes: Vec<_> = heightmap
|
||||||
.chunks
|
.chunks
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ use bevy::{prelude::*, window::PrimaryWindow};
|
|||||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
||||||
use world_generation::{hex_utils::HexCoord, prelude::Map};
|
use world_generation::{hex_utils::HexCoord, prelude::Map};
|
||||||
|
|
||||||
use crate::{camera_system::components::PhosCamera, prelude::PhosChunkRegistry};
|
use crate::{
|
||||||
|
camera_system::components::PhosCamera,
|
||||||
|
prelude::{PhosChunkRegistry, RebuildChunk},
|
||||||
|
};
|
||||||
|
|
||||||
use super::chunk_rebuild::ChunkRebuildQueue;
|
use super::chunk_rebuild::ChunkRebuildQueue;
|
||||||
|
|
||||||
@@ -16,11 +19,12 @@ impl Plugin for TerraFormingTestPlugin {
|
|||||||
|
|
||||||
fn deform(
|
fn deform(
|
||||||
cam_query: Query<(&GlobalTransform, &Camera), With<PhosCamera>>,
|
cam_query: Query<(&GlobalTransform, &Camera), With<PhosCamera>>,
|
||||||
|
mut commands: Commands,
|
||||||
window: Query<&Window, With<PrimaryWindow>>,
|
window: Query<&Window, With<PrimaryWindow>>,
|
||||||
mouse: Res<ButtonInput<MouseButton>>,
|
mouse: Res<ButtonInput<MouseButton>>,
|
||||||
rapier_context: Res<RapierContext>,
|
rapier_context: Res<RapierContext>,
|
||||||
mut heightmap: ResMut<Map>,
|
mut heightmap: ResMut<Map>,
|
||||||
mut rebuild: ResMut<ChunkRebuildQueue>,
|
chunks: Res<PhosChunkRegistry>,
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
) {
|
) {
|
||||||
let mut multi = 0.;
|
let mut multi = 0.;
|
||||||
@@ -52,22 +56,22 @@ fn deform(
|
|||||||
QueryFilter::only_fixed(),
|
QueryFilter::only_fixed(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some((_, dist)) = collision {
|
if let Some((e, dist)) = collision {
|
||||||
let contact_point = cam_ray.get_point(dist);
|
let contact_point = cam_ray.get_point(dist);
|
||||||
let contact_coord = HexCoord::from_world_pos(contact_point);
|
let contact_coord = HexCoord::from_world_pos(contact_point);
|
||||||
let cur_height = heightmap.sample_height(&contact_coord);
|
let cur_height = heightmap.sample_height(&contact_coord);
|
||||||
heightmap.set_height(&contact_coord, cur_height + 1. * time.delta_seconds() * multi);
|
heightmap.set_height(&contact_coord, cur_height + 1. * time.delta_seconds() * multi);
|
||||||
let cur_chunk = contact_coord.to_chunk_index(heightmap.width);
|
let cur_chunk = contact_coord.to_chunk_index(heightmap.width);
|
||||||
|
commands.entity(e).insert(RebuildChunk);
|
||||||
if contact_coord.is_on_chunk_edge() {
|
if contact_coord.is_on_chunk_edge() {
|
||||||
let neighbors = contact_coord.get_neighbors();
|
let neighbors = contact_coord.get_neighbors();
|
||||||
let mut other_chunks: Vec<_> = neighbors
|
neighbors
|
||||||
.iter()
|
.iter()
|
||||||
.map(|c| c.to_chunk_index(heightmap.width))
|
.map(|c| c.to_chunk_index(heightmap.width))
|
||||||
.filter(|c| c != &cur_chunk)
|
.filter(|c| c != &cur_chunk)
|
||||||
.collect();
|
.for_each(|c| {
|
||||||
rebuild.queue.append(&mut other_chunks);
|
commands.entity(chunks.chunks[c]).insert(RebuildChunk);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
rebuild.queue.push(cur_chunk);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,3 +44,6 @@ impl PhosChunkRegistry {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct RebuildChunk;
|
||||||
|
|||||||
Reference in New Issue
Block a user