0 votes

i have a viewport node where transparent_bg = true

var img = get_node("viewport").get_viewport().get_texture().get_data()
img.save_png("res://temp_png.png")

the viewport texture background are transparent in the editor, but black when its saved.
any idea?

asked Feb 23 in Engine by AiTechEye (101 points)

Have you checked the texture data to see whether it has alpha data?

Got it, i had to convert the format

2 Answers

+1 vote
Best answer

VIEWPORT.get_texture().get_data() returns an Image in the format, FORMAT_RGBAH.
You can check this by using IMAGE.get_format()

It will return 15, which is the enum value for IMAGE.FORMAT_RGBAH
When you save it with save_png(), all the transparent pixels are black.

You can simply convert the image to FORMAT_RGBA8, and then when you save it, it will preserve its transparency.

var my_img : Image = my_viewport.get_texture().get_data()
print(my_img.get_format())

my_img.convert(Image.FORMAT_RGBA8)
print(my_img.get_format())

my_img.flip_y()
my_img.save_png(my_path)
answered Feb 24 by Error7Studios (52 points)
selected Feb 24 by AiTechEye
+1 vote

img.convert(Image.FORMAT_RGBA8)

answered Feb 24 by AiTechEye (101 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.