跳到主要内容
← Advanced Rendering

Advanced Rendering

TAA - Jitter

shader-learning 自学第 19 课:TAA - Jitter

代码复盘 GLSL

第 19 课:TAA - Jitter

本课需 多 pass / 后处理,暂以代码复盘呈现。

实现

uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 prevViewMatrix;
uniform float iTime;
uniform vec2 iResolution;

in vec3 position;
in vec3 normal;
in vec4 color;

out vec4 vColor;
out vec3 vNormal;
out vec3 vPosWorld;
out vec4 vPosScreen;
out vec4 vPosScreenPrev;

float random(vec2 point) {
  const float SEED = 1234.5678;
  float d = dot(point, vec2(SEED, SEED));
  float r = fract(sin(d) * SEED);
  r = r * 2.0 - 1.0;
  return r;
}

void main() {
  vColor = color;
  vNormal = normalize(mat3(transpose(inverse(modelMatrix))) * normal);

  vec4 worldPos = modelMatrix * vec4(position, 1.0);
  vec4 posScreen = projectionMatrix * viewMatrix * worldPos;

  vPosWorld = worldPos.xyz;
  vPosScreen = posScreen;
  vPosScreenPrev = projectionMatrix * prevViewMatrix * worldPos;

  float rx = random(position.xy + iTime);
  float ry = random(position.zy + iTime);
  vec2 jitter = vec2(rx, ry) / iResolution;

  mat4 jitteredProj = projectionMatrix;
  jitteredProj[2][0] += jitter.x; // Horizontal shift
  jitteredProj[2][1] += jitter.y; // Vertical shift

  // Add jitter here
  gl_Position = jitteredProj * viewMatrix * worldPos;
}