- Velocity buffer:Forward 第二颜色附件,vertex 计算 clip 空间运动向量 (UBO 尾部追加 projNoJitter/prevView/prevProj;上一帧实例矩阵 SSBO Set0 binding2,与当前实例数组平行对齐——转动的物体速度正确不鬼影) - 子像素抖动:SceneRenderer Halton(2,3)×8 序列,抖动只进光栅化投影 - TAA resolve pass:velocity 反推历史位置 + 3×3 邻域 clamp 去鬼影 + mix(cur,hist,0.9) 时域累积;TAAColor 自引用历史 - RG 历史读补 post-barrier:采样后恢复镜像 resting layout(TAA 的历史 是 ColorAttachment layout,不恢复则下帧 entry oldLayout 脱节) - 管线:Forward(HDR+Velocity) → TAA(TAAColor) → Bloom/Tonemap 改源 TAAColor - 编译零错误、validation 零警告、8 秒冒烟通过(10 pass 全活跃)
60 lines
1.8 KiB
GLSL
60 lines
1.8 KiB
GLSL
#version 450
|
||
|
||
const int MAX_LIGHTS = 4;
|
||
|
||
struct Light {
|
||
vec4 direction;
|
||
vec4 color;
|
||
};
|
||
|
||
layout(location = 0) in vec2 fragUV;
|
||
layout(location = 1) in vec3 fragNormal;
|
||
layout(location = 2) in vec3 fragWorldPos;
|
||
layout(location = 3) in vec3 fragLightSpacePos;
|
||
layout(location = 4) in vec2 fragVelocity;
|
||
|
||
layout(location = 0) out vec4 outColor;
|
||
layout(location = 1) out vec2 outVelocity; // TAA velocity 附件
|
||
|
||
layout(set = 0, binding = 0) uniform UniformBufferObject {
|
||
mat4 view;
|
||
mat4 proj;
|
||
mat4 lightSpace;
|
||
vec4 ambientColor;
|
||
Light lights[MAX_LIGHTS];
|
||
} ubo;
|
||
|
||
layout(set = 1, binding = 0) uniform sampler2D texSampler;
|
||
layout(set = 2, binding = 0) uniform sampler2D shadowMap;
|
||
|
||
// 第 0 盏灯(KeyLight)的阴影因子:0.3 影中 / 1.0 受光。
|
||
// 采样器为 clamp-to-border 白色:光视锥外自动 1.0(无阴影)。
|
||
float ShadowFactor(vec3 N) {
|
||
// NDC [-1,1] → UV [0,1](ortho 的 w=1,无需透视除法)
|
||
vec2 uv = fragLightSpacePos.xy * 0.5 + 0.5;
|
||
float closest = texture(shadowMap, uv).r;
|
||
|
||
// 斜率缩放 bias:掠射面用更大的 bias 压 acne
|
||
vec3 L = normalize(-ubo.lights[0].direction.xyz);
|
||
float bias = max(0.004 * (1.0 - dot(N, L)), 0.0012);
|
||
|
||
return (fragLightSpacePos.z - bias > closest) ? 0.3 : 1.0;
|
||
}
|
||
|
||
void main() {
|
||
vec3 albedo = texture(texSampler, fragUV).rgb;
|
||
vec3 N = normalize(fragNormal);
|
||
|
||
vec3 result = ubo.ambientColor.rgb * albedo;
|
||
|
||
for (int i = 0; i < MAX_LIGHTS; ++i) {
|
||
vec3 L = normalize(-ubo.lights[i].direction.xyz);
|
||
float diff = max(dot(N, L), 0.0);
|
||
float shade = (i == 0) ? ShadowFactor(N) : 1.0;
|
||
result += ubo.lights[i].color.rgb * ubo.lights[i].color.a * diff * albedo * shade;
|
||
}
|
||
|
||
outColor = vec4(result, 1.0);
|
||
outVelocity = fragVelocity;
|
||
}
|