Reputation: 171
Godot Version 4.3
Question I’m encountering an issue with how Godot processes my PNG image. I have a map where each country is assigned a specific, unique color (e.g., France = #00bbd4, Italy = #ff3131, etc.). However, when I load the scenery, the colors seem to change slightly(the color of a country which was first set to one specific color, is now set to round about 30 similar colors), which then interferes with how i like to display the map and the countries. I have a Sprite2D where i set the “Texture” to my png. I have a regions.txt file which is a dictionary where the colors are described: { “#00bbd4” : “France” ,“#ff3131” : “Italy” ,“#f1c30f” : “Spain” ,“#00cc00” : “Berber” }
my main.gd file processes the image:
extends Node2D
@onready var mapImage = $Sprite2D
func _ready():
load_regions()
func _process(delta):
pass
func load_regions():
var image = mapImage.get_texture().get_image()
var pixel_color_dict = get_pixel_color_dict(image)
var regions_dict = import_file("res://Map_data/regions.txt")
for region_color in regions_dict:
var region = load("res://Scenes/Region_Area.tscn").instantiate()
region.region_name = regions_dict[region_color]
region.set_name(region_color)
get_node("Regions").add_child(region)
var polygons = get_polygons(image, region_color, pixel_color_dict)
for polygon in polygons:
var region_collision = CollisionPolygon2D.new()
var region_polygon = Polygon2D.new()
region_collision.polygon = polygon
region_polygon.polygon = polygon
region.add_child(region_collision)
region.add_child(region_polygon)
mapImage.queue_free()
func get_pixel_color_dict(image):
var pixel_color_dict = {}
for y in range(image.get_height()):
for x in range(image.get_width()):
var pixel_color = "#" + str(image.get_pixel(int(x), int(y)).to_html(false))
if pixel_color not in pixel_color_dict:
pixel_color_dict[pixel_color] = []
pixel_color_dict[pixel_color].append(Vector2(x,y))
return pixel_color_dict
func get_polygons(image, region_color, pixel_color_dict):
var targetImage = Image.create(image.get_size().x,image.get_size().y, false, Image.FORMAT_RGBA8)
for value in pixel_color_dict[region_color]:
targetImage.set_pixel(value.x,value.y, "#ffffff")
var bitmap = BitMap.new()
bitmap.create_from_image_alpha(targetImage)
var polygons = bitmap.opaque_to_polygons(Rect2(Vector2(0,0), bitmap.get_size()), 0.1)
return polygons
#Import JSON files and converts to lists or dictionary
func import_file(filepath):
var file = FileAccess.open(filepath, FileAccess.READ)
if file != null:
return JSON.parse_string(file.get_as_text().replace("_", " "))
else:
print("Failed to open file:", filepath)
return null
My question: How can I ensure that Godot displays and processes my PNG exactly as it is from the Original, without altering colors in any way? Is there a specific import setting or workaround that guarantees the image remains unchanged?
Any help or insights would be greatly appreciated! Thanks in advance.
Upvotes: 0
Views: 20