0 votes

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?

in Engine by (12 points)

2 Answers

+1 vote

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.

by (675 points)

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

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

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

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

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.

0 votes

You can use a viewport set to render target to generate a dynamic texture you can then use as mask. See https://godotengine.org/qa/9249/mask-a-sprite-with-a-dynamic-mask?show=9249#q9249

by (121 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.