Hello!
I always used this code based from Intel code to compute the Screen Space AABB from the Bounding Sphere.
It's based from this sample code:
https://www.intel.com/content/www/us/en/developer/articles/technical/deferred-rendering-for-current-and-future-rendering-pipelines.html
void UpdateClipRegionRoot(in float nc, in float lc, in float lz, in float Radius, in float CameraScale, inout float ClipMin, inout float ClipMax)
{
float nz = (Radius - nc * lc) / lz;
float pz = (lc * lc + lz * lz - Radius * Radius) / (lz - (nz / nc) * lc);
if (pz > 0.0f)
{
float c = -nz * CameraScale / nc;
if (nc > 0.0f)
ClipMin = max(ClipMin, c);
else
ClipMax = min(ClipMax, c);
}
}
void UpdateClipRegion(in float lc, in float lz, in float Radius, in float CameraScale, inout float ClipMin, inout float ClipMax)
{
float rSq = Radius * Radius;
float lcSqPluslzSq = lc * lc + lz * lz;
float d = rSq * lc * lc - lcSqPluslzSq * (rSq - lz * lz);
if (d > 0.0f)
{
float a = Radius * lc;
float b = sqrt(d);
float nx0 = (a + b) / lcSqPluslzSq;
float nx1 = (a - b) / lcSqPluslzSq;
UpdateClipRegionRoot(nx0, lc, lz, Radius, CameraScale, ClipMin, ClipMax);
UpdateClipRegionRoot(nx1, lc, lz, Radius, CameraScale, ClipMin, ClipMax);
}
}
void ComputeClipRegion(in float3 Center, in float Radius, out float4 ClipRegion)
{
ClipRegion = float4(1.0f, 1.0f, 0.0f, 0.0f);
if ((Center.z + Radius) >= CameraNear)
{
float2 ClipMin = float2(-1.0f, -1.0f);
float2 ClipMax = float2(+1.0f, +1.0f);
UpdateClipRegion(Center.x, Center.z, Radius, Projection[0].x, ClipMin.x, ClipMax.x);
UpdateClipRegion(Center.y, Center.z, Radius, Projection[1].y, ClipMin.y, ClipMax.y);
ClipRegion = float4(ClipMin, ClipMax);
}
}
void ComputeBoundingBox(in float3 Center, in float Radius, out float4 Bounds)
{
ComputeClipRegion(Center, Radius, Bounds);
Bounds = 0.5f * float4(Bounds.x, -Bounds.w, Bounds.z, -Bounds.y) + 0.5f;
}
It works very good but I wonder if there is a more modern and efficient way to do it?
Something to note is you have Point Light, Spot Light, Tube Light and Rect Light in lighting calculations.
I actually compute the bounding sphere in all these types so it's not the most possible efficient way.
But for point light in all cases you would need a Sphere to Screen Space AABB.
Thanks a lot for all the help and resources about this topic!
If you can recommend another way for the sphere to screen space AABB, it's very welcome too!