Cinemachine (1) A brief introduction to VirtualCamera and Brain

Introduction

In the game, the effect of the camera is very important, it will directly affect the picture presented in the eyes of the player, a good effect can directly improve the player's gaming experience, giving an immersive feeling. For example, in an FPS game, we often need the camera to follow our character and do some first-person third-person switching. When the character enters the room, we need to adjust the camera position to prevent it from being blocked by the wall. When using a magnifier, we need the camera to observe the distance. The picture and so on. I thought that to achieve these effects we need to write a lot of control codes to control our Camera, but with Cinemachine , everything will become simpler.

Cinemachine is a set of Camera processing components launched by Unity in the 2017 version. With Cinemachine we can achieve many camera functions without typing code. For example, the requirements mentioned above, Cinemachine can help us quickly achieve, in addition, we can also use Cinemachine plus Timeline to do some cutscenes and other effects.

Official document: https://docs.unity3d.com/Packages/[email protected]/manual/index.html

We can install Cinemachine through Package Manager (as shown in the left picture below). After installation, there will be an additional Cinemachine option in our Unity toolbar for us to use (as shown in the right picture below).

                           

Note: The version used in this article is Cinemachine2.6, which requires Unity version 2018.4.17f1 or newer.

 

Simple to use

After installing Cinemachine, let's take a look at what happens after clicking Create Virtual Camera under the Cinemachine menu.  We can find that there is an additional GameObject named CM vcam1 in our scene, which is mounted with the  CinemachineVirtualCamera  component (as shown in the left picture below, which will be called later) VirtualCamera). At the same time, our Main Camera also has a CinemachineBrain component (as shown in the right picture below). These two components are the core of our Cinemachine.

                                            

Then we create a new Cube in the scene and drag this Cube to the settings of Follow and Look At of VirtualCamera . At this point, we will find that our Main Camera is looking at our Cube, and when we move the Cube, the Camera will move with it.

Without writing a line of code, the simplest function of Camera following objects is realized!

Blend

Next, we create a second VirtualCamera (CM vcam2), and the screen will change to the screen seen by the new VirtualCamera. We adjust the coordinates and rotation of the VirtualCamera and look at the Cube from another angle, as shown in the figure below.

Then we hide the VirtualCamera, redisplay the CM vcam1 screen, and then run Unity. After running, we then set CM vcam2 to active state. At this time, we can find that our screen will switch from CM vcam1 to CM vcam2, as shown in the figure below:

This process is called Blend , and the effect of lens switching (mixing) is achieved without code.

 

CinemachineVirtualCamera

CinemachineVirtualCamera is one of the core components of Cinemachine. We used its Follow and Look At to simply realize the camera's function of following and looking at objects. Next, let's take a detailed look at the role of its various settings.

Base

Let's take a look at the meaning of these basic settings as shown below

Status

At any time, our VirtualCamera is in one of the following three states:

Live The VirtualCamera that currently controls the Camera (with CinemachineBrain component) is in the Live state. When one VirtualCamera is mixed to another, both VirtualCameras are in the Live state during the process. When the mixing ends or under other circumstances, there will only be one VirtualCamera in the Live state at the same time.
Standby

VirtualCamera that does not control the Camera is in Standby state. The VirtualCamera in this state will still follow and look at the set target, and it will be updated every frame. The VirtualCamera in this state belongs to the active state (active and enable are equal to true), and its priority value is less than or equal to the VirtualCamera in the Live state.

Note: 2.6.3 Added StandbyUpdate setting, you can set the update frequency (see below for details)

Disabled When VirtualCamera is in the inactive state (active or enable is equal to false), it is in the Disable state. In this state, there is no performance consumption, that is, the Camera will not be controlled, the target will not be followed, and it will not be updated every frame. However, if the VirtualCamera participates in mixing or is called by Timeline, it can still control the Camera

Solo button: By selecting this button, the current VirtualCamera can be temporarily set to the Live state to help us view the corresponding lens effect.

Game Window Guides

After it is turned on, some auxiliary lines will be displayed in the Game view (as shown below). These auxiliary lines will appear when VirtualCamera looks at ( Look At ) a specified GameObject and Aim is set to Composer or Group Composer or follow ( Follow ) a specified target and Body is set to Framing Transpose r.

Note: This setting applies to all VirtualCamera. For example, if we turn on or turn off this function in one of the VirtualCamera, then this setting of all VirtualCamera will be turned on or off.

Save During Play

After it is turned on, we can directly save our modifications to the VirtualCamera settings in the running state, without the need for us to remember these modified properties by copying and pasting.

