查看: 1143|回复: 0
打印 上一主题 下一主题

[其他] Unity3D技术之相机使用技巧-推拉变焦浅析

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38266
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-1-31 20:13:31 |只看该作者 |倒序浏览
推拉变焦(又称“伸缩”变焦)
推拉变焦是一种广为人知的视觉效果,相机朝目标对象移动的同时进行缩放。使得该对象大致看起来还是相同大小,但场景中的所有其他对象都改变了视角。推拉变焦处理巧妙,可突出目标对象,因为该对象是图像场景中唯一没有改变位置的物体。变焦也可有意进行快速处理,造成定向障碍的效果。

如屏幕上看到的一样,刚好符合垂直内视椎体的对象将占据视图的整个高度。不论对象到相机的距离有多远,不论视野如何,都是如此。例如,可将相机移近对象,然后拓宽视角,使对象刚好符合内视椎体的高度。该特定对象在屏幕上将显示相同大小,但其他所有对象的大小将随着距离和视野的改变而改变。这是推拉变焦效果的实质。

169.jpg


在代码中创造这种效果可在变焦开始时于对象所在位置减少内视椎体的高度。然后随着相机的移动,找到新距离并调整视野,保持在对象位置的相同高度。下列代码可完成该效果:-
  1. var target: Transform;

  2. private var initHeightAtDist: float;
  3. private var dzEnabled: boolean;


  4. // Calculate the frustum height at a given distance from the camera.
  5. function FrustumHeightAtDistance(distance: float) {
  6.         return 2.0 * distance * Mathf.Tan(camera.fieldOfView * 0.5 * Mathf.Deg2Rad);
  7. }


  8. // Calculate the FOV needed to get a given frustum height at a given distance.
  9. function FOVForHeightAndDistance(height: float, distance: float) {
  10.         return 2 * Mathf.Atan(height * 0.5 / distance) * Mathf.Rad2Deg;
  11. }


  12. // Start the dolly zoom effect.
  13. function StartDZ() {
  14.         var distance = Vector3.Distance(transform.position, target.position);
  15.         initHeightAtDist = FrustumHeightAtDistance(distance);
  16.         dzEnabled = true;
  17. }


  18. // Turn dolly zoom off.
  19. function StopDZ() {
  20.         dzEnabled = false;
  21. }


  22. function Start() {
  23.         StartDZ();
  24. }


  25. function Update () {
  26.         if (dzEnabled) {
  27.                 // Measure the new distance and readjust the FOV accordingly.
  28.                 var currDistance = Vector3.Distance(transform.position, target.position);
  29.                 camera.fieldOfView = FOVForHeightAndDistance(initHeightAtDist, currDistance);
  30.         }

  31.         // Simple control to allow the camera to be moved in and out using the up/down arrows.
  32.         transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * 5);
  33. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-2 17:01 , Processed in 0.127105 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部