#How do I make a sprite rect bigger for sprite.OverrideGeometry?

1 messages · Page 1 of 1 (latest)

heavy moss
#

So I am adding deformation to my sprite and I am updating the meshes like this every frame:
sprite.OverrideGeometry(deformedVerts, triangles);
But I am getting this error:
"Invalid vertex array. Some vertices are outside of the Sprite rectangle: "

The sprite.rect is read-only so I was wondering how I can make the sprite rect bigger to adjust to the new vertices.

Thanks in advance!

forest nimbus
# heavy moss So I am adding deformation to my sprite and I am updating the meshes like this e...

You can’t directly resize sprite.rect since it’s read-only — the error usually means your deformed vertices are ending up outside the original rect bounds. A common workaround is either to create a new sprite with a larger rect (e.g. using Sprite.Create) or adjust your deformation so vertices stay within the rect space.

Quick question: are you trying to do large mesh displacements (like stretching beyond the sprite’s original size) or just subtle deformations within its bounds? @heavy moss

heavy moss
forest nimbus
# heavy moss Im doing individual vertex displacements where vertices could end up outside of ...

Creating a new Sprite each frame would be very expensive and cause unnecessary GC allocations, so that’s not a good long-term approach. A more common solution is to generate a sprite with a larger rect once (big enough to cover your max possible vertex displacement) and then keep reusing it while you override the geometry every frame.

That way Unity won’t throw the “vertices outside of rect” error, and you won’t take the performance hit of constantly rebuilding sprites.

Do you already have an idea of the maximum displacement range your vertices could reach, or is it highly dynamic? That’ll determine how large you should make the initial sprite. @heavy moss

heavy moss
#

Techincally the maximum displacement is inifnite but I highly doubt that any vertex moves outside of a rect about 5x as big as the original