put tex_pos in color

This commit is contained in:
Arkitu 2024-09-27 19:01:30 +02:00
parent 46055a073b
commit e9f0dfde27
2 changed files with 15 additions and 15 deletions

View File

@ -13,11 +13,13 @@ use texture::{Texture, TEXTURE_DIMENSIONS};
#[derive(Clone, Copy, Zeroable, Pod, Debug)] #[derive(Clone, Copy, Zeroable, Pod, Debug)]
pub struct Vertex { pub struct Vertex {
pub pos: [f32; 2], pub pos: [f32; 2],
/// Rgba color by default but if texture flag is not set.
/// Else the first 2 f32 are texture coordinates and the 2 last are not important
pub color: [f32; 4], pub color: [f32; 4],
pub tex_pos: [f32; 2],
/// Each bit is used as a flag : /// Each bit is used as a flag :
/// 1: Scaled and moved according to camera /// 1: Scaled and moved according to camera
/// 2: Grayscale /// 2: Grayscale
/// 4: Texture instead of color
/// ///
/// For exemple 0b01 corresponds to Scaled and 0b11 to Scaled and Grayscale /// For exemple 0b01 corresponds to Scaled and 0b11 to Scaled and Grayscale
pub effect: u32, pub effect: u32,
@ -26,23 +28,20 @@ impl Vertex {
const DESC: VertexBufferLayout<'static> = VertexBufferLayout { const DESC: VertexBufferLayout<'static> = VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress, array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex, step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4, 2 => Float32x2, 3 => Uint32], attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4, 2 => Uint32],
}; };
pub fn new_col(pos: [f32; 2], color: [f32; 4], effect: u32) -> Self { pub fn new_col(pos: [f32; 2], color: [f32; 4], effect: u32) -> Self {
Self { Self {
pos, pos,
color, color,
tex_pos: [0.; 2],
effect effect
} }
} }
pub fn new_tex(pos: [f32; 2], tex_pos: [u16; 2], effect: u32) -> Self { pub fn new_tex(pos: [f32; 2], tex_pos: [u16; 2], effect: u32) -> Self {
Self { Self {
pos, pos,
color: [0.; 4], color: [tex_pos[0] as f32/TEXTURE_DIMENSIONS[0] as f32, tex_pos[1] as f32/TEXTURE_DIMENSIONS[1] as f32, 0., 0.],
tex_pos: [tex_pos[0] as f32/TEXTURE_DIMENSIONS[0] as f32, tex_pos[1] as f32/TEXTURE_DIMENSIONS[1] as f32], effect: effect | 0b100
effect
} }
} }
} }

View File

@ -5,9 +5,8 @@ struct Uniforms {
@group(0) @binding(0) var<uniform> uniforms : Uniforms; @group(0) @binding(0) var<uniform> uniforms : Uniforms;
struct VertexOutput { struct VertexOutput {
@location(0) tex_pos: vec2f, @location(0) color: vec4f,
@location(1) color: vec4f, @location(1) effect: u32,
@location(2) effect: u32,
@builtin(position) pos: vec4f @builtin(position) pos: vec4f
} }
@ -15,8 +14,7 @@ struct VertexOutput {
fn vs_main( fn vs_main(
@location(0) pos: vec2f, @location(0) pos: vec2f,
@location(1) color: vec4f, @location(1) color: vec4f,
@location(2) tex_pos: vec2f, @location(2) effect: u32
@location(3) effect: u32
) -> VertexOutput { ) -> VertexOutput {
var screen_pos: vec4f; var screen_pos: vec4f;
if (effect & 1) == 0 { if (effect & 1) == 0 {
@ -25,7 +23,6 @@ fn vs_main(
screen_pos = vec4f((pos - uniforms.camera) * uniforms.zooms, 0, 1); screen_pos = vec4f((pos - uniforms.camera) * uniforms.zooms, 0, 1);
} }
var out = VertexOutput( var out = VertexOutput(
tex_pos,
color, color,
effect, effect,
screen_pos screen_pos
@ -40,8 +37,12 @@ var s_diffuse: sampler;
@fragment @fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4f { fn fs_main(in: VertexOutput) -> @location(0) vec4f {
var tex_col = textureSample(t_diffuse, s_diffuse, in.tex_pos); var color: vec4f;
var color = vec4f((tex_col.rgb * tex_col.a) + (in.color.rgb * in.color.a), tex_col.a + in.color.a); if (in.effect & 4) == 0 {
color = in.color;
} else {
color = textureSample(t_diffuse, s_diffuse, in.color.xy);
}
// Grayscale // Grayscale
if (in.effect & 2) != 0 { if (in.effect & 2) != 0 {
var v = (color.r*0.299) + (color.g*0.587) + (color.b*0.114); var v = (color.r*0.299) + (color.g*0.587) + (color.b*0.114);