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

72 lines
1.3 KiB
Plaintext
Raw Normal View History

2020-06-02 11:46:18 +02:00
const static float PI = 3.1415926535897932f;
const static uint MAX_PARTICLES = 65536;
2022-02-24 22:38:26 +01:00
const static uint BLOCK_SIZE = 32;
2020-06-02 11:46:18 +02:00
2020-10-03 11:00:10 +02:00
struct ViewParameter
{
float4x4 viewMatrix;
float4x4 projectionMatrix;
2024-02-23 08:31:21 +01:00
float4x4 inverseProjection;
2020-10-03 11:00:10 +02:00
float4 cameraPos_WS;
2022-02-24 22:38:26 +01:00
float2 screenDimensions;
2020-10-03 11:00:10 +02:00
}
2024-04-23 19:11:06 +02:00
ParameterBlock<ViewParameter> pViewParams;
2020-10-03 11:00:10 +02:00
2024-02-23 08:31:21 +01:00
float4 clipToView(float4 clip)
{
float4 view = mul(pViewParams.inverseProjection, clip);
view = view / view.w;
return view;
}
float4 screenToView(float4 screen)
2020-06-02 11:46:18 +02:00
{
2023-11-08 23:27:21 +01:00
float2 texCoord = screen.xy / pViewParams.screenDimensions;
2020-06-02 11:46:18 +02:00
2022-11-17 16:47:42 +01:00
// Convert to clip space
2024-02-23 08:31:21 +01:00
float4 clip = float4( float2( texCoord.x, 1.0f-texCoord.y ) * 2.0f - 1.0f, screen.z, screen.w);
return clipToView(clip);
2020-06-02 11:46:18 +02:00
}
struct Plane
{
2022-02-24 22:38:26 +01:00
float3 n;
float d;
2024-02-01 22:54:20 +01:00
bool pointInside(float3 point)
{
2024-04-05 10:41:59 +02:00
return dot(n, point) - d > 0.0f;
2024-02-01 22:54:20 +01:00
}
2020-06-02 11:46:18 +02:00
};
struct Frustum
{
2023-10-24 15:01:09 +02:00
Plane sides[4];
bool pointInside(float3 point)
{
for(int p = 0; p < 4; ++p)
{
2024-04-05 10:41:59 +02:00
if(!sides[p].pointInside(point))
2023-10-24 15:01:09 +02:00
{
return false;
}
}
return true;
}
2020-06-02 11:46:18 +02:00
};
Plane computePlane(float3 p0, float3 p1, float3 p2)
{
2022-02-24 22:38:26 +01:00
Plane plane;
2020-06-02 11:46:18 +02:00
2022-02-24 22:38:26 +01:00
float3 v0 = p1 - p0;
float3 v2 = p2 - p0;
2020-06-02 11:46:18 +02:00
2022-02-24 22:38:26 +01:00
plane.n = normalize(cross(v0, v2));
2020-06-02 11:46:18 +02:00
2022-02-24 22:38:26 +01:00
plane.d = dot(plane.n, p0);
2020-06-02 11:46:18 +02:00
2022-02-24 22:38:26 +01:00
return plane;
2020-06-02 11:46:18 +02:00
}