Camera 摄像机

优质
小牛编辑
137浏览
2023-12-01

Cameras are the devices that capture and display the world to the player. By customizing and manipulating cameras, you can make the presentation of your game truly unique. You can have an unlimited number of cameras in a scene. They can be set to render in any order, at any place on the screen, or only certain parts of the screen.

相机(Camera)是向玩家捕获和显示世界的设备。通过自定义和操纵摄像机,你可以使你的游戏表现得真正独特。您在场景中摄像机的数量不受限制。他们可以以任何顺序设定放置在屏幕上的任何地方,或在屏幕的某些部分。

Unity灵活的Camera对象
Unity's flexible Camera object
Unity灵活的Camera对象

Properties 属性

  • Clear Flags 清除标识 Determines which parts of the screen will be cleared. This is handy when using multiple Cameras to draw different game elements.
    确定了屏幕哪些部分将被清除。这是为了方便使用多个摄像机画不同的游戏元素。
  • Background 背景 Color applied to the remaining screen after all elements in view have been drawn and there is no skybox.
    应用于视图中的所有元素绘制后,和没有天空盒的情况下,剩余屏幕的颜色
  • Culling Mask 消隐遮罩 Include or omit layers of objects to be rendered by the Camera. Assign layers to your objects in the Inspector.
    包含层或忽略层将被相机(Camera)渲染。在检视窗口向对象分配层。
  • Projection 投影 Toggles the camera's capability to simulate perspective.
    切换相机的能力,以模拟透视。
  •     Perspective 透视 Camera will render objects with perspective intact.
    摄像机(Camera)将使物体和透视完好无损。
  •     Orthographic 正交 Camera will render objects uniformly, with no sense of perspective.
    相机会均匀地渲染物体,没有透视感。
  • Size 大小 (当正交被选择上)
    (when Orthographic is selected) The viewport size of the Camera when set to Orthographic.
    当摄像机设置为正交模式,摄影机视口的大小
  • Field of view 视野 Width of the Camera's view angle, measured in degrees along the local Y axis.
    相机的视野,沿着本地Y轴测量的度为单位。
  • Clipping Planes 裁剪面 Distances from the camera to start and stop rendering.
    相机到开始和结束渲染的距离
  •     Near 近 The closest point relative to the camera that drawing will occur.
    相对于相机,绘制将发生的最近点。
  •     Far 远 The furthest point relative to the camera that drawing will occur.
    相对于相机,绘制将发生的最远点。
  • Normalized View Port Rect
    规范化的视口矩形 Four values that indicate where on the screen this camera view will be drawn, in Screen Coordinates (values 0-1).
    标明这台相机视图将会在屏幕上绘制的屏幕坐标(值0 - 1) 的4个值。
  •     X The beginning horizontal position that the camera view will be drawn.
    将得出相机视图的开始水平位置。
  •     Y The beginning vertical position that the camera view will be drawn.
    将得出相机视图的开始垂直位置。
  •     W (Width) 宽度 Width of the camera output on the screen. 摄像机输出在屏幕上的宽度。
  •     H (Height) 高度 Height of the camera output on the screen. 摄像机输出在屏幕上的高度。
  • Depth 深度 The camera's position in the draw order. Cameras with a larger value will be drawn on top of cameras with a smaller value.
    相机在渲染顺序上的位置。具有较低深度的相机将在较高深度的相机之前渲染。
  • Rendering Path 渲染路径 Options for defining what rendering methods will be used by the camera.
    定义什么绘制方法将被用于相机的选项。
  •     Use Player Settings 使用玩家设置 This camera will use whichever Rendering Path is set in the Player Settings.
    在玩家设置(Player Settings.)相机使用哪个渲染路径。
  •     Vertex Lit 顶点光照 All objects rendered by this camera will be rendered as Vertex-Lit objects.
    所有被这个相机渲染的物体都将渲染成Vertex-Lit物体。
  •     Forward All objects will be rendered with one pass per material, as was standard in Unity 2.x.
    所有对象每材质渲染只渲染一次,和Unity 2.x中的标准一样。
  •     Deferred Lighting 延迟照明
        (Unity Pro only) All objects will be drawn once without lighting, then lighting of all objects will be rendered together at the end of the render queue.
    所有物体将在无光照的环境渲染一次,然后在渲染队列尾部将物体的光照一起渲染出来。
  • Target Texture 目标纹理
    (Unity Pro/Advanced only) Reference to a Render Texture that will contain the output of the Camera view. Making this reference will disable this Camera's capability to render to the screen.
    参见 渲染纹理 (Render Texture),渲染纹理 (Render Texture)包含相机视图输出。这会使相机渲染在屏幕上的能力被禁止。

