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

155 lines
5.9 KiB
Plaintext
Raw Normal View History

2023-11-13 09:07:23 +01:00
import Common;
2023-10-09 17:20:30 +02:00
struct MaterialParameter
{
2023-11-26 12:47:48 +01:00
float3 position_WS;
2024-05-06 18:36:16 +02:00
float2 texCoords[MAX_TEXCOORDS];
2023-11-26 12:47:48 +01:00
float3 vertexColor;
2023-11-13 09:07:23 +01:00
};
2023-10-09 17:20:30 +02:00
2023-11-13 09:07:23 +01:00
struct LightingParameter
{
2024-12-27 17:06:43 +01:00
float3x3 tangentToWorld;
float3 viewDir_WS;
float3 position_WS;
2024-12-21 20:47:57 +01:00
}
2024-12-27 17:06:43 +01:00
float3x3 qTangentToMatrix(float4 q){
q = normalize(q);
float qx2 = q.x + q.x;
float qy2 = q.y + q.y;
float qz2 = q.z + q.z;
float qxqx2 = q.x * qx2;
float qxqy2 = q.x * qy2;
float qxqz2 = q.x * qz2;
float qxqw2 = q.w * qx2;
float qyqy2 = q.y * qy2;
float qyqz2 = q.y * qz2;
float qyqw2 = q.w * qy2;
float qzqz2 = q.z * qz2;
float qzqw2 = q.w * qz2;
float3x3 m = float3x3(1.0 - (qyqy2 + qzqz2), qxqy2 + qzqw2, qxqz2 - qyqw2,
qxqy2 - qzqw2, 1.0 - (qxqx2 + qzqz2), qyqz2 + qxqw2,
qxqz2 + qyqw2, qyqz2 - qxqw2, 1.0 - (qxqx2 + qyqy2));
m[2] = normalize(cross(m[0], m[1])) * ((q.w < 0.0) ? -1.0 : 1.0);
return m;
}
float4 qTangentSlerp(float4 q0, float4 q1, float t){
float co = dot(q0, q1), so, s0, s1, s2 = 1.0, Omega;
if(co < 0.0){
co = -co;
s2 = -s2;
}
if((1.0 - co) > 1e-8){
Omega = acos(co);
so = sin(Omega);
s0 = sin((1.0 - t) * Omega) / so;
s1 = sin(t * Omega) / so;
} else {
s0 = 1.0 - t;
s1 = t;
}
float4 r = ((q0 * s0) + (q1 * (s1 * s2)));
return r * ((((q0.w < 0.0) || (q1.w < 0.0)) != (r.w < 0.0)) ? -1.0 : 1.0);
2024-12-21 20:47:57 +01:00
}
2023-11-13 09:07:23 +01:00
// data passed to fragment shader
struct FragmentParameter
{
2024-05-15 15:27:13 +02:00
float4 position_CS : SV_Position;
2024-06-19 10:33:19 +02:00
#ifndef POS_ONLY
2024-12-27 17:06:43 +01:00
float3 normal_WS : NORMALWS;
float3 tangent_WS : TANGENTWS;
float3 biTangent_WS : BITANGENTWS;
2024-11-02 22:19:08 +01:00
float3 position_WS : POSITIONWS;
float3 vertexColor : COLOR;
float4 texCoords0 : TEXCOORDS0;
float4 texCoords1 : TEXCOORDS1;
float4 texCoords2 : TEXCOORDS2;
float4 texCoords3 : TEXCOORDS3;
2024-12-21 20:47:57 +01:00
MaterialParameter getMaterialParameter()
{
MaterialParameter result;
result.position_WS = position_WS;
2024-08-26 21:49:09 +02:00
result.texCoords[0] = texCoords0.xy;
result.texCoords[1] = texCoords0.zw;
result.texCoords[2] = texCoords1.xy;
result.texCoords[3] = texCoords1.zw;
result.texCoords[4] = texCoords2.xy;
result.texCoords[5] = texCoords2.zw;
result.texCoords[6] = texCoords3.xy;
result.texCoords[7] = texCoords3.zw;
2024-12-21 20:47:57 +01:00
result.vertexColor = vertexColor;
return result;
}
LightingParameter getLightingParameter()
2024-07-17 14:34:00 +02:00
{
2024-12-21 20:47:57 +01:00
LightingParameter result;
2024-12-27 17:06:43 +01:00
result.tangentToWorld = getTangentToWorld();
result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS);
result.position_WS = position_WS;
2024-12-21 20:47:57 +01:00
return result;
2024-07-17 14:34:00 +02:00
}
2024-12-25 14:59:08 +01:00
float3x3 getTangentToWorld()
{
2024-12-30 23:47:24 +01:00
// in theory, transposing would make this a world-to-tangent matrix, but because we are building the matrix ourselves
// and something with matrix layouts is working the opposite direction, we need to transpose here
return transpose(float3x3(tangent_WS, biTangent_WS, normal_WS));
2024-12-25 14:59:08 +01:00
}
2024-06-19 10:33:19 +02:00
#endif
2024-07-08 13:46:49 +02:00
static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords)
{
FragmentParameter result;
result.position_CS = f0.position_CS * barycentricCoords.x + f1.position_CS * barycentricCoords.y + f2.position_CS * barycentricCoords.z;
#ifndef POS_ONLY
2025-01-29 16:15:48 +01:00
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
2024-12-27 17:06:43 +01:00
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * barycentricCoords.z;
2025-01-29 16:15:48 +01:00
result.position_WS = f0.position_WS * barycentricCoords.x + f1.position_WS * barycentricCoords.y + f2.position_WS * barycentricCoords.z;
result.vertexColor = f0.vertexColor * barycentricCoords.x + f1.vertexColor * barycentricCoords.y + f2.vertexColor * barycentricCoords.z;
result.texCoords0 = f0.texCoords0 * barycentricCoords.x + f1.texCoords0 * barycentricCoords.y + f2.texCoords0 * barycentricCoords.z;
result.texCoords1 = f0.texCoords1 * barycentricCoords.x + f1.texCoords1 * barycentricCoords.y + f2.texCoords1 * barycentricCoords.z;
result.texCoords2 = f0.texCoords2 * barycentricCoords.x + f1.texCoords2 * barycentricCoords.y + f2.texCoords2 * barycentricCoords.z;
result.texCoords3 = f0.texCoords3 * barycentricCoords.x + f1.texCoords3 * barycentricCoords.y + f2.texCoords3 * barycentricCoords.z;
2024-07-08 13:46:49 +02:00
#endif
return result;
}
2023-11-13 09:07:23 +01:00
};
// data retrieved from VertexData
2023-11-09 22:15:51 +01:00
struct VertexAttributes
{
2023-11-27 21:08:27 +01:00
float3 position_MS;
2024-06-19 10:33:19 +02:00
#ifndef POS_ONLY
2024-12-31 12:10:26 +01:00
float4 qTangent;
2023-11-13 09:07:23 +01:00
float3 vertexColor;
2024-05-06 18:36:16 +02:00
float2 texCoords[MAX_TEXCOORDS];
2024-06-19 10:33:19 +02:00
#endif
2024-12-27 17:06:43 +01:00
FragmentParameter getParameter(float4x4 transformMatrix, float4x4 inverseTransformMatrix)
2023-11-13 09:07:23 +01:00
{
2023-11-27 21:08:27 +01:00
float4 modelPos = float4(position_MS, 1);
float4 worldPos = mul(transformMatrix, modelPos);
2024-10-01 16:56:04 +02:00
float4 viewPos = mul(pViewParams.viewMatrix, worldPos);
float4 clipPos = mul(pViewParams.projectionMatrix, viewPos);
FragmentParameter result;
result.position_CS = clipPos;
2024-06-19 10:33:19 +02:00
#ifndef POS_ONLY
2024-12-31 12:10:26 +01:00
float3x3 tbn = qTangentToMatrix(qTangent);
result.tangent_WS = normalize(mul(transformMatrix, float4(tbn[0], 0)).xyz);
result.biTangent_WS = normalize(mul(transformMatrix, float4(tbn[1], 0)).xyz);
result.normal_WS = normalize(mul(transformMatrix, float4(tbn[2], 0)).xyz);
2023-11-13 09:07:23 +01:00
result.vertexColor = vertexColor;
2024-12-21 20:47:57 +01:00
result.position_WS = worldPos.xyz;
2024-08-26 21:49:09 +02:00
result.texCoords0 = float4(texCoords[0], texCoords[1]);
result.texCoords1 = float4(texCoords[2], texCoords[3]);
result.texCoords2 = float4(texCoords[4], texCoords[5]);
result.texCoords3 = float4(texCoords[6], texCoords[7]);
2024-06-19 10:33:19 +02:00
#endif
2023-11-13 09:07:23 +01:00
return result;
}
2023-11-09 22:15:51 +01:00
};