纳金网

标题: Unity3D技术之相机使用技巧-推拉变焦浅析 [打印本页]

作者: 狂风大尉    时间: 2015-1-31 20:13
标题: Unity3D技术之相机使用技巧-推拉变焦浅析
推拉变焦(又称“伸缩”变焦)
推拉变焦是一种广为人知的视觉效果,相机朝目标对象移动的同时进行缩放。使得该对象大致看起来还是相同大小,但场景中的所有其他对象都改变了视角。推拉变焦处理巧妙,可突出目标对象,因为该对象是图像场景中唯一没有改变位置的物体。变焦也可有意进行快速处理,造成定向障碍的效果。

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

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. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5