Tilemap cell UV
A small utility to get the cell UV coordinates in a tilemap
Shader code
shader_type canvas_item;
// The size of each square cell normalized
// So if your cell is 8px by 8px the value should be 1.0 / 8.0
// if your cell is 16px by 16px the value should be 1.0 / 16.0
const float tile_size = 1.0 / 8.0;
vec2 get_cell_uv(vec2 uv, vec2 pixel_size) {
vec2 ut = uv;
ut.x = mod(1.0 - uv.x, pixel_size.x / tile_size) / pixel_size.x * tile_size;
ut.y = mod(1.0 - uv.y, pixel_size.y / tile_size) / pixel_size.y * tile_size;
return ut;
}
void fragment() {
vec2 cell_uv = get_cell_uv(UV, TEXTURE_PIXEL_SIZE);
COLOR = vec4(cell_uv.x, cell_uv.y, 0.0, 1.0);
}shader_type canvas_item;
// The size of each square cell normalized
// So if your cell is 8px by 8px the value should be 1.0 / 8.0
// if your cell is 16px by 16px the value should be 1.0 / 16.0
const float tile_size = 1.0 / 8.0;
vec2 get_cell_uv(vec2 uv, vec2 pixel_size) {
vec2 ut = uv;
ut.x = mod(1.0 - uv.x, pixel_size.x / tile_size) / pixel_size.x * tile_size;
ut.y = mod(1.0 - uv.y, pixel_size.y / tile_size) / pixel_size.y * tile_size;
return ut;
}
void fragment() {
vec2 cell_uv = get_cell_uv(UV, TEXTURE_PIXEL_SIZE);
COLOR = vec4(cell_uv.x, cell_uv.y, 0.0, 1.0);
}