CinemachineBrain的属性简介

news2024/9/19 10:43:30

CinemachineBrain的属性简介

CinemachineBrain是Unity Cinemachine的核心组件,它和Camera组件挂载在一起,监控场景中所有的virtual camera。CinemachineBrain在inspector中暴露的属性如下:

CinemachineBrain的属性简介1

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的进度。

CinemachineBrain的属性简介2

Show Debug Text字段,如果勾上,就会在Game窗口中显示当前live的virtual camera信息。

CinemachineBrain的属性简介3

Show Camera Frustum字段,如果勾上,就会在Scene窗口显示当前live的virtual camera视锥体,不用选中任何GameObject。

CinemachineBrain的属性简介4

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 Updatevirtual camera不会自动更新,需要手动调用brain.ManualUpdate()

Smart Update具体是如何实现的呢?通过搜索UpdateMethods.SmartUpdate,可以查到相关的代码集中在ManualUpdateDoFixedUpdate这两个函数上。容易猜到,触发这两个函数的时机,一个是在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的设置里修改是不生效的:

CinemachineBrain的属性简介5

Default Blend表示两个virtual camera混合的方式,Unity默认提供了若干种,当然也可以自定义。

btw,在旧版本中CinemachineBrain监听的事件也在CinemachineBrain组件中,而新版本(3.1.0)这部分已经剥离出来,单独作为Cinemachine Brain Event组件存在了。现在支持六种事件:

CinemachineBrain的属性简介6

Reference

[1] Cinemachine Brain component

[2] Cinemachine(一)VirtualCamera和Brain的简单介绍

[3] Order of execution for event functions

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1916865.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

深度学习和NLP中的注意力和记忆

深度学习和NLP中的注意力和记忆 文章目录 一、说明二、注意力解决了什么问题&#xff1f;#三、关注的代价#四、机器翻译之外的关注#五、注意力&#xff08;模糊&#xff09;记忆&#xff1f;# 一、说明 深度学习的最新趋势是注意力机制。在一次采访中&#xff0c;现任 OpenAI 研…

矩阵分解及其在机器学习中的应用

阵分解是一种广泛应用于数据挖掘和机器学习领域的技术&#xff0c;它通过将一个高维数据集分解为多个低维的数据集&#xff0c;以降低数据的复杂性、提高计算效率&#xff0c;并发现数据中的隐含结构。本文将详细介绍矩阵分解的基本概念、主要方法及其在机器学习中的应用。 一、…

Spark项目通用开发框架

文章目录 1. 大数据项目结构2. 类说明2.1 公共接口类2.2 TaskNameEnum指定每个任务的名称2.3 TaskRunner中编写任务的业务逻辑 3. 任务执行脚本 每个公司内部都有一套自己的架子&#xff0c;一般新人来了就直接在已有的架子上开发业务。 以下仅仅作为记录下自己使用的架子&…

低代码平台赋能企业全面数字化转型

引言&#xff1a;在当今这个日新月异的数字化时代&#xff0c;企业正面临着前所未有的机遇与挑战。为了保持竞争力并实现可持续发展&#xff0c;企业亟需进行全面的数字化转型。而低代码平台作为数字化转型的重要工具&#xff0c;正以其独特的优势赋能企业&#xff0c;推动其向…

SQL Server 查询死锁以及解决死锁的基本知识(图文)

目录 1. 基本知识2. 查看和解锁被锁的表3. 查看和处理数据库堵塞 1. 基本知识 在 SQL Server 中&#xff0c;死锁是指两个或多个进程互相等待对方持有的资源&#xff0c;从而无法继续执行的现象 要解决死锁问题&#xff0c;首先需要识别并分析死锁的发生原因&#xff0c;然后…

C++基础语法:链表和数据结构

前言 "打牢基础,万事不愁" .C的基础语法的学习 引入 链表是最基础的数据集合,对标数组.数组是固定长度,随机访问,链表是非固定长度,不能随机访问.数组查找快,插入慢;链表是插入快,查找慢. 前面推导过"数据结构算法数据集合".想建立一个数据集合,就要设计数…

K8S中部署 Nacos 集群

1. 准备 GitK8Skubectlhelm 咱也没想到 K8S 部署系列能搞这么多次&#xff0c;我一个开发天天干运维的活&#xff0c;前端后端运维测试工程师实至名归。 2. 方案选择 https://github.com/nacos-group/nacos-k8s 我替你们看了一下&#xff0c;有好几种方式能部署&#xff…

极狐Gitlab安装部署

GitLab 是一个基于 Git 的开源 DevOps 平台&#xff0c;提供代码仓库管理、CI/CD&#xff08;持续集成和持续交付&#xff09;、项目管理、监控和安全等功能。它集成了多种工具&#xff0c;帮助开发团队在一个平台上进行代码开发、测试、部署和运维。以下是 GitLab 的主要功能和…

