Godot Version
4.3
Context
I want to create a minimap based on the TileMapLayers I used.
I iterate over all my TileMapLayers, calculate the scale based on the Minimap size and create the pixels on the image.
I can’t wrap my hand around the correct way to calculate this.
Either the maps are small in the corner or the width is correct but not the height, like in the screenshot.
Code of the current implementation:
func create_minimap():
var maps := get_tree().get_nodes_in_group("map")
var image := Image.create_empty(64, 64, false, Image.FORMAT_RGB8)
for map: TileMapLayer in maps:
var map_scale = Vector2(1.0, 1.0)
#map_scale = Vector2(image.get_size()) / Vector2(map.get_used_rect().size)
map_scale = Vector2(image.get_size()) / (get_viewport().get_visible_rect().size / Vector2(map.tile_set.tile_size))
for cell_coords in map.get_used_cells():
var tile = map.get_cell_tile_data(cell_coords)
var color_coords = (
(Vector2(map.get_used_rect().position))
+
(Vector2(cell_coords))
)
image.fill_rect(Rect2(Vector2(color_coords) * map_scale, map_scale.max(Vector2(1, 1))), tile.get_custom_data("color"))
%Minimap.texture = ImageTexture.create_from_image(image)
Right now I get the visible viewport rect, because that’s all I have right now while I’m testing this. In the future the TileMapLayer size will be bigger than the viewport.
The fill_rect
I think I should use to account for scales that aren’t square.
Screenshot:
Question
How do I calculate the scale correctly?