Details 细节

Cameras are essential for displaying your game to the player. They can be customized, scripted, or parented to achieve just about any kind of effect imaginable. For a puzzle game, you might keep the Camera static for a full view of the puzzle. For a first-person shooter, you would parent the Camera to the player character, and place it at the character's eye level. For a racing game, you'd likely want to have the Camera follow your player's vehicle.

相机(Cameras)是显示你的游戏玩家必不可少的。他们可以进行定制,可以为之编写脚本,或者parented,达到任何所可以想象到的效果。对于一个拼图游戏,你可以让摄像机静止地看见整个拼图。对于一个第一人称射击游戏,你会相机作为玩家角色的父物体,把它放在人物的眼睛的高度。对于一个赛车游戏,你可能更希望把相机跟随您的玩家的车辆。

You can create multiple Cameras and assign each one to a different Depth. Cameras are drawn from low Depth to high Depth. In other words, a Camera with a Depth of 2 will be drawn on top of a Camera with a depth of 1. You can adjust the values of the Normalized View Port Rectangle property to resize and position the Camera's view onscreen. This can create multiple mini-views like missile cams, map views, rear-view mirrors, etc.

您可以创建多个摄像机(Cameras),每一个分配到不同的深度。相机从低深度到高深度进行绘制。换句话说,深度为2的摄像机将会绘制在深度为1的相机的上面。(深度为2的相机会在深度为1的相机绘制后绘制。)你可以调整规范化视口矩形( Normalized View Port Rectangle)的值来调整相机视口在屏幕上的大小和位置。这可以创造像导弹凸轮,地图视野​​,后视镜等多个迷你视野.(mini-views)

Render Path 渲染路径

Unity supports different Rendering Paths. You should choose which one you use depending on your game content and target platform / hardware. Different rendering paths have different features and performance characteristics that mostly affect Lights and Shadows.
The rendering Path used by your project is chosen in Player Settings. Additionally, you can override it for each Camera.

Unity支持不同的渲染路径。你应该根据你的游戏内容和目标的平台/硬件选择使用哪一个(渲染路径)。不同的渲染路径具有不同的特点和性能特点,主要影响灯光与阴影。您的项目所使用的渲染路径在玩家设置(Player Settings)里选择。此外,您可以每个摄像机中覆盖它。

For more info on rendering paths, check the rendering paths page.

渲染路径的详细信息,看"渲染路径"页面。

Clear Flags 清除标识

Each Camera stores color and depth information when it renders its view. The portions of the screen that are not drawn in are empty, and will display the skybox by default. When you are using multiple Cameras, each one stores its own color and depth information in buffers, accumulating more data as each Camera renders. As any particular Camera in your scene renders its view, you can set the Clear Flags to clear different collections of the buffer information. This is done by choosing one of the four options:

每个摄像机在渲染场景时会存储颜色和深度的信息。不绘制在屏幕的部分是空的,并且默认情况下会显示天空盒。当您使用多台摄像机,在各自缓冲区存储它自己的颜色和深度信息,更多的数据会被各摄像机渲染。由于在你的场景任何特定相机渲染它的视野,你可以设置清除标志,清除不同的缓冲区信息。这是通过选择四个选项之一来完成的:

Skybox 天空盒

This is the default setting. Any empty portions of the screen will display the current Camera's skybox. If the current Camera has no skybox set, it will default to the skybox chosen in the Render Settings (found in Edit->Render Settings). It will then fall back to the Background Color. Alternatively a Skybox component can be added to the camera. If you want to create a new Skybox, you can use this guide.

这是默认设置。屏幕上的任何空的部分将显示当前相机的天空盒。如果当前的相机没有设置天空盒,它会默认在渲染设置(Render Settings )选择天空盒(在 Edit->Render Settings可以找到)。它将会变回背景色。另外天空盒组件可以添加到相机上。如果你想创建一个新的天空盒,您可以使用本指南。(you can use this guide)

