0 votes

I have 100 .png images loaded into an array of objects (named splash_planets) as StreamTextures. I need to know how to reference them to allow scaling. I am able to draw them using the following:

for splash_planet in splash_planets:
    var center = Vector2(splash_planet._x, splash_planet._y)
    draw_texture(splash_planet._texture, center)

Now I want to do something like:

draw_texture(splash_planet._texture, center, (scale_x, scale_y))

Thanks in advance for any advice.

Godot version 3.5.1
in Engine by (19 points)

1 Answer

0 votes
Best answer

This excellent answer was provided by Theraot on Stack Overflow:

First of all, in this line:

drawtexture(splashplanet._texture, center)
You would be drawing the texture with the upper left corner on the coordinates of center. If you want to draw the texture with the center on center, you do this:

var texture := splashplanet.texture
var size := texture.getsize()
draw
texture(texture, center - size * 0.5)
Since you are using drawtexture and you want to scale, you can use drawset_transform:

draw_set_transform(Vector2.ZERO, 0.0, Vector2(scale_x, scale_y))

As you can see the third parameter is the scale. The first two parameters are translation and rotation. Once you have set the transform you can do the draw call as usual:

draw_set_transform(Vector2.ZERO, 0.0, Vector2(scale_x, scale_y))

var texture := splash_planet._texture
var size := texture.get_size()
draw_texture(texture, center - size * 0.5)

Calling drawsettransform again (or calling drawsettransform_matrix) will overwrite the previos transform.

Alternatively you can use drawtexturerect:

var texture := splash_planet._texture
var size := texture.get_size() * Vector2(scale_x, scale_y)
var rect := Rect2(center - size * 0.5, size)
draw_texture_rect(texture, rect, false)

In this case we compute the rectangle where to draw the texture. We also specify we don't want to tile it (the false as third argument), so Godot will stretch it.

You might also be interested in drawtexturerect_region, which allows you to draw a rectangular region of the source texture.

by (19 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.