Mask sprite using polygon.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pontifex
:warning: Old Version Published before Godot 3 was released.

I need to show only part of a sprite that is in bounds of a polygon. Every solution I’ve seen made use of lights and static textures. In my case it has to be a polygon, possibly changing at runtime. What is the best way to accomplish that?

:bust_in_silhouette: Reply From: lukas

You can do this with Polygon2D. Set a texture in its property editor. Then in editor or via code (set_uv) you can set uv (play with it to see how it works). It has a drawback - edges are quite rough.

This is exactly what I want to avoid. But well… I may have no choice.

Pontifex | 2016-04-17 18:00

Maybe (I hope since I am also interested in this topic) someone comes with better solution.

lukas | 2016-04-17 18:02

Until that happens, any tips on how to calculate new UV when changing Polygon2D shape, to avoid texture stretching?

Pontifex | 2016-04-17 18:05

No (note that it does not definitively mean that it can’t be calculated).

lukas | 2016-04-18 09:48

Sorry to rez an ancient thread, but for future reference when people stumble onto here, here’s how to re-UV basic polygon translations without hard-coding it:

  • Save one set of UVs into variables at the start, before modifying any UV values.
  • Every UV update, calculate the difference between the same UV values now and those starting values (reset distance), then add the new UV offset you want.
  • Add this combined shifted UV distance to all UVs, since it’s just the offset from the last position to the new position.

More advanced polygon deformations would need to multiply or divide from UV’s number range to the positional number range. But individual edits would still subtract the reset distance above in some capacity, to line it up with the rest of the polygon.

shy | 2019-04-20 17:02

:bust_in_silhouette: Reply From: sleepprogger

You can use a viewport set to render target to generate a dynamic texture you can then use as mask. See Mask a sprite with a dynamic mask - Archive - Godot Forum