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;
            }
        }
    }

相关推荐

  1. EntitiesSample_9. CrossQuery

    2024-06-10 20:50:02       37 阅读
  2. EntitiesSample_12. FixedTimestep

    2024-06-10 20:50:02       26 阅读
  3. <span style='color:red;'>9</span>

    9

    2024-06-10 20:50:02      42 阅读
  4. 9.9算法

    2024-06-10 20:50:02       52 阅读
  5. <span style='color:red;'>9</span>. 进程

    9. 进程

    2024-06-10 20:50:02      42 阅读
  6. ARMday<span style='color:red;'>9</span>

    ARMday9

    2024-06-10 20:50:02      59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-10 20:50:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-10 20:50:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-10 20:50:02       82 阅读
  4. Python语言-面向对象

    2024-06-10 20:50:02       91 阅读

热门阅读

  1. PostgreSQL:在CASE WHEN语句中使用SELECT语句

    2024-06-10 20:50:02       35 阅读
  2. fastapi实例

    2024-06-10 20:50:02       31 阅读
  3. 生物神经网络 原理分析研读03

    2024-06-10 20:50:02       36 阅读
  4. 受够了“系统异常”!

    2024-06-10 20:50:02       39 阅读
  5. cell的复用机制和自定义cell

    2024-06-10 20:50:02       48 阅读