I’ve been trying to set the color of a tile based on a color in a texture. The texture’s size is equivalent to the size of the tilemap, where each pixel represents one tile. I need this info because I want to apply an alpha value over the tiles color based on that texture’s data.
I’m relatively new to shaders, so I’m struggling to figure out how to translate the tiles UVs to map to the correct pixel in the texture.
I’ve tried the following code, and it sort of works:
vec2 global_pos = FRAGCOORD.xy;
vec2 tile_position = floor(global_pos / tile_size);
vec2 uv = tile_position / (tilemap_size / tile_size);
vec4 color_map_color = texture(corruption_base_texture, uv);
vec4 final_color = mix(COLOR, color_map_color, 0.5);
COLOR = final_color;
This allows me to get the correct value from the texture, though the coordinates are totally off. It creates an effect where the TileMap itself is almost like a window that I’m looking at the correct texture through.
My guess is that I need to apply additional offsets about camera position and whatnot, but exactly how, I’m not sure.
Any information would be helpful, including links to shader code that works with some of these offsets that I need. I looked at several other shaders on the GodotShaders website, but most were implementing their entirely own TileMap, rather than using the built in one.