When we stop running, we will scan for our modified properties to save, and we can use ctrl+z to roll back these modifications.

Note: Like Game Window Guides, it is a setting that applies to all VirtualCamera.

Cinemachine provides us with a tag  [SaveDuringPlay] that can support the above features. We can add this tag to our custom component to save the modification at runtime. In addition, if some properties in our component do not want to save the modified value, you can add [NoSaveDuringPlay] in front of the property to filter. E.g:

[SaveDuringPlay]
public class MyComponent : MonoBehaviour
{
    public int a;
    [NoSaveDuringPlay] public string b;
}

Priority

Weight, the larger the value, the higher the priority. For example: a VirtualCamera in a non-Live state, when its Priority value is greater than or equal to the VirtualCamera in the current Live state and is in the active state, then CinemachineBrain will select it as the new VirtualCamera in the Live state.

Note: When using Timeline to process VirtualCamera, this property has no effect

Follow

Follow the target, that is, VirtualCamera will follow the set target and move. The Position of the Camera will be updated according to the target set by the VirtualCamera in the Live state combined with the settings in the Body . If you don't set the target, the Camera's Position will be synchronized with VirtualCamera. For example, we can use Timeline to add animation to VirtualCamera, and then Camera will produce the same effect.

Look At

Look at the target, that is, the target the Camera looks at (Transform.LookAt). The Rotation of the Camera will be updated according to the target set by the VirtualCamera in the Live state combined with the settings in Aim . If you don't set the target, the Rotation of the Camera will be synchronized with the VirtualCamera.

Standby Update

Set the update frequency of VirtualCamera in Standby state (new in 2.6.3), there are three options as follows

Always Update every frame
Never Do not update
Round Robin Update occasionally, the specific update frequency depends on the number of VirtualCamera in Standby status

Lens

The following three settings correspond to the same settings in Camera, and the values ​​in Camera will be synchronized with this. For example, for a Camera with CinemachineBrain, when the FOV value of the corresponding VirtualCamera in the Live state is changed to 77, then the FOV value in the Camera will also change to 77 accordingly.

Field Of View FOV, control the window size of Camera
Near Clip Plane The closest the camera can see
Far Clip Plane The farthest the camera can see

Presets : There is a small drop-down box on the right side of Field Of View. Click Edit Presets to create or modify the Asset file used as a preset. The contents of the Lens Presets Asset file are as follows:

Presets can help us quickly set FOV and other attributes.

Dutch : Used to modify the z-axis value of Rotating in Camera , the value range is -180~180.

Transitions

2.6.3 New

BlendHint

As you can see from the first example, when a VirtualCamera is mixed with another VirtualCamera, the Camera will have a course of action, and it will be accompanied by the rotation of the Camera. The setting can choose the route mode when mixing to this VirtualCamera or mixing from this VirtualCamera to others.

None Coordinates and orientations are mixed in a standard linear
SphericalPosition If two VirtualCamera have the same LookAt target, then the path of the coordinates will be like on the plane of a circle.
CylindricalPosition If two VirtualCameras have a common LookAt target, then the path of the coordinates will be like on a cylindrical surface.
ScreenSpaceAimWhenTargetsDiffer Standard linear position blend, radial blend between LookAt targets

Inherit Position : When enabled, when the VirtualCamera state changes to Live, it will inherit the Position information of the VirtualCamera in the previous Live state.

On Camera Live : callback

 

Body

The setting of Body is mainly used to specify the movement rules of VirtualCamera, mainly in the following seven categories:

Do nothing Do not move VirtualCamera
3rd Person Follow Applicable to third-person or first-person follow-up effects
Framing Transposer Move with a fixed screen space relationship according to the goal set by Follow
Hard Lock To Target Move in a fixed relationship according to the goal set by Follow
Orbital Transposer According to the target set by Follow, move in a variable relationship, for example, you can choose to receive player input
Tracked Dolly Move along a preset path
Transpose Move in a fixed relationship according to the goal set by Follow

Since there are so many expanded parameters for different settings, I won't introduce too much here, but briefly introduce a few common settings. For other settings, please refer to the documentation yourself. If you use them later, you will also introduce them.

Binding Mode

This option mainly affects the calculation method of Offset and Damping of VirtualCamera. There are the following coordinate spaces for selection:

