I have this system, that creates events on trigger enter:
[UpdateAfter(typeof(EndFramePhysicsSystem))]
public class TriggerVolumeSystem : SystemBase
{
EndSimulationEntityCommandBufferSystem m_EntityCommandBufferSystem;
BuildPhysicsWorld m_BuildPhysicsWorldSystem;
StepPhysicsWorld m_StepPhysicsWorldSystem;
NativeArray<int> m_TriggerEntitiesIndex;
protected override void OnCreate()
{
m_EntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
m_StepPhysicsWorldSystem = World.GetOrCreateSystem<StepPhysicsWorld>();
m_TriggerEntitiesIndex = new NativeArray<int>(1, Allocator.Persistent);
m_TriggerEntitiesIndex[0] = 0;
}
protected override void OnDestroy()
{
m_TriggerEntitiesIndex.Dispose();
}
struct TriggerEntities
{
public Entity VolumeEntity;
public Entity OverlappingEntity;
}
[BurstCompile]
struct GetTriggerEventCount : ITriggerEventsJob
{
[NativeFixedLength(1)] public NativeArray<int> NumTriggerEvents;
// unsafe writing to this?
public void Execute(TriggerEvent triggerEvent)
{
NumTriggerEvents[0]++;
}
}
[BurstCompile]
struct ListTriggerEventEntitiesJob : ITriggerEventsJob
{
[ReadOnly] public ComponentDataFromEntity<PhysicsVelocity> PhysicsVelocityGroup;
[ReadOnly] public ComponentDataFromEntity<TriggerVolume> VolumeGroup;
[NativeFixedLength(1)] public NativeArray<int> NumTriggerEntities;
public NativeArray<TriggerEntities> TriggerEntities;
public void Execute(TriggerEvent triggerEvent)
{
Entity entityA = triggerEvent.Entities.EntityA;
Entity entityB = triggerEvent.Entities.EntityB;
bool isBodyATrigger = VolumeGroup.Exists(entityA);
bool isBodyBTrigger = VolumeGroup.Exists(entityB);
// Ignoring Triggers overlapping other Triggers
if (isBodyATrigger && isBodyBTrigger)
return;
bool isBodyADynamic = PhysicsVelocityGroup.Exists(entityA);
bool isBodyBDynamic = PhysicsVelocityGroup.Exists(entityB);
// Ignoring overlapping static bodies
if ((isBodyATrigger && !isBodyBDynamic) ||
(isBodyBTrigger && !isBodyADynamic))
return;
// Increment the output counter in a thread safe way.
var count = ++NumTriggerEntities[0] - 1;
TriggerEntities[count] = new TriggerEntities()
{
VolumeEntity = isBodyATrigger ? entityA : entityB,
OverlappingEntity = isBodyATrigger ? entityB : entityA,
};
}
}
[BurstCompile]
struct UpdateOverlappingJob : IJob
{
[ReadOnly] public NativeArray<TriggerEntities> TriggerEntites;
[NativeFixedLength(1)] [ReadOnly] public NativeArray<int> TriggerEntitiesCount;
public ComponentDataFromEntity<OverlappingTriggerVolume> OverlappingGroup;
public void Execute()
{
for (int index = 0; index < TriggerEntitiesCount[0]; index++)
{
var entities = TriggerEntites[index];
if (OverlappingGroup.Exists(entities.OverlappingEntity))
{
var component = OverlappingGroup[entities.OverlappingEntity];
component.CurrentFrame++;
OverlappingGroup[entities.OverlappingEntity] = component;
}
}
}
}
[BurstCompile]
struct AddNewOverlappingJob : IJob
{
public EntityCommandBuffer CommandBuffer;
public int isTornadoActive;
[DeallocateOnJobCompletion] [ReadOnly] public NativeArray<TriggerEntities> TriggerEntities;
[NativeFixedLength(1)] [ReadOnly] public NativeArray<int> TriggerEntitiesCount;
//[ReadOnly] public ComponentDataFromEntity<OverlappingTriggerVolume> OverlappingGroup;
[ReadOnly] public ComponentDataFromEntity<TriggerVolume> TriggerGroup;
int triggerType; // 1 - Player, 2 - Fireball
public void Execute()
{
for (int i = 0; i < TriggerEntitiesCount[0]; i++)
{
var entities = TriggerEntities[i];
var overlappingEntity = entities.OverlappingEntity;
if (!TriggerGroup.Exists(entities.VolumeEntity))
return;
var triggerComponent = TriggerGroup[entities.VolumeEntity];
var triggerDamageEvent = new TriggerDamageEvent
{
volumeEntity = entities.VolumeEntity,
overlappingEntity = overlappingEntity
};
Entity newE = Entity.Null;
Entity newE2 = Entity.Null;
Entity newE3 = Entity.Null;
switch ((TriggerVolumeType)triggerComponent.Type)
{
case TriggerVolumeType.Fireball:
triggerDamageEvent.damageType = 1;
newE = CommandBuffer.CreateEntity();
CommandBuffer.AddComponent<TriggerDamageEvent>(newE, triggerDamageEvent);
triggerType = 2;
break;
case TriggerVolumeType.None:
default:
break;
}
}
}
}
protected override void OnUpdate()
{
// Get the number of TriggerEvents so that we can allocate a native array
m_TriggerEntitiesIndex[0] = 0;
JobHandle getTriggerEventCountJobHandle = new GetTriggerEventCount
{
NumTriggerEvents = m_TriggerEntitiesIndex,
}.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, Dependency);
getTriggerEventCountJobHandle.Complete();
// Get the list of overlapping bodies
var triggerEntities = new NativeArray<TriggerEntities>(m_TriggerEntitiesIndex[0], Allocator.TempJob);
m_TriggerEntitiesIndex[0] = 0;
var physicsVelocityGroup = GetComponentDataFromEntity<PhysicsVelocity>(true);
JobHandle listTriggerEventEntitiesJobHandle = new ListTriggerEventEntitiesJob
{
PhysicsVelocityGroup = physicsVelocityGroup,
VolumeGroup = GetComponentDataFromEntity<TriggerVolume>(true),
TriggerEntities = triggerEntities,
NumTriggerEntities = m_TriggerEntitiesIndex,
}.Schedule(m_StepPhysicsWorldSystem.Simulation, ref m_BuildPhysicsWorldSystem.PhysicsWorld, Dependency);
var triggerGroup = GetComponentDataFromEntity<TriggerVolume>(true);
JobHandle addNewJobHandle = new AddNewOverlappingJob
{
CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer(),
TriggerEntities = triggerEntities,
TriggerEntitiesCount = m_TriggerEntitiesIndex,
TriggerGroup = triggerGroup,
//OverlappingGroup = overlappingGroup,
isTornadoActive = GameplayData.isPlayerTorandoActive
}.Schedule(listTriggerEventEntitiesJobHandle);
m_EntityCommandBufferSystem.AddJobHandleForProducer(addNewJobHandle);
Dependency = addNewJobHandle;
}
}
There is a situation where there are many rigidbodies on the floor. These bodies do not have triggers or Raises Collision Events checkbox.
This is how it looks in editor:
And this is how it looks in build:
As I understand it, all collisions are processed and this system waits for them to complete. NarrowPhase:CreateContactsJob takes a long time.
But I only have about 500 rigidbodies on the floor, why does it take so long to process?
Any ideas?

