Dolly Zoom

35 篇文章 0 订阅

原文链接:Dolly Zoom (又名“Trombone”效果)
Dolly Zoom是一种有名的视觉效果,相机像一个目标物体移动同时缩小相机比例。结果是目标物体看起来大小没有变化,但是场景中所有其他物体都会按照透视的规则改变。缓慢的进行这个过程,dolly zoom能够达到突出目标物体的效果,因为它是场景中唯一不改变位置的物体。也可以故意快速进行,以创造出一种迷失方向的印象。
一个恰好和视锥垂直高度一致的物体会在屏幕上占满视图的高度方向。不用管物体与相机的距离和相机的FOV,这一点都是成立的。比如可以将相机像物体移动,同时将fov调宽,物体仍然能刚好占满视锥的高度。特定的目标物体在屏幕上看起来大小没有变化,所其他所有物体的大小会随着距离和FOV的变化而变化。这就是dolly zoom效果的本质。
alt text
使用代码来制造这种效果,需要在开始缩放时保存物体的视锥高度。接着随着相机移动,根据新的距离调整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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值