diff --git a/engine/world_generation/src/heightmap.rs b/engine/world_generation/src/heightmap.rs index 8eeee8b..c8a6593 100644 --- a/engine/world_generation/src/heightmap.rs +++ b/engine/world_generation/src/heightmap.rs @@ -29,7 +29,7 @@ pub fn generate_chunk(chunk_x: f64, chunk_z: f64, cfg: &GenerationConfig, seed: &cfg, &noise, ); - result[x + z * Chunk::SIZE] = sample.floor(); + result[x + z * Chunk::SIZE] = sample; } } return Chunk { diff --git a/engine/world_generation/src/mesh_generator.rs b/engine/world_generation/src/mesh_generator.rs index a3e4450..5daa750 100644 --- a/engine/world_generation/src/mesh_generator.rs +++ b/engine/world_generation/src/mesh_generator.rs @@ -71,11 +71,10 @@ fn create_tile( let tex_x = texture_index % 11; let tex_y = texture_index / 11; let x_min = tex_x as f32 / 11.; - let x_max = (tex_x + 1) as f32 / 11.; let y_min = tex_y as f32 / 8.; - let tx_unit = x_max - x_min; + const TX_UNIT: Vec2 = Vec2::new(1. / 11., 1. / 8.); - let uv_offset = Vec2::new(x_min + tx_unit / 2., y_min + tx_unit / 2.); + let uv_offset = Vec2::new(x_min + TX_UNIT.x / 2., y_min + TX_UNIT.y / 2.); let idx = verts.len() as u32; uvs.push(uv_offset); @@ -83,7 +82,7 @@ fn create_tile( for i in 0..6 { let p = pos + HEX_CORNERS[i]; verts.push(p); - let uv = (HEX_CORNERS[i].xz() * tx_unit / 2.) + uv_offset; + let uv = (HEX_CORNERS[i].xz() * TX_UNIT / 2.) + uv_offset; uvs.push(uv); indices.push(idx); indices.push(idx + 1 + i as u32); @@ -103,7 +102,7 @@ fn create_tile( uvs, indices, Vec2::new(x_min, y_min), - Vec2::new(x_min + tx_unit, y_min + tx_unit), + Vec2::new(x_min + TX_UNIT.x, y_min + TX_UNIT.y), ); } } diff --git a/game/main/assets/shaders/world/chunk.wgsl b/game/main/assets/shaders/world/chunk.wgsl new file mode 100644 index 0000000..e633154 --- /dev/null +++ b/game/main/assets/shaders/world/chunk.wgsl @@ -0,0 +1,56 @@ +#import bevy_pbr::{ + forward_io::VertexOutput, + mesh_view_bindings::view, + pbr_types::{STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT, PbrInput, pbr_input_new}, + pbr_functions as fns, +} +#import bevy_core_pipeline::tonemapping::tone_mapping + +@group(2) @binding(0) var my_array_texture: texture_2d_array; +@group(2) @binding(1) var my_array_texture_sampler: sampler; + +@fragment +fn fragment( + @builtin(front_facing) is_front: bool, + mesh: VertexOutput, +) -> @location(0) vec4 { + let layer = i32(mesh.world_position.x) & 0x3; + + // Prepare a 'processed' StandardMaterial by sampling all textures to resolve + // the material members + var pbr_input: PbrInput = pbr_input_new(); + + pbr_input.material.base_color = textureSample(my_array_texture, my_array_texture_sampler, mesh.uv, layer); +#ifdef VERTEX_COLORS + pbr_input.material.base_color = pbr_input.material.base_color * mesh.color; +#endif + + let double_sided = (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u; + + pbr_input.frag_coord = mesh.position; + pbr_input.world_position = mesh.world_position; + pbr_input.world_normal = fns::prepare_world_normal( + mesh.world_normal, + double_sided, + is_front, + ); + + pbr_input.is_orthographic = view.projection[3].w == 1.0; + + pbr_input.N = fns::apply_normal_mapping( + pbr_input.material.flags, + mesh.world_normal, + double_sided, + is_front, +#ifdef VERTEX_TANGENTS +#ifdef STANDARD_MATERIAL_NORMAL_MAP + mesh.world_tangent, +#endif +#endif + mesh.uv, + view.mip_bias, + ); + pbr_input.V = fns::calculate_view(mesh.world_position, pbr_input.is_orthographic); + + return tone_mapping(fns::apply_pbr_lighting(pbr_input), view.color_grading); +} \ No newline at end of file diff --git a/game/main/src/main.rs b/game/main/src/main.rs index f173c1b..05026e8 100644 --- a/game/main/src/main.rs +++ b/game/main/src/main.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy::window::PresentMode; use bevy_inspector_egui::quick::WorldInspectorPlugin; mod phos; mod prelude; @@ -13,11 +14,8 @@ fn main() { title: "Phos".into(), name: Some("phos".into()), resolution: (1920.0, 1080.0).into(), - resizable: false, - enabled_buttons: bevy::window::EnabledButtons { - maximize: false, - ..Default::default() - }, + resizable: true, + present_mode: PresentMode::AutoNoVsync, ..default() }), ..default() diff --git a/game/main/src/phos.rs b/game/main/src/phos.rs index f47d556..f871d4b 100644 --- a/game/main/src/phos.rs +++ b/game/main/src/phos.rs @@ -1,3 +1,5 @@ +use bevy::pbr::MaterialExtension; +use bevy::render::render_resource::{AsBindGroup, ShaderRef}; use bevy::{pbr::CascadeShadowConfig, prelude::*}; use camera_system::PhosCameraPlugin; use iyes_perf_ui::prelude::*; @@ -166,3 +168,16 @@ fn create_map( commands.insert_resource(heightmap); } + +#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] +struct ChunkMaterial { + #[texture(0, dimension = "2d_array")] + #[sampler(1)] + array_texture: Handle, +} + +impl MaterialExtension for ChunkMaterial { + fn fragment_shader() -> ShaderRef { + "shaders/world/chunk.wgsl".into() + } +}