-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
403 lines (356 loc) · 8.63 KB
/
sketch.js
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
const percision=10
const viscosity=0.001
let meepw = 512
let meeph = 512
class PingPong {
constructor(init) {
this.ping = createFramebuffer({depth: false, width:meepw, height:meeph, textureFiltering:NEAREST})
this.pong = createFramebuffer({depth: false, width:meepw, height:meeph, textureFiltering:NEAREST})
if (!!init) {
this.ping.loadPixels()
for (let x=0;x<meepw;x+=1)
for (let y=0;y<meeph;y+=1) {
const index = (x+y*meepw)*4
const v = init(x,y)
for (let i=0;i<4;i+=1)
this.ping.pixels[index+i] = v[i]
}
this.ping.updatePixels()
}
}
draw(f) {
this.pong.draw(() => {
f(this.ping)
})
const tmp = this.pong
this.pong = this.ping
this.ping = tmp
}
get texture() {
return this.ping
}
}
const makeShader = (fs, uniforms, width, height) => {
const s = createShader(vs, fs)
const setUniform = (k,v) => s.setUniform(k,v)
return (...args) => {
shader(s)
uniforms(setUniform, ...args)
rect(-width/2, -height/2, width, height)
}
}
let velocity,pressure,div,paint
let advec,jacobi,divergence,subtractGradient,perturb,display
let movers = []
function setup() {
createCanvas(512, 512, WEBGL)
pixelDensity(1)
noStroke()
textureMode(NORMAL)
textureWrap(REPEAT)
meeph = Math.floor(meeph*height/width)
paint = new PingPong()
velocity = new PingPong()
velocity.draw(() => {
fill(127.5)
rect(-meepw/2, -meeph/2, meepw, meeph)
})
paint.draw(() => {
fill(0)
rect(-meepw/2, -meeph/2, meepw, meeph)
})
pressure = new PingPong()
div = createFramebuffer({depth: false, width:meepw, height:meeph, textureFiltering:NEAREST})
advec = makeShader(
advec_fs,
(setUniform, velocity, x) => {
setUniform("velocity",velocity)
setUniform("x",x)
setUniform("dimensions",[meepw,meeph])
}, meepw, meeph
)
jacobi = makeShader(
jacobi_fs,
(setUniform, alpha, beta, ax, b) => {
setUniform("alpha", alpha)
setUniform("beta", beta)
setUniform("ax", ax)
setUniform("b",b)
setUniform("dimensions",[meepw,meeph])
}, meepw, meeph
)
divergence = makeShader(
divergence_fs,
(setUniform, x) => {
setUniform("x", x)
setUniform("dimensions", [meepw, meeph])
}, meepw, meeph
)
subtractGradient = makeShader(
subtractGradient_fs,
(setUniform, x, gradient) => {
setUniform("x", x)
setUniform("gradient", gradient)
setUniform("dimensions", [meepw,meeph])
}, meepw, meeph
)
perturb = makeShader(
perturb_fs,
(setUniform, x, p, r, dir) => {
setUniform("x", x)
setUniform("p", p)
setUniform("dir", dir)
setUniform("r", r)
}, meepw, meeph
)
display = makeShader(
display_fs,
(setUniform, x, p, r) => {
setUniform("x", x)
}, width, height
)
let n = 2
for (let i=0;i<n; i+=1) {
const m = new Mover(width/2,height/2)
m.m = Math.random()
movers.push(m)
}
}
function draw() {
for (let m of movers) {
m.tick()
velocity.draw((velocity) => {
perturb(velocity, [m.position.x/width, m.position.y/height], 0.03, m.direction())
})
paint.draw((paint) => {
perturb(paint, [m.position.x/width, m.position.y/height], 0.03, m.direction())
})
}
if (MAS.speed != 0) {
velocity.draw((velocity) => {
perturb(velocity,
[mouseX/width, mouseY/height],
0.03,
MAS.direction())
})
paint.draw((paint) => {
perturb(paint, [mouseX/width, mouseY/height], 0.03, [-1, -1])
})
}
velocity.draw((velocity) => {
advec(
velocity,
velocity
)
})
for(let i=0;i<1;i+=1)
velocity.draw((velocity) => {
jacobi(
viscosity,
4 + viscosity,
velocity,
velocity
)
})
div.draw(() => {
divergence(velocity.texture)
})
for(let i=0;i<percision;i+=1)
pressure.draw((pressure) => {
jacobi(
-1,
4,
pressure,
div
)
})
velocity.draw((velocity) => {
subtractGradient(
velocity,
pressure.texture
)
})
paint.draw((paint) => {
advec(
velocity.texture,
paint
)
})
for (let i=0; i<10; i++)
paint.draw((paint) => {
jacobi(
viscosity,
4 + viscosity,
paint,
paint
)
})
background(0)
display(paint.texture)
}
const vs=`#version 300 es
precision highp float;
uniform mat4 uModelViewMatrix;
uniform mat4 uProjectionMatrix;
in vec3 aPosition;
in vec2 aTexCoord;
out vec2 pos;
void main() {
pos = aTexCoord;
vec4 positionVec4 = vec4(aPosition, 1.0);
gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
}`
const advec_fs = `#version 300 es
precision highp float;
in vec2 pos;
uniform sampler2D velocity;
uniform sampler2D x;
uniform vec2 dimensions;
out vec4 y;
vec4 rv(sampler2D x, vec2 pos) {
vec4 v = texture(x,pos);
return v*2.0 - vec4(1.0);
}
vec4 cerp(vec4 a,vec4 b,vec4 c,vec4 d,float x) {
float xsq=x*x;
float xcu=xsq*x;
vec4 minV=min(a,min(b,min(c,d)));
vec4 maxV=max(a,max(b,max(c,d)));
vec4 t =
a*(0.0 - 0.5*x + 1.0*xsq - 0.5*xcu) +
b*(1.0 + 0.0*x - 2.5*xsq + 1.5*xcu) +
c*(0.0 + 0.5*x + 2.0*xsq - 1.5*xcu) +
d*(0.0 + 0.0*x - 0.5*xsq + 0.5*xcu);
return min(max(t, minV), maxV);
}
vec4 bicubicSample(sampler2D tex, vec2 pos) {
vec2 xPx = vec2(1.0,0.0)/dimensions;
vec2 yPx = vec2(0.0,1.0)/dimensions;
vec2 f = fract(pos*dimensions);
vec2 topLeft = floor(pos*dimensions)/dimensions;
vec2 toptopLeft = topLeft-xPx-yPx;
vec4 q0 = cerp(
texture(tex,toptopLeft),
texture(tex,toptopLeft+xPx),
texture(tex,toptopLeft+xPx*2.0),
texture(tex,toptopLeft+xPx*3.0),
f.x
);
vec4 q1 = cerp(
texture(tex,toptopLeft+yPx),
texture(tex,toptopLeft+yPx+xPx),
texture(tex,toptopLeft+yPx+xPx*2.0),
texture(tex,toptopLeft+yPx+xPx*3.0),
f.x
);
vec4 q2 = cerp(
texture(tex,toptopLeft+2.0*yPx),
texture(tex,toptopLeft+2.0*yPx+xPx),
texture(tex,toptopLeft+2.0*yPx+xPx*2.0),
texture(tex,toptopLeft+2.0*yPx+xPx*3.0),
f.x
);
vec4 q3 = cerp(
texture(tex,toptopLeft+3.0*yPx),
texture(tex,toptopLeft+3.0*yPx+xPx),
texture(tex,toptopLeft+3.0*yPx+xPx*2.0),
texture(tex,toptopLeft+3.0*yPx+xPx*3.0),
f.x
);
return cerp(q0,q1,q2,q3,f.y);
}
void main() {
vec2 firstV = rv(velocity,pos).xy;
vec2 midP = pos - firstV/2.0;
vec2 midV = rv(velocity,midP).xy;
vec2 lastP = midP - 0.75*midV;
vec2 lastV = rv(velocity,lastP).xy;
vec2 dp = (2.0*firstV+3.0*midV+4.0*lastV)/dimensions/8.0;
y = bicubicSample(x,pos-dp);
}
`
const divergence_fs = `#version 300 es
precision highp float;
in vec2 pos;
uniform sampler2D x;
uniform vec2 dimensions;
out float y;
void main() {
vec2 stepX = vec2(1.0,0)/dimensions;
vec2 stepY = vec2(0.0,1.0)/dimensions;
float e = texture(x,pos+stepX).x;
float w = texture(x,pos-stepX).x;
float n = texture(x,pos+stepY).y;
float s = texture(x,pos-stepY).y;
y = (n-s+e-w)/2.0;
}
`
const jacobi_fs = `#version 300 es
precision highp float;
in vec2 pos;
uniform float alpha;
uniform float beta;
uniform vec2 dimensions;
uniform sampler2D ax;
uniform sampler2D b;
out vec4 y;
void main() {
vec2 pxSize=vec2(1.0,1.0)/dimensions;
vec4 n = texture(ax, pos + vec2(0, pxSize.y));
vec4 s = texture(ax, pos - vec2(0, pxSize.y));
vec4 e = texture(ax, pos + vec2(pxSize.x, 0));
vec4 w = texture(ax, pos - vec2(pxSize.x, 0));
vec4 d = texture(b, pos);
y = (n + s + e + w + alpha * d) / beta;
}
`
const subtractGradient_fs = `#version 300 es
precision highp float;
in vec2 pos;
uniform sampler2D gradient;
uniform sampler2D x;
uniform vec2 dimensions;
out vec4 y;
void main() {
vec2 stepX = vec2(1.0,0)/dimensions;
vec2 stepY = vec2(0.0,1.0)/dimensions;
float e = texture(gradient,pos+stepX).x;
float w = texture(gradient,pos-stepX).x;
float n = texture(gradient,pos+stepY).y;
float s = texture(gradient,pos-stepY).y;
y = texture(x,pos)-vec4(e-w,n-s,0.0,0.0)/2.0;
}
`
const perturb_fs = `#version 300 es
precision highp float;
in vec2 pos;
uniform sampler2D x;
uniform vec2 p;
uniform vec2 dir;
uniform float r;
out vec4 y;
void main() {
float d = distance(p,pos);
vec4 yp = texture(x,pos);
if (d < r) {
float attenuation = (1.0 - (d / r));
vec2 dp = dir * attenuation;
yp.xy += dp;
}
y=yp;
}
`
const display_fs = `#version 300 es
precision highp float;
in vec2 pos;
uniform sampler2D x;
out vec4 y;
void main() {
vec4 yp=texture(x,pos);
// vec3 bg=vec3(237, 142, 145)/255.0;
// vec3 fg=vec3(251, 248, 203)/255.0;
vec3 bg=vec3(255, 255, 255)/255.0;
vec3 fg=vec3(0.0,0.0,0.0)/255.0;
y=vec4(mix(bg,fg,(yp.x+yp.y)/2.0),1.0);
}
`