texture
This commit is contained in:
parent
0a58032400
commit
46055a073b
Binary file not shown.
Before Width: | Height: | Size: 186 B After Width: | Height: | Size: 462 B |
BIN
assets/texture.pxi
Normal file
BIN
assets/texture.pxi
Normal file
Binary file not shown.
@ -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<Window>) -> 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) {
|
||||
|
@ -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,
|
||||
|
@ -5,7 +5,7 @@ struct Uniforms {
|
||||
@group(0) @binding(0) var<uniform> 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;
|
||||
}
|
||||
|
@ -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::<Vec<_>>();
|
||||
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::<Vec<_>>();
|
||||
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);
|
||||
|
@ -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,
|
||||
|
@ -67,22 +67,22 @@ impl UI {
|
||||
pub fn render(&self, vertices: &mut Vec<Vertex>, indices: &mut Vec<u32>) {
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user