默认情况下,视锥体围绕摄像机的中心线对称安放,但这并不是必须的。视锥体可设置为“倾斜的”,即一侧与中心线的角度小于对侧与中心线的角度。
这种做法使得图像一侧的透视看起来更加紧凑,给人的印象是观察者非常靠近在该边缘处可见的对象。此功能的一个用法示例是赛车游戏;如果视锥体在其底部边缘变平,则从观察者的角度看起来会更贴近道路,凸显了速度感。
在内置渲染管线中,使用斜视锥体的摄像机只能使用前向渲染路径。如果摄像机设置为使用延迟着色 (Deferred Shading) 渲染路径并使视锥体倾斜,Unity 会强制该摄像机使用前向渲染路径。
虽然 Camera 组件没有专门用于设置视锥体倾斜度的功能,但可以通过启用摄像机的 Physical Camera 属性并应用 Lens Shift 设置,或通过添加脚本来更改摄像机的投影矩阵,从而实现这样的功能。
启用摄像机的 Physical Camera 属性可显示 Lens Shift 选项。可以使用这些选项来沿着 X 和 Y 轴偏移摄像机的焦点中心,从而最大限度减少渲染图像的失真。
移动镜头可以减小与移位方向相反侧的视锥体角度。例如,向上移动镜头时,视锥体底部与摄像机中心线之间的角度会变小。
有关 Physical Camera 选项的更多信息,请参阅关于物理摄像机的文档。
有关设置各个 Physical Camera 属性的更多信息,请参阅 Camera 组件参考。
以下脚本示例显示了如何通过更改摄像机的投影矩阵来快速实现斜视锥体。请注意,仅当游戏运行播放模式时才能看到脚本的效果。
using UnityEngine;
using System.Collections;
public class ExampleScript : MonoBehaviour {
void SetObliqueness(float horizObl, float vertObl) {
Matrix4x4 mat = Camera.main.projectionMatrix;
mat[0, 2] = horizObl;
mat[1, 2] = vertObl;
Camera.main.projectionMatrix = mat;
}
}
C# 脚本示例
没有必要了解投影矩阵如何使用它。horizObl 和 vertObl 值分别设置水平和垂直倾斜量。值为零表示无倾斜。正值使视锥体向右或向上移动,从而使左边或底边变平。负值使视锥体向左或向下移动,从而使视锥体的右边或顶边变平。如果将此脚本添加到摄像机并在游戏运行时将游戏切换到 Scene 视图,则可以直接看到效果;在检视面板中改变 horizObl 和 vertObl 的值时,摄像机视锥体的线框会发生变化。任一变量中的值为 1 或 –1 表示视锥体的一侧与中心线完全齐平。此范围之外的值也是允许使用的,但通常没有必要。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.