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

CanvasItem.draw_rect() culling problems #23711

Closed
wingedadventurer opened this issue Nov 14, 2018 · 8 comments · Fixed by #37626
Closed

CanvasItem.draw_rect() culling problems #23711

wingedadventurer opened this issue Nov 14, 2018 · 8 comments · Fixed by #37626

Comments

@wingedadventurer
Copy link
Contributor

wingedadventurer commented Nov 14, 2018

bugsquad edit: The root of the problem was recognized in this comment: #23711 (comment)


Godot version:
3.1 alpha 2

OS/device including version:
Windows 10

Issue description:
Take a look at the gif below first:

draw_rect

So each box consists of 2 rectangles that are drawn using draw_rect(). However when I move the camera so they are positioned at either top or left edge, if one of those rects is outside, both of them disappear. The rects can even be separated, doesn't matter. This even happened at the rightmost edge of screen.

On Discord we suspected it's because of culling. But it doesn't make sense to cull anything because there are visible parts on the screen.

I've tried drawing a lot of rects and got mixed results, but I was unable to understand how exactly it works. Feel free to play around in the minimal reproduction project.

Minimal reproduction project:
draw_rect invisible.zip

@kubecz3k
Copy link
Contributor

kubecz3k commented Nov 15, 2018

This happens because there is no way to define a size of Node2D (it's just a point, without width or height).
If you convert your node to for example to Control you will have correct results when you define the width and height for this control (both Node2D and Control are extending CanvasItem so you don't need to change the code).
edit: above statements are wrong, see #23711 (comment)

@kubecz3k kubecz3k changed the title draw_rect() issue at the edges of window (Camera2D) Way to define CanvasItem size for occlusion culling Nov 15, 2018
@wingedadventurer
Copy link
Contributor Author

@kubecz3k Will it work if I scale the Control node?

@kubecz3k
Copy link
Contributor

Actually, I think maybe it is a good idea to give some way to define a size for culling inside the CanvasItem (so reopening).

@kubecz3k kubecz3k reopened this Nov 15, 2018
@kubecz3k
Copy link
Contributor

@wingedadventurer I think the scale will be taken into account, but you for sure will need to define some width and height first (if the size is 0 it will still be 0 after scaling)

@kubecz3k
Copy link
Contributor

kubecz3k commented Nov 15, 2018

@wingedadventurer another node which you can use is Sprite (if you for some reason don't want to use Control one). Sprite will have the size of the texture that is assigned to it, plus it will take scale into account (so you can attach small transparent texture and regulate the culling with the scale).

@bojidar-bg
Copy link
Contributor

bojidar-bg commented Nov 15, 2018

This is either a bug or a misuse of Rect2.
The sample code contains this:

var left = Rect2(Vector2(-16, -16), Vector2(16, 32))
var right = Rect2(Vector2(16, -16), Vector2(-16, 32))

Note that the the Rect2 takes (position, size), and thus right is a Rect2 with negative size.

Printing left.merge(right) results in (-16, -16, 16, 32), that is, it thinks that both rects together have a size of 16, 32, which is wrong.

Printing left.merge(right.abs()) results in (-16, -16, 32, 32), which is correct, and additionally, passing right.abs() to draw_rect results in proper clipping.

So, either Rect2.merge() should be fixed to work with negative sizes, or the VisualServer should run rects through abs before merging them, or this should be documented in draw_rect's documentation.

@kubecz3k kubecz3k changed the title Way to define CanvasItem size for occlusion culling CanvasItem.draw_rect() culling problems Nov 15, 2018
@kubecz3k kubecz3k added this to the 3.1 milestone Nov 15, 2018
@wingedadventurer
Copy link
Contributor Author

What @bojidar-bg says is indeed true, making Rect2's size positive doesn't result in said bug anymore. Thanks!

The reason I had negative rect size is because I was drawing a door that opens by splitting. Therefore each door wing had variable width. For consistency in the code, I drew each wing from the edges, which works fine for the first one, but gives negative rect for second one.

Glad we sorted this out. 😄

@akien-mga
Copy link
Member

Related to #25829.

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

Successfully merging a pull request may close this issue.

5 participants