From 46055a073b374abc8cafbd266a263de3b3bc46fa Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Thu, 26 Sep 2024 20:57:30 +0200 Subject: [PATCH] texture --- assets/texture.png | Bin 186 -> 462 bytes assets/texture.pxi | Bin 0 -> 918 bytes src/graphics.rs | 34 +++++++++++++++-------- src/graphics/texture.rs | 15 +--------- src/shader.wgsl | 14 +++------- src/state.rs | 6 ++-- src/state/entity.rs | 60 ++++++++++++++++++++-------------------- src/state/ui.rs | 20 +++++++------- 8 files changed, 71 insertions(+), 78 deletions(-) create mode 100644 assets/texture.pxi diff --git a/assets/texture.png b/assets/texture.png index 8d639837e4e8f368f1fd834840208f199e66a984..7f47dc46b26e0d25c9b3aa694407dba5c4a56b42 100644 GIT binary patch literal 462 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=jKx9jP7LeL$-D$|Tv8)E(|mmy zw18|52FCVG1{RPKAeI7R1_tH@j10^`nh_+nfC(<^uz(rC1}St4blLz^X5s1L7*fIb z_L?nUlYz*ALIXazgBFHWEd37;9C#r($F^a!dqGOV2MM`@6HivKnEQ72i`29@`FK6v z-iZ-38!mVj%0T+-zKNi+Av$js|63fXh|*1n{q$$!@Fv3R@RZ(Hb{2^EgTw+=X8 zcq3l@?0WIup1bR;)$h#6PWS$PeAR9X*)54%^|u^R?Obo}Fv*;;ELyGPs%zc)wk7Yn zW=GXCJ9tODzC6iebLf=ZvWY95wSI~IP`UU$e_QD-Xh9U>13OHWrnmvv4FO#pD?oLm3^ delta 123 zcmX@dyo*t>Gr-TCmrII^fq{Y7)59eQNDF{42OE%-|NK93qM}Ejzyf9l79gJyD$8IJ zTKEabR`GOk46zVQPGDf_Ou762fBn0^`yb7GQ!h|2($jVzgQMpJ&@6`o9Bd!5g0q3@ O89ZJ6T-G@yGywq2njy9T diff --git a/assets/texture.pxi b/assets/texture.pxi new file mode 100644 index 0000000000000000000000000000000000000000..18b7112d81de7804cf278062c91030775dbb9705 GIT binary patch literal 918 zcmd;J5MYo}<6zLL7_&j8YsN zMgm3>Mhf)`3>pkt>Ku$h?7=yS#Tlt7Qal`67zMU4N^D_N_|K@s!||U{;6J0pe-O_| zjDtal#VseX1gHv0+^Lg|6J~{ggM@?924*fU-^9#31)s#q)FJ^!Nr-Hu1IRi91}h}@ zIkhl%2Kc%2a!GLk74dp{xC8-d2OxIjU;~ngri-e96k~CayA#8@b22Z19GBDx&op0O z1}z|)gMqOg!L7iM5Phn!mpeF=S&%ZaltkX^SC4$H7%qqPONAp7q^1&cFR#)z105jqm-oU3qn} z@Pw!n@s(`C0@AO~>PWtE{EkZJ%$nucg{3Ih`#XX}53Spq*kK4Mh%;8pfG+{-S_v6B6Cwg!tqgNadM z@ZLv9n06Q#NvoY;N?vgJhRES730pZ;cBUOQ-XJBxdzoQT;Vls%#iIg=6B$A_F?0kn zcBz%9FZ`SsRcOV(j6vFWr&@=+#DO};w8gjG!k#ouW?gxbgJE;O*4L*qPkDjd1PewD M22T)|f#HKa05)Fung9R* literal 0 HcmV?d00001 diff --git a/src/graphics.rs b/src/graphics.rs index dfb0822..e51b006 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -7,20 +7,19 @@ use winit::{event::{Event, WindowEvent}, window::Window}; use crate::{state::State, App}; mod texture; -use texture::Texture; +use texture::{Texture, TEXTURE_DIMENSIONS}; #[repr(C)] #[derive(Clone, Copy, Zeroable, Pod, Debug)] pub struct Vertex { pub pos: [f32; 2], pub color: [f32; 4], - pub tex_coords: [f32; 2], + pub tex_pos: [f32; 2], /// Each bit is used as a flag : /// 1: Scaled and moved according to camera /// 2: Grayscale - /// 4: Whiter /// - /// For exemple 0b011 corresponds to Scaled and Grayscale + /// For exemple 0b01 corresponds to Scaled and 0b11 to Scaled and Grayscale pub effect: u32, } impl Vertex { @@ -29,6 +28,23 @@ impl Vertex { step_mode: wgpu::VertexStepMode::Vertex, attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4, 2 => Float32x2, 3 => Uint32], }; + + pub fn new_col(pos: [f32; 2], color: [f32; 4], effect: u32) -> Self { + Self { + pos, + color, + tex_pos: [0.; 2], + effect + } + } + pub fn new_tex(pos: [f32; 2], tex_pos: [u16; 2], effect: u32) -> Self { + Self { + pos, + color: [0.; 4], + tex_pos: [tex_pos[0] as f32/TEXTURE_DIMENSIONS[0] as f32, tex_pos[1] as f32/TEXTURE_DIMENSIONS[1] as f32], + effect + } + } } #[repr(C)] @@ -58,7 +74,6 @@ pub struct Graphics<'a> { uniforms_buf: Buffer, uniforms_bind_group: BindGroup, diffuse_bind_group: BindGroup, - diffuse_texture: Texture } impl<'a> Graphics<'a> { pub async fn init(state: &State, window: Arc) -> Self { @@ -204,8 +219,6 @@ impl<'a> Graphics<'a> { .get_default_config(&adapter, 1, 1) .unwrap(); surface.configure(&device, &surface_config); - - let diffuse_bytes = include_bytes!("../assets/texture.png"); let diffuse_texture = Texture::from_bytes(&device, &queue, diffuse_bytes, "happy-tree.png"); @@ -215,11 +228,11 @@ impl<'a> Graphics<'a> { entries: &[ wgpu::BindGroupEntry { binding: 0, - resource: wgpu::BindingResource::TextureView(&diffuse_texture.view), // CHANGED! + resource: wgpu::BindingResource::TextureView(&diffuse_texture.view), }, wgpu::BindGroupEntry { binding: 1, - resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler), // CHANGED! + resource: wgpu::BindingResource::Sampler(&diffuse_texture.sampler), } ], label: Some("diffuse_bind_group"), @@ -237,8 +250,7 @@ impl<'a> Graphics<'a> { index_buf, uniforms_buf, uniforms_bind_group, - diffuse_bind_group, - diffuse_texture + diffuse_bind_group } } pub fn window_event(&mut self, event: &WindowEvent, window: &Window) { diff --git a/src/graphics/texture.rs b/src/graphics/texture.rs index f183c85..914c49a 100644 --- a/src/graphics/texture.rs +++ b/src/graphics/texture.rs @@ -1,19 +1,6 @@ use image::GenericImageView; -const TEXTURE_DIMENSIONS: [usize; 2] = [16, 16]; - -#[derive(Debug, Clone, Copy)] -pub enum Color { - Void = 0, - Forest = 1, - Beach = 2, - Grass = 3 -} -impl Into<[f32; 2]> for Color { - fn into(self) -> [f32; 2] { - [(self as usize % TEXTURE_DIMENSIONS[0]) as f32 / TEXTURE_DIMENSIONS[0] as f32, (self as usize / TEXTURE_DIMENSIONS[1]) as f32 / TEXTURE_DIMENSIONS[1] as f32] - } -} +pub const TEXTURE_DIMENSIONS: [usize; 2] = [64, 64]; pub struct Texture { pub texture: wgpu::Texture, diff --git a/src/shader.wgsl b/src/shader.wgsl index a9682c8..6fc06d6 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -5,7 +5,7 @@ struct Uniforms { @group(0) @binding(0) var uniforms : Uniforms; struct VertexOutput { - @location(0) tex_coords: vec2f, + @location(0) tex_pos: vec2f, @location(1) color: vec4f, @location(2) effect: u32, @builtin(position) pos: vec4f @@ -15,7 +15,7 @@ struct VertexOutput { fn vs_main( @location(0) pos: vec2f, @location(1) color: vec4f, - @location(2) tex_coords: vec2f, + @location(2) tex_pos: vec2f, @location(3) effect: u32 ) -> VertexOutput { var screen_pos: vec4f; @@ -25,7 +25,7 @@ fn vs_main( screen_pos = vec4f((pos - uniforms.camera) * uniforms.zooms, 0, 1); } var out = VertexOutput( - tex_coords, + tex_pos, color, effect, screen_pos @@ -40,18 +40,12 @@ var s_diffuse: sampler; @fragment fn fs_main(in: VertexOutput) -> @location(0) vec4f { - var tex_col = textureSample(t_diffuse, s_diffuse, in.tex_coords); + var tex_col = textureSample(t_diffuse, s_diffuse, in.tex_pos); var color = vec4f((tex_col.rgb * tex_col.a) + (in.color.rgb * in.color.a), tex_col.a + in.color.a); // Grayscale if (in.effect & 2) != 0 { var v = (color.r*0.299) + (color.g*0.587) + (color.b*0.114); color = vec4f(v, v, v, color.a); } - // Whiter - if (in.effect & 4) != 0 { - color.r += 0.4; - color.g += 0.4; - color.b += 0.4; - } return color; } diff --git a/src/state.rs b/src/state.rs index afa19ee..d62aa65 100644 --- a/src/state.rs +++ b/src/state.rs @@ -195,7 +195,7 @@ impl State { self.indices = Vec::new(); for (c, cd) in self.map.voronoi.iter_cells().zip(self.map.cells_data.iter()).filter(|(_,cd)| cd.kind != CellKind::Forest) { - let mut tex_coords = cd.color(); + let mut color = cd.color(); if c.site() == self.selected_tile { color[0] = (color[0]+0.4).clamp(0., 1.); color[1] = (color[1]+0.4).clamp(0., 1.); @@ -204,7 +204,7 @@ impl State { let vs = c.iter_vertices().collect::>(); let i = self.vertices.len() as u32; for v in vs.iter() { - self.vertices.push(Vertex { pos: [v.x as f32, v.y as f32], tex_coords, effect:2 }); + self.vertices.push(Vertex::new_col([v.x as f32, v.y as f32], color, 1)); } for v in 1..(vs.len()-1) as u32 { self.indices.push(i); @@ -227,7 +227,7 @@ impl State { let vs = c.iter_vertices().collect::>(); let i = self.vertices.len() as u32; for v in vs.iter() { - self.vertices.push(Vertex { pos: [v.x as f32, v.y as f32], color, effect:2 }); + self.vertices.push(Vertex::new_col([v.x as f32, v.y as f32], color, 1)); } for v in 1..(vs.len()-1) as u32 { self.indices.push(i); diff --git a/src/state/entity.rs b/src/state/entity.rs index 34b1c22..b90ede5 100644 --- a/src/state/entity.rs +++ b/src/state/entity.rs @@ -63,31 +63,31 @@ impl Entity { ( [ // back left leg - Vertex { pos: [-0.5, 0.3], color: dark, effect:2 }, - Vertex { pos: [-0.4 + (now.sin()*0.1), -0.7 + (now.cos().max(-0.5)*0.1)], color: dark, effect:2 }, - Vertex { pos: [-0.25, 0.1], color: dark, effect:2 }, + Vertex::new_col([-0.5, 0.3], dark, 1), + Vertex::new_col([-0.4 + (now.sin()*0.1), -0.7 + (now.cos().max(-0.5)*0.1)], dark, 1), + Vertex::new_col([-0.25, 0.1], dark, 1), // back right leg - Vertex { pos: [-0.5, 0.3], color, effect:2 }, - Vertex { pos: [-0.4 + ((now + 1.).sin()*0.1), -0.7 + ((now + 1.).cos().max(-0.5)*0.1)], color, effect:2 }, - Vertex { pos: [-0.25, 0.1], color, effect:2 }, + Vertex::new_col([-0.5, 0.3], color, 1), + Vertex::new_col([-0.4 + ((now + 1.).sin()*0.1), -0.7 + ((now + 1.).cos().max(-0.5)*0.1)], color, 1), + Vertex::new_col([-0.25, 0.1], color, 1), // front left leg - Vertex { pos: [0.3, 0.2], color: dark, effect:2 }, - Vertex { pos: [0.4 + ((now-1.).sin()*0.1), -0.7 + ((now-1.).cos().max(-0.5)*0.1)], color: dark, effect:2 }, - Vertex { pos: [0.5, 0.3], color: dark, effect:2 }, + Vertex::new_col([0.3, 0.2], dark, 1), + Vertex::new_col([0.4 + ((now-1.).sin()*0.1), -0.7 + ((now-1.).cos().max(-0.5)*0.1)], dark, 1), + Vertex::new_col([0.5, 0.3], dark, 1), // front right leg - Vertex { pos: [0.3, 0.2], color, effect:2 }, - Vertex { pos: [0.4 + ((now-2.).sin()*0.1), -0.7 + ((now-2.).cos().max(-0.5)*0.1)], color, effect:2 }, - Vertex { pos: [0.5, 0.3], color, effect:2 }, + Vertex::new_col([0.3, 0.2], color, 1), + Vertex::new_col([0.4 + ((now-2.).sin()*0.1), -0.7 + ((now-2.).cos().max(-0.5)*0.1)], color, 1), + Vertex::new_col([0.5, 0.3], color, 1), // body // 3 - Vertex { pos: [-0.3, 0.], color, effect:2 }, - Vertex { pos: [0.4, -0.1], color, effect:2 }, + Vertex::new_col([-0.3, 0.], color, 1), + Vertex::new_col([0.4, -0.1], color, 1), // 11 - Vertex { pos: [0.3, 0.4], color, effect:2 }, + Vertex::new_col([0.3, 0.4], color, 1), ], [ 0,1,2, @@ -104,31 +104,31 @@ impl Entity { ( [ // back left leg - Vertex { pos: [-0.5, 0.3], color: dark, effect:2 }, - Vertex { pos: [-0.4, -0.75], color: dark, effect:2 }, - Vertex { pos: [-0.25, 0.1], color: dark, effect:2 }, + Vertex::new_col([-0.5, 0.3], dark, 1), + Vertex::new_col([-0.4, -0.75], dark, 1), + Vertex::new_col([-0.25, 0.1], dark, 1), // back right leg - Vertex { pos: [-0.5, 0.3], color, effect:2 }, - Vertex { pos: [-0.4, -0.75], color, effect:2 }, - Vertex { pos: [-0.25, 0.1], color, effect:2 }, + Vertex::new_col([-0.5, 0.3], color, 1), + Vertex::new_col([-0.4, -0.75], color, 1), + Vertex::new_col([-0.25, 0.1], color, 1), // front left leg - Vertex { pos: [0.3, 0.2], color: dark, effect:2 }, - Vertex { pos: [0.4, -0.75], color: dark, effect:2 }, - Vertex { pos: [0.5, 0.3], color: dark, effect:2 }, + Vertex::new_col([0.3, 0.2], dark, 1), + Vertex::new_col([0.4, -0.75], dark, 1), + Vertex::new_col([0.5, 0.3], dark, 1), // front right leg - Vertex { pos: [0.3, 0.2], color, effect:2 }, - Vertex { pos: [0.4, -0.75], color, effect:2 }, - Vertex { pos: [0.5, 0.3], color, effect:2 }, + Vertex::new_col([0.3, 0.2], color, 1), + Vertex::new_col([0.4, -0.75], color, 1), + Vertex::new_col([0.5, 0.3], color, 1), // body // 3 - Vertex { pos: [-0.3, 0.], color, effect:2 }, - Vertex { pos: [0.4, -0.1], color, effect:2 }, + Vertex::new_col([-0.3, 0.], color, 1), + Vertex::new_col([0.4, -0.1], color, 1), // 11 - Vertex { pos: [0.3, 0.4], color, effect:2 }, + Vertex::new_col([0.3, 0.4], color, 1), ], [ 0,1,2, diff --git a/src/state/ui.rs b/src/state/ui.rs index e948cda..0f7d257 100644 --- a/src/state/ui.rs +++ b/src/state/ui.rs @@ -67,22 +67,22 @@ impl UI { pub fn render(&self, vertices: &mut Vec, indices: &mut Vec) { let vs = [ // Terrain - Vertex { color: SECONDARY_COLOR, pos: [-1., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], effect: 0 }, - Vertex { color: SECONDARY_COLOR, pos: [-1., -1.], effect: 0 }, - Vertex { color: SECONDARY_COLOR, pos: [0., -1.], effect: 0 }, - Vertex { color: SECONDARY_COLOR, pos: [0., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], effect: 0 }, + Vertex::new_tex([-1., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], [0, 1], 0), + Vertex::new_tex([-1., -1.], [0, 12], 0), + Vertex::new_tex([0., -1.], [11, 12], 0), + Vertex::new_tex([0., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], [11, 1], 0), // Entities - // 3, - // 2, - Vertex { color: SECONDARY_COLOR, pos: [1., -1.], effect: 0 }, - Vertex { color: SECONDARY_COLOR, pos: [1., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], effect: 0 } + // Vertex::new_tex([0., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], [12, 1], 0), + // Vertex::new_tex([0., -1.], [11, 12], 0), + // Vertex::new_tex([1., -1.], [] 0), + // Vertex::new_tex([1., -1.+(BOTTOM_TAB_BAR_HEIGHT*2.)], 0) ]; let ids = [ 0,1,2, 2,3,0, - 3,2,4, - 4,5,3 + // 3,2,4, + // 4,5,3 ]; let i = vertices.len() as u32; vertices.extend_from_slice(&vs);