atadenizoktay / godot-click-through-transparent-window

A demo project for the Godot Engine that features a transparent window with click-through capability.
https://atadenizoktay.itch.io/godot-cttw
MIT License
42 stars 1 forks source link

Is there a way to make a moving Sprite2D background transparent? #4

Open creeper-0910 opened 8 months ago

creeper-0910 commented 8 months ago

I would like to move a live2d model on a transparent window using gd_cubism. However, since the model is moving, using CollisionPolygon2D will leave an extra border and the background color will be visible when the model moves. I would like to know if you know how to solve this problem. Thanks for the great project! I am using translation. Sorry if my English is not clear.

atadenizoktay commented 8 months ago

Hello @creeper-0910 ,

I did not quite understand what you meant with your first sentence. Particularly the "extra border" and "background-color being visible" sections.

You can still make a moving model's background transparent with this project. For every frame, you need to update the boundaries of the model as a polygon. By doing this, you tell the application which area to "draw" and which area to not. Of course, I assume that the model you use is in 2D space.

Check this script.

func _physics_process(_delta: float) -> void:
    _update_click_polygon()

## Updates the clickable area, preventing inputs from passing through the
## window outside of the defined region.
func _update_click_polygon() -> void:
    var click_polygon: PackedVector2Array = _ClickPolygon.polygon
    for vec_i in range(click_polygon.size()):
        click_polygon[vec_i] = to_global(click_polygon[vec_i])
    get_window().mouse_passthrough_polygon = click_polygon

This section in the script updates the polygon which is both drawn and that can capture the mouse input. If you call this function every frame while moving the model like I did, it will update accordingly.

I tried my best to understand and help. If this is not what you were looking for, please send a video so that I can help further. 😅

creeper-0910 commented 8 months ago

As shown in this video, CollisionPolygon2D does not follow the model as it moves. Background color is the default clear color. https://github.com/atadenizoktay/godot-click-through-transparent-window/assets/56744841/c567846c-7b93-435a-bc8b-0bca5e34e66c

atadenizoktay commented 8 months ago

Well, that behavior is as expected.

In the code, the line var click_polygon: PackedVector2Array = _ClickPolygon.polygon sets the click_polygon variable to the polygon of the _ClickPolygon node. If you have a visual whose visual boundaries change over time, you also need to update the click polygon accordingly.

An example code might look like this.

func _physics_process(_delta: float) -> void:
    _update_click_polygon()

## Updates the clickable area, preventing inputs from passing through the
## window outside of the defined region.
func _update_click_polygon() -> void:
    var click_polygon: PackedVector2Array = _calculate_click_polygon()
    for vec_i in range(click_polygon.size()):
        click_polygon[vec_i] = to_global(click_polygon[vec_i])
    get_window().mouse_passthrough_polygon = click_polygon

## Calculates and returns the click polygon for the next frame.
func _calculate_click_polygon() -> PackedVector2Array:
    # your logic