From b365a9d835e568f11334daafed3bc4e93992233c Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Wed, 24 Apr 2024 19:38:27 -0400 Subject: [PATCH 1/5] Update assets --- game/main/assets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/main/assets b/game/main/assets index bded995..b89f50b 160000 --- a/game/main/assets +++ b/game/main/assets @@ -1 +1 @@ -Subproject commit bded995e5a133f730dfcd1d680c053a2ab475842 +Subproject commit b89f50b3ad18e5f36f98cac3f63b4a285d37ab8c From 985523a1cbb0baa4b908d56f6c57e1d805d33b39 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Wed, 24 Apr 2024 20:08:43 -0400 Subject: [PATCH 2/5] Update assets --- game/main/assets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game/main/assets b/game/main/assets index b89f50b..60527f4 160000 --- a/game/main/assets +++ b/game/main/assets @@ -1 +1 @@ -Subproject commit b89f50b3ad18e5f36f98cac3f63b4a285d37ab8c +Subproject commit 60527f41d5552092a21713ce118c5d51160d431e From 54d0f762d2dba6d793af6e9385969b9c543befcf Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Wed, 24 Apr 2024 20:25:39 -0400 Subject: [PATCH 3/5] fixed texture index packing --- engine/world_generation/src/mesh_generator.rs | 11 ++++++----- game/main/assets | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/engine/world_generation/src/mesh_generator.rs b/engine/world_generation/src/mesh_generator.rs index 8b7a2e1..6e331c8 100644 --- a/engine/world_generation/src/mesh_generator.rs +++ b/engine/world_generation/src/mesh_generator.rs @@ -76,6 +76,7 @@ pub fn generate_chunk_mesh( return mesh; } +const TEX_MULTI: Vec2 = Vec2::new(1000., 1.); fn create_tile( pos: Vec3, neighbors: &[Option; 6], @@ -90,13 +91,13 @@ fn create_tile( let side_tex_off = Vec2::new(side_texture_index as f32, 0.); let idx = verts.len() as u32; - uvs.push(uv_offset + tex_off); + uvs.push((uv_offset / TEX_MULTI) + tex_off); verts.push(pos); for i in 0..6 { let p = pos + HEX_CORNERS[i]; verts.push(p); let uv = (HEX_CORNERS[i].xz() / 2.) + uv_offset; - uvs.push(uv + tex_off); + uvs.push((uv / TEX_MULTI) + tex_off); indices.push(idx); indices.push(idx + 1 + i as u32); indices.push(idx + 1 + ((i as u32 + 1) % 6)); @@ -145,7 +146,7 @@ fn create_tile_wall( indices.push(idx + 3); uvs.push(Vec2::ZERO + tex_off); - uvs.push(Vec2::new(1., 0.) + tex_off); - uvs.push(Vec2::new(0., pos.y - height) + tex_off); - uvs.push(Vec2::new(1., pos.y - height) + tex_off); + uvs.push((Vec2::new(1., 0.) / TEX_MULTI) + tex_off); + uvs.push((Vec2::new(0., pos.y - height) / TEX_MULTI) + tex_off); + uvs.push((Vec2::new(1., pos.y - height) / TEX_MULTI) + tex_off); } diff --git a/game/main/assets b/game/main/assets index 60527f4..b5c3a16 160000 --- a/game/main/assets +++ b/game/main/assets @@ -1 +1 @@ -Subproject commit 60527f41d5552092a21713ce118c5d51160d431e +Subproject commit b5c3a166a086062525ed738f68e93c5207a43bac From a65848fac80e64d77129e72849c65bd689dbc1e6 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Wed, 24 Apr 2024 21:18:47 -0400 Subject: [PATCH 4/5] biome mapping --- .vscode/launch.json | 2 +- engine/world_generation/src/heightmap.rs | 14 +++---- game/camera_system/src/lib.rs | 3 +- game/main/assets | 2 +- game/main/src/main.rs | 2 +- game/main/src/phos.rs | 3 +- game/main/src/prelude.rs | 39 ++----------------- .../src/shader_extensions/chunk_material.rs | 18 +++++++++ game/main/src/shader_extensions/mod.rs | 1 + 9 files changed, 34 insertions(+), 50 deletions(-) create mode 100644 game/main/src/shader_extensions/chunk_material.rs create mode 100644 game/main/src/shader_extensions/mod.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index 8cc2e22..4243aed 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,7 +7,7 @@ { "type": "cppvsdbg", "stopAtEntry": false, - "console": "externalTerminal", + // "console": "externalTerminal", "request": "launch", "name": "Debug", "program": "${workspaceRoot}/target/debug/phos.exe", diff --git a/engine/world_generation/src/heightmap.rs b/engine/world_generation/src/heightmap.rs index 7ae0d0f..6ca4530 100644 --- a/engine/world_generation/src/heightmap.rs +++ b/engine/world_generation/src/heightmap.rs @@ -32,12 +32,10 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed: &noise, ); result[x + z * Chunk::SIZE] = sample; - moisture[x + z * Chunk::SIZE] = sample_simple( - x as f64 + chunk_x * Chunk::SIZE as f64, - z as f64 + chunk_z * Chunk::SIZE as f64, - &cfg.layers[0], - &noise, - ) as f32; + moisture[x + z * Chunk::SIZE] = noise.get([ + (x as f64 + chunk_x * Chunk::SIZE as f64) / &cfg.noise_scale, + (z as f64 + chunk_z * Chunk::SIZE as f64) / &cfg.noise_scale, + ]) as f32; temp[x + z * Chunk::SIZE] = sample_tempurature( z as f32 + chunk_z as f32 * Chunk::SIZE as f32, sample, @@ -57,10 +55,10 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed: fn sample_tempurature(z: f32, height: f32, cfg: &GenerationConfig, equator: f32) -> f32 { let d = (equator - z).abs(); let max_d = equator.max(cfg.get_total_height() as f32 - equator); - let t_mod = d.remap(0., max_d, 0., 1.); + let t_mod = d.remap(0., max_d, 0., 1.).clamp(0., 1.); // let max_d = d.max() - return height.remap(0., 100., 0., 1.).clamp(0., 1.) * t_mod; + return (height.remap(0., 50., 0., 1.).clamp(0., 1.) + t_mod) / 2.; } fn sample_point(x: f64, z: f64, cfg: &GenerationConfig, noise: &impl NoiseFn) -> f32 { diff --git a/game/camera_system/src/lib.rs b/game/camera_system/src/lib.rs index 46e8b8c..dc6bba6 100644 --- a/game/camera_system/src/lib.rs +++ b/game/camera_system/src/lib.rs @@ -25,8 +25,7 @@ impl Plugin for PhosCameraPlugin { } } -fn setup(mut commands: Commands, mut msaa: ResMut) { - *msaa = Msaa::Off; +fn setup(mut commands: Commands) { commands.spawn(( Camera3dBundle { transform: Transform::from_xyz(0., 30., 0.) diff --git a/game/main/assets b/game/main/assets index b5c3a16..256b49c 160000 --- a/game/main/assets +++ b/game/main/assets @@ -1 +1 @@ -Subproject commit b5c3a166a086062525ed738f68e93c5207a43bac +Subproject commit 256b49cc29096a2528790bd6087db907ad77e377 diff --git a/game/main/src/main.rs b/game/main/src/main.rs index bdeec35..7f669f7 100644 --- a/game/main/src/main.rs +++ b/game/main/src/main.rs @@ -5,7 +5,7 @@ use bevy_inspector_egui::quick::WorldInspectorPlugin; mod phos; mod prelude; - +mod shader_extensions; use phos::PhosGamePlugin; fn main() { diff --git a/game/main/src/phos.rs b/game/main/src/phos.rs index 7b6f476..c93ba16 100644 --- a/game/main/src/phos.rs +++ b/game/main/src/phos.rs @@ -1,4 +1,5 @@ use crate::prelude::*; +use crate::shader_extensions::chunk_material::ChunkMaterial; use bevy::asset::LoadState; use bevy::core_pipeline::experimental::taa::TemporalAntiAliasPlugin; use bevy::pbr::ExtendedMaterial; @@ -8,7 +9,7 @@ use bevy_rapier3d::render::RapierDebugRenderPlugin; use camera_system::PhosCameraPlugin; use iyes_perf_ui::prelude::*; use world_generation::biome_painter::{ - self, BiomePainterAsset, BiomePainterLoadState, BiomePainterPlugin, + BiomePainterAsset, BiomePainterLoadState, BiomePainterPlugin, }; use world_generation::hex_utils::offset_to_world; use world_generation::tile_manager::{TileAsset, TileAssetLoadState, TileAssetPlugin, TileManager}; diff --git a/game/main/src/prelude.rs b/game/main/src/prelude.rs index 2f60177..bcc1d05 100644 --- a/game/main/src/prelude.rs +++ b/game/main/src/prelude.rs @@ -1,9 +1,6 @@ -use bevy::asset::{Asset, Handle}; -use bevy::pbr::{MaterialExtension, MaterialExtensionKey, MaterialExtensionPipeline, MaterialPipeline, MaterialPipelineKey}; -use bevy::prelude::{Component, Image, Mesh, Resource, TypePath}; -use bevy::render::mesh::{Indices, MeshVertexBufferLayout}; -use bevy::render::render_resource::{AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError}; -use world_generation::prelude::ATTRIBUTE_TEXTURE_INDEX; +use bevy::asset::Handle; + +use bevy::prelude::{Component, Image, Resource}; #[derive(Resource)] pub struct ChunkAtlas { @@ -19,33 +16,3 @@ pub struct PhosMap { #[derive(Component)] pub struct PhosChunk; - - -#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] -pub struct ChunkMaterial { - #[texture(100, dimension = "2d_array")] - #[sampler(101)] - pub array_texture: Handle, -} - -impl MaterialExtension for ChunkMaterial { - fn fragment_shader() -> ShaderRef { - "shaders/world/chunk.wgsl".into() - } - - // fn specialize( - // _pipeline: &MaterialExtensionPipeline, - // descriptor: &mut RenderPipelineDescriptor, - // layout: &MeshVertexBufferLayout, - // _key: MaterialExtensionKey, - // ) -> Result<(), SpecializedMeshPipelineError> { - // let vertex_layout = layout.get_layout(&[ - // Mesh::ATTRIBUTE_POSITION.at_shader_location(0), - // Mesh::ATTRIBUTE_UV_0.at_shader_location(1), - // Mesh::ATTRIBUTE_NORMAL.at_shader_location(2), - // ATTRIBUTE_TEXTURE_INDEX.at_shader_location(3), - // ])?; - // descriptor.vertex.buffers = vec![vertex_layout]; - // Ok(()) - // } -} diff --git a/game/main/src/shader_extensions/chunk_material.rs b/game/main/src/shader_extensions/chunk_material.rs new file mode 100644 index 0000000..fdd8842 --- /dev/null +++ b/game/main/src/shader_extensions/chunk_material.rs @@ -0,0 +1,18 @@ +use bevy::asset::{Asset, Handle}; +use bevy::pbr::MaterialExtension; +use bevy::reflect::TypePath; +use bevy::render::render_resource::{AsBindGroup, ShaderRef}; +use bevy::render::texture::Image; + +#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] +pub struct ChunkMaterial { + #[texture(100, dimension = "2d_array")] + #[sampler(101)] + pub array_texture: Handle, +} + +impl MaterialExtension for ChunkMaterial { + fn fragment_shader() -> ShaderRef { + "shaders/world/chunk.wgsl".into() + } +} diff --git a/game/main/src/shader_extensions/mod.rs b/game/main/src/shader_extensions/mod.rs new file mode 100644 index 0000000..ad9d4df --- /dev/null +++ b/game/main/src/shader_extensions/mod.rs @@ -0,0 +1 @@ +pub mod chunk_material; From a8763ae98ae73603d4064220df003b81c78f2c95 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sat, 27 Apr 2024 19:19:41 -0400 Subject: [PATCH 5/5] Update mesh_generator.rs --- engine/world_generation/src/mesh_generator.rs | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/engine/world_generation/src/mesh_generator.rs b/engine/world_generation/src/mesh_generator.rs index 6e331c8..798d77b 100644 --- a/engine/world_generation/src/mesh_generator.rs +++ b/engine/world_generation/src/mesh_generator.rs @@ -24,6 +24,31 @@ const HEX_CORNERS: [Vec3; 6] = [ Vec3::new(-INNER_RADIUS, 0., 0.5 * OUTER_RADIUS), ]; +const HEX_NORMALS: [Vec3; 6] = [ + Vec3::new( + INNER_RADIUS / 2., + 0., + (OUTER_RADIUS + 0.5 * OUTER_RADIUS) / 2., + ), + Vec3::Z, + Vec3::new( + INNER_RADIUS / -2., + 0., + (OUTER_RADIUS + 0.5 * OUTER_RADIUS) / 2., + ), + Vec3::new( + INNER_RADIUS / -2., + 0., + (OUTER_RADIUS + 0.5 * OUTER_RADIUS) / -2., + ), + Vec3::NEG_Z, + Vec3::new( + INNER_RADIUS / 2., + 0., + (OUTER_RADIUS + 0.5 * OUTER_RADIUS) / -2., + ), +]; + pub fn generate_chunk_mesh( chunk: &Chunk, map: &Map, @@ -35,6 +60,7 @@ pub fn generate_chunk_mesh( let mut verts = Vec::with_capacity(vertex_count); let mut uvs = Vec::with_capacity(vertex_count); let mut indices = Vec::with_capacity(vertex_count); + let mut normals = Vec::with_capacity(vertex_count); for z in 0..Chunk::SIZE { for x in 0..Chunk::SIZE { @@ -57,6 +83,7 @@ pub fn generate_chunk_mesh( &mut verts, &mut uvs, &mut indices, + &mut normals, // &mut tex, tile.texture_id, tile.side_texture_id, @@ -70,9 +97,8 @@ pub fn generate_chunk_mesh( ) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts) .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) - .with_inserted_indices(Indices::U32(indices)) - .with_duplicated_vertices() - .with_computed_flat_normals(); + .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) + .with_inserted_indices(Indices::U32(indices)); return mesh; } @@ -83,6 +109,7 @@ fn create_tile( verts: &mut Vec, uvs: &mut Vec, indices: &mut Vec, + normals: &mut Vec, texture_index: u32, side_texture_index: u32, ) { @@ -93,6 +120,7 @@ fn create_tile( let idx = verts.len() as u32; uvs.push((uv_offset / TEX_MULTI) + tex_off); verts.push(pos); + normals.push(Vec3::Y); for i in 0..6 { let p = pos + HEX_CORNERS[i]; verts.push(p); @@ -101,6 +129,7 @@ fn create_tile( indices.push(idx); indices.push(idx + 1 + i as u32); indices.push(idx + 1 + ((i as u32 + 1) % 6)); + normals.push(Vec3::Y); } for i in 0..neighbors.len() { @@ -108,7 +137,7 @@ fn create_tile( match cur_n { Some(n_height) => { if n_height < pos.y { - create_tile_wall(pos, i, n_height, verts, uvs, indices, side_tex_off); + create_tile_wall(pos, i, n_height, verts, uvs, indices, normals, side_tex_off); } } _ => {} @@ -123,6 +152,7 @@ fn create_tile_wall( verts: &mut Vec, uvs: &mut Vec, indices: &mut Vec, + normals: &mut Vec, tex_off: Vec2, ) { let p1 = HEX_CORNERS[(dir) % 6] + pos; @@ -137,6 +167,12 @@ fn create_tile_wall( verts.push(p3); verts.push(p4); + let n = HEX_NORMALS[dir].normalize(); + normals.push(n); + normals.push(n); + normals.push(n); + normals.push(n); + indices.push(idx); indices.push(idx + 2); indices.push(idx + 1);