protected override void OnUpdate()
            {
                var boxChunks     = boxGroup.CreateArchetypeChunkArray(Allocator.TempJob);
                var sphereChuncks = sphereGroup.CreateArchetypeChunkArray(Allocator.TempJob);
                var capsuleChunks = capsuleGroup.CreateArchetypeChunkArray(Allocator.TempJob);

                for (int i = 0, length = boxChunks.Length; i < length; ++i)
                {
                    ArchetypeChunk       boxChunk   = boxChunks[i];
                    var                  tempEntity = boxChunk.GetNativeArray(GetArchetypeChunkEntityType());
                    NativeArray <Entity> entities   = new NativeArray <Entity>(tempEntity.Length, Allocator.TempJob);
                    tempEntity.CopyTo(entities);
                    UpdateBoxColliders(entities,
                                       boxChunk.GetComponentObjects(GetArchetypeChunkComponentType <BoxCollider>(true), EntityManager));
                    entities.Dispose();
                }

                for (int i = 0, length = sphereChuncks.Length; i < length; ++i)
                {
                    ArchetypeChunk       sphereChunk = sphereChuncks[i];
                    var                  tempEntity  = sphereChunk.GetNativeArray(GetArchetypeChunkEntityType());
                    NativeArray <Entity> entities    = new NativeArray <Entity>(tempEntity.Length, Allocator.TempJob);
                    tempEntity.CopyTo(entities);
                    UpdateSphereColliders(entities,
                                          sphereChunk.GetComponentObjects(GetArchetypeChunkComponentType <SphereCollider>(true), EntityManager));
                    entities.Dispose();
                }

                for (int i = 0, length = capsuleChunks.Length; i < length; ++i)
                {
                    ArchetypeChunk       capsuleChunk = capsuleChunks[i];
                    var                  tempEntity   = capsuleChunk.GetNativeArray(GetArchetypeChunkEntityType());
                    NativeArray <Entity> entities     = new NativeArray <Entity>(tempEntity.Length, Allocator.TempJob);
                    tempEntity.CopyTo(entities);
                    UpdateCapsuleColliders(entities,
                                           capsuleChunk.GetComponentObjects(GetArchetypeChunkComponentType <CapsuleCollider>(true), EntityManager));
                    entities.Dispose();
                }

                boxChunks.Dispose();
                sphereChuncks.Dispose();
                capsuleChunks.Dispose();

                var colliderChunks = colliderGroup.CreateArchetypeChunkArray(Allocator.TempJob);

                for (int i = 0, length = colliderChunks.Length; i < length; ++i)
                {
                    ArchetypeChunk       colliderChunk = colliderChunks[i];
                    var                  tempEntity    = colliderChunk.GetNativeArray(GetArchetypeChunkEntityType());
                    NativeArray <Entity> entities      = new NativeArray <Entity>(tempEntity.Length, Allocator.TempJob);
                    tempEntity.CopyTo(entities);
                    for (int j = 0, entityCount = entities.Length; j < entityCount; ++j)
                    {
                        ColliderComponentData colliderInfo = new ColliderComponentData();
                        Entity entity = entities[j];
                        if (GetComponentDataFromEntity <BoxColliderComponentData>(true).Exists(entity))
                        {
                            colliderInfo.Volume |= E_CollisionVolume.BOX;
                        }
                        if (GetComponentDataFromEntity <SphereColliderComponentData>(true).Exists(entity))
                        {
                            colliderInfo.Volume |= E_CollisionVolume.SPHERE;
                        }
                        if (GetComponentDataFromEntity <CapsuleColliderComponentData>(true).Exists(entity))
                        {
                            colliderInfo.Volume |= E_CollisionVolume.CAPSULE;
                        }
                        Utils.Utils.AddOrSetComponent(entity, colliderInfo, GetComponentDataFromEntity <ColliderComponentData>(true), EntityManager);
                        EntityManager.RemoveComponent <DirtyColliderComponentData>(entity);
                    }
                    entities.Dispose();
                }
                if (World.GetExistingManager <PreCollisionSystem>() != null)
                {
                    World.GetExistingManager <PreCollisionSystem>().HasChanged = true;
                }
                if (colliderChunks.IsCreated)
                {
                    colliderChunks.Dispose();
                }
            }
 public static bool HasBoxCollider(ColliderComponentData data)
 {
     return((data.Volume & E_CollisionVolume.BOX) == E_CollisionVolume.BOX);
 }
 public static bool HasSphereCollider(ColliderComponentData data)
 {
     return((data.Volume & E_CollisionVolume.SPHERE) == E_CollisionVolume.SPHERE);
 }
 public static bool HasCapsuleCollider(ColliderComponentData data)
 {
     return((data.Volume & E_CollisionVolume.CAPSULE) == E_CollisionVolume.CAPSULE);
 }
