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)]
pub struct Vertex {
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 tex_pos: [f32; 2],
/// Each bit is used as a flag :
/// 1: Scaled and moved according to camera
/// 2: Grayscale
/// 4: Texture instead of color
///
/// For exemple 0b01 corresponds to Scaled and 0b11 to Scaled and Grayscale
pub effect: u32,
@ -26,23 +28,20 @@ impl Vertex {
const DESC: VertexBufferLayout<'static> = VertexBufferLayout {
array_stride: std::mem::size_of::<Vertex>() as wgpu::BufferAddress,
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 {
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
color: [tex_pos[0] as f32/TEXTURE_DIMENSIONS[0] as f32, tex_pos[1] as f32/TEXTURE_DIMENSIONS[1] as f32, 0., 0.],
effect: effect | 0b100
}
}
}

View File

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