Lock To Target VirtualCamera calculates the offset relative to the model space of the Follow target. When the target rotates, the Camera will also rotate, keeping the offset unchanged.
Lock To Target With World Up The difference with Lock To Target is that in this mode, the x and z rotations of the model will be ignored. Only when the y axis rotates, the Camera will follow the rotation.
Lock To Target No Roll 与Lock To Target不同的是,该模式下会忽略模型的z的转动,当x或y轴转动时,Camera会跟着旋转
Lock To Target On Assign 测试时感觉和Lock To Target没什么区别,待定。。。
World Space VirtualCamera在世界坐标中相对于Follow目标原点的计算偏移,当目标旋转时,VirtualCamera的位置不会发生变化
Simple Follow With World Up VirtualCamera在世界坐标中相对于Follow目标在世界坐标中移动的方向的计算偏移和阻尼(不受在Y轴移动影响),例如一开始Camera在目标的世界坐标z轴负方向位置,当目标向世界坐标x轴正方向移动时,Camera会慢慢移动到目标的世界坐标x轴负方向位置。类似于小弟在屁股后面跟着的效果

注:单独测试跟随效果的时候最好不要设置Look At目标,以防干扰。

Damping

阻尼,数值越小,Camera响应的越快。当Damping设置为0时,Camera会和目标同步运动,若Damping大于0,Camera的运动会慢于目标。

X Damping 维持offset的x轴值时的阻尼
Y Damping 维持offset的y轴值时的阻尼
Z Damping 维持offset的z轴值时的阻尼
Pitch Damping 当目标沿自身x轴转动时,Camera跟随目标时的阻尼
Yaw Damping 当目标沿自身y轴转动时,Camera跟随目标时的阻尼
Roll Damping 当目标沿自身z轴转动时,Camera跟随目标时的阻尼

 

Aim

Aim的设置主要用于指定VirtualCamera的旋转规则,主要有如下六大类:

Do nothing 不对Virtual Camera做任何旋转
Composer 保持Camera始终看向目标
Group Composer 可以使Camera看向多个目标,如果看向的目标是Cinemachine Target Group,会自动调整Camera的FOV和距离,来确保组里的所有对象都能被看见
Hard Look At 保持Look At的目标始终在屏幕中间,该选项没有额外的设置
POV 通过用户输入来旋转Virtual Camera
Same As Follow Target Virtual Camera的Rotation值保持和Follow目标的Rotation值相同(因此使用此模式必须设置Follow目标)。如果我们Body选择的是Hard Lock to Target,那么就可以通过Follow的目标来控制Camera的路径和旋转

 

Noise

噪点,使用该属性可以利用Virtual Camera模拟Camera抖动的效果。Cinemachine中有一个名为 Basic Multi Channel Perlin 的组件,可以利用Perlin noise来移动Virtual Camera(Perlin noise是一种可以通过自然行为计算随机运动的技术)。

Noise Profile

Basic Multi Channel Perlin组件需要设置一个配置文件,每个配置文件都属于一个Asset,用来定义随着时间变化的噪点行为。

Cinemachine自带了几个配置文件,我们可以编辑它们或者创建自己的配置文件,内容如下:

Pivot Offset

当相机因为抖动旋转时,偏移相机中心点的坐标。(2.6.3新增)

Amplitude Gain

振幅,该值会与Profile中设置的Amplitude值相乘,例如设置为1,等于使用Profile中设置的Amplitude。

Amplitude值越大,则抖动的幅度会越大。

Frequency Gain

频率,该值会与Profile中设置的Frequency值相乘,例如设置为1,等于使用Profile中设置的Frequency。

Frequency值越大,则抖动的速度会越快。

 

Extensions

该部分内容较多,留到后续讲解。

 

CinemachineBrain

CinemachineBrain是Cinemachine中另一个核心组件,可以称之为大脑。它挂载在Camera上,监控着场景中所有active状态(Live和Standby)的VirtualCamera。若一个inactive状态的VirtualCamera,其Priority值大于等于当前Live状态的VirtualCamera,当我们将其设置为active状态,那么CinemachineBrain就会选择它作为新的Live状态的VirtualCamera。我们可以使用这种方法来实现镜头的切换。

注:我们也可以使用Timeline来控制VirtualCamera,这种情况Timeline的处理逻辑会覆盖CinemachineBrain的。

CinemachineBrain的设置如下:

Show Debug Text

开启后会在Game视图中显示当前Live状态下的VirtualCamera的文本信息,如图:

Show Camera Frustum

开启后在Scene视图中会始终显示Camera的视锥体。

Ignore Time Scale

使VirtualCamera响应用户的输入和阻尼时忽视TimeScale的设置。

World Up Override