Solid Color 纯色

Any empty portions of the screen will display the current Camera's Background Color.

任何空部分,屏幕显示为当前相机的背景色。

Depth Only 仅深度

If you wanted to draw a player's gun without letting it get clipped inside the environment, you would set one Camera at Depth 0 to draw the environment, and another Camera at Depth 1 to draw the weapon alone. The weapon Camera's Clear Flags should be set to to depth only. This will keep the graphical display of the environment on the screen, but discard all information about where each object exists in 3-D space. When the gun is drawn, the opaque parts will completely cover anything drawn, regardless of how close the gun is to the wall.

如果你想绘制一个玩家的枪,又不让它内部环境被裁剪,你会设置深度为0的相机绘制环境,和另一个深度为1的相机单独绘制武器。武器相机的清除标志(Clear Flags )应设置 为depth only。


The gun is drawn last, after clearing the depth buffer of the cameras before it
枪是最后绘制的,清理相机的深度缓冲之后。

Don't Clear 不清除

This mode does not clear either the color or the depth buffer. The result is that each frame is drawn over the next, resulting in a smear-looking effect. This isn't typically used in games, and would likely be best used with a custom shader.

此模式不清除颜色或深度缓存。每一帧在下一帧结束后绘制,看上去像是涂抹(smear-looking)的效果。这在游戏中不常用,最好是在自定义着色器(custom shader)上使用。

 

Clip Planes 裁剪面

The Near and Far Clip Plane properties determine where the Camera's view begins and ends. The planes are laid out perpendicular to the Camera's direction and are measured from the its position. The Near plane is the closest location that will be rendered, and the Far plane is the furthest.

近裁剪面及远裁剪面属性确定相机视野的开始和结束。平面是布置在与相机的方向垂直的位置,并从它的位置测量。近裁剪面是最近的渲染位置,远平面是最远的渲染位置。

The clipping planes also determine how depth buffer precision is distributed over the scene. In general, to get better precision you should move the Near plane as far as possible.

裁剪平面,也决定了深度缓存精度如何分布在场景。在一般情况下,为了获得更好的精度,你应该把近裁剪面移动到尽可能远的地方。

Note that the near and far clip planes together with the planes defined by the field of view of the camera describe what is popularly known as the camera frustum. Unity ensures that when rendering your objects those which are completely outside of this frustum are not displayed. This is called Frustum Culling. Frustum Culling happens irrespective of whether you use Occlusion Culling in your game.

请注意,近裁剪面及远裁剪面和以相机视野所定义的平面一起普遍称为相机的平截头体。Unity确保渲染对象时,那些完全超出这个平截头体的物体不显示。这就是所谓的平截头体消隐(Frustum Culling)。不论你的游戏是否使用遮挡剔除(Occlusion Culling),都会进行平截头体剔除。

For performance reasons, you might want to cull small objects earlier. For example, small rocks and debris could be made invisible at much smaller distance than large buildings. To do that, put small objects into a separate layer and setup per-layer cull distances using Camera.layerCullDistances script function.

出于性能方面的原因,你可能最先剔除小物件。例如,在远小于大型建筑物的距离,小石块和碎片可以隐藏掉。

Culling Mask 消隐遮罩

The Culling Mask is used for selectively rendering groups of objects using Layers. More information on using layers can be found here.

消隐遮罩是利用层选择性地渲染组对象。运用层的更多信息可以在这里找到。

Commonly, it is good practice to put your User Interface on a different layer, then render it by itself with a separate Camera set to render the UI layer by itself.

一般情况下,你的用户界面放在不同的层,然后渲染本身与设置一个单独的相机渲染UI层。这是很好的做法。

In order for the UI to display on top of the other Camera views, you'll also need to set the Clear Flags to Depth only and make sure that the UI Camera's Depth is higher than the other Cameras.

为了UI(用户界面)渲染在其他相机的景物渲染之前,你需要设定清除标识( Clear Flags)为只深度(Depth only)确保UI相机的深度高于其他照相机。

Normalized Viewport Rectangle 规范化视口矩形

