-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
612 lines (466 loc) · 17.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Boid Beats 3D</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="./src/main.css">
<style type="text/css">
body {
background: rgb(0, 0, 0);
overflow:hidden;
}
body,
html {
margin: 0px;
padding: 0px;
}
circle {
stroke: white;
stroke-opacity: 0.25;
stroke-width: 2px;
}
#audio {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
opacity: 0.0;
transition: opacity 1s;
}
#audio:hover {
opacity: 0.8;
}
#audio.paused {
opacity: 0.8;
}
#overlay {
color: white;
position: absolute;
top: 0;
left: 0;
}
a {
color: white;
font-family: Arial;
}
/* canvas{
filter: sepia(0.8) hue-rotate(180deg) saturate(2);
} */
</style>
</head>
<body>
<audio id="audio" crossorigin="anonymous" class="paused" src="https://cdn.glitch.com/7c659aa6-fe5f-4610-bdf3-3fd76117d9a5%2F300%20Violin%20Orchestra.mp3?1551626906037" loop controls></audio>
<!-- shader for bird's position -->
<script id="fragmentShaderPosition" type="x-shader/x-fragment">
uniform float time;
uniform float delta;
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 tmpPos = texture2D( texturePosition, uv );
vec3 position = tmpPos.xyz;
vec3 velocity = texture2D( textureVelocity, uv ).xyz;
float phase = tmpPos.w;
phase = mod( ( phase + delta +
length( velocity.xz ) * delta * 3. +
max( velocity.y, 0.0 ) * delta * 6. ), 62.83 );
gl_FragColor = vec4( position + velocity * delta * 15. , phase );
}
</script>
<!-- shader for bird's velocity -->
<script id="fragmentShaderVelocity" type="x-shader/x-fragment">
uniform float time;
uniform float testing;
uniform float delta; // about 0.016
uniform float separationDistance; // 20
uniform float alignmentDistance; // 40
uniform float cohesionDistance; //
uniform float freedomFactor;
uniform vec3 predator;
uniform int[256] music;
uniform int[256] lmusic;
const float width = resolution.x;
const float height = resolution.y;
const float PI = 3.141592653589793;
const float PI_2 = PI * 2.0;
// const float VISION = PI * 0.55;
float zoneRadius = 40.0;
float zoneRadiusSquared = 1600.0;
float separationThresh = 0.45;
float alignmentThresh = 0.65;
const float UPPER_BOUNDS = BOUNDS;
const float LOWER_BOUNDS = -UPPER_BOUNDS;
const float SPEED_LIMIT = 1.0;
float rand( vec2 co ){
return fract( sin( dot( co.xy, vec2(12.9898,78.233) ) ) * 43758.5453 );
}
// All components are in the range [0…1], including hue.
vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
// All components are in the range [0…1], including hue.
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
float colorStrength(vec3 c){
float before=float(lmusic[int(floor(mod(rgb2hsv(c.xyz).x*32.00,32.00)))])/256.0;
float noww=float(music[int(floor(mod(rgb2hsv(c.xyz).x*32.00,32.00)))])/256.0;
return before;
}
void main() {
zoneRadius = separationDistance + alignmentDistance + cohesionDistance;
separationThresh = separationDistance / zoneRadius;
alignmentThresh = ( separationDistance + alignmentDistance ) / zoneRadius;
zoneRadiusSquared = zoneRadius * zoneRadius;
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 birdPosition, birdVelocity;
vec3 selfPosition = texture2D( texturePosition, uv ).xyz;
vec3 selfVelocity = texture2D( textureVelocity, uv ).xyz;
vec3 selfColor = texture2D( textureColor, uv ).xyz;
float dist;
vec3 dir; // direction
float distSquared;
float separationSquared = separationDistance * separationDistance;
float cohesionSquared = cohesionDistance * cohesionDistance;
float f;
float percent;
vec3 velocity = selfVelocity;
float limit = SPEED_LIMIT;
dir = predator * UPPER_BOUNDS - selfPosition;
dir.z = 0.;
// dir.z *= 0.6;
dist = length( dir );
distSquared = dist * dist;
float preyRadius = 150.0;
float preyRadiusSq = preyRadius * preyRadius;
// move birds away from predator
if ( dist < preyRadius ) {
f = ( distSquared / preyRadiusSq - 1.0 ) * delta * 1000.;
velocity += normalize( dir ) * f;
limit += 5.0;
}
// if (testing == 0.0) {}
// if ( rand( uv + time ) < freedomFactor ) {}
// Attract flocks to the center
vec3 central = vec3( 0., 0., 0. );
dir = selfPosition - central;
dist = length( dir );
//dir.y *= 2.5;
float globeSize=200.0;
velocity -= normalize(vec3(dir.x,dir.y,dir.z))*max(0.0,dot(normalize(vec3(dir.x,dir.y,dir.z)),selfVelocity)) *min(max(length(dir)-globeSize,0.0),0.5)*2.0;
if(length(dir)>globeSize+1.0){
velocity -= normalize(vec3(dir.x,dir.y,dir.z))*abs(dot(normalize(vec3(dir.x,dir.y,dir.z)),selfVelocity)) *min(max(length(dir)-globeSize,0.0),1.0);
}
float minDist=0.0001;
float tSum=0.0;
vec3 vDelta=vec3(0.0);
vec3 nCenter=vec3(0.0);
float nCount=0.0;
for (int bN=0;bN<7;bN++){
float maxDist=100000.0;
float mX=0.0;
float mY=0.0;
for ( float y = 0.0; y < height; y++ ) {
for ( float x = 0.0; x < width; x++ ) {
vec2 ref = vec2( x + 0.5, y + 0.5 ) / resolution.xy;
birdPosition = texture2D( texturePosition, ref ).xyz;
vec3 birdColor = texture2D( textureColor, ref ).xyz;
dir = birdPosition - selfPosition;
dist = length( dir )*(2.0+length(birdColor-selfColor)/sqrt(3.0)*0.5);
if ( dist < minDist ) continue;
distSquared = dist * dist;
//if ( distSquared > zoneRadiusSquared || bcount>6 ) continue;
if( dist>maxDist) continue;
maxDist=dist;
mX=x;
mY=y;
}
}
minDist=maxDist;
float x=mX;
float y=mY;
vec2 ref = vec2( x + 0.5, y + 0.5 ) / resolution.xy;
birdPosition = texture2D( texturePosition, ref ).xyz;
nCenter+=birdPosition;
nCount+=1.0;
dir = birdPosition - selfPosition;
dist = length( dir );
distSquared = dist * dist;
percent = distSquared / zoneRadiusSquared;
float tm=2.0;
if ( percent < separationThresh ) { // low
// Separation - Move apart for comfort
//float threshDelta = 1.0 - separationThresh;
float adjustedPercent = ( separationThresh-percent ) / separationThresh;
f = 1.0;//percent;//1.0;///(percent+1.0);
birdVelocity = texture2D( textureVelocity, ref ).xyz;
vDelta += normalize( -dir*adjustedPercent+birdVelocity*(tm+abs(dot(normalize(selfVelocity-birdVelocity),dir)))/limit ) * f;
tSum+=f;
} else {
// Attraction / Cohesion - move closer
//float threshDelta = 1.0 - separationThresh;
//float adjustedPercent = ( percent - separationThresh ) / threshDelta;
f = 1.0;//percent;//1.0;///(percent+1.0);
birdVelocity = texture2D( textureVelocity, ref ).xyz;
vDelta += normalize( dir+birdVelocity*(tm+abs(dot(normalize(selfVelocity-birdVelocity),dir)))/limit ) * f;
tSum+=f;
}
}
nCenter*=1.0/nCount;
velocity+=vDelta/tSum*delta*100.0;//+normalize(nCenter-selfPosition)*delta;
float m=max(log(colorStrength(selfColor))-log(1.0/256.0),0.0)*10.0;
velocity=normalize(velocity)*100.0;
limit+=6.0+m;
// this make tends to fly around than down or up
// if (velocity.y > 0.) velocity.y *= (1. - 0.2 * delta);
// Speed Limits
//if ( length( velocity ) < 6.0 ) {
// velocity = normalize( velocity )*6.0 ;
//}
if ( length( velocity ) > limit ) {
velocity = normalize( velocity ) * (max(limit,length(selfVelocity)-delta*1000.0));
}
if ( length( velocity ) > 100.0 ) {
velocity = normalize( velocity ) *100.0;
}
gl_FragColor = vec4( velocity, 1.0 );
}
</script>
<!-- shader for bird's velocity -->
<script id="fragmentShaderColor" type="x-shader/x-fragment">
uniform float time;
uniform float testing;
uniform float delta; // about 0.016
uniform float separationDistance; // 20
uniform float alignmentDistance; // 40
uniform float cohesionDistance; //
uniform float freedomFactor;
uniform vec3 predator;
uniform int[256] music;
uniform int[256] lmusic;
const float width = resolution.x;
const float height = resolution.y;
const float PI = 3.141592653589793;
const float PI_2 = PI * 2.0;
// const float VISION = PI * 0.55;
float zoneRadius = 40.0;
float zoneRadiusSquared = 1600.0;
float separationThresh = 0.45;
float alignmentThresh = 0.65;
const float UPPER_BOUNDS = BOUNDS;
const float LOWER_BOUNDS = -UPPER_BOUNDS;
const float SPEED_LIMIT = 9.0;
float rand( vec2 co ){
return fract( sin( dot( co.xy, vec2(12.9898,78.233) ) ) * 43758.5453 );
}
// All components are in the range [0…1], including hue.
vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
// All components are in the range [0…1], including hue.
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
float colorStrength(vec3 c){
float before=float(lmusic[int(floor(mod(rgb2hsv(c.xyz).x*32.00,32.00)))])/256.0;
float noww=float(music[int(floor(mod(rgb2hsv(c.xyz).x*32.00,32.00)))])/256.0;
return before;
}
void main() {
zoneRadius = separationDistance + alignmentDistance + cohesionDistance;
separationThresh = separationDistance / zoneRadius;
alignmentThresh = ( separationDistance + alignmentDistance ) / zoneRadius;
zoneRadiusSquared = zoneRadius * zoneRadius;
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 birdPosition, birdVelocity;
vec3 selfPosition = texture2D( texturePosition, uv ).xyz;
vec3 selfVelocity = texture2D( textureVelocity, uv ).xyz;
vec3 selfColor = texture2D( textureColor, uv ).xyz;
float dist;
vec3 dir; // direction
float distSquared;
float separationSquared = separationDistance * separationDistance;
float cohesionSquared = cohesionDistance * cohesionDistance;
float f;
float percent;
vec3 velocity = selfVelocity;
vec3 ccolor = selfColor;
float ccount=1.0;
float limit = SPEED_LIMIT;
dir = predator * UPPER_BOUNDS - selfPosition;
dir.z = 0.;
// dir.z *= 0.6;
dist = length( dir );
distSquared = dist * dist;
float preyRadius = 150.0;
float preyRadiusSq = preyRadius * preyRadius;
// move birds away from predator
if ( dist < preyRadius ) {
f = ( distSquared / preyRadiusSq - 1.0 ) * delta * 100.;
velocity += normalize( dir ) * f;
limit += 5.0;
}
// if (testing == 0.0) {}
// if ( rand( uv + time ) < freedomFactor ) {}
// Attract flocks to the center
vec3 central = vec3( 0., 0., 0. );
vec3 centralColor=vec3(0.0);
dir = selfPosition - central;
dist = length( dir );
float M=1.0;
dir.y *= 2.5;
//velocity -= normalize( dir ) * delta * 5.;
for ( float y = 0.0; y < height; y++ ) {
for ( float x = 0.0; x < width; x++ ) {
vec2 ref = vec2( x + 0.5, y + 0.5 ) / resolution.xy;
birdPosition = texture2D( texturePosition, ref ).xyz;
vec3 birdColor = texture2D( textureColor, ref ).xyz;
vec3 birdVelocity = texture2D( textureVelocity, ref ).xyz;
centralColor+=birdColor/width/height;
dir = birdPosition - selfPosition;
vec3 cdir=birdColor - selfColor;
dist = length( dir );
if ( dist < 0.0001 ) continue;
distSquared = dist * dist;
if ( distSquared > zoneRadiusSquared ) continue;
percent = distSquared / zoneRadiusSquared;
float colorSep=length(birdVelocity-selfVelocity)/(length(birdVelocity)+length(selfVelocity));
float colorSepS=colorSep*colorSep;
f=1.0-colorSep*2.0;
f=f;//*delta*M;//*percent*delta;
vec3 radd=pow(delta,0.5)*(vec3(rand(vec2(time+dir.x,selfColor.x)),rand(vec2(time+1.2+dir.y,selfColor.y+0.6)),rand(vec2(time+3.1+selfVelocity.z+dir.z,selfColor.z+7.73)))-0.5);
float powC= length(birdVelocity);//colorStrength(birdColor);
ccolor+=1.0/(percent+1.0)*powC*(selfColor+normalize(cdir+(1.0-length(cdir)/sqrt(3.0))*normalize(birdVelocity)*(min(f,0.0)))*f);//*f);
ccount+=1.0/(percent+1.0)*powC;//+abs(f);
}
}
// this make tends to fly around than down or up
// if (velocity.y > 0.) velocity.y *= (1. - 0.2 * delta);
float lrp=pow(0.5,delta*40.0);
ccolor=selfColor*lrp+ccolor/ccount*(1.0-lrp);
// Speed Limits
if ( length( velocity ) > limit ) {
velocity = normalize( velocity ) * limit;
}
//if ( length( ccolor ) > 1.0 ) {
// ccolor = normalize( ccolor ) * 1.0;
//}
if(delta>0.0){
//ccolor+=pow(delta,0.5)*0.1*(vec3(rand(vec2(time,selfColor.x)),rand(vec2(time+1.2,selfColor.y+0.6)),rand(vec2(time+3.1+selfVelocity.z,selfColor.z+7.73)))-0.5);
}
ccolor=vec3(clamp(0.0,1.0,ccolor.x),clamp(0.0,1.0,ccolor.y),clamp(0.0,1.0,ccolor.z));
float intp=pow(lrp,1.0);//delta*M;
ccolor=(hsv2rgb(vec3(rgb2hsv(ccolor).x+delta*(rand(selfPosition.xy)-0.5)*2.0,1.0,0.5))+(vec3(0.5)-centralColor)*0.25)*intp+(1.0-intp)*ccolor;
ccolor=vec3(clamp(0.0,1.0,ccolor.x),clamp(0.0,1.0,ccolor.y),clamp(0.0,1.0,ccolor.z));
gl_FragColor = vec4( ccolor, 1.0 );
}
</script>
<script type="x-shader/x-vertex" id="birdVS">
attribute vec2 reference;
attribute float birdVertex;
attribute vec3 birdColor;
uniform sampler2D texturePosition;
uniform sampler2D textureVelocity;
uniform sampler2D textureColor;
varying vec4 vColor;
varying vec3 selfVelocity;
varying float z;
uniform float time;
void main() {
vec4 tmpPos = texture2D( texturePosition, reference );
vec4 tmpColor = texture2D( textureColor, reference );
vec3 pos = tmpPos.xyz;
vec3 velocity = normalize(texture2D( textureVelocity, reference ).xyz);
vec3 newPosition = position;
if ( birdVertex == 4.0 || birdVertex == 7.0 ) {
// flap wings
newPosition.y = sin( tmpPos.w ) * 5.;
}
newPosition = mat3( modelMatrix ) * newPosition;
velocity.z *= -1.;
float xz = length( velocity.xz );
float xyz = 1.;
float x = sqrt( 1. - velocity.y * velocity.y );
float cosry = velocity.x / xz;
float sinry = velocity.z / xz;
float cosrz = x / xyz;
float sinrz = velocity.y / xyz;
mat3 maty = mat3(
cosry, 0, -sinry,
0 , 1, 0 ,
sinry, 0, cosry
);
mat3 matz = mat3(
cosrz , sinrz, 0,
-sinrz, cosrz, 0,
0 , 0 , 1
);
newPosition = maty * matz * newPosition;
newPosition += pos;
z = newPosition.z;
selfVelocity=texture2D( textureVelocity, reference ).xyz;
vec3 velColor=tmpColor.xyz;//velocity/2.0+0.5;
velColor=velColor/(0.299*velColor.x + 0.587*velColor.y + 0.114*velColor.z)/2.0;
vColor = vec4( velColor, 1.0 );
gl_Position = projectionMatrix * viewMatrix * vec4( newPosition, 1.0 );
}
</script>
<!-- bird geometry shader -->
<script type="x-shader/x-fragment" id="birdFS">
varying vec4 vColor;
varying vec3 selfVelocity;
varying float z;
uniform vec3 color;
uniform int[256] music;
uniform int[256] lmusic;
// All components are in the range [0…1], including hue.
vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
// All components are in the range [0…1], including hue.
vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
float colorStrength(vec3 c){
float before=float(lmusic[int(floor(mod(rgb2hsv(c.xyz).x*32.00,32.00)))])/256.0;
float noww=float(music[int(floor(mod(rgb2hsv(c.xyz).x*32.00,32.00)))])/256.0;
return before;
}
void main() {
// Fake colors for now
vec3 z2 = 0.2 + ( 1000. - z ) / 1000. * vColor.xyz;
float loud=length(selfVelocity)*0.02+0.1;
gl_FragColor = vec4( z2*loud, 1. );
}
</script>
<script type="module" src="./src/main.js">
</script>
</body>
</html>