Files
Seele/res/shaders/lib/Bounding.slang
T

60 lines
1.7 KiB
Plaintext
Raw Normal View History

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;
}
bool insideFrustum(float4x4 transform, Frustum frustum)
{
2024-03-31 10:21:09 +02:00
float3 transformed = mul(transform, float4(getCenter(), 1)).xyz;
float maxScale = max(max(transform[0][0], transform[1][1]), transform[2][2]);
float scaledRange = getRadius() * maxScale;
bool result = true;
for(int i = 0; i < 4 && result; ++i)
{
2024-04-05 10:41:59 +02:00
if(dot(frustum.sides[i].n, transformed) - frustum.sides[i].d < -scaledRange)
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;
2023-10-24 15:01:09 +02:00
bool insideFrustum(float4x4 transform, Frustum frustum)
{
2023-12-15 11:57:13 +01:00
float4 corners[8];
corners[0] = mul(transform, float4(min.x, min.y, min.z, 1.0f));
corners[1] = mul(transform, float4(min.x, min.y, max.z, 1.0f));
corners[2] = mul(transform, float4(min.x, max.y, min.z, 1.0f));
corners[3] = mul(transform, float4(min.x, max.y, max.z, 1.0f));
corners[4] = mul(transform, float4(max.x, min.y, min.z, 1.0f));
corners[5] = mul(transform, float4(max.x, min.y, max.z, 1.0f));
corners[6] = mul(transform, float4(max.x, max.y, min.z, 1.0f));
corners[7] = mul(transform, float4(max.x, max.y, max.z, 1.0f));
2023-10-24 15:01:09 +02:00
for(int i = 0; i < 8; ++i)
{
2024-02-20 21:07:17 +01:00
float3 adjusted = corners[i].xyz / corners[i].w;
2024-04-05 10:41:59 +02:00
if(frustum.pointInside(adjusted))
2023-10-24 15:01:09 +02:00
{
2024-02-23 12:50:40 +01:00
return true;
2023-10-24 15:01:09 +02:00
}
2024-02-23 12:50:40 +01:00
2023-10-24 15:01:09 +02:00
}
return false;
}
};