Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
creeper-0910 opened this issue Oct 13, 2023 · 3 comments
Open

Comments

@creeper-0910
Copy link

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
Copy link
Owner

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
Copy link
Author

creeper-0910 commented Oct 15, 2023

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants