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:
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:
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);
}