Adding ShaderMaterial to TileMap Causes strange offset

Godot Version

4.2

Question

I’ve made a TileMap, each tile of which is 32x32 pixels. It looks good in the viewport at first:
Screenshot 2024-05-19 193427

I want to add a ShaderMaterial (test.material) which colors tiles which are within the player’s movement range. I’ve created a ShaderMaterial which does what I want, except that when I add it to the TileMap’s Material slot, it shifts tiles near the edge of the starting Camera view to the right by half of a tile:
Screenshot 2024-05-19 193721

What could be causing this behavior? I’ve included below the shader script (test.gdshader), which is attached in the test.material’s Shader slot.

shader_type canvas_item;
render_mode skip_vertex_transform;
const float tile_size = 32.0;

varying flat vec2 vertex_pos[2];
uniform vec2 player_pos;
uniform float attack_range;
uniform float move_range;

void vertex() {
	// Called for every vertex the material is visible on.
	vertex_pos[0] = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy + vec2(16.0,16.0);
}
void fragment() {
    // Called for every pixel the material is visible on.
    float distX = abs(vertex_pos[0].y - player_pos.y);
    float distY = abs(vertex_pos[0].x - player_pos.x);
    float l1_dist = distX + distY;
    float in_move_range = 0.0;
    float in_attack_range = 0.0;
    if(l1_dist < move_range*tile_size + 1.0){
    	in_move_range = 1.0;
    }

    vec4 textureColor = texture(TEXTURE,UV);
    if (in_move_range == 1.0){
    	COLOR = mix(textureColor, 0.8*vec4(0.0,0.8,0.7,1.0), .7*in_move_range);
    }
    else if (in_attack_range == 1.0){
    	COLOR = mix(textureColor, 0.8*vec4(1.0,0.1,0.1,1.0), .7*in_attack_range);
    }

disable texture padding for your tileset
image

might be related to this:

Thanks for your answer - unfortunately that didn’t fix the issue, still getting the same strange behavior. I’ll look through the question you linked, too, though.

i still not understand the question fully, have you tried this on a sprite2d see if the behavior still happening?

I’ll try to check out what happens on a 2d sprite - I don’t really have experience adding ShaderMaterials to these objects, though. In the meantime, I noticed that if, in the shader script, I remove the line render_mode = skip_vertex_transforms, then the problem disappears.