CinemachineBrain的属性简介
CinemachineBrain是Unity Cinemachine的核心组件,它和Camera组件挂载在一起,监控场景中所有的virtual camera。CinemachineBrain在inspector中暴露的属性如下:
Live Camera和Live Blend分别表示当前active的virtual camera以及blend的进度(如果有的话)。相关的代码可以在CinemachineBrainEditor
找到:
[CustomEditor(typeof(CinemachineBrain))]
[CanEditMultipleObjects]
class CinemachineBrainEditor : UnityEditor.Editor
{
CinemachineBrain Target => target as CinemachineBrain;
public override VisualElement CreateInspectorGUI()
{
var ux = new VisualElement();
ux.ContinuousUpdate(() =>
{
if (target == null || liveCamera == null)
return;
liveCamera.value = Target.ActiveVirtualCamera as CinemachineVirtualCameraBase;
liveBlend.value = Target.ActiveBlend != null ? Target.ActiveBlend.Description : string.Empty;
});
return ux;
}
}
可以看到引用的是CinemachineBrain的ActiveVirtualCamera和ActiveBlend字段。
public class CinemachineBrain : MonoBehaviour, ICameraOverrideStack, ICinemachineMixer
{
/// <summary>
/// Get the current active virtual camera.
/// </summary>
public ICinemachineCamera ActiveVirtualCamera
=> CinemachineCore.SoloCamera ?? m_BlendManager.ActiveVirtualCamera;
/// <summary>
/// Get the current blend in progress. Returns null if none.
/// It is also possible to set the current blend, but this is not a recommended usage
/// unless it is to set the active blend to null, which will force completion of the blend.
/// </summary>
public CinemachineBlend ActiveBlend
{
get => m_BlendManager.ActiveBlend;
set => m_BlendManager.ActiveBlend = value;
}
}
ActiveBlend.Description
描述了从当前virtual camera切换到下一个virtual camera的进度。
Show Debug Text字段,如果勾上,就会在Game窗口中显示当前live的virtual camera信息。
Show Camera Frustum字段,如果勾上,就会在Scene窗口显示当前live的virtual camera视锥体,不用选中任何GameObject。
Ignore Time Scale字段,勾上的话会使Cinemachine实时响应用户输入和阻尼,说白了就是不受time scale的影响。这里同样可以查看相关代码:
float GetEffectiveDeltaTime(bool fixedDelta)
{
if (CinemachineCore.UniformDeltaTimeOverride >= 0)
return CinemachineCore.UniformDeltaTimeOverride;
if (CinemachineCore.SoloCamera != null)
return Time.unscaledDeltaTime;
if (!Application.isPlaying)
return m_BlendManager.GetDeltaTimeOverride();
if (IgnoreTimeScale)
return fixedDelta ? Time.fixedDeltaTime : Time.unscaledDeltaTime;
return fixedDelta ? Time.fixedDeltaTime : Time.deltaTime;
}
fixedDelta
参数表示是否要使用Time.fixedDeltaTime
,如果为false,在勾选的情况下,就会选择Time.unscaledDeltaTime
,而不是Time.deltaTime
。
World Up Override字段,指定的transform的Y轴定义了Cinemachine在世界空间的up向量。如果为空,则默认为(0,1,0)。这点在代码中同样有体现:
/// <summary>Get the default world up for the virtual cameras.</summary>
public Vector3 DefaultWorldUp => (WorldUpOverride != null) ? WorldUpOverride.transform.up : Vector3.up;
Channel Mask字段主要用于分屏效果的实现。这种情况下,场景中会有多个camera,也就会有多个CinemachineBrain组件的存在。为了确定某一个virtual camera是属于哪个CinemachineBrain管理,就需要用到Channel Mask字段。相关的判断代码如下:
/// <summary>Returns true if camera is on a channel that is handles by this Brain.</summary>
/// <param name="vcam">The camera to check</param>
/// <returns></returns>
public bool IsValidChannel(CinemachineVirtualCameraBase vcam)
=> vcam != null && ((uint)vcam.OutputChannel & (uint)ChannelMask) != 0;
不过在大部分情况下,只需要一个屏幕,场景中也就只有一个CinemachineBrain,此时Channel Mask字段保持默认值即可,默认值为-1,这样转为uint就是0xffffffff了。
/// <summary>The CinemachineBrain will find the highest-priority CinemachineCamera that outputs
/// to any of the channels selected. CinemachineCameras that do not output to one of these
/// channels will be ignored. Use this in situations where multiple CinemachineBrains are
/// needed (for example, Split-screen).</summary>
[Tooltip("The CinemachineBrain will find the highest-priority CinemachineCamera that outputs to "
+ "any of the channels selected. CinemachineCameras that do not output to one of these "
+ "channels will be ignored. Use this in situations where multiple CinemachineBrains are "
+ "needed (for example, Split-screen).")]
public OutputChannels ChannelMask = (OutputChannels)(-1); // default is Everything
Update Method字段表示virtual camera更新position和rotation的时机,有以下几种。
Update Method | |
---|---|
Fixed Update | 和物理模块保持同步,在FixedUpdate时更新 |
Late Update | 在MonoBehaviour的LateUpdate时更新 |
Smart Update | 根据virtual camera当前的更新情况更新,推荐设置 |
Manual Update | virtual camera不会自动更新,需要手动调用brain.ManualUpdate() |
Smart Update具体是如何实现的呢?通过搜索UpdateMethods.SmartUpdate
,可以查到相关的代码集中在ManualUpdate
和DoFixedUpdate
这两个函数上。容易猜到,触发这两个函数的时机,一个是在LateUpdate,一个是在FixedUpdate期间:
void LateUpdate()
{
if (UpdateMethod != UpdateMethods.ManualUpdate)
ManualUpdate();
}
// Instead of FixedUpdate() we have this, to ensure that it happens
// after all physics updates have taken place
IEnumerator AfterPhysics()
{
while (true)
{
// FixedUpdate can be called multiple times per frame
yield return m_WaitForFixedUpdate;
DoFixedUpdate();
}
}
由Unity的脚本执行顺序[3]可知,DoFixedUpdate
会在Unity所有的FixedUpdate执行之后立刻执行。
决定在哪个Update阶段进行更新的逻辑,位于UpdateTracker.OnUpdate
这个函数:
public void OnUpdate(int currentFrame, UpdateClock currentClock, Matrix4x4 pos)
{
if (lastPos == pos)
return;
if (currentClock == UpdateClock.Late)
++numWindowLateUpdateMoves;
else if (lastFrameUpdated != currentFrame) // only count 1 per rendered frame
++numWindowFixedUpdateMoves;
lastPos = pos;
UpdateClock choice;
if (numWindowFixedUpdateMoves > 3 && numWindowLateUpdateMoves < numWindowFixedUpdateMoves / 3)
choice = UpdateClock.Fixed;
else
choice = UpdateClock.Late;
if (numWindows == 0)
PreferredUpdate = choice;
if (windowStart + kWindowSize <= currentFrame)
{
#if DEBUG_LOG_NAME
Debug.Log(name + ": Window " + numWindows + ": Late=" + numWindowLateUpdateMoves + ", Fixed=" + numWindowFixedUpdateMoves);
#endif
PreferredUpdate = choice;
++numWindows;
windowStart = currentFrame;
numWindowLateUpdateMoves = (PreferredUpdate == UpdateClock.Late) ? 1 : 0;
numWindowFixedUpdateMoves = (PreferredUpdate == UpdateClock.Fixed) ? 1 : 0;
}
}
其主要逻辑,就是采样前一段时间kWindowSize = 30
帧内,virtual camera的target position在LateUpdate和FixedUpdate时发生变化的次数,如果FixedUpdate次数是LateUpdate次数的三倍以上,那就选择在FixedUpdate更新virtual camera,否则选择LateUpdate。
Blend Update Method表示混合并更新主相机的时机,推荐使用Late Update。
Lens Mode Override表示是否允许virtual camera修改主相机的模式(透视,正交,物理)。如果不勾上,那么在virtual camera的设置里修改是不生效的:
Default Blend表示两个virtual camera混合的方式,Unity默认提供了若干种,当然也可以自定义。
btw,在旧版本中CinemachineBrain监听的事件也在CinemachineBrain组件中,而新版本(3.1.0)这部分已经剥离出来,单独作为Cinemachine Brain Event组件存在了。现在支持六种事件:
Reference
[1] Cinemachine Brain component
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » CinemachineBrain的属性简介
发表评论 取消回复