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
|
|
|
// data used by light environment
|
|
|
|
|
struct LightingParameter
|
|
|
|
|
{
|
2024-05-15 15:27:13 +02:00
|
|
|
float3x3 tbn;
|
2024-07-15 17:55:22 +02:00
|
|
|
float3 normal_TS;
|
|
|
|
|
float3 position_TS;
|
|
|
|
|
float3 viewDir_TS;
|
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-11-02 22:19:08 +01:00
|
|
|
float3 normal_WS : NORMALWS;
|
|
|
|
|
float3 tangent_WS : TANGENTWS;
|
|
|
|
|
float3 biTangent_WS : BITANGENTWS;
|
|
|
|
|
float3 position_WS : POSITIONWS;
|
|
|
|
|
float3 vertexColor : COLOR;
|
|
|
|
|
float4 texCoords0 : TEXCOORDS0;
|
|
|
|
|
float4 texCoords1 : TEXCOORDS1;
|
|
|
|
|
float4 texCoords2 : TEXCOORDS2;
|
|
|
|
|
float4 texCoords3 : TEXCOORDS3;
|
2023-11-13 09:07:23 +01:00
|
|
|
MaterialParameter getMaterialParameter()
|
|
|
|
|
{
|
|
|
|
|
MaterialParameter result;
|
|
|
|
|
result.position_WS = position_WS;
|
|
|
|
|
result.vertexColor = vertexColor;
|
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;
|
2023-11-13 09:07:23 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
LightingParameter getLightingParameter()
|
|
|
|
|
{
|
|
|
|
|
LightingParameter result;
|
2024-07-15 17:55:22 +02:00
|
|
|
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
|
|
|
|
result.tbn = tbn;
|
|
|
|
|
result.position_TS = mul(tbn, position_WS);
|
2024-10-07 20:14:00 +02:00
|
|
|
result.viewDir_TS = mul(tbn, normalize(pViewParams.cameraPosition_WS.xyz - position_WS));
|
2024-07-15 17:55:22 +02:00
|
|
|
result.normal_TS = mul(tbn, normal_WS);
|
2023-11-13 09:07:23 +01:00
|
|
|
return result;
|
|
|
|
|
}
|
2024-07-17 14:34:00 +02:00
|
|
|
float3x3 getTangentToWorld()
|
|
|
|
|
{
|
|
|
|
|
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
|
|
|
|
return transpose(tbn);
|
|
|
|
|
}
|
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
|
2024-07-16 15:32:51 +02:00
|
|
|
result.normal_WS = f0.normal_WS * barycentricCoords.x + f1.normal_WS * barycentricCoords.y + f2.normal_WS * barycentricCoords.z;
|
2024-07-08 13:46:49 +02:00
|
|
|
result.tangent_WS = f0.tangent_WS * barycentricCoords.x + f1.tangent_WS * barycentricCoords.y + f2.tangent_WS * barycentricCoords.z;
|
|
|
|
|
result.biTangent_WS = f0.biTangent_WS * barycentricCoords.x + f1.biTangent_WS * barycentricCoords.y + f2.biTangent_WS * barycentricCoords.z;
|
|
|
|
|
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;
|
2024-08-26 21:49:09 +02:00
|
|
|
//for(uint i = 0; i < MAX_TEXCOORDS; ++i)
|
|
|
|
|
//{
|
|
|
|
|
// result.texCoords[i] = f0.texCoords[i] * barycentricCoords.x + f1.texCoords[i] * barycentricCoords.y + f2.texCoords[i] * 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
|
2023-11-13 09:07:23 +01:00
|
|
|
float3 normal_MS;
|
|
|
|
|
float3 tangent_MS;
|
|
|
|
|
float3 biTangent_MS;
|
|
|
|
|
float3 vertexColor;
|
2024-05-06 18:36:16 +02:00
|
|
|
float2 texCoords[MAX_TEXCOORDS];
|
2024-06-19 10:33:19 +02:00
|
|
|
#endif
|
2023-11-13 09:07:23 +01:00
|
|
|
FragmentParameter getParameter(float4x4 transformMatrix)
|
|
|
|
|
{
|
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);
|
2024-05-30 21:24:21 +02:00
|
|
|
FragmentParameter result;
|
|
|
|
|
result.position_CS = clipPos;
|
2024-06-19 10:33:19 +02:00
|
|
|
#ifndef POS_ONLY
|
2024-05-15 15:27:13 +02:00
|
|
|
float3x3 normalMatrix = float3x3(transformMatrix);
|
|
|
|
|
float3 T = mul(normalMatrix, tangent_MS);
|
|
|
|
|
float3 N = mul(normalMatrix, normal_MS);
|
|
|
|
|
float3 B = mul(normalMatrix, biTangent_MS);
|
|
|
|
|
result.normal_WS = N;
|
|
|
|
|
result.tangent_WS = T;
|
|
|
|
|
result.biTangent_WS = B;
|
|
|
|
|
result.position_WS = worldPos.xyz;
|
2023-11-13 09:07:23 +01:00
|
|
|
result.vertexColor = vertexColor;
|
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
|
|
|
};
|