跳到主要内容
← Advanced Rendering

Advanced Rendering

Reconstruct Position from Depth

post-process

实时渲染 · 网格 GLSL

第 8 课:Reconstruct Position from Depth

实现:post-process

实现

uniform vec2 iResolution;
uniform mat4 inverseProjectionMatrix;
uniform mat4 inverseViewMatrix;
uniform sampler2D iDepthBuffer;

out vec4 FragColor;

const float farPlane  = 100.0;
const float nearPlane = 0.1;

void main() {
  vec2 uv = gl_FragCoord.xy / iResolution.xy;

  float depth = texture(iDepthBuffer, uv).r;
  vec3 ndc = vec3(uv, depth) * 2.0 - 1.0;
  vec4 viewPos = inverseProjectionMatrix * vec4(ndc, 1.0);
  viewPos /= viewPos.w;
  vec4 worldPos = inverseViewMatrix * viewPos;

  // replace with position restore
  float linearDepth = depth * 2.0 - 1.0;
  linearDepth = (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - linearDepth * (farPlane - nearPlane));
  linearDepth /= farPlane * 0.4;

  FragColor = vec4(vec3(worldPos), 1.0);
}

复盘笔记

  • post-process