experimenting with water surface mesh
This commit is contained in:
@@ -224,7 +224,7 @@ fn sample_ground(pos: Vec3, heightmap: &Map) -> f32 {
|
||||
let mut ground_height = if heightmap.is_in_bounds(&tile_under) {
|
||||
heightmap.sample_height(&tile_under)
|
||||
} else {
|
||||
heightmap.sea_level
|
||||
heightmap.sealevel
|
||||
};
|
||||
|
||||
for n in neighbors {
|
||||
@@ -234,8 +234,8 @@ fn sample_ground(pos: Vec3, heightmap: &Map) -> f32 {
|
||||
}
|
||||
}
|
||||
}
|
||||
if ground_height < heightmap.sea_level {
|
||||
ground_height = heightmap.sea_level;
|
||||
if ground_height < heightmap.sealevel {
|
||||
ground_height = heightmap.sealevel;
|
||||
}
|
||||
return ground_height;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ fn chunk_rebuilder(
|
||||
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 {
|
||||
#[cfg(feature = "tracing")]
|
||||
@@ -46,7 +47,8 @@ fn chunk_rebuilder(
|
||||
#[cfg(feature = "tracing")]
|
||||
let _spawn_span = info_span!("Rebuild Task").entered();
|
||||
let mut queue = CommandQueue::default();
|
||||
let (mesh, collider_data, _, _) = prepare_chunk_mesh(&chunk_data, chunk_offset, chunk_index);
|
||||
let (mesh, water_mesh, collider_data, _, _) =
|
||||
prepare_chunk_mesh(&chunk_data, chunk_data.sealevel, chunk_offset, chunk_index, map_size);
|
||||
#[cfg(feature = "tracing")]
|
||||
let trimesh_span = info_span!("Chunk Trimesh").entered();
|
||||
let c = Collider::trimesh_with_flags(
|
||||
|
||||
@@ -32,12 +32,13 @@ use crate::{
|
||||
chunk_material::ChunkMaterial,
|
||||
water_material::{WaterMaterial, WaterSettings},
|
||||
},
|
||||
utlis::{
|
||||
chunk_utils::{paint_map, prepare_chunk_mesh_with_collider},
|
||||
},
|
||||
utlis::chunk_utils::{paint_map, prepare_chunk_mesh_with_collider},
|
||||
};
|
||||
|
||||
use super::{chunk_rebuild::ChunkRebuildPlugin, render_distance_system::RenderDistanceVisibility, terraforming_test::TerraFormingTestPlugin};
|
||||
use super::{
|
||||
chunk_rebuild::ChunkRebuildPlugin, render_distance_system::RenderDistanceVisibility,
|
||||
terraforming_test::TerraFormingTestPlugin,
|
||||
};
|
||||
|
||||
pub struct MapInitPlugin;
|
||||
|
||||
@@ -52,7 +53,6 @@ impl Plugin for MapInitPlugin {
|
||||
app.add_plugins(BiomeAssetPlugin);
|
||||
|
||||
app.add_plugins(ResourceInspectorPlugin::<GenerationConfig>::default());
|
||||
app.add_plugins(ResourceInspectorPlugin::<WaterInspect>::default());
|
||||
app.register_type::<ExtendedMaterial<StandardMaterial, WaterMaterial>>();
|
||||
app.register_asset_reflect::<ExtendedMaterial<StandardMaterial, WaterMaterial>>();
|
||||
app.add_plugins((
|
||||
@@ -93,12 +93,7 @@ impl Plugin for MapInitPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Resource, Reflect, Default)]
|
||||
#[reflect(Resource)]
|
||||
struct WaterInspect(Handle<ExtendedMaterial<StandardMaterial, WaterMaterial>>);
|
||||
|
||||
fn setup_materials(
|
||||
mut commands: Commands,
|
||||
mut phos_assets: ResMut<PhosAssets>,
|
||||
mut water_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, WaterMaterial>>>,
|
||||
) {
|
||||
@@ -119,7 +114,6 @@ fn setup_materials(
|
||||
..default()
|
||||
},
|
||||
});
|
||||
commands.insert_resource(WaterInspect(water_material.clone()));
|
||||
phos_assets.water_material = water_material;
|
||||
}
|
||||
|
||||
@@ -239,12 +233,19 @@ fn spawn_map(
|
||||
) {
|
||||
paint_map(&mut heightmap, &biome_painter, &tile_assets, &tile_mappers);
|
||||
|
||||
let map_size = UVec2::new(heightmap.width as u32, heightmap.height as u32);
|
||||
let chunk_meshes: Vec<_> = heightmap
|
||||
.chunks
|
||||
.par_iter()
|
||||
.map(|chunk: &Chunk| {
|
||||
let index = offset_to_index(chunk.chunk_offset, heightmap.width);
|
||||
return prepare_chunk_mesh_with_collider(&heightmap.get_chunk_mesh_data(index), chunk.chunk_offset, index);
|
||||
return prepare_chunk_mesh_with_collider(
|
||||
&heightmap.get_chunk_mesh_data(index),
|
||||
heightmap.sealevel,
|
||||
chunk.chunk_offset,
|
||||
index,
|
||||
map_size,
|
||||
);
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -257,36 +258,52 @@ fn spawn_map(
|
||||
0.,
|
||||
(Chunk::SIZE / 2) as f32 * 1.5,
|
||||
);
|
||||
for (mesh, collider, pos, index) in chunk_meshes {
|
||||
for (chunk_mesh, water_mesh, collider, pos, index) in chunk_meshes {
|
||||
// let mesh_handle = meshes.a
|
||||
let 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(visibility_offset),
|
||||
collider,
|
||||
));
|
||||
registry.chunks.push(chunk.id());
|
||||
let chunk = commands
|
||||
.spawn((
|
||||
MaterialMeshBundle {
|
||||
mesh: meshes.add(chunk_mesh),
|
||||
material: atlas.chunk_material_handle.clone(),
|
||||
transform: Transform::from_translation(pos),
|
||||
..default()
|
||||
},
|
||||
PhosChunk::new(index),
|
||||
RenderDistanceVisibility::default().with_offset(visibility_offset),
|
||||
collider,
|
||||
))
|
||||
.id();
|
||||
let water = commands
|
||||
.spawn((
|
||||
MaterialMeshBundle {
|
||||
mesh: meshes.add(water_mesh),
|
||||
material: atlas.water_material.clone(),
|
||||
transform: Transform::from_translation(pos),
|
||||
..default()
|
||||
},
|
||||
PhosChunk::new(index),
|
||||
NotShadowCaster,
|
||||
RenderDistanceVisibility::default().with_offset(visibility_offset),
|
||||
))
|
||||
.id();
|
||||
registry.chunks.push(chunk);
|
||||
registry.waters.push(water);
|
||||
}
|
||||
}
|
||||
|
||||
commands.spawn((
|
||||
MaterialMeshBundle {
|
||||
transform: Transform::from_translation(heightmap.get_center()),
|
||||
mesh: meshes.add(
|
||||
Plane3d::default()
|
||||
.mesh()
|
||||
.size(heightmap.get_world_width(), heightmap.get_world_height()),
|
||||
),
|
||||
material: atlas.water_material.clone(),
|
||||
..default()
|
||||
},
|
||||
NotShadowCaster,
|
||||
));
|
||||
// commands.spawn((
|
||||
// MaterialMeshBundle {
|
||||
// transform: Transform::from_translation(heightmap.get_center()),
|
||||
// mesh: meshes.add(
|
||||
// Plane3d::default()
|
||||
// .mesh()
|
||||
// .size(heightmap.get_world_width(), heightmap.get_world_height()),
|
||||
// ),
|
||||
// material: atlas.water_material.clone(),
|
||||
// ..default()
|
||||
// },
|
||||
// NotShadowCaster,
|
||||
// ));
|
||||
|
||||
commands.insert_resource(registry);
|
||||
generator_state.set(GeneratorState::Idle);
|
||||
|
||||
@@ -30,12 +30,14 @@ impl PhosChunk {
|
||||
#[derive(Resource, Default)]
|
||||
pub struct PhosChunkRegistry {
|
||||
pub chunks: Vec<Entity>,
|
||||
pub waters: Vec<Entity>,
|
||||
}
|
||||
|
||||
impl PhosChunkRegistry {
|
||||
pub fn new(size: usize) -> Self {
|
||||
return Self {
|
||||
chunks: Vec::with_capacity(size),
|
||||
waters: Vec::with_capacity(size),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,17 @@ use bevy::log::*;
|
||||
use bevy::{
|
||||
asset::Assets,
|
||||
ecs::system::Res,
|
||||
math::{IVec2, Vec3},
|
||||
math::{IVec2, UVec2, Vec3},
|
||||
render::mesh::Mesh,
|
||||
};
|
||||
use bevy_rapier3d::geometry::{Collider, TriMeshFlags};
|
||||
use rayon::iter::{IntoParallelRefMutIterator, ParallelIterator};
|
||||
use world_generation::{
|
||||
biome_painter::BiomePainter,
|
||||
generators::{chunk_colliders::generate_chunk_collider, mesh_generator::generate_chunk_mesh},
|
||||
generators::{
|
||||
chunk_colliders::generate_chunk_collider,
|
||||
mesh_generator::{generate_chunk_mesh, generate_chunk_water_mesh},
|
||||
},
|
||||
hex_utils::offset_to_world,
|
||||
prelude::{Chunk, Map, MeshChunkData},
|
||||
tile_manager::TileAsset,
|
||||
@@ -50,16 +53,20 @@ pub fn paint_chunk(
|
||||
|
||||
pub fn prepare_chunk_mesh(
|
||||
chunk: &MeshChunkData,
|
||||
sealevel: f32,
|
||||
chunk_offset: IVec2,
|
||||
chunk_index: usize,
|
||||
) -> (Mesh, (Vec<Vec3>, Vec<[u32; 3]>), Vec3, usize) {
|
||||
map_size: UVec2,
|
||||
) -> (Mesh, Mesh, (Vec<Vec3>, Vec<[u32; 3]>), Vec3, usize) {
|
||||
#[cfg(feature = "tracing")]
|
||||
let _gen_mesh = info_span!("Generate Chunk").entered();
|
||||
let mesh = generate_chunk_mesh(chunk);
|
||||
let chunk_mesh = generate_chunk_mesh(chunk);
|
||||
let water_mesh = generate_chunk_water_mesh(chunk, sealevel, map_size.x as usize, map_size.y as usize);
|
||||
let col_data = generate_chunk_collider(chunk);
|
||||
|
||||
return (
|
||||
mesh,
|
||||
chunk_mesh,
|
||||
water_mesh,
|
||||
col_data,
|
||||
offset_to_world(chunk_offset * Chunk::SIZE as i32, 0.),
|
||||
chunk_index,
|
||||
@@ -68,15 +75,18 @@ pub fn prepare_chunk_mesh(
|
||||
|
||||
pub fn prepare_chunk_mesh_with_collider(
|
||||
chunk: &MeshChunkData,
|
||||
sealevel: f32,
|
||||
chunk_offset: IVec2,
|
||||
chunk_index: usize,
|
||||
) -> (Mesh, Collider, Vec3, usize) {
|
||||
let (mesh, (col_verts, col_indicies), pos, index) = prepare_chunk_mesh(chunk, chunk_offset, chunk_index);
|
||||
map_size: UVec2,
|
||||
) -> (Mesh, Mesh, Collider, Vec3, usize) {
|
||||
let (chunk_mesh, water_mesh, (col_verts, col_indicies), pos, index) =
|
||||
prepare_chunk_mesh(chunk, sealevel, chunk_offset, chunk_index, map_size);
|
||||
let collider: Collider;
|
||||
{
|
||||
#[cfg(feature = "tracing")]
|
||||
let _collider_span = info_span!("Create Collider Trimesh").entered();
|
||||
collider = Collider::trimesh_with_flags(col_verts, col_indicies, TriMeshFlags::DELETE_DUPLICATE_TRIANGLES);
|
||||
}
|
||||
return (mesh, collider, pos, index);
|
||||
return (chunk_mesh, water_mesh, collider, pos, index);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user