forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactFlagsSystem.cs
More file actions
77 lines (68 loc) · 3 KB
/
Copy pathContactFlagsSystem.cs
File metadata and controls
77 lines (68 loc) · 3 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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Physics;
using Unity.Physics.Systems;
namespace Modify
{
[UpdateInGroup(typeof(PhysicsCreateJacobiansGroup), OrderFirst = true)]
public partial struct ContactFlagsSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<ModificationType>();
state.RequireForUpdate<PhysicsWorldSingleton>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var simulationSingleton = SystemAPI.GetSingletonRW<SimulationSingleton>().ValueRW;
if (simulationSingleton.Type == SimulationType.NoPhysics)
{
return;
}
var world = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld;
state.Dependency = new SetContactFlagsJob
{
modificationData = SystemAPI.GetComponentLookup<ModificationType>()
}.Schedule(simulationSingleton, ref world, state.Dependency);
}
// This job reads the modify component and sets some data on the contact, to get propagated to the jacobian
// for processing in our jacobian modifier job. This is necessary because some flags require extra data to
// be allocated along with the jacobian (e.g., SurfaceVelocity data typically does not exist). We also set
// user data bits in the jacobianFlags to save us from looking up the ComponentLookup later.
[BurstCompile]
struct SetContactFlagsJob : IContactsJob
{
[ReadOnly] public ComponentLookup<ModificationType> modificationData;
public void Execute(ref ModifiableContactHeader manifold, ref ModifiableContactPoint contact)
{
Entity entityA = manifold.EntityA;
Entity entityB = manifold.EntityB;
ModificationType.Type typeA = ModificationType.Type.None;
if (modificationData.HasComponent(entityA))
{
typeA = modificationData[entityA].type;
}
ModificationType.Type typeB = ModificationType.Type.None;
if (modificationData.HasComponent(entityB))
{
typeB = modificationData[entityB].type;
}
if (ContactsSystem.IsModificationType(
ModificationType.Type.SurfaceVelocity, typeA, typeB))
{
manifold.JacobianFlags |= JacobianFlags.EnableSurfaceVelocity;
}
if (ContactsSystem.IsModificationType(
ModificationType.Type.InfiniteInertia, typeA, typeB) ||
ContactsSystem.IsModificationType(
ModificationType.Type.BiggerInertia, typeA, typeB))
{
manifold.JacobianFlags |= JacobianFlags.EnableMassFactors;
}
}
}
}
}