-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
514 lines (416 loc) · 15.2 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
<DOCTYPE html>
<html lang="en">
<head>
<title>Game Graphics Final Project</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
font-family:Monospace;
font-size:13px;
text-align:center;
font-weight: bold;
text-align:center;
}
a {
color:#0078ff;
}
#info {
color:#fff;
position: absolute;
top: 0px; width: 100%;
padding: 5px;
z-index:100;
}
#stats { position: absolute; top:0; left: 0 }
#stats #fps { background: transparent !important }
#stats #fps #fpsText { color: #888 !important }
#stats #fps #fpsGraph { display: none }
</style>
</head>
<body>
<div id="container"></div>
<script id="texVS" type="x-shader/x-vertex">
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
attribute vec3 position;
attribute vec2 uv;
varying vec2 vUV;
void main() {
vec4 position = viewMatrix * modelMatrix * vec4(position, 1.0);
vUV = uv;
gl_Position = projectionMatrix * position;
}
</script>
<script id="texFS" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D tex;
varying vec2 vUV;
void main() {
vec4 c = texture2D(tex, vUV);
gl_FragColor = vec4(c.rgb, 1.0);
}
</script>
<script id="skyboxVS" type="x-shader/x-vertex">
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
attribute vec3 position;
varying vec3 vWorldPosition;
void main() {
vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
vWorldPosition = worldPosition.xyz;
vec4 p = viewMatrix * modelMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * p;
}
</script>
<script id="skyboxFS" type="x-shader/x-fragment">
precision mediump float;
uniform samplerCube tCube;
varying vec3 vWorldPosition;
void main() {
gl_FragColor = textureCube( tCube, vec3( vWorldPosition ) );
}
</script>
<script id="neigh-vs" type="x-shader/x-vertex">
precision mediump float;
precision mediump int;
attribute float the_index;
uniform sampler2D u_pos_tex;
uniform float u_grid_vol;
uniform float u_grid_tex_size;
varying vec4 v_color;
varying float v_index;
vec2 index2D(float indexPosition, bool obj){
vec2 index;
index.x = (mod(indexPosition, u_grid_tex_size)+0.5)/u_grid_tex_size;
index.y = (floor(indexPosition/u_grid_tex_size)+0.5)/u_grid_tex_size;
if(obj) index = 2.0 * index.xy - 1.0;
return index;
}
void main() {
vec3 pos = texture2D(u_pos_tex, index2D(the_index, false)).rgb;
vec3 pos_in_grid = floor(pos);
float grid_index = pos_in_grid.x + u_grid_vol * pos_in_grid.y + u_grid_vol * pos_in_grid.z;
gl_Position = vec4(index2D(grid_index, true), the_index/(u_grid_tex_size * u_grid_tex_size), 1.0);
v_color = vec4(the_index);
}
</script>
<script id="neigh-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 v_color;
void main(){
gl_FragColor = v_color;
}
</script>
<script id="density-vel-pos-vs" type="x-shader/x-vertex">
precision mediump float;
uniform float u_grid_tex_size;
attribute float the_index;
varying float v_index;
vec2 index2D(float indexPosition, bool obj){
vec2 index;
index.x = (mod(indexPosition, u_grid_tex_size)+0.5)/u_grid_tex_size;
index.y = (floor(indexPosition/u_grid_tex_size)+0.5)/u_grid_tex_size;
if(obj) index = 2.0 * index.xy - 1.0;
return index;
}
void main(){
v_index = the_index;
gl_Position = vec4(index2D(the_index, true), 0.0, 1.0);
}
</script>
<script id="density-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_pos_tex;
uniform sampler2D u_vel_tex;
uniform sampler2D u_neighbor_tex;
uniform float u_restDensity;
uniform float u_grid_vol;
uniform float u_grid_tex_size;
uniform vec3 u_neighbors[27];
uniform float u_mass;
uniform float u_maxSearchRatio;
uniform float u_weightDefaultConstant;
varying float v_index;
float wDefault(vec3 distance){
float answ = 0.0;
float ratio = length(distance);
if(ratio < u_maxSearchRatio){
float r2 = ratio * ratio;
float max = u_maxSearchRatio * u_maxSearchRatio;
float diff = max - r2;
answ = u_weightDefaultConstant * diff * diff * diff;
}
return answ;
}
vec2 index2D(float indexPosition){
vec2 index;
index.x = (mod(indexPosition, u_grid_tex_size)+0.5)/u_grid_tex_size;
index.y = (floor(indexPosition/u_grid_tex_size)+0.5)/u_grid_tex_size;
return index;
}
void main(){
vec3 pos = texture2D(u_pos_tex, index2D(v_index)).rgb;
vec3 vel = texture2D(u_vel_tex, index2D(v_index)).rgb;
vec3 gridpos = floor(pos.xyz);
float density = 0.0;
for(int i = 0; i < 27; i++){
vec3 n_gridpos = gridpos + u_neighbors[i];
float n_index = n_gridpos.x + n_gridpos.y + ( u_grid_vol) * n_gridpos.z;
if(n_index >= 0.0 && n_index < (u_grid_tex_size * u_grid_tex_size)){
vec4 v_indices = texture2D(u_neighbor_tex, index2D(n_index));
if(v_indices.r != 0.0) density += u_mass * wDefault(pos - texture2D(u_pos_tex, index2D(v_indices.r)).rgb);
if(v_indices.g != 0.0) density += u_mass * wDefault(pos - texture2D(u_pos_tex, index2D(v_indices.g)).rgb);
if(v_indices.b != 0.0) density += u_mass * wDefault(pos - texture2D(u_pos_tex, index2D(v_indices.b)).rgb);
if(v_indices.a != 0.0) density += u_mass * wDefault(pos - texture2D(u_pos_tex, index2D(v_indices.a)).rgb);
}
}
gl_FragColor = vec4(density, 0.0, 0.0, 1.0);
}
</script>
<script id="vel-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_pos_tex;
uniform sampler2D u_vel_tex;
uniform sampler2D u_density_tex;
uniform sampler2D u_neighbor_tex;
uniform float u_grid_vol;
uniform float u_grid_tex_size;
uniform vec3 u_neighbors[27];
uniform float u_mass;
uniform float u_maxSearchRatio;
uniform float u_kConstant;
uniform float u_weightPressureConstant;
uniform float u_weightViscosityConstant;
uniform float u_restitution;
uniform float u_viscosity;
uniform float u_restDensity;
uniform float u_dt;
varying float v_index;
vec2 index2D(float indexPosition){
vec2 index;
index.x = (mod(indexPosition, u_grid_tex_size)+0.5)/u_grid_tex_size;
index.y = (floor(indexPosition/u_grid_tex_size)+0.5)/u_grid_tex_size;
return index;
}
vec3 wPressure(vec3 distance){
vec3 answ = vec3(0.0);
float ratio = length(distance);
if(ratio < u_maxSearchRatio && ratio > 0.0){
float diff = u_maxSearchRatio - ratio;
answ = u_weightPressureConstant * normalize(distance) * diff * diff;
}
return answ;
}
float wViscosity(vec3 dist){
float answ = 0.0;
float ratio = length(dist);
if(ratio < u_maxSearchRatio){
answ = u_weightViscosityConstant * (u_maxSearchRatio - ratio);
}
return answ;
}
vec3 sumPressure(float n_index, vec3 pos, float pressure){
vec2 n_i = index2D(n_index);
vec3 n_pos = texture2D(u_pos_tex, n_i).rgb;
float n_density = texture2D(u_density_tex, n_i).r;
if(n_density > 0.0){
return vec3((u_kConstant * (n_density - u_restDensity) + pressure) * u_mass * wPressure(n_pos-pos) / u_restDensity);
}
}
vec3 sumVisc(float n_index, vec3 pos, vec3 vel, float pressure){
vec2 n_i = index2D(n_index);
vec3 n_pos = texture2D(u_pos_tex, n_i).rgb;
vec3 n_vel = texture2D(u_vel_tex, n_i).rgb;
float n_density = texture2D(u_density_tex, n_i).r;
if(n_density > 0.0){
return vec3((n_vel - vel) * u_mass * wViscosity(pos - n_pos) / u_restDensity);
}
}
void main(){
vec2 tex_ind = index2D(v_index);
vec3 pos = texture2D(u_pos_tex, tex_ind).rgb;
vec3 vel = texture2D(u_vel_tex, tex_ind).rgb;
float density = texture2D(u_density_tex, tex_ind).r;
vec3 gridpos = floor(pos);
float pressure = u_kConstant * (density - u_restDensity);
vec3 pressureForce = vec3(0.0);
vec3 viscosityForce = vec3(0.0);
vec3 internalForces = vec3(0.0);
if(density > 0.0){
for(int i = 0; i < 27; i++){
vec3 n_gridpos = gridpos + u_neighbors[i];
float n_index = n_gridpos.x + n_gridpos.y + u_grid_vol * n_gridpos.z;
vec4 vert_ind = texture2D(u_neighbor_tex, index2D(n_index));
pressureForce += sumPressure(vert_ind.r, pos, pressure) * 4.0;
viscosityForce += sumVisc(vert_ind.r, pos, vel, pressure) * 4.0;
}
internalForces = (0.5 * pressureForce + u_viscosity * viscosityForce);
internalForces /= density;
}
vec3 x_loc = pos - vec3(50.0);
vec3 contractpt = min(vec3(40.0), max(vec3(-40.0), x_loc));
float dist = length((contractpt+0.5) - pos);
if(dist > 0.0 && length(vel) > 0.0){
vec3 norm = normalize(sign(contractpt - x_loc));
float rest = u_restitution * dist / (u_dt * length(vel));
vel -= (1.0 + u_restitution) * dot(vel, norm) * norm;
}
vec3 acc = vec3(0.0, -9.8, 0.0) + internalForces;
vel += u_dt * acc.xyz;
gl_FragColor = vec4(vel, 1.0);
}
</script>
<script id="pos-fs" type="x-shader/x-vertex">
precision mediump float;
uniform sampler2D u_pos_tex;
uniform sampler2D u_vel_tex;
uniform float u_dt;
uniform float u_grid_tex_size;
varying float v_index;
vec2 index2D(float indexPosition){
vec2 index;
index.x = (mod(indexPosition, u_grid_tex_size)+0.5)/u_grid_tex_size;
index.y = (floor(indexPosition/u_grid_tex_size)+0.5)/u_grid_tex_size;
return index;
}
void main(){
vec2 tex_ind = index2D(v_index);
vec3 pos = texture2D(u_pos_tex, tex_ind).rgb;
vec3 vel = texture2D(u_vel_tex, tex_ind).rgb;
pos = pos.xyz + vel.xyz * u_dt;
gl_FragColor = vec4(pos, 1.0);
}
</script>
<script id="sph-vs" type="x-shader/x-vertex">
attribute float the_index;
uniform sampler2D u_pos_tex;
uniform sampler2D u_vel_tex;
uniform sampler2D u_density_tex;
uniform float u_grid_tex_size;
varying vec4 v_color;
vec2 index2D(float indexPosition, bool obj){
vec2 index = vec2(0.0);
index.x = (mod(indexPosition, u_grid_tex_size)+0.5)/u_grid_tex_size;
index.y = (floor(indexPosition/u_grid_tex_size)+0.5)/u_grid_tex_size;
if(obj) index = 2.0 * index.xy - 1.0;
return index;
}
void main() {
gl_PointSize = 3.0;
vec3 center = vec3(100.0);
vec3 box = vec3(96.0);
vec3 pos = texture2D(u_pos_tex, index2D(the_index, false)).rgb;
vec3 x_loc = pos - center;
vec3 ctp_loc = min(box, max(-box, x_loc));
vec3 contractpt = ctp_loc + center;
float dist = length(contractpt - pos);
float density = texture2D(u_density_tex, index2D(the_index, false)).r;
if(dist > 0.0) pos = contractpt;
if(density == 0.0) pos = vec3(0.0);
v_color = vec4(0.0, 0.0, 1.0 * pos.y * 0.035 + 0.2, 1.0);
gl_Position = projectionMatrix * modelMatrix * viewMatrix * vec4(pos, 1.0);
}
</script>
<script id="sph-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 v_color;
void main(){
gl_FragColor = v_color;
}
</script>
<script type="x-shader/x-vertex" id="firework-vs">
uniform float amplitude;
attribute float size;
attribute vec3 customColor;
varying vec3 vColor;
varying vec4 pos;
void main() {
vColor = customColor;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_PointSize = size * ( 300.0 / -mvPosition.z );
gl_Position = projectionMatrix * mvPosition;
pos = gl_Position;
}
</script>
<script type="x-shader/x-fragment" id="firework-fs">
uniform vec3 color;
uniform sampler2D texture;
varying vec3 vColor;
varying vec4 pos;
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec2 fade(vec2 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise
float cnoise(vec2 P)
{
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
void main() {
vec4 c = texture2D( texture, gl_PointCoord );
vec4 color = vec4(cnoise(pos.xy*vec2(6,3)), cnoise(pos.xy*vec2(10,2)), cnoise(pos.xy*vec2(15,2)), 0.0);
gl_FragColor = mix(c, color, 0.5);
}
</script>
<script src="./utils/stats.min.js"></script>
<script src="./utils/three.min.js"></script>
<script src="./utils/ShaderGodRays.js"></script>
<script src="./utils/projector.js"></script>
<script src="./utils/GPUParticleSystem.js"></script>
<script src="./utils/OBJLoader.js"></script>
<script src="./utils/dat.gui.min.js"></script>
<script src="./utils/OrbitControls.js"></script>
<body>
<div id="container"></div>
<script src="./SPH/sph.js"></script>
<script src="./Fireworks/Firework.js"></script>
<script src="./GodRays/GodRays.js"></script>
<script src="Driver.js"></script>
<script>main()</script>
<body>
</html>