Godot: Sprite converters/generators not accessible via script

Created on 12 Aug 2019  路  4Comments  路  Source: godotengine/godot

Godot version:
v3.1.1.stable.official x64, Ubuntu

Issue description:
Sprite converters/generators developed in #27553 are not accessible via script.
Making them available via script is essential to implement modding in certain games.
It's been already suggested in the pull request comments.

Requests:

27553#issuecomment-483554733

https://godotengine.org/qa/12450/there-way-create-collision-shapes-based-the-contour-sprite?show=12452#c12452

https://www.reddit.com/r/godot/comments/9lbttx/how_to_draw_custom_collisionshape2d_godot_30/ejfng5m/

archived feature proposal core editor

Most helpful comment

The tools to generate polygons from an image are already available, you'll need to build the collision, mesh, or polygon2d from that.

extends Node2D

var texture
var polygons = []

func _ready():
    # You need to get the image from the texture or load the image
    # directly if you imported it as an Image
    texture = preload("res://yourtexturehere.png")
    var image = t.get_data()
    # The important part starts here
    var bitmap = BitMap.new()
    # create the bitmap from the image, you can set the alpha threshold here
    bitmap.create_from_image_alpha(image, 0.1)
    var rect = Rect2(Vector2(), image.get_size())
    # grow it if you need
    bitmap.grow_mask(2, rect)
    # this will convert all the opaque parts into polygons
    polygons = bitmap.opaque_to_polygons(rect, 2)
    # create the CollisionPolygon2d
    var collision = CollisionPolygon2D.new()
    # set its polygon to the first polygon you've got
    collision.polygon = polygons[0]
    collision.position = Vector2(10, 100)
    # and add it to the node
    add_child(collision)
    update()

func _draw():
    # this is just to draw the poly and texture at the same time 
    draw_set_transform(Vector2(100, 100), 0, Vector2(1, 1))
    draw_texture(texture, Vector2())
    for poly in polygons:
        draw_colored_polygon(poly, Color(1, 1, 1, 0.1))

All 4 comments

The tools to generate polygons from an image are already available, you'll need to build the collision, mesh, or polygon2d from that.

extends Node2D

var texture
var polygons = []

func _ready():
    # You need to get the image from the texture or load the image
    # directly if you imported it as an Image
    texture = preload("res://yourtexturehere.png")
    var image = t.get_data()
    # The important part starts here
    var bitmap = BitMap.new()
    # create the bitmap from the image, you can set the alpha threshold here
    bitmap.create_from_image_alpha(image, 0.1)
    var rect = Rect2(Vector2(), image.get_size())
    # grow it if you need
    bitmap.grow_mask(2, rect)
    # this will convert all the opaque parts into polygons
    polygons = bitmap.opaque_to_polygons(rect, 2)
    # create the CollisionPolygon2d
    var collision = CollisionPolygon2D.new()
    # set its polygon to the first polygon you've got
    collision.polygon = polygons[0]
    collision.position = Vector2(10, 100)
    # and add it to the node
    add_child(collision)
    update()

func _draw():
    # this is just to draw the poly and texture at the same time 
    draw_set_transform(Vector2(100, 100), 0, Vector2(1, 1))
    draw_texture(texture, Vector2())
    for poly in polygons:
        draw_colored_polygon(poly, Color(1, 1, 1, 0.1))

Yeah, I mean... we should be able to do that with a single line of code.
Thanks for the code snippet though, trying it right now.
Works like a charm, thanks.

Thanks @mrcdk ! I'm using your code and it works perfectly! :)

But I'm with @char0xff here. I think we should have a dedicated function.

Something like create_collision_polygon_2d(sprite, parent), where:

  • sprite would be the Sprite used to generate the CollisionPolygon2D.
  • parent would be the sprite's parent node where the new collision would be attached to.

I think adding the whole Sprite instead of just the Texture is better because we then have access to all the sprite's properties, meaning the texture of course, but also size and scale, which could be used to better position the new collision (in case the sprite is not centered, for example).

And we must know the parent node because, well, the new collision has to be attached somewhere :)

I guess this could be thought further, but here are my two cents.

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

Was this page helpful?
0 / 5 - 0 ratings