纳金网
标题:
Unity3D技术之相机使用技巧-推拉变焦浅析
[打印本页]
作者:
狂风大尉
时间:
2015-1-31 20:13
标题:
Unity3D技术之相机使用技巧-推拉变焦浅析
推拉变焦(又称“伸缩”变焦)
推拉变焦是一种广为人知的视觉效果,相机朝目标对象移动的同时进行缩放。使得该对象大致看起来还是相同大小,但场景中的所有其他对象都改变了视角。推拉变焦处理巧妙,可突出目标对象,因为该对象是图像场景中唯一没有改变位置的物体。变焦也可有意进行快速处理,造成定向障碍的效果。
如屏幕上看到的一样,刚好符合垂直内视椎体的对象将占据视图的整个高度。不论对象到相机的距离有多远,不论视野如何,都是如此。例如,可将相机移近对象,然后拓宽视角,使对象刚好符合内视椎体的高度。该特定对象在屏幕上将显示相同大小,但其他所有对象的大小将随着距离和视野的改变而改变。这是推拉变焦效果的实质。
2015-1-31 20:12 上传
下载附件
(16.59 KB)
在代码中创造这种效果可在变焦开始时于对象所在位置减少内视椎体的高度。然后随着相机的移动,找到新距离并调整视野,保持在对象位置的相同高度。下列代码可完成该效果:-
var target: Transform;
private var initHeightAtDist: float;
private var dzEnabled: boolean;
// Calculate the frustum height at a given distance from the camera.
function FrustumHeightAtDistance(distance: float) {
return 2.0 * distance * Mathf.Tan(camera.fieldOfView * 0.5 * Mathf.Deg2Rad);
}
// Calculate the FOV needed to get a given frustum height at a given distance.
function FOVForHeightAndDistance(height: float, distance: float) {
return 2 * Mathf.Atan(height * 0.5 / distance) * Mathf.Rad2Deg;
}
// Start the dolly zoom effect.
function StartDZ() {
var distance = Vector3.Distance(transform.position, target.position);
initHeightAtDist = FrustumHeightAtDistance(distance);
dzEnabled = true;
}
// Turn dolly zoom off.
function StopDZ() {
dzEnabled = false;
}
function Start() {
StartDZ();
}
function 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 * 5);
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5