three.js NDC空间转视图空间(getViewPosition)

three.js NDC空间转视图空间

请添加图片描述

NDC空间转视图空间, 比较常用。很多效果依赖于此。
包括:

  • GTAOShader
  • ReflectorForSSRPass
  • SSRShader

下面看一下它的庐山真面目。

1、计算clipW

视图空间(view) 应用投影矩阵后转换到 剪裁空间(clip)
[ A 0 B 0 0 C D 0 0 0 E F 0 0 − 1 0 ] [ v i e w x v i e w y v i e w z 1 ] = [ c l i p x c l i p y c l i p z c l i p w ] \left[ \begin{matrix} A & 0 & B & 0 \\ 0 & C & D & 0 \\ 0 & 0 & E & F \\ 0 & 0 & -1 & 0 \end{matrix} \right] \left[ \begin{matrix} view_{x} \\ view_{y} \\ view_{z} \\ 1 \\ \end{matrix} \right] = \left[ \begin{matrix} clip_{x} \\ clip_{y} \\ clip_{z} \\ clip_{w} \\ \end{matrix} \right] A0000C00BDE100F0 viewxviewyviewz1 = clipxclipyclipzclipw

投影矩阵按列存储在glsl中。那么就会存储为:
[ A 0 0 0 0 C 0 0 B D E − 1 0 0 F 0 ] \left[ \begin{matrix} A & 0 & 0 & 0 \\ 0 & C & 0 & 0 \\ B & D & E & -1 \\ 0 & 0 & F & 0 \end{matrix} \right] A0B00CD000EF0010

于是:

// clipZ = E * viewZ + F
clipZ = (viewZ * cameraProjectionMatrix[2][2]) + cameraProjectionMatrix[2][3]

// clipW = -1 * viewZ + 0
clipW = (viewZ * cameraProjectionMatrix[3][2]) + cameraProjectionMatrix[3][3] = -viewZ
2、计算viewZ

原理见: three.js 中的 perspectiveDepthToViewZ

float getDepth( const in vec2 uv ) {
  return texture2D( tDepth, uv ).x;
}

float getViewZ( const in float depth ) {
  return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );
}

float depth = getDepth( vUv );
float viewZ = getViewZ( depth );
3、计算 viewPos
vec3 getViewPosition(
  const in vec2 uv,
  const in float depth,
  const in float clipW
) {
  // ndc
  vec4 clipPosition = vec4( ( vec3(uv, depth) - 0.5 ) * 2.0, 1.0 );

  // clip
  clipPosition *= clipW;

  // view
  return ( cameraInverseProjectionMatrix * clipPosition ).xyz;
}

vec3 viewPosition = getViewPosition( vUv, depth, clipW );

相关推荐

  1. 美国空军出版物:网络空间作战

    2024-07-17 21:36:03       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 21:36:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 21:36:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 21:36:03       58 阅读
  4. Python语言-面向对象

    2024-07-17 21:36:03       69 阅读

热门阅读

  1. 关于HDFS 和HBase

    2024-07-17 21:36:03       18 阅读
  2. python基础语法

    2024-07-17 21:36:03       22 阅读
  3. C#线程池介绍及应用

    2024-07-17 21:36:03       20 阅读
  4. Collections.unmodifiableList

    2024-07-17 21:36:03       18 阅读
  5. 自动驾驶,革了谁的命

    2024-07-17 21:36:03       24 阅读
  6. linux service小例

    2024-07-17 21:36:03       20 阅读
  7. 正则表达式

    2024-07-17 21:36:03       21 阅读
  8. 笔记:运行时动态更改Ioc服务的实现

    2024-07-17 21:36:03       23 阅读
  9. 力扣—最大连续1的个数 III

    2024-07-17 21:36:03       22 阅读
  10. Netty HTTP

    2024-07-17 21:36:03       17 阅读
  11. 后仿综述 Gate Level Simulation: A Comprehensive Overview

    2024-07-17 21:36:03       19 阅读