Is it possible to find out what area a Sprite2D are visible?
Asked Answered
L

11

0

After a scene is rendered, and a bunch of Sprites have been overlaid on top of each other, is it possible to query a sprite2d to see which parts of it are still visible?

If so, how?

Lancastrian answered 4/5, 2023 at 1:27 Comment(0)
L
0

Ak.. messed the title up a bit, sorry, should read:

Is it possible to find out what areas of a Sprite2D are visible?

Lancastrian answered 4/5, 2023 at 1:28 Comment(0)
S
0

What do you need this for? And what do you mean by "area" of a sprite?

Shoreline answered 4/5, 2023 at 3:4 Comment(0)
T
0

You could get_rect the sprite2Ds, and do some math with Rect2s. I'm less than an expert on 2D, but it's an idea.

Transudate answered 4/5, 2023 at 3:23 Comment(0)
L
0

Shoreline

Long story I'm trying to find out what parts of the sprite is visible, to apply a glow effect to only part of it, as a way to solve of this problem: https://github.com/naturally-intelligent/godot-4-glow-2d

Lancastrian answered 4/5, 2023 at 3:33 Comment(0)
S
0

Lancastrian Draw the occluders too, but with a shader that punches holes into the alpha channel.

Shoreline answered 4/5, 2023 at 4:14 Comment(0)
L
0

Shoreline

Sorry, I don't understand.

Lancastrian answered 4/5, 2023 at 5:16 Comment(0)
S
0

Lancastrian Looking at your setup/code, you put clones of glowing sprites into a subviewport, then pick up that viewport's texture and blur it.
What I'm saying is put the clones of all sprites into that subviewport, let the clones of glowing sprites have normal appearance (as they already do), but draw all other sprites (the potential occluders) using a shader that subtracts sprite's alpha from what's already in the framebuffer - effectively punching alpha holes in places of occluder sprites. The end result should be a texture containing only visible parts of glowing sprites, everything else will be alpha-masked-out.

Shoreline answered 4/5, 2023 at 12:21 Comment(0)
L
0

Shoreline

I don't get it still. Wouldn't that delete all the glow sprites, since the occluder sprites are all bigger than the glow sprites... there would be a need to differentiate which ones are on top of each other:

Lancastrian answered 4/5, 2023 at 13:44 Comment(0)
S
0

Lancastrian The draw order determines what is on top of what. The idea is to replicate the whole scene but draw the occluders as alpha-holes. It's effectively creating an alpha mask that reveals only the pixels that need to be blurred. It's exactly what your initial question asks for.
Sprites as entities are irrelevant here (except for defining the draw order). It all happens at the pixel level.

Shoreline answered 4/5, 2023 at 13:50 Comment(0)
L
0

Shoreline

Thanks for the help.

But I don't think it would work because of the draw order.
If it were possible to take into account the draw order, then it would work.
I suppose with multiple shader passes, alpha cutting for each occluder sprite, it would end up working. Seems expensive tho, and difficult to set up.

Much harder to do 2D glow than it appears!

Lancastrian answered 4/5, 2023 at 21:15 Comment(0)
S
0

Lancastrian I don't think you're following what I'm saying.
The draw order should be copied from the original sprites and it's taken into account automatically.

Here's a demo; one non-glow sprite sandwiched between two glowing sprites:

Btw I just copypasted your shader code for the demo, although it looks a bit wonky because it does not blur to the same extent in both directions. You should probably use two-pass gaussian or similar. But that's for another topic.

Shoreline answered 4/5, 2023 at 22:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.