Hey, Im trying to make a shader akin to this video: https://youtu.be/nYch_TIkq6w?t=482 . The TLDW is: Take two textures, one a source with RG pixel values that are UV coordinates to a second texture, which is the palette map. For instance RGBA value of (13,1,0,1) would map to the pixel position x=13 y = 1 in the second map/palette texture.
I tried implementing this in WGSL like so, but I get very strange behaviour, and usually end up getting rendered pixels that are all 0,0,0., or all map to the 4 top left pixels in my palette, indicating that the palette_uv values are incorrect. The sampled values inside uv_map are correct, bc when i return them, the image is the same as my source image.
a minimal working example is here: https://github.com/RaminKav/test_shaders
@group(1) @binding(0)
var source_color_texture: texture_2d<f32>;
@group(1) @binding(1)
var source_texture_sampler: sampler;
@group(1) @binding(2)
var lookup_color_texture: texture_2d<f32>;
@group(1) @binding(3)
var lookup_texture_sampler: sampler;
@fragment
fn fragment(
#import bevy_sprite::mesh2d_vertex_output
) -> @location(0) vec4<f32> {
let uv_map_dims = vec2<f32>(textureDimensions(source_color_texture));
let palette_dims = vec2<f32>(textureDimensions(lookup_color_texture));
// snap value algorithem to remove weird fuzzy pixels/interpolation
let uv_map_uv = uv - (uv % (1f/uv_map_dims)) + (1f / (uv_map_dims * 2f));
let uv_map = textureSample(
source_color_texture,
source_texture_sampler,
uv_map_uv
);
let palette_uv = ((uv_map.rg * 255f) + vec2<f32>(0.5)) / palette_dims;
let color = textureSample(
lookup_color_texture,
lookup_texture_sampler,
palette_uv
);
return vec4<f32>(color.rgb, uv_map.a);
}
Ive tried playing around with the math in the let palette_uv = ... line, but i can never get the result I want. Sometimes I can see some of the colors in my palette show up, but never the correct result.