-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDLA.cs
476 lines (388 loc) · 14 KB
/
DLA.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Valve.VR.Extras;
public class DLA : MonoBehaviour {
// State
private bool isPaused = false;
private enum Modes {Point, Rings, Circles, Spheres, Blanket, Line, Wave};
private Modes currentMode = Modes.Spheres;
// Mesh
private GameObject meshContainer;
private MeshCollider mesh;
private Vector3 meshOrigin;
private Vector3 meshSize;
// Walkers
private List<GameObject> activeWalkers = new List<GameObject>();
private List<GameObject> inactiveWalkers = new List<GameObject>();
List<GameObject> walkersToRemove;
private float walkerSize = .01f;
private int maxWalkerAge = 10000;
private float maxWalkerDistance = 100f;
private float walkerSpeed = .01f;
private float jitterAmount = .0036f;
private float minHeight = 1f;
private float maxHeight = 2f;
private float minRadius = .01f;
private float maxRadius = .06f;
private int spawnInterval = 30;
private int lastSpawnTime = 0;
private bool isSpawning = true;
// Materials
public Material walkerMaterial;
public Material aggregateMaterial;
// SteamVR laser pointer
public SteamVR_LaserPointer laserPointer;
private bool laserIsHovering = false;
//===========================================================
// Main setup
//===========================================================
void Start() {
// Get the mesh
meshContainer = GameObject.FindWithTag("MeshContainer");
mesh = GameObject.FindWithTag("Mesh").GetComponent<MeshCollider>();
// Mesh information
meshOrigin = new Vector3(
meshContainer.transform.position.x,
meshContainer.transform.position.y,
meshContainer.transform.position.z
);
meshSize = new Vector3(
mesh.bounds.size.x,
mesh.bounds.size.y,
mesh.bounds.size.z
);
}
//===========================================================
// Main update loop
//===========================================================
void Update() {
// Reset simulation with 'r'
if(Input.GetKeyDown("r")) {
Reset();
}
// Toggle pause using Space
if(Input.GetKeyDown(KeyCode.Space)) {
isPaused = !isPaused;
}
// Number keys to change mode
if(Input.GetKeyDown("1")) { currentMode = Modes.Point; spawnInterval = 2; }
if(Input.GetKeyDown("2")) { currentMode = Modes.Rings; spawnInterval = 20; }
if(Input.GetKeyDown("3")) { currentMode = Modes.Circles; spawnInterval = 20; }
if(Input.GetKeyDown("4")) { currentMode = Modes.Spheres; spawnInterval = 20; }
if(Input.GetKeyDown("5")) { currentMode = Modes.Blanket; spawnInterval = 100; }
if(Input.GetKeyDown("6")) { currentMode = Modes.Line; }
if(Input.GetKeyDown("7")) { currentMode = Modes.Wave; }
if(!isPaused) {
// Spawn walkers at the point where the laser pointer ray meets the active meshes
if(isSpawning && laserIsHovering) {
if(Time.frameCount >= lastSpawnTime + spawnInterval) {
RaycastHit hit;
Ray ray = new Ray(laserPointer.transform.position, laserPointer.transform.forward);
bool bHit = Physics.Raycast(ray, out hit);
if(bHit) {
SpawnWalkersAbove(hit.point);
}
lastSpawnTime = Time.frameCount;
}
}
walkersToRemove = new List<GameObject>();
foreach(GameObject walker in activeWalkers) {
// Handle movement of all active walkers
if(walker.layer == LayerMask.NameToLayer("Walkers")) {
WalkerScript ws = walker.GetComponent<WalkerScript>();
// Flag for removal any walkers that are too old or too far away from the mesh
if(ws.age > maxWalkerAge || Vector3.Distance(walker.transform.position, meshContainer.transform.position) > maxWalkerDistance) {
walkersToRemove.Add(walker);
// Move all active walkers
} else {
// Move towards origin of mesh
// walker.transform.position = Vector3.MoveTowards(walker.transform.position, meshContainer.transform.position + targetOffset + Random.insideUnitSphere * 10f, 1.0f * Time.deltaTime);
// Move walkers forward
// walker.transform.position += new Vector3(0,0,walkerSpeed);
// Move walkers downward
walker.transform.position += new Vector3(0,-walkerSpeed,0);
// Add motion jitter
walker.transform.position += Random.insideUnitSphere * jitterAmount;
}
} else if(walker.layer == LayerMask.NameToLayer("Aggregated")) {
walker.GetComponent<Renderer>().material = aggregateMaterial;
}
}
// Remove walkers that have been flagged for removal
foreach(GameObject walker in walkersToRemove) {
inactiveWalkers.Add(walker);
activeWalkers.Remove(walker);
}
}
}
//===========================================================
// Reset simulation by removing all walkers
//===========================================================
void Reset() {
foreach(GameObject walker in activeWalkers) {
Destroy(walker);
}
foreach(GameObject walker in inactiveWalkers) {
Destroy(walker);
}
activeWalkers = new List<GameObject>();
inactiveWalkers = new List<GameObject>();
}
//===========================================================
// Abstraction function to use appropriate walker
// spawn functions based on current mode
//===========================================================
void SpawnWalkersAbove(Vector3 point) {
switch(currentMode) {
// Spawn single walker
case Modes.Point:
SpawnWalkerAt(
point.x,
minHeight,
point.z
);
break;
// Spawn walkers in rings
case Modes.Rings:
SpawnWalkersInRing(
point.x,
minHeight,
point.z,
Random.Range(minRadius, maxRadius)
);
break;
// Spawn walkers in circles (filled)
case Modes.Circles:
SpawnWalkersInCircle(
point.x,
minHeight,
point.z,
Random.Range(minRadius, maxRadius)
);
break;
// Spawn walkers in spheres
case Modes.Spheres:
SpawnWalkersInSphere(
point.x,
minHeight,
point.z,
minRadius + (maxRadius-minRadius)/2
);
break;
// Spawn walkers along a straight line
case Modes.Line:
SpawnWalkersAlongLine();
break;
// Spawn walkers along a sine wave
case Modes.Wave:
SpawnWalkersAlongWave();
break;
// Spawn walkers evenly along the entire mesh area
case Modes.Blanket:
SpawnWalkerBlanket();
break;
}
}
//===========================================================
// Functions for spawning walkers in specific layouts
//===========================================================
// Create walkers along the edge of a circle
void SpawnWalkersInRing(float centerX, float centerY, float centerZ, float radius) {
float circumference = Mathf.PI * (radius * 2);
int maxPoints = Mathf.FloorToInt(circumference / walkerSize);
int numPoints = Mathf.FloorToInt(maxPoints * .75f);
// int numPoints = Random.Range(5,maxPoints);
// int numPoints = 100;
for(var i=1; i<=numPoints; i++) {
float angle = (360f / numPoints) * i;
float x = centerX + radius * Mathf.Cos(angle * Mathf.Deg2Rad);
float z = centerZ + radius * Mathf.Sin(angle * Mathf.Deg2Rad);
SpawnWalkerAt(x, centerY, z);
}
}
// Create walkers within a circle
void SpawnWalkersInCircle(float centerX, float centerY, float centerZ, float radius) {
// int numPoints = Random.Range(10, Mathf.FloorToInt(radius*10));
int numPoints = 200;
for(var i=1; i<=numPoints; i++) {
Vector2 coords = Random.insideUnitCircle * radius * 2;
SpawnWalkerAt(
centerX + coords.x,
centerY,
centerZ + coords.y
);
}
}
// Create walkers randomly within a sphere
void SpawnWalkersInSphere(float centerX, float centerY, float centerZ, float radius) {
// int numPoints = Random.Range(10, Mathf.FloorToInt(radius*10));
int numPoints = 50;
for(var i=1; i<=numPoints; i++) {
Vector3 coords = Random.insideUnitSphere * radius * 2;
SpawnWalkerAt(
centerX + coords.x,
centerY + coords.y,
centerZ + coords.z
);
}
}
// Create walkers along a straight line
void SpawnWalkersAlongLine() {
int maxNumPoints = Mathf.FloorToInt(meshSize.x / walkerSize);
// int numPoints = maxNumPoints;
int numPoints = 1000;
for(var i=0; i<numPoints; i++) {
// SpawnWalkerAt(i * walkerSize, 2f, meshSizeZ/2); // straight line, evenly spaced walkers
// Range fan alone X axis
SpawnWalkerAt(
Random.Range(meshOrigin.x - meshSize.x/2, meshOrigin.x + meshSize.x/2),
Random.Range(minHeight, maxHeight),
meshOrigin.z
);
}
}
// Create walkers along a sine wave
void SpawnWalkersAlongWave() {
int numPeriods = 2;
float amplitude = .2f;
int numPoints = 2000;
for(int i=0; i<numPoints; i++) {
float x = Random.Range(meshOrigin.x - meshSize.x/2, meshOrigin.x + meshSize.x/2);
float z = Mathf.Sin(
map(
x,
meshOrigin.x - meshSize.x/2,
meshOrigin.x + meshSize.x/2,
0f,
360f * numPeriods
) * Mathf.Deg2Rad
) * amplitude;
SpawnWalkerAt(
x,
Random.Range(minHeight, maxHeight),
meshOrigin.z + z
);
}
}
// Create walkers evenly across entire surface of mesh
void SpawnWalkerBlanket() {
int numPoints = 1000;
float xStart = Random.Range(-meshSize.x/2, (meshSize.x/2) * .75f);
float xEnd = Random.Range(xStart, meshSize.x);
float zStart = Random.Range(-meshSize.z/2, (meshSize.z/2) * .75f);
float zEnd = Random.Range(zStart, meshSize.z);
for(int i=0; i<numPoints; i++) {
/********
Rock7
*********/
// walker.transform.position = Random.insideUnitSphere * walkerCloudDiameter + walkerOffset;
// walker.transform.position = new Vector3(Random.Range(-5f, 5f), Random.Range(0,0), Random.Range(-10f,-5f));
// walker.transform.position = new Vector3(Random.Range(0,9f), Random.Range(-4f,8f), Random.Range(-20f,3f));
/********
Log01
*********/
// Front - entire log
// SpawnWalkerAt(
// Random.Range(0, meshSizeX),
// Random.Range(0, meshSizeY),
// Random.Range(-2f, -10f)
// );
// Top - entire log
// SpawnWalkerAt(
// Random.Range(0, meshSizeX),
// Random.Range(minHeight, maxHeight),
// Random.Range(0, meshSizeZ)
// );
// Top - patch
SpawnWalkerAt(
meshOrigin.x + Random.Range(xStart, xEnd),
Random.Range(minHeight, maxHeight),
meshOrigin.z + Random.Range(zStart, zEnd)
);
}
}
//===========================================================
//
//===========================================================
// Create a single walker at a given position
void SpawnWalkerAt(float x, float y, float z) {
GameObject walker = CreateWalker();
walker.transform.position = new Vector3(x, y, z);
activeWalkers.Add(walker);
}
//===========================================================
//
//===========================================================
// Create and set up a generic walker with a Rigidbody, material, WalkerScript, and other configs
GameObject CreateWalker() {
// Create and configure a sphere for the walker
GameObject walker = GameObject.CreatePrimitive(PrimitiveType.Sphere);
walker.transform.localScale = new Vector3(walkerSize, walkerSize, walkerSize);
walker.layer = LayerMask.NameToLayer("Walkers");
// Apply the assigned walker material and turn off shadows
Renderer r = walker.GetComponent<Renderer>();
r.material = walkerMaterial;
r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
r.receiveShadows = false;
// Attach the collision detection script
walker.AddComponent<WalkerScript>();
// Create and configure Rigidbody
Rigidbody rb = walker.gameObject.AddComponent<Rigidbody>();
rb.transform.localScale = new Vector3(walkerSize, walkerSize, walkerSize);
rb.useGravity = false;
rb.collisionDetectionMode = CollisionDetectionMode.Continuous;
rb.constraints = RigidbodyConstraints.FreezeAll;
return walker;
}
//===========================================================
// Utilities
//===========================================================
// Map a number from one range to another
float map(float s, float a1, float a2, float b1, float b2) {
return b1 + (s-a1)*(b2-b1)/(a2-a1);
}
//===========================================================
// SteamVR inputs
//===========================================================
void Awake() {
laserPointer.PointerClick += PointerClick;
laserPointer.PointerIn += PointerInside;
laserPointer.PointerOut += PointerOutside;
}
public void PointerClick(object sender, PointerEventArgs e) {
// if(
// e.target.name == "Log01" ||
// e.target.name == "Rock2" ||
// e.target.name == "Rock4" ||
// e.target.name == "Rock7" ||
// e.target.name == "Rock10"
// ) {
// isSpawning = !isSpawning;
// }
}
public void PointerInside(object sender, PointerEventArgs e) {
if(
e.target.name == "Log01" ||
e.target.name == "Rock2" ||
e.target.name == "Rock4" ||
e.target.name == "Rock7" ||
e.target.name == "Rock10"
) {
laserIsHovering = true;
}
}
public void PointerOutside(object sender, PointerEventArgs e) {
if(
e.target.name == "Log01" ||
e.target.name == "Rock2" ||
e.target.name == "Rock4" ||
e.target.name == "Rock7" ||
e.target.name == "Rock10"
) {
laserIsHovering = false;
}
}
}