-
Notifications
You must be signed in to change notification settings - Fork 406
/
SpatialHashing.cs
380 lines (339 loc) · 15.1 KB
/
SpatialHashing.cs
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using UnityEngine;
using UnityEngine.Profiling;
using Unity.Jobs;
using Unity.Collections;
using UnityEngine.Rendering;
using System.Collections;
public class SpatialHashing : MonoBehaviour {
public bool useSpatialHash = false, useEnumerator = false;
public int numParticles = 10000;
[Range(-1f, 1f)]
public float floorHeight = 0.001f;
[Range(0.25f, 10f)]
public float sphereRadius = 2.5f;
[Range(0.01f, 0.5f)]
public float particleDiameter = 0.2f;
public Mesh sphereMesh;
public Material sphereMaterial;
SpatialHashData data;
Matrix4x4[] particles;
Matrix4x4[] instanceMatrices = new Matrix4x4[1023];
public struct SpatialHashData : IDisposable {
public NativeArray<Vector3> particles, prevParticles, offsets;
public NativeArray<Matrix4x4> particleMatrices;
public SpatialHash hash;
public SpatialHashData(int numParticles, float floorHeight, float particleDiameter) {
particles = new NativeArray<Vector3 >(numParticles, Allocator.Persistent);
prevParticles = new NativeArray<Vector3 >(numParticles, Allocator.Persistent);
offsets = new NativeArray<Vector3 >(numParticles, Allocator.Persistent);
particleMatrices = new NativeArray<Matrix4x4>(numParticles, Allocator.Persistent);
hash = new SpatialHash(particleDiameter, numParticles);
for (int i = 0; i < particles.Length; i++) {
particles[i] = UnityEngine.Random.insideUnitSphere * 5f;
particles[i] = new Vector3(particles[i].x, Mathf.Abs(particles[i].y - floorHeight) + floorHeight, particles[i].z);
prevParticles[i] = particles[i];
offsets[i] = Vector3.zero;
particleMatrices[i] = Matrix4x4.identity;
}
hash.create(particles, particleDiameter);
}
[Unity.Burst.BurstCompile]
public struct Integrate : IJobParallelFor {
public NativeArray<Vector3> particles, prevParticles;
public void Execute(int i) {
// Apply Verlet
Vector3 tempPos = particles[i];
particles[i] += (particles[i] - prevParticles[i]) + (Vector3.down * 0.001f); ;// * 0.95f;
prevParticles[i] = tempPos;
}
}
[Unity.Burst.BurstCompile]
public struct CalculateCollisionOffsetsNaive : IJobParallelFor {
public NativeArray<Vector3> offsets;
[ReadOnly]
public NativeArray<Vector3> particles;
[ReadOnly]
public float particleDiameter;
public void Execute(int i) {
// Collide Particles with each other
offsets[i] = Vector3.zero; int samples = 0;
for (int j = 0; j < particles.Length; j++) {
if (i != j) {
Vector3 offset = particles[i] - particles[j];
float offsetMagnitude = offset.magnitude;
if (offsetMagnitude < particleDiameter) {
Vector3 normalizedOffset = offset / offsetMagnitude;
offsets[i] += normalizedOffset * (particleDiameter - offsetMagnitude) * 0.5f;
samples++;
}
}
}
if (samples > 0) { offsets[i] /= samples; }
}
}
[Unity.Burst.BurstCompile]
public struct ConstructSpatialHash : IJob {
public SpatialHash hash;
[ReadOnly]
public NativeArray<Vector3> particles;
[ReadOnly]
public float particleDiameter;
public void Execute() {
hash.create(particles, particleDiameter);
}
}
[Unity.Burst.BurstCompile]
public struct CalculateCollisionOffsetsHash : IJobParallelFor {
//public SpatialHash hash;
public NativeArray<Vector3> offsets;
[ReadOnly]
public NativeArray<Vector3> particles;
// Need to specify these separately because they're ReadOnly here, but writable in another function
[ReadOnly]
public float particleDiameter, spacing;
[ReadOnly]
public int tableSize;
[ReadOnly]
public NativeArray<int> cellStart, cellEntries;
public void Execute(int i) {
// Query the spatial hash
int x0 = SpatialHash.intCoord(particles[i].x - particleDiameter, spacing);
int y0 = SpatialHash.intCoord(particles[i].y - particleDiameter, spacing);
int z0 = SpatialHash.intCoord(particles[i].z - particleDiameter, spacing);
int x1 = SpatialHash.intCoord(particles[i].x + particleDiameter, spacing);
int y1 = SpatialHash.intCoord(particles[i].y + particleDiameter, spacing);
int z1 = SpatialHash.intCoord(particles[i].z + particleDiameter, spacing);
offsets[i] = Vector3.zero; int samples = 0;
for (int xi = x0; xi <= x1; xi++) {
for (int yi = y0; yi <= y1; yi++) {
for (int zi = z0; zi <= z1; zi++) {
int h = SpatialHash.hashCoords(xi, yi, zi, tableSize);
int start = cellStart[h];
int end = cellStart[h + 1];
for (int q = start; q < end; q++) {
int j = cellEntries[q];
// ------ Ideally this code could be wrapped inside of the hash's query iterator ------
if (i != j) {
// Collide these two particles together
Vector3 offset = particles[i] - particles[j];
float offsetMagnitude = offset.magnitude;
if (offsetMagnitude < particleDiameter) {
Vector3 normalizedOffset = offset / offsetMagnitude;
offsets[i] += normalizedOffset * (particleDiameter - offsetMagnitude) * 0.5f;
samples++;
}
}
// ------------------------------------------------------------------------------------
}
}
}
}
if (samples > 0) { offsets[i] /= samples; }
}
}
[Unity.Burst.BurstCompile]
public struct CalculateCollisionOffsetsHashEnumerator : IJobParallelFor {
[ReadOnly]
public SpatialHash hash;
public NativeArray<Vector3> offsets;
[ReadOnly]
public NativeArray<Vector3> particles;
// Need to specify these separately because they're ReadOnly here, but writable in another function
[ReadOnly]
public float particleDiameter, spacing;
public void Execute(int i) {
offsets[i] = Vector3.zero; int samples = 0;
foreach (int j in hash.QueryPosition(particles[i], particleDiameter)) {
if (i != j) {
// Collide these two particles together
Vector3 offset = particles[i] - particles[j];
float offsetMagnitude = offset.magnitude;
if (offsetMagnitude < particleDiameter) {
Vector3 normalizedOffset = offset / offsetMagnitude;
offsets[i] += normalizedOffset * (particleDiameter - offsetMagnitude) * 0.5f;
samples++;
}
}
}
if (samples > 0) { offsets[i] /= samples; }
}
}
[Unity.Burst.BurstCompile]
public struct ApplyCollisionOffsets : IJobParallelFor {
public NativeArray<Vector3> particles, offsets;
public NativeArray<Matrix4x4> particleMatrices;
public float floorHeight, particleDiameter, sphereRadius;
public void Execute(int i) {
particles[i] += offsets[i];
// Bound to Ground
particles[i] = new Vector3(particles[i].x, Mathf.Max(floorHeight, particles[i].y), particles[i].z);
// Bound to Sphere
if (particles[i].magnitude > sphereRadius) { particles[i] = particles[i].normalized * sphereRadius; }
particleMatrices[i] = Matrix4x4.TRS(particles[i], new Quaternion(0f, 0f, 0f, 1f), new Vector3(1f, 1f, 1f) * particleDiameter * 0.5f);
}
}
public void Dispose() {
particles.Dispose();
prevParticles.Dispose();
offsets.Dispose();
particleMatrices.Dispose();
hash.Dispose();
}
}
public void Update() {
if (!data.particles.IsCreated || data.particles.Length != numParticles) {
if (data.particles.IsCreated) { data.Dispose(); }
data = new SpatialHashData(numParticles, floorHeight, particleDiameter);
}
// Integrate, Collide, and Apply
JobHandle previousHandle = new SpatialHashData.Integrate() {
particles = data.particles, prevParticles = data.prevParticles
}.Schedule(data.particles.Length, 32);
if (useSpatialHash) {
previousHandle = new SpatialHashData.ConstructSpatialHash() {
hash = data.hash, particles = data.particles, particleDiameter = particleDiameter
}.Schedule(dependsOn: previousHandle);
if (useEnumerator) {
previousHandle = new SpatialHashData.CalculateCollisionOffsetsHashEnumerator() {
hash = data.hash, particles = data.particles, offsets = data.offsets,
particleDiameter = particleDiameter, spacing = particleDiameter
}.Schedule(data.particles.Length, 32, dependsOn: previousHandle);
} else {
previousHandle = new SpatialHashData.CalculateCollisionOffsetsHash() {
particles = data.particles, offsets = data.offsets, particleDiameter = particleDiameter,
cellStart = data.hash.cellStart, cellEntries = data.hash.cellEntries, spacing = particleDiameter, tableSize = data.hash.tableSize
}.Schedule(data.particles.Length, 32, dependsOn: previousHandle);
}
} else {
// Calculate the n^2 collisions
previousHandle = new SpatialHashData.CalculateCollisionOffsetsNaive() {
particles = data.particles, offsets = data.offsets, particleDiameter = particleDiameter
}.Schedule(data.particles.Length, 32, dependsOn: previousHandle);
}
previousHandle = new SpatialHashData.ApplyCollisionOffsets() {
particles = data.particles, particleMatrices = data.particleMatrices,
offsets = data.offsets, floorHeight = floorHeight, particleDiameter = particleDiameter, sphereRadius = sphereRadius
}.Schedule(data.particles.Length, 32, dependsOn: previousHandle);
previousHandle.Complete();
// Draw the particles 1023 at a time
data.particleMatrices.CopyTo(particles);
int particlesDrawn = 0;
while (particlesDrawn < numParticles) {
int particlesToDraw = Mathf.Min(1023, numParticles - particlesDrawn);
System.Array.Copy(particles, particlesDrawn, instanceMatrices, 0, particlesToDraw);
Graphics.DrawMeshInstanced(sphereMesh, 0, sphereMaterial, instanceMatrices, particlesToDraw, null, UnityEngine.Rendering.ShadowCastingMode.Off);
particlesDrawn += particlesToDraw;
}
}
public void Start() { if (data.particles.IsCreated) { data.Dispose(); } particles = new Matrix4x4[numParticles]; }
public void OnDestroy() { data.Dispose(); }
}
public struct SpatialHash : IEnumerable {
private Vector3 _pos;
public float spacing, maxDist; public int tableSize;
public NativeArray<int> cellStart, cellEntries;
public SpatialHash(float spacing, int maxNumObjects) {
this.spacing = spacing;
tableSize = 2 * maxNumObjects;
cellStart = new NativeArray<int>(tableSize + 1, Allocator.Persistent);
cellEntries = new NativeArray<int>(maxNumObjects, Allocator.Persistent);
_pos = Vector3.zero;
maxDist = spacing;
}
// Static Functions
public static int hashCoords(int xi, int yi, int zi, int tableSize) {
var h = (xi * 92837111) ^ (yi * 689287499) ^ (zi * 283923481); // fantasy function
return Mathf.Abs(h) % tableSize;
}
public static int intCoord(float coord, float spacing) {
return (int)Mathf.Floor(coord / spacing);
}
public static int hashPos(Vector3 pos, float spacing, int tableSize) {
return hashCoords(
intCoord(pos.x, spacing),
intCoord(pos.y, spacing),
intCoord(pos.z, spacing), tableSize);
}
public void create(NativeArray<Vector3> pos, float spacing) {
int numObjects = Mathf.Min(pos.Length, cellEntries.Length);
// 1. Determine cell sizes
for (int i = 0; i < cellStart.Length; i++) { cellStart [i] = 0; }
for (int i = 0; i < cellEntries.Length; i++) { cellEntries[i] = 0; }
for (int i = 0; i < numObjects; i++) { cellStart[hashPos(pos[i], spacing, tableSize)]++; }
// 2. Determine cells starts
int start = 0;
for (int i = 0; i < tableSize; i++) {
start += cellStart[i];
cellStart[i] = start;
}
cellStart[tableSize] = start; // Guard
// 3. Fill in objects ids
for (var i = 0; i < numObjects; i++) {
int h = hashPos(pos[i], spacing, tableSize);
cellStart[h]--;
cellEntries[cellStart[h]] = i;
}
}
public SpatialHash QueryPosition(Vector3 pos, float maxDist) { _pos = pos; this.maxDist = maxDist; return this; }
IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator)GetEnumerator(); }
public SpatialHashEnumerator GetEnumerator() {
return new SpatialHashEnumerator(_pos, maxDist, spacing, tableSize, cellStart, cellEntries);
}
public void Dispose() {
cellStart.Dispose(); cellEntries.Dispose();
}
}
public struct SpatialHashEnumerator : IEnumerator {
public Vector3 pos; public Vector3Int corner1, curCell; public int position, end;
public float spacing, maxDist; public int tableSize;
[ReadOnly]
public NativeArray<int> cellStart, cellEntries;
public SpatialHashEnumerator(Vector3 pos, float maxDist, float spacing,
int tableSize, NativeArray<int> cellStart, NativeArray<int> cellEntries) {
this.pos = pos;
this.maxDist = maxDist;
this.spacing = spacing;
this.tableSize = tableSize;
this.cellStart = cellStart;
this.cellEntries = cellEntries;
this.curCell = new Vector3Int(SpatialHash.intCoord(pos.x - maxDist, spacing),
SpatialHash.intCoord(pos.y - maxDist, spacing),
SpatialHash.intCoord(pos.z - maxDist, spacing));
this.corner1 = new Vector3Int(SpatialHash.intCoord(pos.x + maxDist, spacing),
SpatialHash.intCoord(pos.y + maxDist, spacing),
SpatialHash.intCoord(pos.z + maxDist, spacing));
int h = SpatialHash.hashCoords(curCell.x, curCell.y, curCell.z, tableSize);
position = cellStart[h] - 1; // Enumerators are positioned before the first element until the first MoveNext() call.
end = cellStart[h + 1];
}
public int Current { get { return cellEntries[position]; } }
object IEnumerator.Current { get { return Current; } }
public void Reset() {
this.curCell = new Vector3Int(SpatialHash.intCoord(pos.x - maxDist, spacing),
SpatialHash.intCoord(pos.y - maxDist, spacing),
SpatialHash.intCoord(pos.z - maxDist, spacing));
int h = SpatialHash.hashCoords(curCell.x, curCell.y, curCell.z, tableSize);
position = cellStart[h] - 1; // Enumerators are positioned before the first element until the first MoveNext() call.
end = cellStart[h + 1];
}
public bool MoveNext() {
if(position < end) {
position++;
return true;
} else if (curCell != corner1 + Vector3Int.one) { // TODO: Scrutinize this line!!!!
if (curCell.z <= corner1.z) {
curCell.z++;
} else if (curCell.y <= corner1.y) {
curCell.y++;
} else if (curCell.x <= corner1.x) {
curCell.x++;
}
int h = SpatialHash.hashCoords(curCell.x, curCell.y, curCell.z, tableSize);
position = cellStart[h];
end = cellStart[h + 1];
return true;
}
return false;
}
}