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?
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?
Ak.. messed the title up a bit, sorry, should read:
Is it possible to find out what areas of a Sprite2D are visible?
What do you need this for? And what do you mean by "area" of a sprite?
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.
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 Draw the occluders too, but with a shader that punches holes into the alpha channel.
Sorry, I don't understand.
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.
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 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.
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 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.
© 2022 - 2024 — McMap. All rights reserved.