2026-04-02 23:04:59 +08:00
|
|
|
// XC_BUILTIN_FORWARD_LIT_OPENGL_PS
|
|
|
|
|
#version 430
|
2026-04-05 13:50:52 +08:00
|
|
|
layout(binding = 0) uniform sampler2D uBaseColorTexture;
|
|
|
|
|
layout(binding = 1) uniform sampler2D uShadowMapTexture;
|
2026-04-02 23:04:59 +08:00
|
|
|
|
2026-04-05 13:50:52 +08:00
|
|
|
layout(std140, binding = 0) uniform PerObjectConstants {
|
2026-04-02 23:04:59 +08:00
|
|
|
mat4 gProjectionMatrix;
|
|
|
|
|
mat4 gViewMatrix;
|
|
|
|
|
mat4 gModelMatrix;
|
|
|
|
|
mat4 gNormalMatrix;
|
2026-04-05 15:44:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
layout(std140, binding = 1) uniform LightingConstants {
|
2026-04-02 23:04:59 +08:00
|
|
|
vec4 gMainLightDirectionAndIntensity;
|
|
|
|
|
vec4 gMainLightColorAndFlags;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-05 15:44:37 +08:00
|
|
|
layout(std140, binding = 2) uniform MaterialConstants {
|
2026-04-02 23:04:59 +08:00
|
|
|
vec4 gBaseColorFactor;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-05 15:44:37 +08:00
|
|
|
layout(std140, binding = 3) uniform ShadowReceiverConstants {
|
2026-04-04 23:01:34 +08:00
|
|
|
mat4 gWorldToShadowMatrix;
|
|
|
|
|
vec4 gShadowBiasAndTexelSize;
|
|
|
|
|
vec4 gShadowOptions;
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-02 23:04:59 +08:00
|
|
|
in vec3 vNormalWS;
|
|
|
|
|
in vec2 vTexCoord;
|
2026-04-04 23:01:34 +08:00
|
|
|
in vec3 vPositionWS;
|
2026-04-02 23:04:59 +08:00
|
|
|
|
|
|
|
|
layout(location = 0) out vec4 fragColor;
|
|
|
|
|
|
2026-04-04 23:01:34 +08:00
|
|
|
float ComputeShadowAttenuation(vec3 positionWS) {
|
|
|
|
|
if (gShadowOptions.x < 0.5) {
|
|
|
|
|
return 1.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec4 shadowClip = gWorldToShadowMatrix * vec4(positionWS, 1.0);
|
|
|
|
|
if (shadowClip.w <= 0.0) {
|
|
|
|
|
return 1.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 shadowNdc = shadowClip.xyz / shadowClip.w;
|
|
|
|
|
vec2 shadowUv = vec2(
|
|
|
|
|
shadowNdc.x * 0.5 + 0.5,
|
2026-04-05 12:40:34 +08:00
|
|
|
shadowNdc.y * 0.5 + 0.5);
|
2026-04-04 23:01:34 +08:00
|
|
|
if (shadowUv.x < 0.0 || shadowUv.x > 1.0 ||
|
|
|
|
|
shadowUv.y < 0.0 || shadowUv.y > 1.0 ||
|
2026-04-05 12:40:34 +08:00
|
|
|
shadowNdc.z < -1.0 || shadowNdc.z > 1.0) {
|
2026-04-04 23:01:34 +08:00
|
|
|
return 1.0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float shadowDepth = texture(uShadowMapTexture, shadowUv).r;
|
2026-04-05 12:40:34 +08:00
|
|
|
float receiverDepth = shadowNdc.z * 0.5 + 0.5 - gShadowBiasAndTexelSize.x;
|
2026-04-04 23:01:34 +08:00
|
|
|
float shadowStrength = clamp(gShadowBiasAndTexelSize.w, 0.0, 1.0);
|
|
|
|
|
return receiverDepth <= shadowDepth ? 1.0 : (1.0 - shadowStrength);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 23:04:59 +08:00
|
|
|
void main() {
|
|
|
|
|
vec4 baseColor = texture(uBaseColorTexture, vTexCoord) * gBaseColorFactor;
|
|
|
|
|
if (gMainLightColorAndFlags.w < 0.5) {
|
|
|
|
|
fragColor = baseColor;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vec3 normalWS = normalize(vNormalWS);
|
|
|
|
|
vec3 directionToLightWS = normalize(gMainLightDirectionAndIntensity.xyz);
|
|
|
|
|
float diffuse = max(dot(normalWS, directionToLightWS), 0.0);
|
2026-04-04 23:01:34 +08:00
|
|
|
float shadowAttenuation = diffuse > 0.0
|
|
|
|
|
? ComputeShadowAttenuation(vPositionWS)
|
|
|
|
|
: 1.0;
|
2026-04-02 23:04:59 +08:00
|
|
|
vec3 lighting = vec3(0.28) +
|
2026-04-04 23:01:34 +08:00
|
|
|
gMainLightColorAndFlags.rgb * (diffuse * gMainLightDirectionAndIntensity.w * shadowAttenuation);
|
2026-04-02 23:04:59 +08:00
|
|
|
fragColor = vec4(baseColor.rgb * lighting, baseColor.a);
|
|
|
|
|
}
|