LLM - 绝对与相对位置编码 与 RoPE 旋转位置编码 源码

欢迎关注我的CSDN:https://spike.blog.csdn.net/ 本文地址:https://spike.blog.csdn.net/article/details/140281680 免责声明:本文来源于个人知识与公开资料,仅用于学术交流,欢迎讨论,不支持转载。 Transformer 是基于 MHSA (多头自注意力),然而,MHSA 对于位置是不敏感…

自定义类型:联合体

像结构体一样&#xff0c;联合体也是由一个或者多个成员组成&#xff0c;这些成员可以是不同的类型。 联合体类型的声明 编译器只为最⼤的成员分配⾜够的内存空间。联合体的特点是所有成员共⽤同⼀块内存空间。所以联合体也叫&#xff1a;共⽤体。 输出结果&#xff1a; 联合体…

AcWing 3381:手机键盘

【题目来源】https://www.acwing.com/problem/content/3384/【题目描述】 请你计算按照手机键盘&#xff08;9键输入法&#xff09;输入字母的方式&#xff0c;键入给定字符串&#xff08;由小写字母构成&#xff09;所花费的时间。 具体键入规则和花费时间如下描述&#xff1a…

科普文:Java对象在堆中的内存结构

概叙 今天来讲些抽象的东西 -- 对象头&#xff0c;因为我在学习的过程中发现很多地方都关联到了对象头的知识点&#xff0c;例如JDK中的 synchronized锁优化 和 JVM 中对象年龄升级等等。 对象内存构成# Java 中通过 new 关键字创建一个类的实例对象&#xff0c;对象存于内存的…

【人工智能】-- 反向传播

个人主页&#xff1a;欢迎来到 Papicatch的博客 课设专栏 &#xff1a;学生成绩管理系统 专业知识专栏&#xff1a; 专业知识 文章目录 &#x1f349;引言 &#x1f349;反向传播 &#x1f348;定义 &#x1f348;反向传播的作用 &#x1f34d;参数优化 &#x1f34d;学…

软件测试学习之-ADB命令

ADB命令 adb工具即Android Debug Bridge&#xff08;安卓调试桥&#xff09; tools。它就是一个命令行窗口&#xff0c;用于通过电脑端与模拟器或者真实设备交互。在某些特殊的情况下进入不了系统&#xff0c;adb就派上用场啦&#xff01; Android程序的开发通常需要使用到一…

解决Anaconda下载pytorch常见问题

1.问题一 安装完Anaconda后&#xff0c;输入conda命令&#xff0c;出现 conda不是内部或外部命令&#xff0c;也不是可运行的程序 或批处理文件。 分析原因&#xff1a;未配置环境到系统变量 解决方法&#xff1a;将Anaconda安装路径和Anaconda目录下的Scripts文件的路径配…

本地部署,GFPGAN: 实用的面部修复算法

目录 什么是 GFPGAN&#xff1f; 技术原理 主要功能 应用场景 本地安装 运行结果 结语 Tip&#xff1a; 在图像处理和计算机视觉领域&#xff0c;面部修复是一个重要且具有挑战性的研究方向。随着深度学习技术的不断进步&#xff0c;许多新的算法被提出&#xff0c;用于…

Linux笔记之使用系统调用sendfile高速拷贝文件

Linux笔记之使用系统调用sendfile高速拷贝文件 code review! 文章目录 Linux笔记之使用系统调用sendfile高速拷贝文件sendfile 性能优势sendfile 系统调用优点&#xff1a;缺点&#xff1a; cp 命令优点&#xff1a;缺点&#xff1a; 实际测试&#xff1a;拷贝5.8个G的文件&a…

《Windows API每日一练》9.1.5 自定义资源

自定义资源&#xff08;Custom Resources&#xff09;是在 Windows 程序中使用的一种资源类型&#xff0c;用于存储应用程序特定的数据、图像、音频、二进制文件等。通过自定义资源&#xff0c;开发者可以将应用程序所需的各种资源文件集中管理和存储&#xff0c;便于在程序中访…

开源可视化Flutter图表库:Graphic

Graphic&#xff1a;用Graphic绘制数据的无限可能- 精选真开源&#xff0c;释放新价值。 概览 Graphic&#xff0c;这个基于Flutter的图表库&#xff0c;以其源自《The Grammar of Graphics》的灵感&#xff0c;为数据可视化提供了一种全新的方法。它不仅仅是一个工具&#xf…

安全策略与用户认证综合实验

一、实验拓扑 二、实验需求 1,DMZ区内的服务器,办公区仅能在办公时间内(9:00-18:00)可以访问,生产区的设备全天可以访问. 2,生产区不允许访问互联网,办公区和游客区允许访问互联网 3,办公区设备10.0.2.10不允许访问DMz区的FTP服务器和HTTP服务器,仅能ping通10.0.3.10 4,办公区…