102 lines
4.6 KiB
GLSL
102 lines
4.6 KiB
GLSL
#version 450
|
||
|
||
const int MAX_LIGHTS = 4;
|
||
|
||
struct Light {
|
||
vec4 color; // rgb = 颜色,a = 强度
|
||
vec4 direction; // 方向光:xyz = 方向(已归一)
|
||
vec4 position; // 点光:xyz = 世界位置
|
||
vec4 params; // x = 类型(0=方向,1=点),y = 衰减半径
|
||
};
|
||
|
||
layout(location = 0) in vec3 inPosition;
|
||
layout(location = 1) in vec3 inNormal;
|
||
layout(location = 2) in vec2 inUV;
|
||
layout(location = 3) in vec3 inTangent;
|
||
layout(location = 4) in uvec4 inBoneIds; // 骨骼槽(palette 下标,非全局节点号)
|
||
layout(location = 5) in vec4 inWeights; // 蒙皮权重(无骨骼顶点 = (1,0,0,0))
|
||
|
||
layout(location = 0) out vec2 fragUV;
|
||
layout(location = 1) out vec3 fragNormal;
|
||
layout(location = 2) out vec3 fragWorldPos;
|
||
layout(location = 3) out float fragViewDepth; // CSM:视图深度(级联选择)
|
||
layout(location = 4) out vec2 fragVelocity; // TAA:clip 空间运动向量
|
||
layout(location = 5) out vec3 fragTangent; // 世界空间切线(法线贴图)
|
||
|
||
// GPUDriven:模型矩阵从实例缓冲按实例索引取。
|
||
// 同一 mesh 的 N 个实体 = 1 次 drawIndexed(instanceCount=N),
|
||
// draw 侧用 firstInstance 指向该 batch 在实例数组中的起始下标。
|
||
layout(std430, set = 0, binding = 1) readonly buffer ObjectBuffer {
|
||
mat4 models[]; // 连续 mat4 数组(16B 对齐,std430/std140 布局一致)
|
||
} objects;
|
||
|
||
// 上一帧实例矩阵(与 ObjectBuffer 同下标 = 同一实例的上一帧姿态)
|
||
layout(std430, set = 0, binding = 2) readonly buffer PrevObjectBuffer {
|
||
mat4 prevModels[];
|
||
} prevObjects;
|
||
|
||
// 骨骼蒙皮:每实例一个 palette 基址(与 models 平行),骨骼矩阵全局扁平
|
||
layout(std430, set = 0, binding = 3) readonly buffer SkinOffsetBuffer {
|
||
uint skinBases[]; // 实例 → palette 基址
|
||
} skinOffsets;
|
||
|
||
layout(std430, set = 0, binding = 4) readonly buffer SkinPaletteBuffer {
|
||
mat4 bones[]; // 全局 palette(槽 0 = identity,静态模型直通)
|
||
} skinPalettes;
|
||
|
||
layout(set = 0, binding = 0) uniform UniformBufferObject {
|
||
mat4 view;
|
||
mat4 proj;
|
||
mat4 lightSpace[3]; // CSM 各级联光空间 VP
|
||
vec4 cascadeSplits; // xyz = 级联 0..2 远边界(视图深度)
|
||
vec4 ambientColor;
|
||
Light lights[MAX_LIGHTS];
|
||
mat4 projNoJitter;
|
||
mat4 prevView;
|
||
mat4 prevProj;
|
||
vec4 cameraPos;
|
||
} ubo;
|
||
|
||
// 4 权重混合的蒙皮矩阵。无骨骼顶点 weights=(1,0,0,0)+ids=0 → identity。
|
||
mat4 SkinMatrix() {
|
||
uint base = skinOffsets.skinBases[gl_InstanceIndex];
|
||
return skinPalettes.bones[base + inBoneIds.x] * inWeights.x +
|
||
skinPalettes.bones[base + inBoneIds.y] * inWeights.y +
|
||
skinPalettes.bones[base + inBoneIds.z] * inWeights.z +
|
||
skinPalettes.bones[base + inBoneIds.w] * inWeights.w;
|
||
}
|
||
|
||
void main() {
|
||
mat4 model = objects.models[gl_InstanceIndex];
|
||
mat4 skin = SkinMatrix();
|
||
|
||
vec3 skinnedPos = (skin * vec4(inPosition, 1.0)).xyz;
|
||
vec4 worldPos = model * vec4(skinnedPos, 1.0);
|
||
gl_Position = ubo.proj * ubo.view * worldPos;
|
||
|
||
// ---- TAA velocity:当前帧与上一帧的 clip 空间位置差(UV 空间)----
|
||
// 抖动不进 velocity:velocity 描述表面点的真实屏幕位移,必须用未抖动投影。
|
||
// 静态内容 → velocity=0 → 历史精确采回同一纹素,只做逐帧抖动的超采样平均
|
||
//(清清楚楚、无重采样模糊)。若把抖动计入 velocity,静止画面也会得到非零
|
||
// 速度 → 每帧亚纹素重采样历史 → 持续发糊(本次"画面很糊"的根因)。
|
||
// v1:蒙皮形变不进 velocity(prev 用同一蒙皮姿态),边缘 ghosting 接受
|
||
vec4 currClip = ubo.projNoJitter * ubo.view * worldPos;
|
||
vec4 prevWorldPos = prevObjects.prevModels[gl_InstanceIndex] * vec4(skinnedPos, 1.0);
|
||
vec4 prevClip = ubo.prevProj * ubo.prevView * prevWorldPos;
|
||
fragVelocity = (currClip.xy / currClip.w - prevClip.xy / prevClip.w) * 0.5;
|
||
|
||
fragWorldPos = worldPos.xyz;
|
||
// CSM:视图深度(正值,与 cascadeSplits 同尺度)
|
||
fragViewDepth = -(ubo.view * worldPos).z;
|
||
|
||
mat3 normalMatrix = mat3(transpose(inverse(model)));
|
||
// 法线先过蒙皮旋转(形变方向),再过模型逆变换
|
||
fragNormal = normalize(normalMatrix * (mat3(skin) * inNormal));
|
||
// 切线随模型变换(法线贴图 TBN;无切线数据保持零,frag 侧退回几何法线)
|
||
fragTangent = normalize(mat3(model) * (mat3(skin) * inTangent));
|
||
// 零向量 normalize 产生 NaN,这里直接透传零,frag 判长度
|
||
fragTangent = dot(inTangent, inTangent) > 0.0 ? fragTangent : vec3(0.0);
|
||
|
||
fragUV = inUV;
|
||
}
|