用于指定VirtualCamera在世界空间中向上的向量,若为空,则为世界空间中Y轴的方向,即 (0, 1, 0)。否则使用设置的目标的Y轴的方向。使用好该设置对于避免万向节锁(gimbal-lock)非常重要。

Update Method

VirtualCamera更新位置和旋转的方式

Fixed Update VirtualCamera的更新与物理模块同步
Late Update 在MonoBehaviour的LateUpdate中更新
Smart Update 每个VirtualCamera根据其目标的更新方式来更新,推荐使用
Manual Update VirtualCamera不会自动更新,必须我们手动的调用ManualUpdate()来更新(应该在Camera跟随或看向的目标移动后调用)

注:Smart Update具体是通过UpdateTracker实现的。在一定时间内(UpdateStatus.kWindowSize = 30,即30帧内),通过调用UpdateTracker.OnUpdate(UpdateTracker.UpdateClock),可以计算出VirtualCamera的目标在fixedUpdate移动次数多还是在lateupdate移动的次数多,用来判断下一段时间用fixedUpdate还是lateUpdate。具体可以看UpdateTracker.OnUpdate方法。

Blend Update Method

主摄像机混合和更新的时机

Fixed Update 仅在Update Method使用的是Fixed Update并且在混合时发现剧烈震动时使用
Late Update 在MonoBehaviour的LateUpdate中出来,推荐使用

Default Blend

用于设置两个VirtualCamera混合的方式,除Cut外,其它方式可在后面设置一个混合的持续时间。

Cut 瞬切,立马显示下个VirtualCamera的画面
Ease In Out S型曲线,混合开始和结束时比较平滑(慢->匀速->慢)
Ease In 混合结束时比较平滑(匀速->慢)
Ease Out 混合开始时比较平滑,然后匀速到结束(慢->匀速)
Hard In 混合开始时很很慢(从超级慢开始加速)
Hard Out 混合结束时很很慢(从较快速度减速到超级慢)
Linear 匀速移动
Custom 自定义混合曲线

Custom Blends

在前面的设置中,设置的是所有的VirtualCamera的混合方式,但是假如当我们的场景中有多个VirtualCamera,并且不同的VirtualCamera之间的混合方式不尽相同的时候,就需要通过该属性来设置不同VirtualCamera之间的混合方式了。会生成一个Asset用来存储数据,具体设置如下。

需要注意的是,在From和To中设置的是VirtualCamera的名称,也就是一个字符串,而非是VirtualCamera的引用。我们可以通过设置内置的**ANY CAMERA**来代表任何一个VirtualCamera。

当要执行Blend操作时,首先会从Custom Blends中查找匹配项,若没有,则使用Default Blend的设置。若有多条匹配项,则优先选择最符合要求的。(例如vc1混合到vc2,一项是From:vc1,To:vc2,一项是From:vc1,To:ANY CAMERA,那么前面那项是更符合要求的,会被使用)若有多条最符合要求的,则选择最先找到的那一条。

Camera Cut Event

当一个VirtualCamera变为Live状态,并且其混合方式为Cut的情况下触发

brain.m_CameraCutEvent.AddListener(CameraCutEvent);

void CameraCutEvent(CinemachineBrain brain)
{
    Debug.Log(brain);
}

Camera Activated Event

当一个VirtualCamera变为Live状态时触发,若带有混合,则触发在混合开始的第一帧。

brain.m_CameraActivatedEvent.AddListener(CameraActivatedEvent);

//第一个参数为新的Live状态的VirtualCamera,第二个参数为上一个Live状态的VirtualCamera
void CameraActivatedEvent(ICinemachineCamera liveCamera, ICinemachineCamera lastCamera)
{
    Debug.Log(liveCamera);
    Debug.Log(lastCamera);
}

 

Cinemachine鼓励我们多创建VirtualCamera,因为VirtualCamera只会消耗很小的性能。我们可以将除了正在使用的VirtualCamera都关闭,来达到最佳的性能。

也建议一个镜头使用一个VirtualCamera,例如两个角色对话的过场动画,我们可以使用三个VirtualCamera,一个看向两个角色中间,另外两个分别给角色特写,然后利用Timeline来同步声音和VirtualCamera。例如一开始开启着看向屏幕中间的VirtualCamera,当其中一个角色需要单独特写的时候,再开启对应的VirtualCamera,当不需要特写的时候关闭,镜头就又会回到看向屏幕中间的VirtualCamera。

暂时先只介绍了具体参数含义,后续将介绍更多的使用情景

Guess you like

Origin blog.csdn.net/wangjiangrong/article/details/108726387