Normalized Viewport Rectangles are specifically for defining a certain portion of the screen that the current camera view will be drawn upon. You can put a map view in the lower-right hand corner of the screen, or a missile-tip view in the upper-left corner. With a bit of design work, you can use Viewport Rectangle to create some unique behaviors.

规范化视口矩形是专门为定义一个当前照相机将会绘制到的屏幕一个特定部分 。你可以在屏幕右下角的放置地图视图,或在左上角放置导弹提示视图。你可以使用视口矩形创造一些独特的行为。

It's easy to create a two-player split screen effect using Normalized Viewport Rectangle. After you have created your two cameras, change both camera H value to be 0.5 then set player one's Y value to 0.5, and player two's Y value to 0. This will make player one's camera display from halfway up the screen to the top, and player two's camera will start at the bottom and stop halfway up the screen.

使用规范化视口矩形(Normalized Viewport Rectangle)可以很容易地创建双玩家(two-player)的分割画面效果。创建了两个摄像头后,改变两个相机的H值为0.5然后设置玩家1的Y值至0.5,玩家2的Y值设置为0。那么玩家1的摄像机就会从屏幕的中间到顶部显示,玩家2的摄像机就会从屏幕底部到屏幕中间显示(把屏幕按上下分成2半,上半部分是玩家1的,下半部分是玩家2的)


Two-player display created with Normalized Viewport Rectangle
用规范化视口矩形制造 双玩家 显示

Orthographic 正交

Marking a Camera as Orthographic removes all perspective from the Camera's view. This is mostly useful for making isometric or 2D games.

标识正交相机模式,从相机的视图中删除所有的透视效果,最主要用在等距或2D游戏中。

Note that fog is rendered uniformly in orthographic camera mode and may therefore not appear as expected. Read more about why in the component reference on Render Settings.

注意雾是在正交相机模式下渲染,因此可能不会如预期般出现。想知道为什么请看组件手册里的渲染设置(component reference on Render Settings.)。


Perspective camera. 透视相机


Orthographic camera. Objects do not get smaller with distance here!
正交相机。在这里物体不随距离而变小

Render Texture 渲染纹理

This feature is only available for Unity Advanced licenses . It will place the camera's view onto a Texture that can then be applied to another object. This makes it easy to create sports arena video monitors, surveillance cameras, reflections etc.

此功能仅Unity高级版提供。它将会把一个相机视图放置在一个将被用在其他物体的纹理(Texture )上。这使得很容易地创建体坛视频监控,监控摄像机,反射等。


A Render Texture used to create a live arena-cam
用渲染纹理做的竞技场摄像机所拍现场

Hints 提示

  • Cameras can be instantiated, parented, and scripted just like any other GameObject.
    相机可以被定制,可以作为父节点,和为之编写脚本,就像GameObject一样。
  • To increase the sense of speed in a racing game, use a high Field of View.
    要增加一个赛车游戏的速度感,使用了高视野(Field of View)。
  • Cameras can be used in physics simulation if you add a Rigidbody Component.
    如果你添加刚体(Rigidbody )组件,相机可用于物理模拟。
  • There is no limit to the number of Cameras you can have in your scenes.
    在你的场景中,相机的数目不受限制。
  • Orthographic cameras are great for making 3D user interfaces
    正交相机是能很好的制作3D用户界面
  • If you are experiencing depth artifacts (surfaces close to each other flickering), try setting Near Plane to as large as possible.
    如果你遇到深入的构件(表面互相紧密会产生闪烁现象),近裁剪面尽可能设置大点。
  • Cameras cannot render to the Game Screen and a Render Texture at the same time, only one or the other.
    相机无法同时渲染游戏画面和渲染纹理,只能是其中一个。
  • Pro license holders have the option of rendering a Camera's view to a texture, called Render-to-Texture, for even more unique effects.
    Unity专业版有渲染摄像机视图到纹理的选项,叫做渲染到纹理(Render-to-Texture),可以做出更多更好的独特效果。
  • Unity comes with pre-installed Camera scripts, found in Components->Camera Control. Experiment with them to get a taste of what's possible.
    Unity伴随着预先安装的摄像机脚本,在Components->Camera Control能找到。试用一下它们,看看能做什么。