@@ -9,6 +9,7 @@ use world_generation::prelude::Map;
|
||||
use world_generation::states::GeneratorState;
|
||||
|
||||
use crate::prelude::RebuildChunk;
|
||||
use crate::prelude::WaterMesh;
|
||||
use crate::{
|
||||
prelude::{PhosChunk, PhosChunkRegistry},
|
||||
utlis::chunk_utils::prepare_chunk_mesh,
|
||||
@@ -16,8 +17,10 @@ use crate::{
|
||||
|
||||
pub struct ChunkRebuildPlugin;
|
||||
|
||||
impl Plugin for ChunkRebuildPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
impl Plugin for ChunkRebuildPlugin
|
||||
{
|
||||
fn build(&self, app: &mut App)
|
||||
{
|
||||
app.init_resource::<PhosChunkRegistry>();
|
||||
app.add_message::<ChunkModifiedEvent>();
|
||||
app.add_message::<TileModifiedEvent>();
|
||||
@@ -30,11 +33,13 @@ fn chunk_rebuilder(
|
||||
mut commands: Commands,
|
||||
chunk_query: Query<(Entity, &PhosChunk), (With<RebuildChunk>, Without<ChunkRebuildTask>)>,
|
||||
heightmap: Res<Map>,
|
||||
) {
|
||||
)
|
||||
{
|
||||
let pool = AsyncComputeTaskPool::get();
|
||||
let map_size = UVec2::new(heightmap.width as u32, heightmap.height as u32);
|
||||
|
||||
for (chunk_entity, idx) in &chunk_query {
|
||||
for (chunk_entity, idx) in &chunk_query
|
||||
{
|
||||
#[cfg(feature = "tracing")]
|
||||
let _spawn_span = info_span!("Rebuild Chunk").entered();
|
||||
info!("Rebuilding Chunk");
|
||||
@@ -62,7 +67,7 @@ fn chunk_rebuilder(
|
||||
world.entity_mut(chunk_entity).insert(c).remove::<ChunkRebuildTask>();
|
||||
});
|
||||
|
||||
return (queue, mesh);
|
||||
return (queue, mesh, water_mesh);
|
||||
});
|
||||
|
||||
commands
|
||||
@@ -73,19 +78,28 @@ fn chunk_rebuilder(
|
||||
}
|
||||
|
||||
fn collider_task_resolver(
|
||||
mut chunks: Query<(&mut ChunkRebuildTask, &Mesh3d), With<PhosChunk>>,
|
||||
mut chunks: Query<(&mut ChunkRebuildTask, &Mesh3d, &WaterMesh), With<PhosChunk>>,
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
) {
|
||||
for (mut task, mesh_handle) in &mut chunks {
|
||||
if let Some((mut c, mesh)) = futures::check_ready(&mut task.task) {
|
||||
)
|
||||
{
|
||||
for (mut task, mesh_handle, water_mesh_handle) in &mut chunks
|
||||
{
|
||||
if let Some((mut c, chunk_mesh, water_mesh)) = futures::check_ready(&mut task.task)
|
||||
{
|
||||
commands.append(&mut c);
|
||||
meshes.insert(mesh_handle.id(), mesh);
|
||||
meshes
|
||||
.insert(mesh_handle.id(), chunk_mesh)
|
||||
.expect("Failed to update chunk mesh");
|
||||
meshes
|
||||
.insert(water_mesh_handle.0, water_mesh)
|
||||
.expect("Failed to update chink water mesh");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct ChunkRebuildTask {
|
||||
pub task: Task<(CommandQueue, Mesh)>,
|
||||
struct ChunkRebuildTask
|
||||
{
|
||||
pub task: Task<(CommandQueue, Mesh, Mesh)>,
|
||||
}
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
#[cfg(feature = "tracing")]
|
||||
use bevy::log::*;
|
||||
use bevy::{
|
||||
light::NotShadowCaster,
|
||||
pbr::ExtendedMaterial,
|
||||
prelude::*,
|
||||
render::render_resource::{ColorTargetState, FragmentState, RenderPipelineDescriptor},
|
||||
};
|
||||
use bevy::{light::NotShadowCaster, pbr::ExtendedMaterial, prelude::*};
|
||||
use bevy_asset_loader::prelude::*;
|
||||
|
||||
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
|
||||
@@ -23,7 +18,7 @@ use world_generation::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
prelude::{PhosAssets, PhosChunk, PhosChunkRegistry},
|
||||
prelude::{PhosAssets, PhosChunk, PhosChunkRegistry, WaterMesh},
|
||||
shader_extensions::{
|
||||
chunk_material::ChunkMaterial,
|
||||
water_material::{WaterMaterial, WaterSettings},
|
||||
@@ -258,19 +253,21 @@ fn spawn_map(
|
||||
for (chunk_mesh, water_mesh, collider, pos, index) in chunk_meshes
|
||||
{
|
||||
// let mesh_handle = meshes.a
|
||||
let water_mesh_handle = meshes.add(water_mesh);
|
||||
let chunk = commands
|
||||
.spawn((
|
||||
Mesh3d(meshes.add(chunk_mesh)),
|
||||
MeshMaterial3d(atlas.chunk_material_handle.clone()),
|
||||
Transform::from_translation(pos),
|
||||
PhosChunk::new(index),
|
||||
WaterMesh(water_mesh_handle.id()),
|
||||
RenderDistanceVisibility::default().with_offset(visibility_offset),
|
||||
collider,
|
||||
))
|
||||
.id();
|
||||
let water = commands
|
||||
.spawn((
|
||||
Mesh3d(meshes.add(water_mesh)),
|
||||
Mesh3d(water_mesh_handle),
|
||||
MeshMaterial3d(atlas.water_material.clone()),
|
||||
Transform::from_translation(pos),
|
||||
PhosChunk::new(index),
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
use bevy::prelude::*;
|
||||
use world_generation::biome_painter::BiomePainterAsset;
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use bevy::prelude::*;
|
||||
use shared::tags::MainCamera;
|
||||
|
||||
use crate::camera_system::components::PhosCamera;
|
||||
|
||||
pub struct RenderDistancePlugin;
|
||||
|
||||
impl Plugin for RenderDistancePlugin {
|
||||
fn build(&self, app: &mut bevy::prelude::App) {
|
||||
impl Plugin for RenderDistancePlugin
|
||||
{
|
||||
fn build(&self, app: &mut bevy::prelude::App)
|
||||
{
|
||||
app.register_type::<RenderDistanceSettings>();
|
||||
app.add_systems(PostUpdate, render_distance_system)
|
||||
.insert_resource(RenderDistanceSettings::default());
|
||||
@@ -15,38 +15,48 @@ impl Plugin for RenderDistancePlugin {
|
||||
|
||||
#[derive(Resource, Reflect)]
|
||||
#[reflect(Resource)]
|
||||
pub struct RenderDistanceSettings {
|
||||
pub struct RenderDistanceSettings
|
||||
{
|
||||
pub render_distance: f32,
|
||||
}
|
||||
|
||||
impl RenderDistanceSettings {
|
||||
pub fn new(distance: f32) -> Self {
|
||||
impl RenderDistanceSettings
|
||||
{
|
||||
pub fn new(distance: f32) -> Self
|
||||
{
|
||||
return Self {
|
||||
render_distance: distance,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RenderDistanceSettings {
|
||||
fn default() -> Self {
|
||||
impl Default for RenderDistanceSettings
|
||||
{
|
||||
fn default() -> Self
|
||||
{
|
||||
Self::new(500.)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct RenderDistanceVisibility {
|
||||
pub struct RenderDistanceVisibility
|
||||
{
|
||||
pub offset: Vec3,
|
||||
}
|
||||
|
||||
impl RenderDistanceVisibility {
|
||||
pub fn with_offset(mut self, offset: Vec3) -> Self {
|
||||
impl RenderDistanceVisibility
|
||||
{
|
||||
pub fn with_offset(mut self, offset: Vec3) -> Self
|
||||
{
|
||||
self.offset = offset;
|
||||
return self;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RenderDistanceVisibility {
|
||||
fn default() -> Self {
|
||||
impl Default for RenderDistanceVisibility
|
||||
{
|
||||
fn default() -> Self
|
||||
{
|
||||
Self { offset: Vec3::ZERO }
|
||||
}
|
||||
}
|
||||
@@ -55,13 +65,18 @@ fn render_distance_system(
|
||||
mut objects: Query<(&Transform, &mut Visibility, &RenderDistanceVisibility)>,
|
||||
camera: Single<&Transform, With<MainCamera>>,
|
||||
settings: Res<RenderDistanceSettings>,
|
||||
) {
|
||||
)
|
||||
{
|
||||
let cam_pos = Vec3::new(camera.translation.x, 0.0, camera.translation.z);
|
||||
for (t, mut vis, r) in objects.iter_mut() {
|
||||
for (t, mut vis, r) in objects.iter_mut()
|
||||
{
|
||||
let dist = (cam_pos - (t.translation + r.offset)).length();
|
||||
if settings.render_distance < dist {
|
||||
if settings.render_distance < dist
|
||||
{
|
||||
*vis = Visibility::Hidden;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
*vis = Visibility::Visible;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
use bevy::{platform::collections::HashSet, prelude::*, window::PrimaryWindow};
|
||||
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
|
||||
use bevy::{platform::collections::HashSet, prelude::*};
|
||||
use shared::{
|
||||
events::{ChunkModifiedEvent, TileModifiedEvent},
|
||||
resources::TileUnderCursor,
|
||||
states::GameplayState,
|
||||
};
|
||||
use world_generation::{hex_utils::HexCoord, prelude::Map, states::GeneratorState};
|
||||
use world_generation::{prelude::Map, states::GeneratorState};
|
||||
|
||||
use crate::{
|
||||
camera_system::components::PhosCamera,
|
||||
prelude::{PhosChunkRegistry, RebuildChunk},
|
||||
};
|
||||
use crate::prelude::{PhosChunkRegistry, RebuildChunk};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct TerraFormingTestPlugin;
|
||||
|
||||
impl Plugin for TerraFormingTestPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
impl Plugin for TerraFormingTestPlugin
|
||||
{
|
||||
fn build(&self, app: &mut App)
|
||||
{
|
||||
app.add_systems(
|
||||
Update,
|
||||
deform
|
||||
@@ -25,6 +24,7 @@ impl Plugin for TerraFormingTestPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn deform(
|
||||
mut commands: Commands,
|
||||
mouse: Res<ButtonInput<MouseButton>>,
|
||||
@@ -33,26 +33,34 @@ fn deform(
|
||||
tile_under_cursor: Res<TileUnderCursor>,
|
||||
mut chunk_modified: MessageWriter<ChunkModifiedEvent>,
|
||||
mut tile_modified: MessageWriter<TileModifiedEvent>,
|
||||
) {
|
||||
)
|
||||
{
|
||||
let mut multi = 0.;
|
||||
if mouse.just_pressed(MouseButton::Left) {
|
||||
if mouse.just_pressed(MouseButton::Left)
|
||||
{
|
||||
multi = 1.;
|
||||
} else if mouse.just_pressed(MouseButton::Right) {
|
||||
}
|
||||
else if mouse.just_pressed(MouseButton::Right)
|
||||
{
|
||||
multi = -1.;
|
||||
}
|
||||
|
||||
if multi == 0. {
|
||||
if multi == 0.
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(contact) = tile_under_cursor.0 {
|
||||
if let Some(contact) = tile_under_cursor.0
|
||||
{
|
||||
#[cfg(feature = "tracing")]
|
||||
let span = info_span!("Deform Mesh").entered();
|
||||
let modified_tiles = heightmap.create_crater(&contact.tile, 5, 5. * multi);
|
||||
let mut chunk_set: HashSet<usize> = HashSet::new();
|
||||
for (tile, height) in modified_tiles {
|
||||
for (tile, height) in modified_tiles
|
||||
{
|
||||
let chunk = tile.to_chunk_index(heightmap.width);
|
||||
if !chunk_set.contains(&chunk) {
|
||||
if !chunk_set.contains(&chunk)
|
||||
{
|
||||
chunk_modified.write(ChunkModifiedEvent { index: chunk });
|
||||
chunk_set.insert(chunk);
|
||||
commands.entity(chunks.chunks[chunk]).insert(RebuildChunk);
|
||||
|
||||
Reference in New Issue
Block a user