TileMap item vertex

Godot Version

v4.3.stable.official [77dcf97d8]

Question

I’m implementing a simple shader for tilemap. It gets the tilemap vertex and if the mouse position is within the tile, it makes the tile red.

How it works: it assumes VERTEX gives only the top left corner position and checks the mouse position according to that.

However, I think, when you rotate the tile when placing by TileMap painting, the VERTEX given also rotates. So the upper left corner is upper right corner, if you rotate the tile clockwise, etc… And this brokes the shader code. Because it assumes the VERTEX point is always upper left corner.

How can I overcome this issue? Is there a way to get the upper left corner as vertex always, regardless of rotation? Or is there a way to pass the rotation info to shader somehow so that it can be checked within the shader code?

Thanks,

shader_type canvas_item;

varying flat vec2 vertexPos;
uniform vec2 globalMousePos;

void vertex() {
	vertexPos = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
}

void fragment() {
	// Called for every pixel the material is visible on.
	float isWithinY = step(vertexPos.y, globalMousePos.y) * step(globalMousePos.y, vertexPos.y + 64.);
	float isWithinX = step(vertexPos.x, globalMousePos.x) * step(globalMousePos.x, vertexPos.x + 64.);
	float isWithin = isWithinY * isWithinX;
    // Sample the texture color
    vec4 textureColor = texture(TEXTURE, UV);

    // Only change the color if the mouse is within the rectangle
    if (isWithin == 1.0) {
        COLOR.r = 1.0; // Set color to red when within
    } 
	else {
        COLOR = textureColor; // Keep the original texture color if not within
    }
}```