forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModifyBroadphasePairsBehaviour.cs
More file actions
87 lines (74 loc) · 3.01 KB
/
Copy pathModifyBroadphasePairsBehaviour.cs
File metadata and controls
87 lines (74 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using UnityEngine;
using System;
using Unity.Burst;
//<todo.eoin.usermod Rename to ModifyOverlappingBodyPairsComponentData?
public struct ModifyBroadphasePairs : IComponentData { }
public class ModifyBroadphasePairsBehaviour : MonoBehaviour, IConvertGameObjectToEntity
{
void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
{
dstManager.AddComponentData(entity, new ModifyBroadphasePairs());
}
}
// A system which configures the simulation step to disable certain broad phase pairs
[UpdateBefore(typeof(StepPhysicsWorld))]
public class ModifyBroadphasePairsSystem : JobComponentSystem
{
EntityQuery m_PairModifierGroup;
BuildPhysicsWorld m_PhysicsWorld;
StepPhysicsWorld m_StepPhysicsWorld;
protected override void OnCreate()
{
m_PhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
m_StepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
m_PairModifierGroup = GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(ModifyBroadphasePairs) }
});
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
if (m_PairModifierGroup.CalculateLength() == 0)
{
return inputDeps;
}
if( m_StepPhysicsWorld.Simulation.Type == SimulationType.NoPhysics )
{
return inputDeps;
}
// Add a custom callback to the simulation, which will inject our custom job after the body pairs have been created
SimulationCallbacks.Callback callback = (ref ISimulation simulation, ref PhysicsWorld world, JobHandle inDeps) =>
{
inDeps.Complete(); //<todo Needed to initialize our modifier
return new DisablePairsJob
{
Bodies = m_PhysicsWorld.PhysicsWorld.Bodies,
Motions = m_PhysicsWorld.PhysicsWorld.MotionVelocities
}.Schedule(simulation, ref world, inputDeps);
};
m_StepPhysicsWorld.EnqueueCallback(SimulationCallbacks.Phase.PostCreateDispatchPairs, callback);
return inputDeps;
}
[BurstCompile]
struct DisablePairsJob : IBodyPairsJob
{
public NativeSlice<RigidBody> Bodies;
[ReadOnly] public NativeSlice<MotionVelocity> Motions;
public unsafe void Execute(ref ModifiableBodyPair pair)
{
// Disable the pair if a box collides with a static object
int indexA = pair.BodyIndices.BodyAIndex;
int indexB = pair.BodyIndices.BodyBIndex;
if ((Bodies[indexA].Collider != null && Bodies[indexA].Collider->Type == ColliderType.Box && indexB >= Motions.Length)
|| (Bodies[indexB].Collider != null && Bodies[indexB].Collider->Type == ColliderType.Box && indexA >= Motions.Length))
{
pair.Disable();
}
}
}
}