Unity DOTS中的baking(三)过滤baking的输出

news2024/9/23 19:24:00

Unity DOTS中的baking(三)过滤baking的输出

默认情况下,在conversation world(baker和baking system运行的环境)下产生的所有entities和components,都会作为baking环节的输出。在baking结束时,Unity必须将自上次baking以来发生变化的任何数据,都要复制到main world。不过,有些数据其实只在编辑时有用,我们希望可以在baking输出的时候将其舍弃掉。为此,Unity也提供了一些过滤的手段进行支持。

首先是属性BakingType,它用来标记一个component,使其只会存在于conversation world,不会输出到main world中。我们来看一个例子:

public class MyAuthoring : MonoBehaviour
{
    public int bakeIntData = 0;
    public ImageGeneratorInfo info;

    class MyBaker : Baker<MyAuthoring>
    {
        public override void Bake(MyAuthoring authoring)
        {
            Debug.Log("==========================Bake Invoked!========================== " + authoring.name);

            DependsOn(authoring.info);

            if(authoring.info == null) return;

            //var transform = authoring.transform;
            var transform = GetComponent<Transform>();

            var entity = GetEntity(TransformUsageFlags.None);
            AddComponent(entity, new MyComponent {
                value = authoring.bakeIntData,
                spacing = authoring.info.Spacing,
                position = transform.position
            });
            AddComponent(entity, new MyComponentBakingType {
                value = authoring.bakeIntData
            });
        }
    }
}

public struct MyComponent : IComponentData
{
    public int value;
    public float spacing;
    public float3 position;
}

[BakingType]
public struct MyComponentBakingType : IComponentData
{
    public int value;
}

这个例子中,我们在Baker里为entity添加了两个component,其中MyComponentBakingType是一个标记了BakingType属性的component。那么,在conversation world下,可以看到这两个component都存在于entity上:

Unity DOTS中的baking(三)1

而在editor world下,就只剩下MyComponent这一个component了:

Unity DOTS中的baking(三)2

接下来我们来研究研究Unity在其背后做了哪些事情。首先对于BakingType属性,Unity在TypeManager中定义了一个与之匹配的常量:

/// <summary>
/// Bitflag set for component types decorated with the <seealso cref="BakingTypeAttribute"/> attribute.
/// </summary>
public const int BakingOnlyTypeFlag = 1 << 20;

然后在add component时,会对component所定义的属性进行判断:

bool isBakingOnlyType = Attribute.IsDefined(type, typeof(BakingTypeAttribute));
if (isBakingOnlyType)
    typeIndex |= BakingOnlyTypeFlag;

TypeManager还对外暴露了IsBakingOnlyType这一get属性:

public bool IsBakingOnlyType 
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)] 
    get 
    { 
        return  (Value & TypeManager.BakingOnlyTypeFlag) != 0; 
    } 
}

这一get属性会在GatherComponentChangesBuildPackedGatherComponentChangesJob两个job中使用,它们用来收集发生变化的components,但包含BakingType属性的component会被过滤掉。这两个job由EntityManagerDiffer类触发:

/// <summary>
/// Generates a detailed change set for the world.
/// All entities to be considered for diffing must have the <see cref="EntityGuid"/> component with a unique value.
/// </summary>
/// <remarks>
/// The resulting <see cref="EntityChanges"/> must be disposed when no longer needed.
/// </remarks>
/// <param name="options">A set of options which can be toggled.</param>
/// <param name="allocator">The allocator to use for the results object.</param>
/// <returns>A set of changes for the world since the last fast-forward.</returns>
public EntityChanges GetChanges(EntityManagerDifferOptions options, AllocatorManager.AllocatorHandle allocator)
{
    var changes = EntityDiffer.GetChanges(
        ref m_CachedComponentChanges,
        srcEntityManager: m_SourceEntityManager,
        dstEntityManager: m_ShadowEntityManager,
        options,
        m_EntityQueryDesc,
        m_BlobAssetCache,
        allocator);

    return changes;
}

从代码中可以猜测出,这里的变化指的是conversation world和shadow world之间的diff,conversation world就是baker和baking system运行的地方,而shadow world则是上一次baking环节输出的拷贝,Unity使用这个shadow world,与当前baking的输出进行对比,只把不同的components和entities拷贝到main world,然后再更新shadow world为当前的conversation world。那么,既然代码中调用的两个job会把包含BakingType属性的component过滤掉,很明显最后输出到main world的components就不包含它们了。

