Improved coverage estimation for SpotLight3D #107993
Open
+50
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem:
When using
SpotLight3D
I noticed that engine often picks subdivision quadrant from the atlas that is too small and shadows look blocky and flicker too much. This is noticeable when the beam is narrow and the camera is looking in the direction almost parallel to the beam, then current estimation code will return a very low coverage number for this scenario, while spot light in fact can fill entire screen and have almost 100% coverage. I think the original code assumes that objects are always placed at the very end of the range ofSpotLight3D
, which is not always the case.Solution:

The simple solution I came up with is to add one more point for estimating coverage - point that is on the main axis of the SpotLight3D and is closest to the camera. This greatly improves estimate for coverage when the beam is narrow. Here is the drawing to explain it:
Corner case explanation: when point happens to be behind the camera we are basically performing an orthogonal projection (same as before) because of the code: points[j].z = -zn, so that estimate still works well.
And a few real examples to compare.


Old estimate (you can see how shadow switches between different resolutions):
New estimate:
Important to note, that for wider beams this change will keep their behavior, as for wider beams the diameter at the base of the cone is going to be the largest estimate anyways. And of course this is just an estimate, proper evaluation of coverage area would require much more computations.