Example #5
0
            protected override JobHandle OnUpdate(JobHandle inputDeps)
            {
                if (HasChanged)
                {
                    int sphereCount = 0, capsuleCount = 0, boxCount = 0;
                    var colliders = PreColliderGroup.GetComponentDataArray <ColliderComponentData>();
                    for (int i = 0, length = colliders.Length; i < length; ++i)
                    {
                        ColliderComponentData colliderComponent = colliders[i];
                        if (Utils.Utils.HasBoxCollider(colliderComponent))
                        {
                            ++boxCount;
                        }
                        if (Utils.Utils.HasSphereCollider(colliderComponent))
                        {
                            ++sphereCount;
                        }
                        if (Utils.Utils.HasCapsuleCollider(colliderComponent))
                        {
                            ++capsuleCount;
                        }
                    }

                    var entityCount = PreColliderGroup.GetEntityArray().Length;
                    Utils.Utils.ExpandOrCreateArray(ref Entities, entityCount, Allocator.Persistent);

                    JobHandle boxHandle = new JobHandle(), sphereHandle = new JobHandle(), capsuleHandle = new JobHandle();

                    if (boxCount != 0)
                    {
                        Utils.Utils.ExpandOrCreateArray(ref Boxcasts, boxCount * 3, Allocator.Persistent);
                        Utils.Utils.ExpandOrCreateHashMap(ref EntityBoxCastMap, boxCount, Allocator.Persistent);

                        BoxPreCollisionJob boxPreCollision = new BoxPreCollisionJob {
                            BoxColliderType = GetArchetypeChunkComponentType <BoxColliderComponentData>(true),
                            PositionType    = GetArchetypeChunkComponentType <Position>(true),
                            RotationType    = GetArchetypeChunkComponentType <Rotation>(true),
                            ScaleType       = GetArchetypeChunkComponentType <Scale>(true),
                            BoxCasts        = EntityBoxCastMap.ToConcurrent(),
                            EntityType      = GetArchetypeChunkEntityType()
                        };

                        boxHandle = boxPreCollision.Schedule(BoxPreCollisionGroup, inputDeps);
                    }

                    if (sphereCount != 0)
                    {
                        Utils.Utils.ExpandOrCreateArray(ref Spherecasts, sphereCount * 3, Allocator.Persistent);
                        Utils.Utils.ExpandOrCreateHashMap(ref EntitySphereCastMap, sphereCount, Allocator.Persistent);

                        SpherePreCollisionJob spherePreCollision = new SpherePreCollisionJob {
                            SphereColliderType = GetArchetypeChunkComponentType <SphereColliderComponentData>(true),
                            PositionType       = GetArchetypeChunkComponentType <Position>(true),
                            ScaleType          = GetArchetypeChunkComponentType <Scale>(true),
                            SphereCasts        = EntitySphereCastMap.ToConcurrent(),
                            EntityType         = GetArchetypeChunkEntityType()
                        };

                        sphereHandle = spherePreCollision.Schedule(SpherePreCollisionGroup, inputDeps);
                    }

                    if (capsuleCount != 0)
                    {
                        Utils.Utils.ExpandOrCreateArray(ref Capsulecasts, capsuleCount * 3, Allocator.Persistent);
                        Utils.Utils.ExpandOrCreateHashMap(ref EntityCapsuleCastMap, capsuleCount, Allocator.Persistent);

                        CapsulePreCollisionJob capsulePreCollision = new CapsulePreCollisionJob {
                            CapsuleColliderType = GetArchetypeChunkComponentType <CapsuleColliderComponentData>(true),
                            PositionType        = GetArchetypeChunkComponentType <Position>(true),
                            RotationType        = GetArchetypeChunkComponentType <Rotation>(true),
                            ScaleType           = GetArchetypeChunkComponentType <Scale>(true),
                            CapsuleCasts        = EntityCapsuleCastMap.ToConcurrent(),
                            EntityType          = GetArchetypeChunkEntityType()
                        };

                        capsuleHandle = capsulePreCollision.Schedule(CapsulePreCollisionGroup, inputDeps);
                    }

                    JobHandle.ScheduleBatchedJobs();

                    NativeArray <Entity> boxEntities     = new NativeArray <Entity>(0, Allocator.Persistent),
                                         sphereEntities  = new NativeArray <Entity>(0, Allocator.Persistent),
                                         capsuleEntities = new NativeArray <Entity>(0, Allocator.Persistent);

                    if (boxCount != 0)
                    {
                        boxHandle.Complete();
                        boxEntities.Dispose();
                        boxEntities = EntityBoxCastMap.GetKeyArray(Allocator.TempJob);
                    }
                    if (sphereCount != 0)
                    {
                        sphereHandle.Complete();
                        sphereEntities.Dispose();
                        sphereEntities = EntitySphereCastMap.GetKeyArray(Allocator.TempJob);
                    }
                    if (capsuleCount != 0)
                    {
                        capsuleHandle.Complete();
                        capsuleEntities.Dispose();
                        capsuleEntities = EntityCapsuleCastMap.GetKeyArray(Allocator.TempJob);
                    }

                    var job = new FinalizePreCollision {
                        BoxCount             = boxCount,
                        SphereCount          = sphereCount,
                        CapsuleCount         = capsuleCount,
                        EntityBoxCastMap     = EntityBoxCastMap,
                        EntitySphereCastMap  = EntitySphereCastMap,
                        EntityCapsuleCastMap = EntityCapsuleCastMap,
                        BoxEntities          = boxEntities,
                        SphereEntities       = sphereEntities,
                        CapsuleEntities      = capsuleEntities,
                        Entities             = Entities,
                        BoxCasts             = Boxcasts,
                        CapsuleCasts         = Capsulecasts,
                        SphereCasts          = Spherecasts,
                    };

                    var handle = job.Schedule(entityCount, 32, inputDeps);

                    Utils.Utils.ScheduleBatchedJobAndComplete(handle);

                    boxEntities.Dispose();
                    sphereEntities.Dispose();
                    capsuleEntities.Dispose();
                    HasChanged = false;
                }
                return(inputDeps);
            }