-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
656 lines (602 loc) · 18.3 KB
/
index.html
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>bird-oid by dmnsgn (https://github.com/dmnsgn)</title>
<style>
:root {
--color-dark: #404040;
--color-light: #f2f2f2;
--color-accent: #fd5e62;
}
body {
margin: 0;
overscroll-behavior: none;
font-family: sans-serif;
color: var(--color-light);
background-color: var(--color-dark);
}
main {
padding: 0 20px;
}
h1 {
position: absolute;
}
</style>
</head>
<body>
<main>
<h1>bird-oid</h1>
</main>
<script async src="web_modules/es-module-shims.js" type="module"></script>
<script src="web_modules/import-map.json" type="importmap-shim"></script>
<script type="module-shim">
import SimplexNoise from "simplex-noise";
import {
Clock,
WebGLRenderer,
Scene,
PerspectiveCamera,
Object3D,
Vector3,
Quaternion,
InstancedBufferAttribute,
CylinderBufferGeometry,
BoxBufferGeometry,
SphereGeometry,
SphereBufferGeometry,
MeshNormalMaterial,
MeshBasicMaterial,
InstancedMesh,
Mesh,
BoxHelper,
AxesHelper,
ArrowHelper,
VertexColors,
Color,
TubeBufferGeometry,
CurvePath,
LineCurve3,
DoubleSide,
} from "three";
import { OrbitControls } from "https://unpkg.com/three@0.139.2/examples/jsm/controls/OrbitControls.js";
import VectorField from "vector-field";
import * as dat from "dat.gui";
import {
System as BoidSystem,
Boid,
Obstacle,
behaviors,
Path,
} from "./index.js";
const system = new BoidSystem({
maxSpeed: 0.9,
maxForce: 0.8,
});
const systemHalfScale = system.scale * 0.5;
const clock = new Clock();
const ENTITIES = {
targets: [],
boids: [],
obstacles: [],
};
// Add target boid
const WANDER_SPEED = 2;
const targetWanderBehavior = {
fn: behaviors.wander,
options: {
distance: system.scale * 0.04,
radius: system.scale * 0.3,
theta: 0,
phi: 0.3,
},
};
const targetBoid = new Boid([
targetWanderBehavior,
{ fn: behaviors.boundsConstrain, scale: 1 },
]);
targetBoid.position = system.getRandomPosition();
system.addBoid(targetBoid);
ENTITIES.targets.push(targetBoid);
// FLOW FIELD
const tempDirection = new Vector3();
const tempColor = new Color();
const simplex = new SimplexNoise(Math.random);
function noiseFn([x, y, z], [stepX, stepY, stepZ]) {
const scale = 0.5;
let n =
simplex.noise4D(
x * scale,
y * scale,
z * scale,
0.05 * clock.elapsedTime
) *
Math.PI *
2;
const theta = n;
const phi = n;
const direction = [
Math.sin(theta) * Math.sin(phi),
Math.cos(theta),
Math.sin(theta) * Math.cos(phi),
];
const mesh = this.field[stepX][stepY][stepZ].mesh;
if (mesh) {
mesh.setDirection(tempDirection.fromArray(direction));
mesh.setColor(tempColor.fromArray([...direction, 1]));
}
return direction;
}
const flowField = new VectorField(noiseFn, 12, system.scale);
flowField.update();
const pathPointsCount = 5;
const path = new Path(
Array.from({ length: pathPointsCount }, (_, i) => {
const offset = Math.random() * system.scale * 0.25;
const radius = systemHalfScale * 0.75;
const theta = (0.8 * i) / (pathPointsCount - 1);
return [
radius * Math.cos(theta * Math.PI * 2),
offset,
radius * Math.sin(theta * Math.PI * 2),
];
}),
system.scale * 0.03
);
// Config
const CONFIG = {
cameraFollowTarget: false,
entities: {
boids: {
enabled: true,
count: 200,
},
targets: {
enabled: true,
count: 1,
},
obstacles: {
enabled: false,
count: 30,
},
},
behaviors: [
{
enabled: false,
fn: behaviors.seek,
scale: 1,
options: {
target: [0, 0, 0],
},
},
{
enabled: false,
fn: behaviors.flee,
scale: 1,
options: {
target: [0, 0, 0],
},
},
{
enabled: false,
fn: behaviors.pursue,
scale: 1,
options: {
target: targetBoid.position,
targetVelocity: targetBoid.velocity,
},
},
{
enabled: true,
fn: behaviors.evade,
scale: 1,
options: {
target: targetBoid.position,
targetVelocity: targetBoid.velocity,
},
},
{
enabled: false,
fn: behaviors.arrive,
scale: 1,
options: {
radius: system.scale * 1,
},
},
{
enabled: true,
fn: behaviors.avoidObstacles,
scale: 4,
options: {
maxAvoidForce: system.scale * 2,
obstacles: ENTITIES.obstacles,
},
},
{
enabled: false,
fn: behaviors.wander,
scale: 1,
options: {
distance: system.scale * 0.04,
radius: system.scale * 0.3,
theta: 0,
phi: 0.2,
},
},
{
enabled: true,
fn: behaviors.followPath,
scale: 1,
options: {
path,
maxSpeed: 0.3,
maxForce: 0.4,
},
},
{
enabled: false,
fn: behaviors.followFlowFieldSimple,
scale: 1,
options: {
flowField,
},
},
{
enabled: false,
fn: behaviors.followFlowField,
scale: 1,
options: {
flowField,
},
},
{
enabled: true,
fn: behaviors.separate,
scale: 1,
options: {
maxDistance: system.scale * 0.04,
},
},
{
enabled: false,
fn: behaviors.cohere,
scale: 1,
options: {
maxDistance: system.scale * 0.04,
},
},
{
enabled: false,
fn: behaviors.align,
scale: 1,
options: {
maxDistance: system.scale * 0.04,
},
},
{
enabled: false,
fn: behaviors.flock,
scale: 1,
options: {
maxDistance: system.scale * 0.09,
},
},
{
enabled: false,
fn: behaviors.followLeaderSimple,
scale: 1,
options: {
leader: targetBoid,
distance: system.scale * 0.04,
radius: system.scale * 0.3,
},
},
{
enabled: false,
fn: behaviors.followLeader,
scale: 1,
options: {
leader: targetBoid,
distance: system.scale * 0.04,
radius: system.scale * 0.3,
},
},
{
enabled: false,
fn: behaviors.boundsConstrain,
scale: 2,
},
{
enabled: true,
fn: behaviors.sphereConstrain,
scale: 2,
options: {
radius: systemHalfScale,
},
},
{
enabled: false,
fn: behaviors.boundsWrapConstrain,
},
{
enabled: false,
fn: behaviors.sphereWrapConstrain,
options: {
radius: systemHalfScale,
},
},
],
};
// GUI
const gui = new dat.GUI();
const systemFolder = gui.addFolder("System");
systemFolder.add(system, "scale", 0, 10);
systemFolder.add(system, "maxSpeed", 0, 10);
systemFolder.add(system, "maxForce", 0, 10);
systemFolder.add(CONFIG, "cameraFollowTarget");
systemFolder.add(CONFIG.entities.boids, "enabled").name("Boids");
systemFolder.add(CONFIG.entities.targets, "enabled").name("Targets");
systemFolder.add(CONFIG.entities.obstacles, "enabled").name("Obstacles");
for (let i = 0; i < CONFIG.behaviors.length; i++) {
const behavior = CONFIG.behaviors[i];
const name = behavior.fn.name;
const behaviorFolder = gui.addFolder(name);
// behaviorFolder.open();
behaviorFolder.add(behavior, "enabled");
if (behavior.scale) behaviorFolder.add(behavior, "scale");
}
// SCENE
const tempObject = new Object3D();
const tempQuaternion = new Quaternion();
const renderer = new WebGLRenderer();
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
const scene = new Scene();
const camera = new PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
Object.assign(camera.position, {
x: 0.5,
y: 0.5,
z: 1.5,
});
const controls = new OrbitControls(camera, renderer.domElement);
Object.assign(controls, {
enableDamping: true,
dampingFactor: 0.05,
screenSpacePanning: false,
maxDistance: 50,
});
// ENTITIES
const getBoidGeometry = () => {
const geometry = new CylinderBufferGeometry(0, 0.01, 0.03, 3, 1);
geometry.rotateX(Math.PI / 2);
return geometry;
};
const boidsMesh = new InstancedMesh(
getBoidGeometry(),
new MeshNormalMaterial(),
CONFIG.entities.boids.count
);
scene.add(boidsMesh);
const targetMesh = new InstancedMesh(
getBoidGeometry(),
new MeshBasicMaterial({ color: 0x00ff00 }),
CONFIG.entities.targets.count
);
scene.add(targetMesh);
const obstacleGeometry = new SphereBufferGeometry(1);
const colorBuffer = new Float32Array(CONFIG.entities.obstacles.count * 3);
for (let i = 0; i < CONFIG.entities.obstacles.count; i++) {
colorBuffer[i * 3 + 0] = 1;
colorBuffer[i * 3 + 1] = Math.random();
colorBuffer[i * 3 + 2] = 0;
}
obstacleGeometry.setAttribute(
"color",
new InstancedBufferAttribute(colorBuffer, 3)
);
const obstacleMaterial = new MeshBasicMaterial();
obstacleMaterial.vertexColors = VertexColors;
const obstacleMesh = new InstancedMesh(
obstacleGeometry,
obstacleMaterial,
CONFIG.entities.obstacles.count
);
scene.add(obstacleMesh);
// INIT SYSTEM
for (let i = 0; i < CONFIG.entities.boids.count; i++) {
const boid = new Boid(CONFIG.behaviors);
boid.position = system.getRandomPosition();
boid.velocity = [
Math.random() * 0.5 * system.maxSpeed,
Math.random() * 0.5 * system.maxSpeed,
Math.random() * 0.5 * system.maxSpeed,
];
system.addBoid(boid);
ENTITIES.boids.push(boid);
}
for (let i = 0; i < CONFIG.entities.obstacles.count; i++) {
const radius =
system.scale * 0.025 + Math.random() * system.scale * 0.025;
const obstacle = new Obstacle(system.getRandomPosition(), radius);
ENTITIES.obstacles.push(obstacle);
}
// DEBUG
const debugMeshes = new Object3D();
const debugFolder = gui.addFolder("Debug");
debugFolder.add(debugMeshes, "visible").name("all");
const debugAxes = new AxesHelper(1);
debugFolder.add(debugAxes, "visible").name("axes");
debugMeshes.add(debugAxes);
const debugBounds = new BoxHelper(
new Mesh(
new BoxBufferGeometry(system.scale, system.scale, system.scale),
new MeshBasicMaterial()
),
0xffffff
);
debugBounds.visible = false;
debugFolder.add(debugBounds, "visible").name("bounds");
debugMeshes.add(debugBounds);
const debugSphereBounds = new Mesh(
new SphereGeometry(systemHalfScale),
new MeshBasicMaterial({
color: 0xffffff,
wireframe: true,
transparent: true,
opacity: 0.2,
})
);
// debugBounds.visible = false;
debugFolder.add(debugSphereBounds, "visible").name("sphere bounds");
debugMeshes.add(debugSphereBounds);
const curvePath = new CurvePath();
for (let i = 1; i < path.points.length; i++) {
const prev = path.points[i - 1];
const current = path.points[i];
curvePath.add(
new LineCurve3(
new Vector3().fromArray(prev),
new Vector3().fromArray(current)
)
);
}
const debugPath = new Mesh(
new TubeBufferGeometry(
curvePath,
path.points.length,
path.radius,
8,
false
),
new MeshBasicMaterial({
color: 0xffffff,
wireframe: true,
side: DoubleSide,
})
);
debugPath.visible = false;
debugFolder.add(debugPath, "visible").name("path");
debugMeshes.add(debugPath);
const debugFlowField = new Object3D();
for (let x = 0; x < flowField.steps[0]; x++) {
for (let y = 0; y < flowField.steps[1]; y++) {
for (let z = 0; z < flowField.steps[2]; z++) {
const mesh = new ArrowHelper(
tempDirection.fromArray(flowField.field[x][y][z].direction),
new Vector3()
.fromArray(flowField.field[x][y][z].position)
.sub(
new Vector3(systemHalfScale, systemHalfScale, systemHalfScale)
),
system.scale * 0.05,
0xffffff
);
debugFlowField.add(mesh);
flowField.field[x][y][z].mesh = mesh;
}
}
}
debugFlowField.visible = false;
debugFolder.add(debugFlowField, "visible").name("field");
debugMeshes.add(debugFlowField);
scene.add(debugMeshes);
// LOOP
const frame = () => {
const dt = Math.min(0.1, clock.getDelta());
// System
system.update(dt);
// Flow field
flowField.update(); // See noiseFn for debug meshes update
// Obstacles
// TODO: CONFIG.obstaclesMoving
obstacleMesh.visible = CONFIG.entities.obstacles.enabled;
if (obstacleMesh.visible) {
for (let i = 0; i < ENTITIES.obstacles.length; i++) {
const obstacle = ENTITIES.obstacles[i];
tempObject.position.set(...obstacle.position);
tempObject.rotation.set(0, 0, 0);
tempObject.scale.set(
obstacle.radius,
obstacle.radius,
obstacle.radius
);
tempObject.updateMatrix();
obstacleMesh.setMatrixAt(i, tempObject.matrix);
}
obstacleMesh.instanceMatrix.needsUpdate = true;
}
tempObject.scale.set(1, 1, 1);
// Target
const configWanderBehavior = CONFIG.behaviors.find(
(behavior) => behavior.fn.name === "wander"
);
configWanderBehavior.options.theta +=
Math.random() * WANDER_SPEED - WANDER_SPEED * 0.5;
configWanderBehavior.options.phi +=
Math.random() * WANDER_SPEED - WANDER_SPEED * 0.5;
targetWanderBehavior.options.theta +=
Math.random() * WANDER_SPEED - WANDER_SPEED * 0.5;
targetWanderBehavior.options.phi +=
Math.random() * WANDER_SPEED - WANDER_SPEED * 0.5;
targetMesh.visible = CONFIG.entities.targets.enabled;
if (targetMesh.visible) {
for (let i = 0; i < ENTITIES.targets.length; i++) {
const target = ENTITIES.targets[i];
tempObject.position.set(...target.position);
tempObject.rotation.setFromQuaternion(
tempQuaternion.setFromUnitVectors(
new Vector3(0, 0, 1),
new Vector3(...target.velocity).normalize()
)
);
tempObject.updateMatrix();
targetMesh.setMatrixAt(i, tempObject.matrix);
}
targetMesh.instanceMatrix.needsUpdate = true;
}
// Boids
boidsMesh.visible = CONFIG.entities.boids.enabled;
if (boidsMesh.visible) {
for (let i = 0; i < ENTITIES.boids.length; i++) {
const boid = ENTITIES.boids[i];
tempObject.position.set(...boid.position);
tempObject.rotation.setFromQuaternion(
tempQuaternion.setFromUnitVectors(
new Vector3(0, 0, 1),
new Vector3(...boid.velocity).normalize()
)
);
tempObject.updateMatrix();
boidsMesh.setMatrixAt(i, tempObject.matrix);
}
boidsMesh.instanceMatrix.needsUpdate = true;
}
if (CONFIG.cameraFollowTarget) {
camera.lookAt(new Vector3().fromArray(targetBoid.position));
}
renderer.render(scene, camera);
controls.update();
requestAnimationFrame(frame);
};
// Kick off
requestAnimationFrame(() => {
document.body.appendChild(renderer.domElement);
clock.start();
frame();
});
// Events
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>