Shaders on animated TileMap shading per tile?

Godot Version

4.2

Question

While trying to apply a shader to an animated TileMap, it seems my shader is being applied per-tile? my shader looks like this:

Without my Shader it looks like this:

https://i.imgur.com/RdmptHu.png

I’m using a shader that applies a mask generated from a BitMap that matches the shape of a Polygon2D, resulting in a black and white image like this:

https://i.imgur.com/oLq6jV1.png

The shader turns the black into alpha and then uses the alpha to mask the TileMap, but it’s not working as expected, how can I make it apply to the entire tilemap?

Current Shader Code:

shader_type canvas_item;

uniform bool on = false;

uniform sampler2D polygon_texture;
uniform bool invert_mask = false;

void fragment() {
    if(on==true){
        vec4 color = texture(TEXTURE, UV);
        vec4 polygon_alpha = texture(polygon_texture, UV);
        if (polygon_alpha.r == 0.0 && polygon_alpha.g == 0.0 && polygon_alpha.b == 0.0) {
            polygon_alpha.a = 0.0;
        }
        if (invert_mask == true) {
            polygon_alpha = 1.0 - polygon_alpha;
        }
        if (polygon_alpha.a == 0.0)
        {
            color.a = 0.0;
        }
        COLOR = color;
    }
}

(Sorry for the mix of 1 embed and 2 links, new folk have limits on what they can use in the forums)

I found this post on the forum. Maybe its of help?

Looks like you need to use SCREEN_UV or use the MODEL_MATRIX/CANVAS_MATRIX, depending on what effect you are exactly going for.

1 Like

Each tile is a quad, so what you could do is render to a viewport and apply the shader to a viewportview. That, I think, is the simple way, tho there are probably better ways depending on what exactly you’re trying to acheive.