I actually found an answer to this problem in this thread: https://www.reddit.com/r/godot/comments/17zdksu/comment/k9zucsi/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
The idea is to use the RenderingServer
object to tweak the rendering of the CanvasItem
.
In the case the CanvasItem
is a Sprite2D
, it works using this simple line in the sprite script:
func _ready() -> void:
RenderingServer.canvas_item_set_custom_rect(get_canvas_item(), true, get_viewport_rect())
However for a TileMap
it doesn’t work, as explained by @mrcdk:
Looks like the Tilemap rendering code creates another canvas_item directly in the rendering server and, AFAICT, it’s not exposed to the scripting side.
godot/scene/2d/tile_map.cpp at 80de898d721f952dac0b102d48bb73d6b02ee1e8 · godotengine/godot · GitHub
Actually my problem was the same as the one in the Reddit thread:
How to dissociate the collision shape of a CanvasItem
from the actual rendering of the node. In other words, how to apply a transform to the rendering of the CanvasItem
that isn’t applied to its collision shape.
The solution I tried to use, as in the Reddit thread, was to apply a shader on the CanvasItem
. But this won’t work for a TileMap
, as explained above.
There is another solution that doesn’t use shaders, provided in the thread once more by @mrcdk. It is the following:
Apply a custom transform directly to the CanvasItem
in the RenderingServer
. Here is an example to apply a simple translation to the node:
var canvas_item = get_canvas_item()
var transform = global_transform
transform.origin.x += 100
RenderingServer.canvas_item_set_transform(canvas_item, transform)
This code has to be executed every time the CanvasItem
position change, because the transform associated with the CanvasItem
inside the RenderingServer
will then be updated accordingly to the new position (without translation).
I did test this solution and it worked to dissociate the collision shape and the actual rendering on screen of the CanvasItem
.
Last thing, don’t be fooled by the Visible Collision Shapes
option:
Enabling Visible Collision Shapes
in the Debug
menu will still show the collision shapes in the wrong position (the position where the tile is drawn) as they are part of the visual side of the TileMap
–@mrcdk