除了BakingType属性之外,Unity还提供了TemporaryBakingType属性标记一个component。这两者有什么区别呢?Unity官方文档中给出了一段说明:

You can also exclude components with the following attributes:

  • [BakingType]: Filters any components marked with this attribute from the baking output.
  • [TemporaryBakingType]: Destroys any components marked with this attribute from the baking output. This means that components marked with this attribute don’t remain from one baking pass to the next, and only exist during the time that a particular baker ran.

可以得知,TemporaryBakingType属于那种阅后即焚的操作,拥有该属性的component,会在触发相应的baking时add到entity上,然后在baking结束时component就会被销毁,这意味着该component也不会一直存在于conversation world。之后,如果不再触发相应的baking,那么该component在conversation world里也不复存在。

这么说有点枯燥,我们还是用代码进行实验,在前面例子的基础上,新增定义一个component,然后在authoring Bake函数中add一下:

[TemporaryBakingType]
public struct MyComponentTempBakingType : IComponentData
{
    public int value;
}

public override void Bake(MyAuthoring authoring)
{
    AddComponent(entity, new MyComponentTempBakingType
    {
        value = authoring.bakeIntData
    });
}

由于它在conversation world中也是转瞬即逝,我们没法直接在编辑器观察到它。这里需要借助一下BakingSystem,BakingSystem是一类只存在baking过程中的system,它负责把各种baker产生的输出做进一步处理,而我们这里就是要观察baker中TemporaryBakingType属性的component。

[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
[BurstCompile]
public partial struct MyAuthoringBakingSystem : ISystem
{
    EntityQuery m_query;

    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
        m_query = SystemAPI.QueryBuilder().WithAll<MyComponentTempBakingType>().Build();
        state.RequireForUpdate(m_query);
    }

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        Debug.Log("=================update my authoring baking system===================");
    }
}

OnCreate会在system创建的时候执行一次,代码中就是获取当前包含MyComponentTempBakingTypecomponent的entity,如果存在这样的entity,则system才会执行OnUpdate。那么,根据我们之前的假设,这里的OnUpdate只会执行一次。实际上也是如此:

Unity DOTS中的baking(三)3

类似地,暗地里Unity在TypeManager中定义了一个flag常量:

/// <summary>
/// Bitflag set for component types decorated with the <seealso cref="TemporaryBakingTypeAttribute"/> attribute.
/// </summary>
public const int TemporaryBakingTypeFlag = 1 << 21;

然后对外暴露名为TemporaryBakingType的get属性:

/// <summary>
/// <seealso cref="TypeIndex.IsTemporaryBakingType"/>
/// </summary>
public bool TemporaryBakingType => IsTemporaryBakingType(TypeIndex);

最终这一属性会被Unity内部的BakingStripSystem所使用,在OnCreate时会创建所有带有该attribute的entity query:

protected override void OnCreate()
{
    var allTypes = TypeManager.AllTypes.Where(t => t.TemporaryBakingType).ToArray();
    m_BakingComponentQueries = new NativeArray<(ComponentType, EntityQuery)>(allTypes.Length, Allocator.Persistent);

    for(int i = 0; i < allTypes.Length; i++)
    {
        var componentType = ComponentType.FromTypeIndex(allTypes[i].TypeIndex);
        EntityQueryDesc desc = new EntityQueryDesc()
        {
            All = new ComponentType[] {componentType},
            Options = EntityQueryOptions.IncludeDisabledEntities | EntityQueryOptions.IncludePrefab
        };
        m_BakingComponentQueries[i] = (componentType, GetEntityQuery(desc));
    }
}

然后每次update时,对符合条件的entity移除掉带有MyComponentTempBakingType属性的component:

protected override void OnUpdate()
{
    using (s_stripping.Auto())
    {
        foreach(var (componentType, query) in m_BakingComponentQueries)
        {
            EntityManager.RemoveComponent(query, componentType);
        }
    }
}

BakingTypeMyComponentTempBakingType都是用来过滤component的,Unity还提供了一种过滤entity的方式,即给需要过滤掉的entity上添加一个名为BakingOnlyEntity的component。

