原文链接:Dolly Zoom (又名“Trombone”效果)
Dolly Zoom是一种有名的视觉效果,相机像一个目标物体移动同时缩小相机比例。结果是目标物体看起来大小没有变化,但是场景中所有其他物体都会按照透视的规则改变。缓慢的进行这个过程,dolly zoom能够达到突出目标物体的效果,因为它是场景中唯一不改变位置的物体。也可以故意快速进行,以创造出一种迷失方向的印象。
一个恰好和视锥垂直高度一致的物体会在屏幕上占满视图的高度方向。不用管物体与相机的距离和相机的FOV,这一点都是成立的。比如可以将相机像物体移动,同时将fov调宽,物体仍然能刚好占满视锥的高度。特定的目标物体在屏幕上看起来大小没有变化,所其他所有物体的大小会随着距离和FOV的变化而变化。这就是dolly zoom效果的本质。
使用代码来制造这种效果,需要在开始缩放时保存物体的视锥高度。接着随着相机移动,根据新的距离调整FOV以保持物体的高度不变。这可以使用下面的代码实现:
using UnityEngine;
using System.Collections;
public class ExampleScript : MonoBehaviour {
public Transform target;
public Camera camera;
private float initHeightAtDist;
private bool dzEnabled;
// Calculate the frustum height at a given distance from the camera.
void FrustumHeightAtDistance(float distance) {
return 2.0f * distance * Mathf.Tan(camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
}
// Calculate the FOV needed to get a given frustum height at a given distance.
void FOVForHeightAndDistance(float height, float distance) {
return 2.0f * Mathf.Atan(height * 0.5f / distance) * Mathf.Rad2Deg;
}
// Start the dolly zoom effect.
void StartDZ() {
var distance = Vector3.Distance(transform.position, target.position);
initHeightAtDist = FrustumHeightAtDistance(distance);
dzEnabled = true;
}
// Turn dolly zoom off.
void StopDZ() {
dzEnabled = false;
}
void Start() {
StartDZ();
}
void Update () {
if (dzEnabled) {
// Measure the new distance and readjust the FOV accordingly.
var currDistance = Vector3.Distance(transform.position, target.position);
camera.fieldOfView = FOVForHeightAndDistance(initHeightAtDist, currDistance);
}
// Simple control to allow the camera to be moved in and out using the up/down arrows.
transform.Translate(Input.GetAxis("Vertical") * Vector3.forward * Time.deltaTime * 5f);
}
}