吴旭华 发表于 2024-6-13 16:39:59

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接口中的句柄变量,都需要 标签修饰,例如:
  public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;
在Chunk作业中,需要利用ArchetypeChunk chunk获取当前要利用的变量值,示例代码是这样的没什么好解释的,就是双循环判定距离

    public struct CollisionJob : IJobChunk
    {
       public ComponentTypeHandle<LocalTransform> LocalTransformTypeHandle;
       public ComponentTypeHandle<DefaultColor> DefaultColorTypeHandle;
      public ComponentTypeHandle<URPMaterialPropertyBaseColor> BaseColorTypeHandle;
       public EntityTypeHandle EntityTypeHandle;

       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;
                var baseColor = baseColors;
                var entity = entities;
                // reset to default color
                baseColor.Value = defaultColors.Value;
                for (int j = 0; j < OtherChunks.Length; j++)
                {
                  var otherChunk = OtherChunks;
                  var otherTranslations = otherChunk.GetNativeArray(ref LocalTransformTypeHandle);
                  var otherEntities = otherChunk.GetNativeArray(EntityTypeHandle);

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

                        if (entity != otherEntity && math.distancesq(transform.Position, otherTranslation.Position) < 1)
                        {
                            baseColor.Value.y = 0.5f; // set green channel
                            break;
                        }
                  }
                }
                baseColors = baseColor;
            }
      }
    }



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: EntitiesSample_9. CrossQuery