forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoidSystem.cs
More file actions
336 lines (298 loc) · 17.6 KB
/
Copy pathBoidSystem.cs
File metadata and controls
336 lines (298 loc) · 17.6 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System.Collections.Generic;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Burst;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Samples.Common;
namespace Samples.Boids
{
public class BoidSystem : JobComponentSystem
{
private ComponentGroup m_BoidGroup;
private ComponentGroup m_TargetGroup;
private ComponentGroup m_ObstacleGroup;
private List<Boid> m_UniqueTypes = new List<Boid>(10);
private List<PrevCells> m_PrevCells = new List<PrevCells>();
struct PrevCells
{
public NativeMultiHashMap<int, int> hashMap;
public NativeArray<int> cellIndices;
public NativeArray<Position> copyTargetPositions;
public NativeArray<Position> copyObstaclePositions;
public NativeArray<Heading> cellAlignment;
public NativeArray<Position> cellSeparation;
public NativeArray<int> cellObstaclePositionIndex;
public NativeArray<float> cellObstacleDistance;
public NativeArray<int> cellTargetPistionIndex;
public NativeArray<int> cellCount;
}
[BurstCompile]
struct HashPositions : IJobParallelFor
{
[ReadOnly] public ComponentDataArray<Position> positions;
public NativeMultiHashMap<int, int>.Concurrent hashMap;
public float cellRadius;
public void Execute(int index)
{
var hash = GridHash.Hash(positions[index].Value, cellRadius);
hashMap.Add(hash, index);
}
}
[BurstCompile]
struct MergeCells : IJobNativeMultiHashMapMergedSharedKeyIndices
{
public NativeArray<int> cellIndices;
public NativeArray<Heading> cellAlignment;
public NativeArray<Position> cellSeparation;
public NativeArray<int> cellObstaclePositionIndex;
public NativeArray<float> cellObstacleDistance;
public NativeArray<int> cellTargetPistionIndex;
public NativeArray<int> cellCount;
[ReadOnly] public NativeArray<Position> targetPositions;
[ReadOnly] public NativeArray<Position> obstaclePositions;
void NearestPosition(NativeArray<Position> targets, float3 position, out int nearestPositionIndex, out float nearestDistance )
{
nearestPositionIndex = 0;
nearestDistance = math.lengthsq(position-targets[0].Value);
for (int i = 1; i < targets.Length; i++)
{
var targetPosition = targets[i].Value;
var distance = math.lengthsq(position-targetPosition);
var nearest = distance < nearestDistance;
nearestDistance = math.select(nearestDistance, distance, nearest);
nearestPositionIndex = math.select(nearestPositionIndex, i, nearest);
}
nearestDistance = math.sqrt(nearestDistance);
}
public void ExecuteFirst(int index)
{
var position = cellSeparation[index].Value / cellCount[index];
int obstaclePositionIndex;
float obstacleDistance;
NearestPosition(obstaclePositions, position, out obstaclePositionIndex, out obstacleDistance);
cellObstaclePositionIndex[index] = obstaclePositionIndex;
cellObstacleDistance[index] = obstacleDistance;
int targetPositionIndex;
float targetDistance;
NearestPosition(targetPositions, position, out targetPositionIndex, out targetDistance);
cellTargetPistionIndex[index] = targetPositionIndex;
cellIndices[index] = index;
}
public void ExecuteNext(int cellIndex, int index)
{
cellCount[cellIndex] += 1;
cellAlignment[cellIndex] = new Heading { Value = cellAlignment[cellIndex].Value + cellAlignment[index].Value };
cellSeparation[cellIndex] = new Position { Value = cellSeparation[cellIndex].Value + cellSeparation[index].Value };
cellIndices[index] = cellIndex;
}
}
[BurstCompile]
struct Steer : IJobParallelFor
{
[ReadOnly] public NativeArray<int> cellIndices;
[ReadOnly] public Boid settings;
[ReadOnly] public NativeArray<Position> targetPositions;
[ReadOnly] public NativeArray<Position> obstaclePositions;
[ReadOnly] public NativeArray<Heading> cellAlignment;
[ReadOnly] public NativeArray<Position> cellSeparation;
[ReadOnly] public NativeArray<int> cellObstaclePositionIndex;
[ReadOnly] public NativeArray<float> cellObstacleDistance;
[ReadOnly] public NativeArray<int> cellTargetPistionIndex;
[ReadOnly] public NativeArray<int> cellCount;
[ReadOnly] public ComponentDataArray<Position> positions;
public float dt;
public ComponentDataArray<Heading> headings;
public void Execute(int index)
{
var forward = headings[index].Value;
var position = positions[index].Value;
var cellIndex = cellIndices[index];
var neighborCount = cellCount[cellIndex];
var alignment = cellAlignment[cellIndex].Value;
var separation = cellSeparation[cellIndex].Value;
var nearestObstacleDistance = cellObstacleDistance[cellIndex];
var nearestObstaclePositionIndex = cellObstaclePositionIndex[cellIndex];
var nearestTargetPositionIndex = cellTargetPistionIndex[cellIndex];
var nearestObstaclePosition = obstaclePositions[nearestObstaclePositionIndex].Value;
var nearestTargetPosition = targetPositions[nearestTargetPositionIndex].Value;
var obstacleSteering = position - nearestObstaclePosition;
var avoidObstacleHeading = (nearestObstaclePosition + math.normalizesafe(obstacleSteering)
* settings.obstacleAversionDistance)-position;
var targetHeading = settings.targetWeight
* math.normalizesafe(nearestTargetPosition - position);
var nearestObstacleDistanceFromRadius = nearestObstacleDistance - settings.obstacleAversionDistance;
var alignmentResult = settings.alignmentWeight
* math.normalizesafe((alignment/neighborCount)-forward);
var separationResult = settings.separationWeight
* math.normalizesafe((position * neighborCount) - separation);
var normalHeading = math.normalizesafe(alignmentResult + separationResult + targetHeading);
var targetForward = math.select(normalHeading, avoidObstacleHeading, nearestObstacleDistanceFromRadius < 0);
var nextHeading = math.normalizesafe(forward + dt*(targetForward-forward));
headings[index] = new Heading {Value = nextHeading};
}
}
protected override void OnStopRunning()
{
for (var i = 0; i < m_PrevCells.Count; i++)
{
m_PrevCells[i].hashMap.Dispose();
m_PrevCells[i].cellIndices.Dispose();
m_PrevCells[i].copyTargetPositions.Dispose();
m_PrevCells[i].copyObstaclePositions.Dispose();
m_PrevCells[i].cellAlignment.Dispose();
m_PrevCells[i].cellSeparation.Dispose();
m_PrevCells[i].cellObstacleDistance.Dispose();
m_PrevCells[i].cellObstaclePositionIndex.Dispose();
m_PrevCells[i].cellTargetPistionIndex.Dispose();
m_PrevCells[i].cellCount.Dispose();
}
m_PrevCells.Clear();
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
EntityManager.GetAllUniqueSharedComponentData(m_UniqueTypes);
var obstaclePositions = m_ObstacleGroup.GetComponentDataArray<Position>();
var targetPositions = m_TargetGroup.GetComponentDataArray<Position>();
// Ingore typeIndex 0, can't use the default for anything meaningful.
for (int typeIndex = 1; typeIndex < m_UniqueTypes.Count; typeIndex++)
{
var settings = m_UniqueTypes[typeIndex];
m_BoidGroup.SetFilter(settings);
var positions = m_BoidGroup.GetComponentDataArray<Position>();
var headings = m_BoidGroup.GetComponentDataArray<Heading>();
var cacheIndex = typeIndex - 1;
var boidCount = positions.Length;
var cellIndices = new NativeArray<int>(boidCount, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var hashMap = new NativeMultiHashMap<int,int>(boidCount,Allocator.TempJob);
var copyTargetPositions = new NativeArray<Position>(targetPositions.Length, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var copyObstaclePositions = new NativeArray<Position>(obstaclePositions.Length, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var cellAlignment = new NativeArray<Heading>(boidCount, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var cellSeparation = new NativeArray<Position>(boidCount, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var cellObstacleDistance = new NativeArray<float>(boidCount, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var cellObstaclePositionIndex = new NativeArray<int>(boidCount, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var cellTargetPositionIndex = new NativeArray<int>(boidCount, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var cellCount = new NativeArray<int>(boidCount, Allocator.TempJob,NativeArrayOptions.UninitializedMemory);
var nextCells = new PrevCells
{
cellIndices = cellIndices,
hashMap = hashMap,
copyObstaclePositions = copyObstaclePositions,
copyTargetPositions = copyTargetPositions,
cellAlignment = cellAlignment,
cellSeparation = cellSeparation,
cellObstacleDistance = cellObstacleDistance,
cellObstaclePositionIndex = cellObstaclePositionIndex,
cellTargetPistionIndex = cellTargetPositionIndex,
cellCount = cellCount
};
if (cacheIndex > (m_PrevCells.Count - 1))
{
m_PrevCells.Add(nextCells);
}
else
{
m_PrevCells[cacheIndex].hashMap.Dispose();
m_PrevCells[cacheIndex].cellIndices.Dispose();
m_PrevCells[cacheIndex].cellObstaclePositionIndex.Dispose();
m_PrevCells[cacheIndex].cellTargetPistionIndex.Dispose();
m_PrevCells[cacheIndex].copyTargetPositions.Dispose();
m_PrevCells[cacheIndex].copyObstaclePositions.Dispose();
m_PrevCells[cacheIndex].cellAlignment.Dispose();
m_PrevCells[cacheIndex].cellSeparation.Dispose();
m_PrevCells[cacheIndex].cellObstacleDistance.Dispose();
m_PrevCells[cacheIndex].cellCount.Dispose();
}
m_PrevCells[cacheIndex] = nextCells;
var hashPositionsJob = new HashPositions
{
positions = positions,
hashMap = hashMap.ToConcurrent(),
cellRadius = settings.cellRadius
};
var hashPositionsJobHandle = hashPositionsJob.Schedule(boidCount, 64, inputDeps);
var initialCellAlignmentJob = new CopyComponentData<Heading>
{
Source = headings,
Results = cellAlignment
};
var initialCellAlignmentJobHandle = initialCellAlignmentJob.Schedule(boidCount, 64, inputDeps);
var initialCellSeparationJob = new CopyComponentData<Position>
{
Source = positions,
Results = cellSeparation
};
var initialCellSeparationJobHandle = initialCellSeparationJob.Schedule(boidCount, 64, inputDeps);
var initialCellCountJob = new MemsetNativeArray<int>
{
Source = cellCount,
Value = 1
};
var initialCellCountJobHandle = initialCellCountJob.Schedule(boidCount, 64, inputDeps);
var initialCellBarrierJobHandle = JobHandle.CombineDependencies(initialCellAlignmentJobHandle, initialCellSeparationJobHandle, initialCellCountJobHandle);
var copyTargetPositionsJob = new CopyComponentData<Position>
{
Source = targetPositions,
Results = copyTargetPositions
};
var copyTargetPositionsJobHandle = copyTargetPositionsJob.Schedule(targetPositions.Length, 2, inputDeps);
var copyObstaclePositionsJob = new CopyComponentData<Position>
{
Source = obstaclePositions,
Results = copyObstaclePositions
};
var copyObstaclePositionsJobHandle = copyObstaclePositionsJob.Schedule(obstaclePositions.Length, 2, inputDeps);
var copyTargetObstacleBarrierJobHandle = JobHandle.CombineDependencies(copyTargetPositionsJobHandle, copyObstaclePositionsJobHandle);
var mergeCellsBarrierJobHandle = JobHandle.CombineDependencies(hashPositionsJobHandle, initialCellBarrierJobHandle, copyTargetObstacleBarrierJobHandle);
var mergeCellsJob = new MergeCells
{
cellIndices = cellIndices,
cellAlignment = cellAlignment,
cellSeparation = cellSeparation,
cellObstacleDistance = cellObstacleDistance,
cellObstaclePositionIndex = cellObstaclePositionIndex,
cellTargetPistionIndex = cellTargetPositionIndex,
cellCount = cellCount,
targetPositions = copyTargetPositions,
obstaclePositions = copyObstaclePositions
};
var mergeCellsJobHandle = mergeCellsJob.Schedule(hashMap,64,mergeCellsBarrierJobHandle);
var steerJob = new Steer
{
cellIndices = nextCells.cellIndices,
settings = settings,
cellAlignment = cellAlignment,
cellSeparation = cellSeparation,
cellObstacleDistance = cellObstacleDistance,
cellObstaclePositionIndex = cellObstaclePositionIndex,
cellTargetPistionIndex = cellTargetPositionIndex,
cellCount = cellCount,
targetPositions = copyTargetPositions,
obstaclePositions = copyObstaclePositions,
dt = Time.deltaTime,
positions = positions,
headings = headings,
};
var steerJobHandle = steerJob.Schedule(boidCount, 64, mergeCellsJobHandle);
inputDeps = steerJobHandle;
}
m_UniqueTypes.Clear();
return inputDeps;
}
protected override void OnCreateManager()
{
m_BoidGroup = GetComponentGroup(
ComponentType.ReadOnly(typeof(Boid)),
ComponentType.ReadOnly(typeof(Position)),
typeof(Heading));
m_TargetGroup = GetComponentGroup(
ComponentType.ReadOnly(typeof(BoidTarget)),
ComponentType.ReadOnly(typeof(Position)));
m_ObstacleGroup = GetComponentGroup(
ComponentType.ReadOnly(typeof(BoidObstacle)),
ComponentType.ReadOnly(typeof(Position)));
}
}
}