godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Allow Sprite's "Convert to Mesh2D", "Convert to Polygon2D" etc be callable in script. #2071

Open Xascoria opened 3 years ago

Xascoria commented 3 years ago

Describe the project you are working on

(Irrelevant) A game with a world map

Describe the problem or limitation you are having in your project

After Creating individuals sprite from a colored country sheet (on runtime), I'm unable to create Area2D that I need to be used for detecting mouse position since I can't create a CollisionShape2D at runtime from a sprite.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Expose the following functions to be callable in script: Sprite: Convert to Mesh2D Sprite: Convert to Polygon2D Sprite: Create CollisionPolygon2D Sibling Sprite: Create LightOccluder Sibling

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

var mesh = Sprite_ref.convert_to_mesh2d() or something along that light

If this enhancement will not be used often, can it be worked around with a few lines of script?

Pretty much impossible to worked around for some of them, like for example Create CollisionPolygon2D Sibling can only be worked around if you use custom vectorization algo on the sprite and then converts them to positional points

Is there a reason why this should be core and not an add-on in the asset library?

The functions are already in the core engine, no reason they shouldn't be accessible via script

clayjohn commented 3 years ago

The functions right now are in editor code, they aren't actually in the core engine. That means that are in the "tools" build of the engine, but they aren't included in the release templates.

Accordingly, this proposal is actually asking for new functions to be added to the engine rather than for functions to be simply exposed to scripting.

One of the nice things about Godot is that the editor is built as a Godot app. This means that you can actually duplicate the editor functionality in GDScript without adding anything into the core engine!

Xascoria commented 3 years ago

The functions right now are in editor code, they aren't actually in the core engine. That means that are in the "tools" build of the engine, but they aren't included in the release templates.

My bad, I just checked the relevant source code for this part and it is indeed in the editor, but I still think it wouldn't makes much sense for something that is doable in the editor but not via script to exist in the engine.

clayjohn commented 3 years ago

I still think it wouldn't makes much sense for something that is doable in the editor but not via script to exist in the engine.

Fair enough! :) I'm just trying to help focus the proposal.

Calinou commented 3 years ago

After Creating individuals sprite from a colored country sheet (on runtime), I'm unable to create Area2D that I need to be used for detecting mouse position since I can't create a CollisionShape2D at runtime from a sprite.

Out of curiosity, can't you use a few premade CollisionShapes for all country flags instead of generating them from the images?

As far as I know, most country flags have the same size/aspect ratio. There are a few exceptions such as Switzerland, but these are usually well-known.

Zireael07 commented 3 years ago

@Calinou: Nope, quote from Wiki: The ratios most commonly used are 2:3, used by 85 of 195 sovereign states, followed by 1:2, used by 54 sovereign states.

That's two different ratios, plus you get all the oddities like Switzerland, Albania and Nepal.

https://en.wikipedia.org/wiki/List_of_aspect_ratios_of_national_flags#:~:text=The%20proportions%20were%20the%20same,3%20for%20the%20naval%20ensign.&text=Officially%20specified%20in%20the%20Law%20on%20the%20National%20Flag%20and%20Other%20Flags.&text=By%20law%2C%20both%201%3A2,3%3A5%20is%20more%20common.

Xascoria commented 3 years ago

Out of curiosity, can't you use a few premade CollisionShapes for all country flags instead of generating them from the images?

@Calinou: Oh mate, you misunderstood, if it's flag I can probably write a func to make me custom sized Rect2D and deal with the exception myself, I'm talking about country borders here

Xrayez commented 3 years ago

If we're going to add such conversion functionality in core, I think we should go for systematic approach for the code which deals with converting different types. Perhaps just like GDScript has convert() function, but this shouldn't be specific to GDScript. Could be technically implemented as a Convert singleton! In fact I think this might be a nice idea to be implemented in Goost too... goostengine/goost#7.

Alternatively, come up with a convention that each class should define it's own convert(to_class: String) method if needed, this way there's no way to bloat the Sprite API.

The problem is the same as for having randomization methods in each of the core class, which also bloats the API... That's why I also previously proposed #1741 so that we can add more useful methods to Random singleton without touching core classes (this is already implemented in Goost, btw).

ChildLearningClub commented 1 year ago

The Shaggy Dev’s Video Titled, “Pixel-perfect collisions on sprites in Godot, seems relevant, so I’m posting it here. He shows how to generate collisions shapes from sprites in code during runtime.