Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_image() gives wrong result on GradientTexture2D #66627

Closed
bionick7 opened this issue Sep 29, 2022 · 4 comments
Closed

get_image() gives wrong result on GradientTexture2D #66627

bionick7 opened this issue Sep 29, 2022 · 4 comments

Comments

@bionick7
Copy link

bionick7 commented Sep 29, 2022

Godot version

4.0 beta2 (mono version)

System information

Ubuntu 22

Issue description

the get_image() command gets an image with both wrong size and wrong general look when called on GradientTexture2D.
It seams the the image is always 64x64 pixels. It depicts a different gradient with generally the same colors.

Steps to reproduce

Put the following code on a Sprite2D and put any gradient not 64x64 pixels in size into "gradient_texture"

extends Sprite2D

@export var gradient_texture: GradientTexture2D 

func _ready():
	var img = gradient_texture.get_image()
	prints(img.get_width(), img.get_height())
	texture = ImageTexture.create_from_image(img)

Minimal reproduction project

GradientTexture2DBug.zip

@timothyqiu
Copy link
Member

Size properties are set after gradient:

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_gradient", "get_gradient");
ADD_PROPERTY(PropertyInfo(Variant::INT, "width", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_width", "get_width");
ADD_PROPERTY(PropertyInfo(Variant::INT, "height", PROPERTY_HINT_RANGE, "1,2048,or_greater,suffix:px"), "set_height", "get_height");

set_gradient() updates the image directly, while set_width() and set_height() defers the update:

void GradientTexture2D::set_width(int p_width) {
ERR_FAIL_COND_MSG(p_width <= 0 || p_width > 16384, "Texture dimensions have to be within 1 to 16384 range.");
width = p_width;
_queue_update();
}

So in _ready(), the image you get still uses the default 64x64 size, and there's a size update pending.

As a workaround, you can put either snippet at the beginning of _ready():

# Wait for one frame
await get_tree().process_frame

# Or something like this to trigger the image update
var g := gradient_texture.gradient
gradient_texture.gradient = null
gradient_texture.gradient = g

@Calinou
Copy link
Member

Calinou commented Sep 30, 2022

The update is likely deferred so that the gradient isn't generated twice when both width and height are set at the same time. Otherwise, whenever the gradient uses a non-default width and height, it'd be generated twice.

We may not be able to fix this without causing this inefficiency, so I'd vote for documenting this in the class reference instead.

@knil79
Copy link

knil79 commented May 8, 2023

This is confirmed in 4.0.2 as well, is there already a commit for the documentation update with the work around?

@KoBeWi
Copy link
Member

KoBeWi commented Aug 29, 2023

Fixed by #81137

@KoBeWi KoBeWi closed this as completed Aug 29, 2023
@KoBeWi KoBeWi added this to the 4.2 milestone Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants