godotengine / godotengine/godot
TileMapLayer's grid and placed tiles are positioned incorrectly inside CanvasLayer with "Follow Viewport" enabled
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
- Reproducible in: v4.3.stable.official [77dcf97d8], v4.2.1.stable.mono.official [b09f793f5]
- Not reproducible in: v3.6.stable.official [de2f0f147]
### System information
Windows 11 - Godot 4.3 Compatibility
### Issue description
I have the following scene:

There is a TileMap node and a CanvasLayer parent node. Everything is set to default values except for the CanvasLayer's "Follow Viewport," which is enabled, and "Scale," which is set to 2 (can be any value above 1).
Here are the values in the inspector:

Now, if we select a tile and hover over the scene, we can see a preview of where it will be placed. However, when we click, the tile is placed in a different position. The grid has the same issue.

Enabling the **View -> Preview Canvas Scale** option does not resolve the issue. The preview and the grid remain misaligned with the placed tile.

### Conclusion
This issue makes it impossible to work on larger scenes because the farther the distance from the center of the scene, the worse the problem becomes. At some point, the placed tile is completely off-screen. The only solution at the moment is to disable the "Follow Viewport" option, draw the tiles, and then enable it again.

### Testing Godot 3
Here is how it looks in Godot **3.6** (v3.6.stable.official [de2f0f147]). The grid is positioned correctly, and the preview matches the actual position of the placed tile.

If I enable the **Preview Canvas Scale** option, the alignment starts to be misaligned. However it doesn't break when we are far away from scene zero coordinates.

### Side notes
- It's also broken with the old (and deprecated) TileMap node
### Steps to reproduce
1. Open "main_scene.tscn"
2. Select "Broken_TileMapLayer"
3. Select any tile on the Tile Map panel
4. Hower over the scene to see the preview and try to place the tile
5. You can also select the "Working_TileMapLayer" to see how it works without CanvasLayer as a parent
It might not be visible if camera is positioned perfectly at zero coordinates, so you should move it.
You can also enable grid to see that it's also misaligned:

### Minimal reproduction project (MRP)
[canvaslayer-and-tilemap-issue.zip](https://github.com/user-attachments/files/17970402/canvaslayer-and-tilemap-issue.zip)
Contributor guide
Research direction
Open main_scene.tscn from the linked minimal reproduction project and compare Broken_TileMapLayer with Working_TileMapLayer while Follow Viewport and Scale are enabled. Reproduce the grid, preview, and placed-tile mismatch away from scene origin; done means the preview, grid, and placed tiles remain aligned, including at larger distances.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- game-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100