-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
435 lines (343 loc) · 14.5 KB
/
index.html
File metadata and controls
435 lines (343 loc) · 14.5 KB
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
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stable Fluids</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stats.js/r16/Stats.min.js"></script>
<script src="main.js"></script>
<script type="x-shader/x-vertex" id="vs">#version 300 es
const vec3[4] POSITIONS = vec3[](
vec3(-1.0, -1.0, 0.0),
vec3(1.0, -1.0, 0.0),
vec3(-1.0, 1.0, 0.0),
vec3(1.0, 1.0, 0.0)
);
const int[6] INDICES = int[](
0, 1, 2,
3, 2, 1
);
void main(void) {
vec3 position = POSITIONS[INDICES[gl_VertexID]];
gl_Position = vec4(position, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="initialize_velocity_fs">#version 300 es
precision highp float;
out vec2 o_velocity;
void main(void) {
o_velocity = vec2(0.0);
}
</script>
<script type="x-shader/x-fragment" id="initialize_density_fs">#version 300 es
precision highp float;
uniform vec2 u_resolution;
out vec3 o_density;
void main(void) {
vec2 st = (2.0 * gl_FragCoord.xy - u_resolution) / min(u_resolution.x, u_resolution.y);
vec3 density = vec3(0.0);
st = step(0.5, fract(st));
density = vec3(st.x);
o_density = density;
}
</script>
<script type="x-shader/x-fragment" id="add_density_fs">#version 300 es
precision highp float;
uniform sampler2D u_density_texture;
out vec3 o_density;
void main(void) {
vec3 density = texelFetch(u_density_texture, ivec2(gl_FragCoord.xy), 0).xyz;
o_density = density;
}
</script>
<script type="x-shader/x-fragment" id="diffuse_density_fs">#version 300 es
precision highp float;
uniform sampler2D u_density_texture;
uniform float u_grid_space;
uniform float u_dt;
uniform float u_diffuse;
out vec3 o_density;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
float N = 1.0 / u_grid_space;
float a = u_dt * u_diffuse * N * N;
vec3 center = texelFetch(u_density_texture, coord, 0).xyz;
vec3 left = texelFetch(u_density_texture, coord + ivec2(-1, 0), 0).xyz;
vec3 right = texelFetch(u_density_texture, coord + ivec2(1, 0), 0).xyz;
vec3 down = texelFetch(u_density_texture, coord + ivec2(0, -1), 0).xyz;
vec3 up = texelFetch(u_density_texture, coord + ivec2(0, 1), 0).xyz;
o_density = (center + a * (left + right + down + up)) / (1.0 + 4.0 * a);
}
</script>
<script type="x-shader/x-fragment" id="advect_density_fs">#version 300 es
precision highp float;
uniform sampler2D u_density_texture;
uniform sampler2D u_velocity_texture;
uniform float u_grid_space;
uniform float u_dt;
out vec3 o_density;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
vec2 velocity = texelFetch(u_velocity_texture, coord, 0).xy;
vec2 prevPos = vec2(coord) * u_grid_space - u_dt * velocity;
vec2 prevCoord = prevPos / u_grid_space;
ivec2 i = ivec2(prevCoord);
vec2 f = fract(prevCoord);
vec3 density00 = texelFetch(u_density_texture, i, 0).xyz;
vec3 density10 = texelFetch(u_density_texture, i + ivec2(1, 0), 0).xyz;
vec3 density01 = texelFetch(u_density_texture, i + ivec2(0, 1), 0).xyz;
vec3 density11 = texelFetch(u_density_texture, i + ivec2(1, 1), 0).xyz;
o_density = mix(mix(density00, density10, f.x), mix(density01, density11, f.x), f.y);
}
</script>
<script type="x-shader/x-fragment" id="add_external_force_fs">#version 300 es
precision highp float;
uniform sampler2D u_velocity_texture;
uniform float u_grid_space;
uniform float u_dt;
uniform float u_force_rad;
uniform vec2 u_force_center;
uniform vec2 u_force_direction;
uniform float u_force_intensity;
out vec2 o_velocity;
void main(void) {
vec2 velocity = texelFetch(u_velocity_texture, ivec2(gl_FragCoord.xy), 0).xy;
vec2 ex_force = smoothstep(u_force_rad, 0.0, length(u_force_center - gl_FragCoord.xy) * u_grid_space) * normalize(u_force_direction) * u_force_intensity;
o_velocity = velocity + u_dt * ex_force;
}
</script>
<script type="x-shader/x-fragment" id="diffuse_velocity_fs">#version 300 es
precision highp float;
uniform sampler2D u_velocity_texture;
uniform float u_grid_space;
uniform float u_dt;
uniform float u_diffuse;
uniform vec2 u_resolution;
out vec2 o_velocity;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
ivec2 coordMax = ivec2(u_resolution);
float N = 1.0 / u_grid_space;
float a = u_dt * u_diffuse * N * N;
ivec2 vL = coord + ivec2(-1, 0);
ivec2 vR = coord + ivec2(1, 0);
ivec2 vD = coord + ivec2(0, -1);
ivec2 vU = coord + ivec2(0, 1);
vec2 center = texelFetch(u_velocity_texture, coord, 0).xy;
vec2 left = texelFetch(u_velocity_texture, vL, 0).xy;
vec2 right = texelFetch(u_velocity_texture, vR, 0).xy;
vec2 down = texelFetch(u_velocity_texture, vD, 0).xy;
vec2 up = texelFetch(u_velocity_texture, vU, 0).xy;
if (vL.x < 0) {
left.x = -center.x;
}
if (vR.x > coordMax.x) {
right.x = -center.x;
}
if (vD.y < 0) {
down.y = -center.y;
}
if (vU.y > coordMax.y) {
up.y = -center.y;
}
o_velocity = (center + a * (left + right + down + up)) / (1.0 + 4.0 * a);
}
</script>
<script type="x-shader/x-fragment" id="advect_velocity_fs">#version 300 es
precision highp float;
uniform sampler2D u_velocity_texture;
uniform float u_grid_space;
uniform float u_dt;
uniform vec2 u_resolution;
out vec2 o_velocity;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
ivec2 coordMax = ivec2(u_resolution);
vec2 velocity = texelFetch(u_velocity_texture, coord, 0).xy;
vec2 prevPos = vec2(coord) * u_grid_space - u_dt * velocity;
vec2 prevCoord = prevPos / u_grid_space;
ivec2 i = ivec2(prevCoord);
vec2 f = fract(prevCoord);
ivec2 vR = i + ivec2(1, 0);
ivec2 vU = i + ivec2(0, 1);
ivec2 vRU = i + ivec2(1, 1);
vec2 velocity00 = texelFetch(u_velocity_texture, i, 0).xy;
vec2 velocity10 = texelFetch(u_velocity_texture, vR, 0).xy;
vec2 velocity01 = texelFetch(u_velocity_texture, vU, 0).xy;
vec2 velocity11 = texelFetch(u_velocity_texture, vRU, 0).xy;
if (vR.x > coordMax.x) {
velocity10.x = -velocity00.x;
}
if (vU.y > coordMax.y) {
velocity01.y = -velocity00.y;
}
if (vRU.x > coordMax.x && vRU.y > coordMax.y) {
velocity11 = -velocity00;
}
o_velocity = mix(mix(velocity00, velocity10, f.x), mix(velocity01, velocity11, f.x), f.y);
}
</script>
<script type="x-shader/x-fragment" id="projection_step01_fs">#version 300 es
precision highp float;
uniform sampler2D u_velocity_texture;
uniform float u_grid_space;
out vec2 o_project;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
vec2 left = texelFetch(u_velocity_texture, coord + ivec2(-1, 0), 0).xy;
vec2 right = texelFetch(u_velocity_texture, coord + ivec2(1, 0), 0).xy;
vec2 down = texelFetch(u_velocity_texture, coord + ivec2(0, -1), 0).xy;
vec2 up = texelFetch(u_velocity_texture, coord + ivec2(0, 1), 0).xy;
float div = -0.5 * u_grid_space * (right.x - left.x + up.y - down.y);
o_project = vec2(0.0, div);
}
</script>
<script type="x-shader/x-fragment" id="projection_step02_fs">#version 300 es
precision highp float;
uniform sampler2D u_project_texture;
out vec2 o_project;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
float div = texelFetch(u_project_texture, coord, 0).y;
float left = texelFetch(u_project_texture, coord + ivec2(-1, 0), 0).x;
float right = texelFetch(u_project_texture, coord + ivec2(1, 0), 0).x;
float down = texelFetch(u_project_texture, coord + ivec2(0, -1), 0).x;
float up = texelFetch(u_project_texture, coord + ivec2(0, 1), 0).x;
o_project = vec2((div + left + right + down + up) / 4.0, div);
}
</script>
<script type="x-shader/x-fragment" id="projection_step03_fs">#version 300 es
precision highp float;
uniform sampler2D u_velocity_texture;
uniform sampler2D u_project_texture;
uniform float u_grid_space;
uniform vec2 u_resolution;
out vec2 o_velocity;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
ivec2 coordMax = ivec2(u_resolution);
ivec2 vL = coord + ivec2(-1, 0);
ivec2 vR = coord + ivec2(1, 0);
ivec2 vD = coord + ivec2(0, -1);
ivec2 vU = coord + ivec2(0, 1);
vec2 velocity = texelFetch(u_velocity_texture, coord, 0).xy;
float center = texelFetch(u_project_texture, coord, 0).x;
float left = texelFetch(u_project_texture, vL, 0).x;
float right = texelFetch(u_project_texture, vR, 0).x;
float down = texelFetch(u_project_texture, vD, 0).x;
float up = texelFetch(u_project_texture, vU, 0).x;
o_velocity = velocity - 0.5 * vec2(right - left, up - down) / u_grid_space;
}
</script>
<script type="x-shader/x-fragment" id="render_velocity_fs">#version 300 es
precision highp float;
#define PI 3.14159265359
uniform sampler2D u_velocity_texture;
float rand(float n) {return fract(sin(n) * 43758.5453123);}
float noise(float p) {
float fl = floor(p);
float fc = fract(p);
return mix(rand(fl), rand(fl + 1.0), fc);
}
out vec4 fragColor;
void main(void) {
ivec2 coord = ivec2(gl_FragCoord.xy);
vec2 center = texelFetch(u_velocity_texture, coord, 0).xy;
vec2 left = texelFetch(u_velocity_texture, coord + ivec2(-1, 0), 0).xy;
vec2 right = texelFetch(u_velocity_texture, coord + ivec2(1, 0), 0).xy;
vec2 down = texelFetch(u_velocity_texture, coord + ivec2(0, -1), 0).xy;
vec2 up = texelFetch(u_velocity_texture, coord + ivec2(0, 1), 0).xy;
vec2 velocity = center + left + right + down + up;
float brightness = min(1.0, pow(length(velocity), 1.5));
vec3 color = vec3(noise(velocity.x) * brightness, noise(velocity.y) * brightness, noise(length(velocity)));
fragColor = vec4(color * 1.5, 1.0);
}
</script>
<script type="x-shader/x-fragment" id="render_density_fs">#version 300 es
precision highp float;
uniform sampler2D u_density_texture;
out vec4 fragColor;
void main(void) {
vec3 density = texelFetch(u_density_texture, ivec2(gl_FragCoord.xy), 0).xyx;
fragColor = vec4(density, 1.0);
}
</script>
</head>
<body>
<canvas id="canvas"></canvas>
<div id="container"></div>
<h1>Stable Fluids</h1>
<div id="parameter_container">
<div class="parameter"><div class="parameter_name">Render<input type="checkbox" id="render"/><span id="disp_render" class="value"></span></div></div><br>
<div class="parameter"><div class="parameter_name">Force Radius<span id="disp_force_rad" class="value"></span></div><input type="range" id="force_rad" min="0.005" max="0.05" value="0.01" step="0.001"/></div><br>
<div class="parameter"><div class="parameter_name">Force Intensity<span id="disp_force_intensity" class="value"></span></div><input type="range" id="force_intensity" min="10" max="500" value="100" step="1"/></div><br>
<div class="parameter"><div class="parameter_name">Diffuse<span id="disp_diffuse" class="value"></span></div><input type="range" id="diffuse" min="0.0" max="1.0" value="0.0" step="0.001"/></div><br>
<div class="parameter"><div class="parameter_name">Time Step<span id="disp_time_step" class="value"></span></div><input type="range" id="time_step" min="0.001" max="0.01" value="0.005" step="0.0001"/></div><br>
</div>
</body>
</html>
<style>
#container {
padding-top: 50px;
position: fixed;
}
canvas {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
}
#parameter_container {
background-color:rgba(20, 20, 20, 0.95);
border-radius: 3%;
padding: 2% 2.5%;
width: calc(10% + 120px);
font-family: 'Sen';
font-size: calc(0.5vw + 11.35px);
font-weight: 700;
color: white;
position: fixed;
display: block;
right: 50px;
transition: all 500ms;
}
.parameter_name {
width: 100%;
padding-bottom: 10px;
}
.parameter {
padding-top: 5px;
}
span {
float: right;
}
input[type="range"] {
-webkit-appearance: none;
appearance: none;
cursor: pointer;
outline: none;
background: rgba(255, 255, 255, 1.0);
height: 3px;
width: calc(100%);
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance:none;
background:rgba(0.0, 0.0, 0.0, 0.0);
height: 15px;
width: 15px;
border: 2px solid #494949;
border-radius: 50%;
}
h1 {
font-family: 'Sen';
font-size: calc(1vw + 25px);
font-weight: 700;
left: 100px;
bottom: 10px;
position: fixed;
color: white;
transition: all 500ms;
}
</style>