Wrong World Position in Tilemap Shader

Zyran65Zyran65 Posts: 2Member

I was trying to write a shader that can change the color around a target object on the tilemap using the following code:

shader_type canvas_item;

uniform float circle_size;
uniform mat4 global_transform;
uniform vec2 target_position;

varying vec2 world_position;

void vertex() {
    world_position = (global_transform * vec4(VERTEX, 1f, 0f)).xy;

void fragment() {
    COLOR = texture(TEXTURE, UV);
    float dist = distance(world_position, target_position);
    COLOR.a = min(1f, dist / circle_size);

However the result ended up looking like this:

Also, when the target moves away from the origin(0, 0), then the circle shown above completely disappears. It looks to me like each quadrant is being rendered using the same VERTEX coordinates starting at (0, 0).

Is there some way to get the correct position of a fragment for each quadrant? Or would I have to find a workaround to achieve this effect?

Tags :


  • ricodricod Posts: 23Member

    I think you're right and also suspect they're shared. I ended up printing the tilemap to a single image and fed it to a Sprite node on top, and then apply any shaders that need to use the entire tilemap.

  • Zyran65Zyran65 Posts: 2Member

    First, thanks a lot for the reply,

    and you're right. After reading a bit through the tilemap code, I believe each quadrant is rendered to a texture starting at (0, 0), which gives the above result.

    Right now rendering the tilemap to a sprite seems to be the only possible solution to me.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file