From 5dc4cdb5d4857253c01bb183a7846de859f782d0 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sun, 7 Apr 2024 01:43:17 -0400 Subject: [PATCH] encode texture index in uv x coord --- engine/world_generation/src/mesh_generator.rs | 31 ++++++------------- game/main/assets/shaders/world/chunk.wgsl | 9 +++--- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/engine/world_generation/src/mesh_generator.rs b/engine/world_generation/src/mesh_generator.rs index d41dd8f..ca63f9e 100644 --- a/engine/world_generation/src/mesh_generator.rs +++ b/engine/world_generation/src/mesh_generator.rs @@ -29,7 +29,6 @@ pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> 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 tex = Vec::with_capacity(vertex_count); for z in 0..Chunk::SIZE { for x in 0..Chunk::SIZE { @@ -46,7 +45,7 @@ pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh { &mut verts, &mut uvs, &mut indices, - &mut tex, + // &mut tex, (height % 7.) as u32, ); } @@ -58,9 +57,7 @@ pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh { ) .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts) .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) - .with_inserted_attribute(Mesh::ATTRIBUTE_UV_1, tex) .with_inserted_indices(Indices::U32(indices)) - // .with_inserted_attribute(ATTRIBUTE_TEXTURE_INDEX, tex) .with_duplicated_vertices() .with_computed_flat_normals(); return mesh; @@ -72,24 +69,22 @@ fn create_tile( verts: &mut Vec, uvs: &mut Vec, indices: &mut Vec, - tex: &mut Vec, texture_index: u32, ) { let uv_offset = Vec2::splat(0.5); + let tex_off = Vec2::new(texture_index as f32, 0.); let idx = verts.len() as u32; - uvs.push(uv_offset); + uvs.push(uv_offset + tex_off); verts.push(pos); - tex.push(Vec2::splat(texture_index as f32)); 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); + uvs.push(uv + tex_off); indices.push(idx); indices.push(idx + 1 + i as u32); indices.push(idx + 1 + ((i as u32 + 1) % 6)); - tex.push(Vec2::splat(texture_index as f32)); } for i in 0..neighbors.len() { @@ -97,7 +92,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, tex, texture_index); + create_tile_wall(pos, i, n_height, verts, uvs, indices, tex_off); } } _ => {} @@ -112,8 +107,7 @@ fn create_tile_wall( verts: &mut Vec, uvs: &mut Vec, indices: &mut Vec, - tex: &mut Vec, - texture_index: u32, + tex_off: Vec2, ) { let p1 = HEX_CORNERS[(dir) % 6] + pos; let p2 = HEX_CORNERS[(dir + 1) % 6] + pos; @@ -135,13 +129,8 @@ fn create_tile_wall( indices.push(idx + 2); indices.push(idx + 3); - tex.push(Vec2::splat(texture_index as f32)); - tex.push(Vec2::splat(texture_index as f32)); - tex.push(Vec2::splat(texture_index as f32)); - tex.push(Vec2::splat(texture_index as f32)); - - uvs.push(Vec2::ZERO); - uvs.push(Vec2::new(1., 0.)); - uvs.push(Vec2::new(0., pos.y - height)); - uvs.push(Vec2::new(1., pos.y - height)); + 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); } diff --git a/game/main/assets/shaders/world/chunk.wgsl b/game/main/assets/shaders/world/chunk.wgsl index a7b8887..cf17b3e 100644 --- a/game/main/assets/shaders/world/chunk.wgsl +++ b/game/main/assets/shaders/world/chunk.wgsl @@ -30,6 +30,7 @@ fn fragment( // vin.world_position = in.world_position; // vin.world_normal = in.world_normal; // vin.uv = in.uv; + // generate a PbrInput struct from the StandardMaterial bindings var pbr_input = pbr_input_from_standard_material(in, is_front); @@ -44,8 +45,10 @@ fn fragment( var out: FragmentOutput; // apply lighting - let layer = u32(in.uv_b.x); - out.color = textureSample(array_texture, array_texture_sampler, in.uv, layer); + let index = floor(in.uv.x - 1) + 1; + var uv = in.uv; + uv.x = in.uv.x - index; + out.color = textureSample(array_texture, array_texture_sampler, uv, u32(index)); out.color *= apply_pbr_lighting(pbr_input); @@ -55,8 +58,6 @@ fn fragment( // note this does not include fullscreen postprocessing effects like bloom. out.color = main_pass_post_lighting_processing(pbr_input, out.color); - // we can optionally modify the final result here - out.color = out.color * 2.0; #endif return out;