+2 votes

Can UV coordinates of Polygon2D be changed in GDScript?

I tried and while I can set the polygon vertices, I cannot set UV coordinates.

I am doing this in an editor (3.0.6) tool script.

uv.resize(0)
uv.append(Vector2(1,1))

Doing this doesn't change UV values in inspector.

in Engine by (14 points)

Is this a bug?

1 Answer

0 votes

(Answering old question because it still comes up on Google and it's a useful question; this answer applies at least to version 3.3.3).

Like Polygon2D.polygon, it seems that Polygon2D.uv returns a copy of the array, not a reference. So calling uv.resize or uv.append only modifies the copy, not the underlying array, and therefore has no effect.

To actually set it, you need to assign the array directly:

poly.uv = PoolVector2Array([
    Vector2(0.0, 0.0),
    Vector2(0.0, 100.0),
    Vector2(100.0, 100.0),
    Vector2(100.0, 0.0),
])

The other thing to watch out for is that the UV coordinates here are specified in PIXELS, not [0,1] as used in a shader. If not specified, they appear to default to the same value as the vertex coordinates themselves (i.e., Polygon2D.uv == Polygon2D.polygon).
(Unfortunately none of this seems to be documented).

One last thing: you may need to restart the editor to make a "tool" script work.

by (14 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.
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 webmaster@godotengine.org with your username.