AddComponent<BakingOnlyEntity>(entity);

这样这个entity就只会存在于conversation world中:

Unity DOTS中的baking(三)4

Unity DOTS中的baking(三)5

背后的实现也很简单,就是有一个专门处理这个component的system,筛选出符合条件的entity,把它们一一销毁掉:

protected override void OnCreate()
{
    _DestroyRemoveEntityInBake = new EntityQueryBuilder(Allocator.Temp)
        .WithAny<RemoveUnusedEntityInBake, BakingOnlyEntity>()
        .WithOptions(EntityQueryOptions.IncludeDisabledEntities | EntityQueryOptions.IncludePrefab)
        .Build(this);
}

protected override void OnUpdate()
{
    EntityManager.DestroyEntity(_DestroyRemoveEntityInBake);
}

Reference

[1] Filter baking output

[2] Baking worlds overview

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

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

相关文章

[SWPUCTF 2021 新生赛]include

他让我们传入一个flag值 我们传入即可看到代码部分 传入一个php的伪类即可 得到经过Base64加密的flag&#xff0c;解密即可

x-shell安装、使用以及配置cuda、cudnn和conda

x-shell安装、使用以及安装最新版本conda x-shell安装远程连接服务器conda安装和环境配置 x-shell安装 x-shell是一款终端模拟软件&#xff0c;用于在Windows界面下远程访问和使用不同系统下的服务器。免费版本下载地址&#xff1a; https://www.xshell.com/zh/free-for-home-…

论文阅读:Learning Lens Blur Fields

这篇文章是对镜头模糊场进行表征学习的研究&#xff0c;镜头的模糊场也就是镜头的 PSF 分布&#xff0c;镜头的 PSF 与物距&#xff0c;焦距&#xff0c;光学系统本身的像差都有关系&#xff0c;实际的 PSF 分布是非常复杂而且数量也很多&#xff0c;这篇文章提出用一个神经网络…

Vulnhub靶机:hacksudo3

一、介绍 运行环境&#xff1a;Virtualbox 攻击机&#xff1a;kali&#xff08;10.0.2.15&#xff09; 靶机&#xff1a;hacksudo3&#xff08;10.0.2.45&#xff09; 目标&#xff1a;获取靶机root权限和flag 靶机下载地址&#xff1a;https://www.vulnhub.com/entry/hac…

Power BI案例-链接Mysql方法

Power BI案例-连锁Mysql 方法1-通过组件mysql-connector-net-8.3.0&#xff1a; 选择文件–获取数据–选择MySQL数据库–选择链接 提示无组件&#xff0c;选择了解详细情况 弹出浏览器&#xff0c;选择下载 不用登陆&#xff0c;可以直接下载 下载的组件如下&#xff1a…

cesium-加载谷歌影像

cesium在开发的时候有可能会加载不同的影像&#xff0c;今天就先看一下加载谷歌的吧。 使用谷歌有个好处就是基本不会出现此区域无卫星图的情况 闲言话语不多说&#xff0c;看代码 <template><div id"cesiumContainer" style"height: 100vh;"&g…

uniapp开发一个交流社区小程序

uniapp开发一个交流社区小程序 假期的时候简单学了一下uniapp&#xff0c;想开发一款类似百度贴吧的交流社区来练练手。本篇文章主要记录开发过程&#xff0c;文末附上项目地址。 主要需要开发以下几个页面。 信息页面热榜页面用户主页用户信息页 信息页面 该页面的功能主要…

国标GB/T 28181详解:设备视音频文件检索消息流程

目 录 一、设备视音频文件检索 二、设备视音频文件检索的基本要求 三、命令流程 1、流程图 2、流程描述 四、协议接口 五、产品说明 六、设备视音频文件检索的作用 七、参考 在国标GBT28181中&#xff0c;定义了设备视音频文件检索消息的流程&#xff0c;主…

ubuntu 安装 kvmQemu no active connection to install on

更新 apt sudo apt update检查虚拟化是否开启 0 不开&#xff0c;其余数字表示开启&#xff0c;开不开都可以&#xff0c;不开性能弱&#xff0c;只能跑 x86 系统 egrep -c (vmx|svm) /proc/cpuinfo安装 sudo apt install -y qemu-kvm virt-manager libvirt-daemon-system virt…

