forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPositionConstraintSystem.cs
More file actions
66 lines (59 loc) · 2.49 KB
/
Copy pathPositionConstraintSystem.cs
File metadata and controls
66 lines (59 loc) · 2.49 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
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;
using Unity.Mathematics;
using Unity.Transforms;
namespace Samples.Common
{
public class PositionConstraintSystem : JobComponentSystem
{
struct PositionConstraintsGroup
{
#pragma warning disable 649
[ReadOnly] public ComponentDataArray<PositionConstraint> positionConstraints;
[ReadOnly] public EntityArray entities;
public readonly int Length;
}
[Inject] private PositionConstraintsGroup m_PositionContraintsGroup;
[Inject] ComponentDataFromEntity<Position> m_TransformPositions;
#pragma warning restore 649
[BurstCompile]
struct ContrainPositions : IJob
{
public ComponentDataFromEntity<Position> positions;
[ReadOnly] public ComponentDataArray<PositionConstraint> positionConstraints;
[ReadOnly] public EntityArray positionConstraintEntities;
public void Execute()
{
for (int i = 0; i < positionConstraints.Length; i++)
{
var childEntity = positionConstraintEntities[i];
var parentEntity = positionConstraints[i].parentEntity;
var childPosition = positions[childEntity].Value;
var parentPosition = positions[parentEntity].Value;
var d = childPosition - parentPosition;
var len = math.length(d);
if (len >= 0.0001f) // TODO- find out why parent/child position are identical
{
var nl = math.min(len, positionConstraints[i].maxDistance);
positions[childEntity] = new Position
{
Value = parentPosition + ((d * nl) / len)
};
}
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var constrainPositionsJob = new ContrainPositions
{
positions = m_TransformPositions,
positionConstraints = m_PositionContraintsGroup.positionConstraints,
positionConstraintEntities = m_PositionContraintsGroup.entities
};
return constrainPositionsJob.Schedule(inputDeps);
}
}
}