跳到主要内容
← Vertex Transformations

Vertex Transformations

Perspective division

w 越小,看起来越近、越大;w 越大,看起来越远、越小。

实时渲染 · 网格 GLSL

第 5 课:Perspective division

实现:w 越小,看起来越近、越大;w 越大,看起来越远、越小。

实现

uniform float iTime;

attribute int index;

const float dx[4] = float[](-0.5,  0.5, 0.5, -0.5);
const float dy[4] = float[](-0.5, -0.5, 0.5,  0.5);

void main() {
  vec2 pos = vec2(dx[index], dy[index]);
  float t = (cos(iTime+3.14) + 1.0) / 2.0;

  // 顶部点: w = 1.0 -> 1.5
  // 底部点: w = 1.0 -> 1.0/1.5
  float w = 1.0;

  if (dy[index] > 0.0) {
    w = mix(1.0, 1.5, t);           // 顶部点
  } else {
    w = mix(1.0, 1.0 / 1.5, t);     // 底部点
  }


  gl_Position = vec4(pos, 0.0, w);
}

复盘笔记

  • w 越小,看起来越近、越大;w 越大,看起来越远、越小。