EntitiesSample_9. CrossQuery

该示例主要的内容:

1.URPMaterialPropertyBaseColor:Unity.Rendering空间下的材质组件,该结构体拥有一个float4颜色的变量,赋值和其他的组件一样

2.EntityQuery转数组

boxQuery.ToComponentDataArray<LocalTransform>(Allocator.Temp);

3.IJobChunk的使用

复制代码
  new CollisionJob
            {
                //获取LocalTransform组件的句柄
                LocalTransformTypeHandle = SystemAPI.GetComponentTypeHandle<LocalTransform>(true),
                //获取DefaultColor组件的句柄
                DefaultColorTypeHandle = SystemAPI.GetComponentTypeHandle<DefaultColor>(true),
                //获取URPMaterialPropertyBaseColor组件的句柄
                BaseColorTypeHandle = SystemAPI.GetComponentTypeHandle<URPMaterialPropertyBaseColor>(),
                //获取实体类型的句柄
                EntityTypeHandle = SystemAPI.GetEntityTypeHandle(),
                //实体查询对象转化为数组句柄
                OtherChunks = boxQuery.ToArchetypeChunkArray(state.WorldUpdateAllocator)
                //作业调度,   Complete():需要全部完成,然后执行下一次 
            }.ScheduleParallel(boxQuery, state.Dependency).Complete();

特别注意的是IJobChunk接口中的句柄变量,都需要 ReadOnly标签修饰,例如:

ReadOnly public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;

在Chunk作业中,需要使用ArchetypeChunk chunk获取当前要操作的变量值,示例代码是这样的没什么好解释的,就是双循环判断距离

复制代码
[BurstCompile]
    public struct CollisionJob : IJobChunk
    {
        [ReadOnly] public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;
        [ReadOnly] public ComponentTypeHandle<DefaultColor> DefaultColorTypeHandle;
        public ComponentTypeHandle<URPMaterialPropertyBaseColor> BaseColorTypeHandle;
        [ReadOnly] public EntityTypeHandle EntityTypeHandle;

        [ReadOnly] public NativeArray<ArchetypeChunk> OtherChunks;

        public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask,
            in v128 chunkEnabledMask)
        {
            var transforms = chunk.GetNativeArray(ref LocalTransformTypeHandle);
            var defaultColors = chunk.GetNativeArray(ref DefaultColorTypeHandle);
            var baseColors = chunk.GetNativeArray(ref BaseColorTypeHandle);
            var entities = chunk.GetNativeArray(EntityTypeHandle);

            for (int i = 0; i < transforms.Length; i++)
            {
                var transform = transforms[i];
                var baseColor = baseColors[i];
                var entity = entities[i];
                // reset to default color
                baseColor.Value = defaultColors[i].Value;
                for (int j = 0; j < OtherChunks.Length; j++)
                {
                    var otherChunk = OtherChunks[j];
                    var otherTranslations = otherChunk.GetNativeArray(ref LocalTransformTypeHandle);
                    var otherEntities = otherChunk.GetNativeArray(EntityTypeHandle);

                    for (int k = 0; k < otherChunk.Count; k++)
                    {
                        var otherTranslation = otherTranslations[k];
                        var otherEntity = otherEntities[k];

                        if (entity != otherEntity && math.distancesq(transform.Position, otherTranslation.Position) < 1)
                        {
                            baseColor.Value.y = 0.5f; // set green channel
                            break;
                        }
                    }
                }
                baseColors[i] = baseColor;
            }
        }
    }
相关推荐
叶帆4 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君4 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子5 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
心前阳光5 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光5 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯5 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案5 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔5 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
玉夏5 天前
【Shader基础】UV 与纹理采样 Part1
unity·着色器·uv
zdr尽职尽责5 天前
Unity录像功能
学习·ui·unity·游戏引擎