展示 HN:OpenGL (Three.js) 的反向透视相机
Show HN: Reverse perspective camera for OpenGL (Three.js)

原始链接: https://github.com/bntre/reverse-perspective-threejs

该项目展示了一个自定义的 OpenGL 投影矩阵,可在透视投影、正交投影和反向透视投影之间实现流畅过渡——一种比熟悉的“多莉变焦”更极端的视觉效果。核心思想是将焦点从相机位置转移到视锥体的形状。 投影类型由单个参数 'p' 控制,代表投影光线的角度正切:正值表示透视投影,零表示正交投影,负值表示反向透视投影。一个“焦点平面”在过渡期间保持恒定大小,简化了矩阵构建。 相机有效地位于该焦点平面上,投影矩阵使用特定值(Sx、Sy、A、B 和 'p')定义,以将深度(近平面/远平面)映射到裁剪空间。`updateProjectionMatrix()` 函数详细说明了计算过程。 提供了一个实时演示 ([https://bntre.github.io/reverse-perspective-threejs/](https://bntre.github.io/reverse-perspective-threejs/)) 和视频 ([https://www.youtube.com/watch?v=_5xI7a7cxBg](https://www.youtube.com/watch?v=_5xI7a7cxBg)),使用了 three.js 库中的模型。

一位开发者在Hacker News分享了一个项目,扩展了OpenGL相机(在Three.js中实现),加入了“反透视”投影,以及标准的透视和正交视图。这允许在这些投影之间进行平滑过渡,尤其可以实现扩展的“多莉变焦”效果,创造出独特的眩晕感,并允许在不分散背景注意力的前提下进行镜头拉伸。 反透视还提供更广阔的空间视野,让观众可以同时看到近处和远处的物体——例如,建筑的立面*和*屋顶。开发者指出,由于它使用了现有渲染流水线中的自定义投影矩阵,因此不会对性能产生影响。 提供了一个在线演示,以及一个交叉眼立体版本,以获得更令人迷失方向的体验。其他用户对该技术在创意编程项目中的应用表现出兴趣,并讨论了潜在的、但最终是不必要的z-test修改。代码可在GitHub上找到。
相关文章

原文

This example demonstrates how to construct a custom OpenGL projection matrix that allows a smooth transition between three types of projection: direct perspective, orthographic projection, and reverse perspective.

For background, see https://en.wikipedia.org/wiki/Reverse_perspective.
This visual effect is somewhat similar to the well-known "Dolly zoom" (https://en.wikipedia.org/wiki/Dolly_zoom), but more radical in nature.

Try it live: https://bntre.github.io/reverse-perspective-threejs/

Demo video: https://www.youtube.com/watch?v=_5xI7a7cxBg

Video cover

In this approach, the camera position becomes irrelevant when the projection changes.
What really matters is how the frustum shape is transformed.

The type of perspective is defined by the inclination angle of a single projection ray:

  • p > 0 corresponds to direct perspective
  • p = 0 corresponds to orthographic projection
  • p < 0 corresponds to reverse perspective

Within the usual near and far clip planes, a so-called focus plane is introduced. As the projection changes, the size of this plane remains constant.

Projections

Projections

It is convenient to define the projection matrix by placing the camera at the center of the focus plane (so that the near < 0 and the far > 0), and by defining p as the tangent of the angle of the projection ray passing through the point (1, 0, 0).

The resulting projection matrix has the following form:

  [ Sx  0   0   0
    0   Sy  0   0
    0   0   A   B
    0   0   -p  1 ]

The values A and B are computed so that the z coordinate in the range [near, far] maps to [-1, 1] in clip space.

See the updateProjectionMatrix() function for details:
https://github.com/bntre/reverse-perspective-threejs/blob/main/index.html#L51

For more information about projection matrices, refer to the excellent article by Song Ho Ahn: https://www.songho.ca/opengl/gl_projectionmatrix.html

  • Animated models from three.js examples
    (the models are not included in this repo; they are loaded at runtime from the three.js repository):
联系我们 contact @ memedata.com