Godot Version
4.x
Question
I have been hitting my head against a problem that I just found is long solved in upstream graphics APIs we’ve been using for decades.
Problem: precise measuring of light source occlusion by any solid a fragment, used to draw suitable glow sprite and /or lens flare effect.
Solution: Turns our Vulkan has a special tool for that - an OcclusionQuery
I’ve seen this working in Source engine games (namely Black Mesa) but when I looked at footage even super old Half-Life: Source has this working.
Basically this render a small shape (sphere) around light source and counts pixels that pass a depth test. Then returns percentage as occlusion factor. This is smoothed in time to avoid single-frame jitter, and as a result a nice halo/flare sprite can be drawn that behaves naturally and responds to any solid geometry occluding the light source in full or partially.
Pros:
- the expensive stuff happens on the GPU
- supports alpha-tested transparency
- native support in Vulkan and OpenGL
Cons:
- Godot doesn’t expose this, so unless a GDextension can rectify that, we’re out of luck
Workarounds:
- Use one or more raycasts to approximate occclusion (only works with collision shapes, expensive, no support fro alpha-tested materials)
- (untested) Setup an extra camera and viewport (one for each light source, argh!) that will draw a circle around the light source, and the count fragments (pixels) in resulting frame buffer that passed depth test… That would be pretty much the same as what OcclusionQuery already does, but super slow and cumbersome… possibly not scaling well to multiple light sources etc. but that’s the only real solution I see that is doable right now.