godot: Image.set_pixel(x, y, Color) does not appear to work on Android

Edit: Reworked and added additional details

Tested versions

  • Reproduced on v4.1.3.stable.official [f06b6836a] and v4.2.1.stable.official [b09f793f5]

System information

Android 14 - Mobile renderer

Issue description

I’ve created an image from a viewport of a 3D model. Within Windows I’m able to change the colors outside of the character model based on the RenderingServer.get_default_clear_color(). On Windows the code works as expected, but when I export to Android it’s not working.

Behold the mighty expected behaviour on Windows 👌: image

Now gaze upon the ineffectiveness of the code when Mobile renderer takes over on Android 😒: Screenshot_20240323-192515

Could this be somehow the PNG is processed and saved?

Steps to reproduce

  1. Download the MRP4.zip and run the code on Windows to see it work.
  2. Setup Android export to deploy to build for Android
  3. Run it on Android and it will not change color of the PNG produced as it did in Windows

What the project structure looks like: image

This is the offending code. is_equal_approx is never true and even if I hardcode the value of the color (as seen here

func change_color():
	var display_img = Image.load_from_file(image_loc)
	for y in display_img.get_height():
		for x in display_img.get_width():
			var pix_color = display_img.get_pixel(x, y)
			var replace = replace_color
			var is_equal = pix_color.is_equal_approx(replace)
			if is_equal:
				display_img.set_pixel(x, y, new_color)
	display_img.resize(256,256)
	rect_take_image.set_texture(ImageTexture.create_from_image(display_img))

Note: replace_color is RenderingServer.get_default_clear_color() which it looks the same to me but somehow Godot misreads the PNG color on Android

Minimal reproduction project (MRP)

MRP4.zip

Source code below at: https://github.com/godotengine/godot/issues/89800#issuecomment-2016634280

About this issue

  • Original URL
  • State: open
  • Created 3 months ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

So I figured out what was happening. I had the project in 4.2, and when I back-ported to 4.1 because of some 3D model issues, it must’ve reset the mobile renderer to Mobile (instead of Forward+).

In the MRP2 I had set the model renderer to Forward+, I changed it to Mobile and now it my MRP3 is actually reproducing hte issue. MRP3.zip

So it appears it’s an issue with Mobile renderer, not Forward+.

Great thanks I toggled a few things and restarted my machine I’m able to debug now. And it appears your instinct was right, it’s slightly different color blue on android. I’ll test on a few devices but probably have a flag to switch colors based on OS.

You can see in the image below, pix_color is the color I want to change, but it’s visibly different than the replace color which is what is set in the Project Settings -> Environment. So maybe it’s an issue with colors cross platforms? Shouldn’t a hex value be the same on different devices even if it looks different?

image