2023-10-24 15:01:09 +02:00
|
|
|
import Common;
|
|
|
|
|
|
2024-03-29 08:49:03 +01:00
|
|
|
struct BoundingSphere
|
|
|
|
|
{
|
|
|
|
|
float4 centerRadius;
|
|
|
|
|
float3 getCenter()
|
|
|
|
|
{
|
|
|
|
|
return centerRadius.xyz;
|
|
|
|
|
}
|
|
|
|
|
float getRadius()
|
|
|
|
|
{
|
|
|
|
|
return centerRadius.w;
|
|
|
|
|
}
|
2024-05-04 09:25:13 +02:00
|
|
|
bool insideFrustum(Frustum frustum)
|
2024-03-29 08:49:03 +01:00
|
|
|
{
|
2024-03-31 10:21:09 +02:00
|
|
|
bool result = true;
|
|
|
|
|
for(int i = 0; i < 4 && result; ++i)
|
|
|
|
|
{
|
2024-05-04 09:25:13 +02:00
|
|
|
if(dot(frustum.sides[i].n, centerRadius.xyz) - frustum.sides[i].d < -getRadius())
|
2024-03-31 10:21:09 +02:00
|
|
|
{
|
|
|
|
|
result = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
2024-03-29 08:49:03 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-24 15:01:09 +02:00
|
|
|
struct AABB
|
|
|
|
|
{
|
|
|
|
|
float3 min;
|
2023-12-11 14:45:37 +01:00
|
|
|
float pad0;
|
2023-10-24 15:01:09 +02:00
|
|
|
float3 max;
|
2023-12-12 11:37:00 +01:00
|
|
|
float pad1;
|
2024-05-05 08:31:40 +02:00
|
|
|
// modified version from https://learnopengl.com/Guest-Articles/2021/Scene/Frustum-Culling
|
2024-05-04 09:25:13 +02:00
|
|
|
bool insideFrustum(Frustum frustum)
|
2024-04-06 08:29:15 +02:00
|
|
|
{
|
2024-05-05 08:31:40 +02:00
|
|
|
float3 center = (min + max) * 0.5;
|
|
|
|
|
float3 extents = float3(max.x - center.x, max.y - center.y, max.z - center.z);
|
|
|
|
|
bool result = true;
|
|
|
|
|
for(int i = 0; i < 4 && result; ++i)
|
2023-10-24 15:01:09 +02:00
|
|
|
{
|
2024-05-05 08:31:40 +02:00
|
|
|
const float r = extents.x * abs(frustum.sides[i].n.x)
|
|
|
|
|
+ extents.y * abs(frustum.sides[i].n.y)
|
|
|
|
|
+ extents.z * abs(frustum.sides[i].n.z);
|
|
|
|
|
|
|
|
|
|
const float signedDistance = dot(frustum.sides[i].n, center) - frustum.sides[i].d;
|
|
|
|
|
if(signedDistance < -r)
|
2023-10-24 15:01:09 +02:00
|
|
|
{
|
2024-05-05 08:31:40 +02:00
|
|
|
result = false;
|
2023-10-24 15:01:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-05 08:31:40 +02:00
|
|
|
return result;
|
2023-10-24 15:01:09 +02:00
|
|
|
}
|
|
|
|
|
};
|