Oracle 面试题 | 09.精选Oracle高频面试题

&#x1f90d; 前端开发工程师、技术日更博主、已过CET6 &#x1f368; 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 &#x1f560; 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 &#x1f35a; 蓝桥云课签约作者、上架课程《Vue.js 和 E…

【备战蓝桥杯】——循环结构终篇

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-yl4Tqejg4LkjZLAM {font-family:"trebuchet ms",verdana,arial,sans-serif;font-siz…

回归预测 | Matlab实现RIME-CNN-LSTM-Attention霜冰优化卷积长短期记忆网络注意力多变量回归预测(SE注意力机制)

回归预测 | Matlab实现RIME-CNN-LSTM-Attention霜冰优化卷积长短期记忆网络注意力多变量回归预测&#xff08;SE注意力机制&#xff09; 目录 回归预测 | Matlab实现RIME-CNN-LSTM-Attention霜冰优化卷积长短期记忆网络注意力多变量回归预测&#xff08;SE注意力机制&#xff0…

HP惠普暗影精灵8P笔记本OMEN Gaming Laptop 16-n0076AX原厂Win11系统镜像恢复出厂预装OEM系统

原装Windows11系统安装包&#xff0c;适用型号(HP暗影8plus笔记本电脑)&#xff1a; 16-n0000AX、16-n0001AX、16-n0002AX、16-n0003AX、16-n0004AX、16-n0005AX 16-n0016AX、16-n0058AX、16-n0059AX、16-n0076AX、16-n0078AX等 链接&#xff1a;https://pan.baidu.com/s/1G…

Matplotlib魅力揭秘:多彩直方图绘制技巧与实战【第56篇—python:Matplotlib多彩直方图绘制】

文章目录 Matplotlib魅力揭秘&#xff1a;多彩直方图绘制技巧与实战1. 普通直方图2. 多变量直方图3. 堆叠直方图4. 分组直方图5. 多个子图直方图6. 折线直方图7. 曲线直方图8. 绘制密度直方图9. 自定义直方图颜色和样式结语 Matplotlib魅力揭秘&#xff1a;多彩直方图绘制技巧与…

C#中使用OpenCvSharp4绘制直线、矩形、圆、文本

C#中使用OpenCvSharp4绘制直线、矩形、圆、文本 继之前的Python中使用Opencv-python库绘制直线、矩形、圆、文本和VC中使用OpenCV绘制直线、矩形、圆和文字&#xff0c;将之前的Python和C示例代码翻译成C#语言&#xff0c;很简单&#xff0c;还是借用OpenCvSharp4库中的Line、…

CSS transition(过渡效果)详解并附带示例

CSS过渡效果&#xff08;CSS transitions&#xff09;是一种在元素属性值发生变化时&#xff0c;通过指定过渡效果来实现平滑的动画效果的方法。通过定义起始状态和结束状态之间的过渡属性&#xff0c;可以使元素的变化更加流畅和可视化。 过渡效果的基本语法如下&#xff1a;…

《幻兽帕鲁》好玩吗?幻兽帕鲁能在Mac上运行吗?

最近一款叫做《幻兽帕鲁》的新游戏走红&#xff0c;成为了Steam游戏平台上&#xff0c;连续3周的销量冠军&#xff0c;有不少Mac电脑用户&#xff0c;利用Crossover成功玩上了《幻兽帕鲁》&#xff0c;其实Crossover已经支持很多3A游戏&#xff0c;包括《赛博朋克2077》《博德之…

20.HarmonyOS App(JAVA)表格布局Layout使用方法

ability_main.xml&#xff0c;实现计算器键盘按钮 <?xml version"1.0" encoding"utf-8"?> <TableLayoutxmlns:ohos"http://schemas.huawei.com/res/ohos"ohos:height"match_parent"ohos:width"match_parent"oho…

STL——空间配置器

空间配置器是STL六大组件之一&#xff0c;它和其他五个组件相互配合&#xff0c;起着很关键的作用。 容器&#xff1a;各种数据结构、如vector、list、stack、deque、queue、set、map、unordered_map等等算法&#xff1a;各种算法&#xff0c;如sort、serach、copy、erase 提供…

AI新宠Arc浏览器真可以取代Chrome吗?

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…