@idle-screens/savers-classic 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-MIVXP2IL.js +3232 -0
- package/dist/chunk-MIVXP2IL.js.map +1 -0
- package/dist/idle-worker.js +1346 -8
- package/dist/idle-worker.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +9 -1971
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/dist/chunk-5EPTQV3C.js +0 -965
- package/dist/chunk-5EPTQV3C.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
+
fluid,
|
|
3
|
+
flurry,
|
|
2
4
|
globe,
|
|
3
5
|
hardRain,
|
|
6
|
+
mystify,
|
|
7
|
+
pipes,
|
|
4
8
|
rainstorm,
|
|
9
|
+
reactionDiffusion,
|
|
5
10
|
spotlight,
|
|
6
11
|
warp
|
|
7
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-MIVXP2IL.js";
|
|
8
13
|
|
|
9
14
|
// src/toasters.ts
|
|
10
15
|
var toastersManifest = {
|
|
@@ -763,226 +768,6 @@ var messages2 = {
|
|
|
763
768
|
mount: (ctx) => new Messages2Instance(ctx)
|
|
764
769
|
};
|
|
765
770
|
|
|
766
|
-
// src/pipes.ts
|
|
767
|
-
var pipesManifest = {
|
|
768
|
-
id: "pipes",
|
|
769
|
-
label: "Pipes",
|
|
770
|
-
passthrough: false,
|
|
771
|
-
minBackend: "canvas2d",
|
|
772
|
-
costTier: "low",
|
|
773
|
-
motionIntensity: "moderate",
|
|
774
|
-
reducedMotionFallback: "static",
|
|
775
|
-
a11y: { flashSafe: true }
|
|
776
|
-
};
|
|
777
|
-
var CELL = 20;
|
|
778
|
-
var PIPE_WIDTH = 8;
|
|
779
|
-
var STEPS_PER_FRAME = 3;
|
|
780
|
-
var FILL_THRESHOLD = 0.65;
|
|
781
|
-
var PALETTE = ["#c0392b", "#2980b9", "#27ae60", "#f39c12", "#8e44ad", "#d4a259", "#7f8c8d"];
|
|
782
|
-
var DX = { 0: 1, 1: 0, 2: -1, 3: 0 };
|
|
783
|
-
var DY = { 0: 0, 1: 1, 2: 0, 3: -1 };
|
|
784
|
-
var PipesInstance = class {
|
|
785
|
-
ctxSaver;
|
|
786
|
-
canvas;
|
|
787
|
-
ctx;
|
|
788
|
-
w = 0;
|
|
789
|
-
h = 0;
|
|
790
|
-
cols = 0;
|
|
791
|
-
rows = 0;
|
|
792
|
-
grid = [];
|
|
793
|
-
filled = 0;
|
|
794
|
-
pipe = null;
|
|
795
|
-
rng;
|
|
796
|
-
frameId = null;
|
|
797
|
-
paused = false;
|
|
798
|
-
constructor(ctx) {
|
|
799
|
-
this.ctxSaver = ctx;
|
|
800
|
-
this.rng = ctx.rng;
|
|
801
|
-
let canvas;
|
|
802
|
-
if (ctx.surface) {
|
|
803
|
-
canvas = ctx.surface;
|
|
804
|
-
} else {
|
|
805
|
-
const el = document.createElement("canvas");
|
|
806
|
-
el.style.cssText = "display:block;width:100%;height:100%";
|
|
807
|
-
el.setAttribute("aria-hidden", "true");
|
|
808
|
-
ctx.host.appendChild(el);
|
|
809
|
-
canvas = el;
|
|
810
|
-
}
|
|
811
|
-
this.canvas = canvas;
|
|
812
|
-
const c2d = canvas.getContext("2d", { alpha: false });
|
|
813
|
-
if (!c2d) throw new Error("pipes: no 2d context");
|
|
814
|
-
this.ctx = c2d;
|
|
815
|
-
this.w = ctx.width;
|
|
816
|
-
this.h = ctx.height;
|
|
817
|
-
this.sizeCanvas();
|
|
818
|
-
this.rebuild();
|
|
819
|
-
this.paused = ctx.reducedMotion;
|
|
820
|
-
if (this.paused) this.renderStill();
|
|
821
|
-
else this.start();
|
|
822
|
-
}
|
|
823
|
-
sizeCanvas() {
|
|
824
|
-
const dpr = Math.min(this.ctxSaver.dpr, 2);
|
|
825
|
-
this.canvas.width = Math.max(1, Math.round(this.w * dpr));
|
|
826
|
-
this.canvas.height = Math.max(1, Math.round(this.h * dpr));
|
|
827
|
-
this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
828
|
-
}
|
|
829
|
-
rebuild() {
|
|
830
|
-
this.cols = Math.max(2, Math.floor(this.w / CELL));
|
|
831
|
-
this.rows = Math.max(2, Math.floor(this.h / CELL));
|
|
832
|
-
this.grid = new Array(this.cols * this.rows).fill(false);
|
|
833
|
-
this.filled = 0;
|
|
834
|
-
this.pipe = null;
|
|
835
|
-
this.paintBackground();
|
|
836
|
-
}
|
|
837
|
-
paintBackground() {
|
|
838
|
-
this.ctx.fillStyle = "#111";
|
|
839
|
-
this.ctx.fillRect(0, 0, this.w, this.h);
|
|
840
|
-
}
|
|
841
|
-
idx(col, row) {
|
|
842
|
-
return row * this.cols + col;
|
|
843
|
-
}
|
|
844
|
-
inBounds(col, row) {
|
|
845
|
-
return col >= 0 && col < this.cols && row >= 0 && row < this.rows;
|
|
846
|
-
}
|
|
847
|
-
spawnPipe() {
|
|
848
|
-
const empty = [];
|
|
849
|
-
for (let i = 0; i < this.grid.length; i++) {
|
|
850
|
-
if (!this.grid[i]) empty.push(i);
|
|
851
|
-
}
|
|
852
|
-
if (empty.length === 0) {
|
|
853
|
-
this.clearGrid();
|
|
854
|
-
return;
|
|
855
|
-
}
|
|
856
|
-
const cell = this.rng.pick(empty);
|
|
857
|
-
const col = cell % this.cols;
|
|
858
|
-
const row = Math.floor(cell / this.cols);
|
|
859
|
-
this.pipe = {
|
|
860
|
-
col,
|
|
861
|
-
row,
|
|
862
|
-
dir: this.rng.int(0, 3),
|
|
863
|
-
color: this.rng.pick(PALETTE)
|
|
864
|
-
};
|
|
865
|
-
this.grid[cell] = true;
|
|
866
|
-
this.filled++;
|
|
867
|
-
this.drawJoint(col, row, this.pipe.color);
|
|
868
|
-
}
|
|
869
|
-
clearGrid() {
|
|
870
|
-
this.grid.fill(false);
|
|
871
|
-
this.filled = 0;
|
|
872
|
-
this.pipe = null;
|
|
873
|
-
this.paintBackground();
|
|
874
|
-
}
|
|
875
|
-
growStep() {
|
|
876
|
-
if (this.filled / this.grid.length > FILL_THRESHOLD) {
|
|
877
|
-
this.clearGrid();
|
|
878
|
-
return;
|
|
879
|
-
}
|
|
880
|
-
if (!this.pipe) {
|
|
881
|
-
this.spawnPipe();
|
|
882
|
-
return;
|
|
883
|
-
}
|
|
884
|
-
const candidates = this.getCandidates(this.pipe);
|
|
885
|
-
if (candidates.length === 0) {
|
|
886
|
-
this.pipe = null;
|
|
887
|
-
return;
|
|
888
|
-
}
|
|
889
|
-
const oldCol = this.pipe.col;
|
|
890
|
-
const oldRow = this.pipe.row;
|
|
891
|
-
const oldDir = this.pipe.dir;
|
|
892
|
-
const newDir = this.rng.pick(candidates);
|
|
893
|
-
this.pipe.dir = newDir;
|
|
894
|
-
const nc = oldCol + DX[newDir];
|
|
895
|
-
const nr = oldRow + DY[newDir];
|
|
896
|
-
this.drawSegment(oldCol, oldRow, nc, nr, this.pipe.color);
|
|
897
|
-
if (newDir !== oldDir) {
|
|
898
|
-
this.drawJoint(oldCol, oldRow, this.pipe.color);
|
|
899
|
-
}
|
|
900
|
-
this.pipe.col = nc;
|
|
901
|
-
this.pipe.row = nr;
|
|
902
|
-
this.grid[this.idx(nc, nr)] = true;
|
|
903
|
-
this.filled++;
|
|
904
|
-
}
|
|
905
|
-
getCandidates(pipe) {
|
|
906
|
-
const out = [];
|
|
907
|
-
for (let d = 0; d < 4; d++) {
|
|
908
|
-
const nc = pipe.col + DX[d];
|
|
909
|
-
const nr = pipe.row + DY[d];
|
|
910
|
-
if (this.inBounds(nc, nr) && !this.grid[this.idx(nc, nr)]) {
|
|
911
|
-
out.push(d);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
if (out.includes(pipe.dir) && this.rng.next() < 0.65) {
|
|
915
|
-
return [pipe.dir];
|
|
916
|
-
}
|
|
917
|
-
return out;
|
|
918
|
-
}
|
|
919
|
-
cellCenter(col, row) {
|
|
920
|
-
return [col * CELL + CELL / 2, row * CELL + CELL / 2];
|
|
921
|
-
}
|
|
922
|
-
drawSegment(c1, r1, c2, r2, color) {
|
|
923
|
-
const [x1, y1] = this.cellCenter(c1, r1);
|
|
924
|
-
const [x2, y2] = this.cellCenter(c2, r2);
|
|
925
|
-
const ctx = this.ctx;
|
|
926
|
-
ctx.strokeStyle = color;
|
|
927
|
-
ctx.lineWidth = PIPE_WIDTH;
|
|
928
|
-
ctx.lineCap = "round";
|
|
929
|
-
ctx.beginPath();
|
|
930
|
-
ctx.moveTo(x1, y1);
|
|
931
|
-
ctx.lineTo(x2, y2);
|
|
932
|
-
ctx.stroke();
|
|
933
|
-
}
|
|
934
|
-
drawJoint(col, row, color) {
|
|
935
|
-
const [x, y] = this.cellCenter(col, row);
|
|
936
|
-
const ctx = this.ctx;
|
|
937
|
-
ctx.fillStyle = color;
|
|
938
|
-
ctx.beginPath();
|
|
939
|
-
ctx.arc(x, y, PIPE_WIDTH * 0.65, 0, Math.PI * 2);
|
|
940
|
-
ctx.fill();
|
|
941
|
-
}
|
|
942
|
-
start() {
|
|
943
|
-
if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
|
|
944
|
-
this.loop();
|
|
945
|
-
}
|
|
946
|
-
stop() {
|
|
947
|
-
if (this.frameId !== null) {
|
|
948
|
-
cancelAnimationFrame(this.frameId);
|
|
949
|
-
this.frameId = null;
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
loop() {
|
|
953
|
-
this.frameId = requestAnimationFrame(() => this.loop());
|
|
954
|
-
for (let i = 0; i < STEPS_PER_FRAME; i++) this.growStep();
|
|
955
|
-
}
|
|
956
|
-
renderStill() {
|
|
957
|
-
this.rebuild();
|
|
958
|
-
for (let i = 0; i < 80; i++) this.growStep();
|
|
959
|
-
}
|
|
960
|
-
setPaused(paused) {
|
|
961
|
-
this.paused = paused;
|
|
962
|
-
if (paused) {
|
|
963
|
-
this.stop();
|
|
964
|
-
} else {
|
|
965
|
-
this.start();
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
resize(width, height, dpr) {
|
|
969
|
-
this.w = width;
|
|
970
|
-
this.h = height;
|
|
971
|
-
if (dpr !== void 0) this.ctxSaver.dpr = dpr;
|
|
972
|
-
this.sizeCanvas();
|
|
973
|
-
this.rebuild();
|
|
974
|
-
if (this.paused) this.renderStill();
|
|
975
|
-
}
|
|
976
|
-
dispose() {
|
|
977
|
-
this.stop();
|
|
978
|
-
if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
|
|
979
|
-
}
|
|
980
|
-
};
|
|
981
|
-
var pipes = {
|
|
982
|
-
manifest: pipesManifest,
|
|
983
|
-
mount: (ctx) => new PipesInstance(ctx)
|
|
984
|
-
};
|
|
985
|
-
|
|
986
771
|
// src/bsod.ts
|
|
987
772
|
var bsodManifest = {
|
|
988
773
|
id: "bsod",
|
|
@@ -1287,1755 +1072,6 @@ var bsod = {
|
|
|
1287
1072
|
mount: (ctx) => new BsodInstance(ctx)
|
|
1288
1073
|
};
|
|
1289
1074
|
|
|
1290
|
-
// src/flurry.ts
|
|
1291
|
-
var flurryManifest = {
|
|
1292
|
-
id: "flurry",
|
|
1293
|
-
label: "Flurry",
|
|
1294
|
-
passthrough: false,
|
|
1295
|
-
minBackend: "canvas2d",
|
|
1296
|
-
costTier: "low",
|
|
1297
|
-
motionIntensity: "calm",
|
|
1298
|
-
reducedMotionFallback: "static",
|
|
1299
|
-
a11y: { flashSafe: true }
|
|
1300
|
-
};
|
|
1301
|
-
var STREAM_COUNT = 5;
|
|
1302
|
-
var PARTICLES_PER_STREAM = 12;
|
|
1303
|
-
var TRAIL_ALPHA = 0.06;
|
|
1304
|
-
var BASE_RADIUS = 6;
|
|
1305
|
-
var FlurryInstance = class {
|
|
1306
|
-
ctxSaver;
|
|
1307
|
-
canvas;
|
|
1308
|
-
ctx;
|
|
1309
|
-
w = 0;
|
|
1310
|
-
h = 0;
|
|
1311
|
-
streams;
|
|
1312
|
-
frameId = null;
|
|
1313
|
-
t = 0;
|
|
1314
|
-
lastTime = 0;
|
|
1315
|
-
paused = false;
|
|
1316
|
-
constructor(ctx) {
|
|
1317
|
-
this.ctxSaver = ctx;
|
|
1318
|
-
this.w = ctx.width;
|
|
1319
|
-
this.h = ctx.height;
|
|
1320
|
-
let canvas;
|
|
1321
|
-
if (ctx.surface) {
|
|
1322
|
-
canvas = ctx.surface;
|
|
1323
|
-
} else {
|
|
1324
|
-
const el = document.createElement("canvas");
|
|
1325
|
-
el.style.cssText = "display:block;width:100%;height:100%";
|
|
1326
|
-
el.setAttribute("aria-hidden", "true");
|
|
1327
|
-
ctx.host.appendChild(el);
|
|
1328
|
-
canvas = el;
|
|
1329
|
-
}
|
|
1330
|
-
this.canvas = canvas;
|
|
1331
|
-
const c2d = canvas.getContext("2d", { alpha: false });
|
|
1332
|
-
if (!c2d) throw new Error("flurry: no 2d context");
|
|
1333
|
-
this.ctx = c2d;
|
|
1334
|
-
this.streams = this.buildStreams(ctx.rng);
|
|
1335
|
-
this.sizeCanvas();
|
|
1336
|
-
this.clearFull();
|
|
1337
|
-
this.paused = ctx.reducedMotion;
|
|
1338
|
-
if (this.paused) {
|
|
1339
|
-
this.renderStill();
|
|
1340
|
-
} else {
|
|
1341
|
-
this.start();
|
|
1342
|
-
}
|
|
1343
|
-
}
|
|
1344
|
-
buildStreams(rng) {
|
|
1345
|
-
const out = [];
|
|
1346
|
-
for (let i = 0; i < STREAM_COUNT; i++) {
|
|
1347
|
-
out.push({
|
|
1348
|
-
hue: (i * 360 / STREAM_COUNT + rng.range(0, 30)) % 360,
|
|
1349
|
-
freqX1: rng.range(0.3, 0.9),
|
|
1350
|
-
freqY1: rng.range(0.3, 0.9),
|
|
1351
|
-
freqX2: rng.range(0.1, 0.4),
|
|
1352
|
-
freqY2: rng.range(0.1, 0.4),
|
|
1353
|
-
phaseX: rng.range(0, Math.PI * 2),
|
|
1354
|
-
phaseY: rng.range(0, Math.PI * 2),
|
|
1355
|
-
ampX: rng.range(0.2, 0.4),
|
|
1356
|
-
ampY: rng.range(0.2, 0.4),
|
|
1357
|
-
speed: rng.range(0.6, 1.2)
|
|
1358
|
-
});
|
|
1359
|
-
}
|
|
1360
|
-
return out;
|
|
1361
|
-
}
|
|
1362
|
-
sizeCanvas() {
|
|
1363
|
-
const dpr = Math.min(this.ctxSaver.dpr, 2);
|
|
1364
|
-
this.canvas.width = Math.max(1, Math.round(this.w * dpr));
|
|
1365
|
-
this.canvas.height = Math.max(1, Math.round(this.h * dpr));
|
|
1366
|
-
this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
1367
|
-
}
|
|
1368
|
-
clearFull() {
|
|
1369
|
-
this.ctx.fillStyle = "#000";
|
|
1370
|
-
this.ctx.fillRect(0, 0, this.w, this.h);
|
|
1371
|
-
}
|
|
1372
|
-
streamPos(s, t) {
|
|
1373
|
-
const cx = this.w / 2;
|
|
1374
|
-
const cy = this.h / 2;
|
|
1375
|
-
const x = cx + Math.sin(t * s.freqX1 + s.phaseX) * this.w * s.ampX + Math.sin(t * s.freqX2 + s.phaseX * 1.7) * this.w * s.ampX * 0.3;
|
|
1376
|
-
const y = cy + Math.cos(t * s.freqY1 + s.phaseY) * this.h * s.ampY + Math.cos(t * s.freqY2 + s.phaseY * 2.1) * this.h * s.ampY * 0.3;
|
|
1377
|
-
return [x, y];
|
|
1378
|
-
}
|
|
1379
|
-
drawFrame(dt) {
|
|
1380
|
-
const ctx = this.ctx;
|
|
1381
|
-
ctx.globalCompositeOperation = "source-over";
|
|
1382
|
-
ctx.fillStyle = `rgba(0,0,0,${TRAIL_ALPHA})`;
|
|
1383
|
-
ctx.fillRect(0, 0, this.w, this.h);
|
|
1384
|
-
ctx.globalCompositeOperation = "lighter";
|
|
1385
|
-
for (const s of this.streams) {
|
|
1386
|
-
const st = this.t * s.speed;
|
|
1387
|
-
for (let p = 0; p < PARTICLES_PER_STREAM; p++) {
|
|
1388
|
-
const offset = p * 0.012;
|
|
1389
|
-
const [x, y] = this.streamPos(s, st - offset);
|
|
1390
|
-
const hue = (s.hue + this.t * 8 + p * 3) % 360;
|
|
1391
|
-
const alpha = 0.15 + 0.1 * (1 - p / PARTICLES_PER_STREAM);
|
|
1392
|
-
const r = BASE_RADIUS * (1 - p * 0.04);
|
|
1393
|
-
const grad = ctx.createRadialGradient(x, y, 0, x, y, r * 3);
|
|
1394
|
-
grad.addColorStop(0, `hsla(${hue},90%,60%,${alpha})`);
|
|
1395
|
-
grad.addColorStop(0.4, `hsla(${hue},80%,50%,${alpha * 0.5})`);
|
|
1396
|
-
grad.addColorStop(1, `hsla(${hue},70%,40%,0)`);
|
|
1397
|
-
ctx.fillStyle = grad;
|
|
1398
|
-
ctx.beginPath();
|
|
1399
|
-
ctx.arc(x, y, r * 3, 0, Math.PI * 2);
|
|
1400
|
-
ctx.fill();
|
|
1401
|
-
}
|
|
1402
|
-
}
|
|
1403
|
-
ctx.globalCompositeOperation = "source-over";
|
|
1404
|
-
}
|
|
1405
|
-
start() {
|
|
1406
|
-
if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
|
|
1407
|
-
this.lastTime = 0;
|
|
1408
|
-
this.loop(0);
|
|
1409
|
-
}
|
|
1410
|
-
stop() {
|
|
1411
|
-
if (this.frameId !== null) {
|
|
1412
|
-
cancelAnimationFrame(this.frameId);
|
|
1413
|
-
this.frameId = null;
|
|
1414
|
-
}
|
|
1415
|
-
}
|
|
1416
|
-
loop(now) {
|
|
1417
|
-
this.frameId = requestAnimationFrame((n) => this.loop(n));
|
|
1418
|
-
const dt = this.lastTime ? (now - this.lastTime) / 1e3 : 1 / 60;
|
|
1419
|
-
this.lastTime = now;
|
|
1420
|
-
this.t += Math.min(dt, 0.1);
|
|
1421
|
-
this.drawFrame(dt);
|
|
1422
|
-
}
|
|
1423
|
-
renderStill() {
|
|
1424
|
-
this.clearFull();
|
|
1425
|
-
for (let i = 0; i < 120; i++) {
|
|
1426
|
-
this.t += 1 / 60;
|
|
1427
|
-
this.drawFrame(1 / 60);
|
|
1428
|
-
}
|
|
1429
|
-
}
|
|
1430
|
-
setPaused(paused) {
|
|
1431
|
-
this.paused = paused;
|
|
1432
|
-
if (paused) {
|
|
1433
|
-
this.stop();
|
|
1434
|
-
} else {
|
|
1435
|
-
this.start();
|
|
1436
|
-
}
|
|
1437
|
-
}
|
|
1438
|
-
resize(width, height, dpr) {
|
|
1439
|
-
this.w = width;
|
|
1440
|
-
this.h = height;
|
|
1441
|
-
if (dpr !== void 0) this.ctxSaver.dpr = dpr;
|
|
1442
|
-
this.sizeCanvas();
|
|
1443
|
-
this.clearFull();
|
|
1444
|
-
if (this.paused) this.renderStill();
|
|
1445
|
-
}
|
|
1446
|
-
dispose() {
|
|
1447
|
-
this.stop();
|
|
1448
|
-
if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
|
|
1449
|
-
}
|
|
1450
|
-
};
|
|
1451
|
-
var flurry = {
|
|
1452
|
-
manifest: flurryManifest,
|
|
1453
|
-
mount: (ctx) => new FlurryInstance(ctx)
|
|
1454
|
-
};
|
|
1455
|
-
|
|
1456
|
-
// src/fluid-shared.ts
|
|
1457
|
-
var DT = 0.025;
|
|
1458
|
-
var DENS_DECAY = 0.993;
|
|
1459
|
-
var VEL_DECAY = 0.98;
|
|
1460
|
-
var DYE_RATE = 1400;
|
|
1461
|
-
var FORCE_RATE = 5;
|
|
1462
|
-
var EMITTER_N = 4;
|
|
1463
|
-
function hue2rgb(h) {
|
|
1464
|
-
h = (h % 360 + 360) % 360;
|
|
1465
|
-
const s = h / 60;
|
|
1466
|
-
const x = 1 - Math.abs(s % 2 - 1);
|
|
1467
|
-
if (s < 1) return [1, x, 0];
|
|
1468
|
-
if (s < 2) return [x, 1, 0];
|
|
1469
|
-
if (s < 3) return [0, 1, x];
|
|
1470
|
-
if (s < 4) return [0, x, 1];
|
|
1471
|
-
if (s < 5) return [x, 0, 1];
|
|
1472
|
-
return [1, 0, x];
|
|
1473
|
-
}
|
|
1474
|
-
function buildEmitters(rng, N3) {
|
|
1475
|
-
const out = [];
|
|
1476
|
-
for (let i = 0; i < EMITTER_N; i++) {
|
|
1477
|
-
const px = rng.range(0, Math.PI * 2);
|
|
1478
|
-
const py = rng.range(0, Math.PI * 2);
|
|
1479
|
-
out.push({
|
|
1480
|
-
hue: (i * 360 / EMITTER_N + rng.range(0, 40)) % 360,
|
|
1481
|
-
fx: rng.range(0.3, 0.8),
|
|
1482
|
-
fy: rng.range(0.3, 0.8),
|
|
1483
|
-
px,
|
|
1484
|
-
py,
|
|
1485
|
-
speed: rng.range(0.5, 1),
|
|
1486
|
-
prevGx: (Math.sin(px) * 0.35 + 0.5) * N3 + 1,
|
|
1487
|
-
prevGy: (Math.cos(py) * 0.35 + 0.5) * N3 + 1
|
|
1488
|
-
});
|
|
1489
|
-
}
|
|
1490
|
-
return out;
|
|
1491
|
-
}
|
|
1492
|
-
function stepEmitters(t, emitters, N3) {
|
|
1493
|
-
return emitters.map((e) => {
|
|
1494
|
-
const st = t * e.speed;
|
|
1495
|
-
const gx = (Math.sin(st * e.fx + e.px) * 0.35 + 0.5) * N3 + 1;
|
|
1496
|
-
const gy = (Math.cos(st * e.fy + e.py) * 0.35 + 0.5) * N3 + 1;
|
|
1497
|
-
const dx = gx - e.prevGx;
|
|
1498
|
-
const dy = gy - e.prevGy;
|
|
1499
|
-
const [r, g, b] = hue2rgb(e.hue + t * 15);
|
|
1500
|
-
e.prevGx = gx;
|
|
1501
|
-
e.prevGy = gy;
|
|
1502
|
-
return { gx, gy, dx, dy, r, g, b };
|
|
1503
|
-
});
|
|
1504
|
-
}
|
|
1505
|
-
|
|
1506
|
-
// src/fluid-gpu.ts
|
|
1507
|
-
var GPU_N = 256;
|
|
1508
|
-
var GPU_STRIDE = GPU_N + 2;
|
|
1509
|
-
var GPU_SZ = GPU_STRIDE * GPU_STRIDE;
|
|
1510
|
-
var GPU_DIFF = 3e-5 * (96 / GPU_N) ** 2;
|
|
1511
|
-
var GPU_VISC = 3e-4 * (96 / GPU_N) ** 2;
|
|
1512
|
-
var GPU_INJECT_R = Math.round(4 * (GPU_N / 96));
|
|
1513
|
-
var GPU_FORCE_RATE = FORCE_RATE * (96 / GPU_N);
|
|
1514
|
-
var JACOBI_DIFF = 10;
|
|
1515
|
-
var JACOBI_PRES = 40;
|
|
1516
|
-
var COPY_WGSL = (
|
|
1517
|
-
/* wgsl */
|
|
1518
|
-
`
|
|
1519
|
-
override SZ: u32;
|
|
1520
|
-
@group(0) @binding(0) var<storage, read_write> dst: array<f32>;
|
|
1521
|
-
@group(0) @binding(1) var<storage, read> src: array<f32>;
|
|
1522
|
-
@compute @workgroup_size(256)
|
|
1523
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1524
|
-
if (gid.x >= SZ) { return; }
|
|
1525
|
-
dst[gid.x] = src[gid.x];
|
|
1526
|
-
}`
|
|
1527
|
-
);
|
|
1528
|
-
var ADD_SOURCE_WGSL = (
|
|
1529
|
-
/* wgsl */
|
|
1530
|
-
`
|
|
1531
|
-
override SZ: u32;
|
|
1532
|
-
struct P { dt: f32 }
|
|
1533
|
-
@group(0) @binding(0) var<uniform> p: P;
|
|
1534
|
-
@group(0) @binding(1) var<storage, read_write> dst: array<f32>;
|
|
1535
|
-
@group(0) @binding(2) var<storage, read> src: array<f32>;
|
|
1536
|
-
@compute @workgroup_size(256)
|
|
1537
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1538
|
-
if (gid.x >= SZ) { return; }
|
|
1539
|
-
dst[gid.x] += p.dt * src[gid.x];
|
|
1540
|
-
}`
|
|
1541
|
-
);
|
|
1542
|
-
var SCALE_WGSL = (
|
|
1543
|
-
/* wgsl */
|
|
1544
|
-
`
|
|
1545
|
-
override SZ: u32;
|
|
1546
|
-
struct P { factor: f32 }
|
|
1547
|
-
@group(0) @binding(0) var<uniform> p: P;
|
|
1548
|
-
@group(0) @binding(1) var<storage, read_write> field: array<f32>;
|
|
1549
|
-
@compute @workgroup_size(256)
|
|
1550
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1551
|
-
if (gid.x >= SZ) { return; }
|
|
1552
|
-
field[gid.x] *= p.factor;
|
|
1553
|
-
}`
|
|
1554
|
-
);
|
|
1555
|
-
var JACOBI_WGSL = (
|
|
1556
|
-
/* wgsl */
|
|
1557
|
-
`
|
|
1558
|
-
override N: u32;
|
|
1559
|
-
override STRIDE: u32;
|
|
1560
|
-
struct P { a: f32, c: f32 }
|
|
1561
|
-
@group(0) @binding(0) var<uniform> p: P;
|
|
1562
|
-
@group(0) @binding(1) var<storage, read_write> dst: array<f32>;
|
|
1563
|
-
@group(0) @binding(2) var<storage, read> x0: array<f32>;
|
|
1564
|
-
@group(0) @binding(3) var<storage, read> prev: array<f32>;
|
|
1565
|
-
@compute @workgroup_size(8, 8)
|
|
1566
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1567
|
-
let i = gid.x + 1u;
|
|
1568
|
-
let j = gid.y + 1u;
|
|
1569
|
-
if (i > N || j > N) { return; }
|
|
1570
|
-
let idx = i + STRIDE * j;
|
|
1571
|
-
dst[idx] = (x0[idx] + p.a * (prev[idx - 1u] + prev[idx + 1u]
|
|
1572
|
-
+ prev[idx - STRIDE] + prev[idx + STRIDE])) / p.c;
|
|
1573
|
-
}`
|
|
1574
|
-
);
|
|
1575
|
-
var BOUNDARY_WGSL = (
|
|
1576
|
-
/* wgsl */
|
|
1577
|
-
`
|
|
1578
|
-
override N: u32;
|
|
1579
|
-
override STRIDE: u32;
|
|
1580
|
-
struct P { b: u32 }
|
|
1581
|
-
@group(0) @binding(0) var<uniform> p: P;
|
|
1582
|
-
@group(0) @binding(1) var<storage, read_write> x: array<f32>;
|
|
1583
|
-
@compute @workgroup_size(64)
|
|
1584
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1585
|
-
let i = gid.x + 1u;
|
|
1586
|
-
if (i > N) { return; }
|
|
1587
|
-
let s = STRIDE;
|
|
1588
|
-
let sl = select(1.0, -1.0, p.b == 1u);
|
|
1589
|
-
let st = select(1.0, -1.0, p.b == 2u);
|
|
1590
|
-
x[s * i] = sl * x[1u + s * i];
|
|
1591
|
-
x[(N + 1u) + s * i] = sl * x[N + s * i];
|
|
1592
|
-
x[i] = st * x[i + s];
|
|
1593
|
-
x[i + s * (N + 1u)] = st * x[i + s * N];
|
|
1594
|
-
if (i == 1u) {
|
|
1595
|
-
x[0u] = x[1u + s];
|
|
1596
|
-
x[s * (N + 1u)] = x[1u + s * N];
|
|
1597
|
-
x[N + 1u] = x[N + s];
|
|
1598
|
-
x[(N + 1u) + s * (N + 1u)] = x[N + s * N];
|
|
1599
|
-
}
|
|
1600
|
-
}`
|
|
1601
|
-
);
|
|
1602
|
-
var ADVECT_WGSL = (
|
|
1603
|
-
/* wgsl */
|
|
1604
|
-
`
|
|
1605
|
-
override N: u32;
|
|
1606
|
-
override STRIDE: u32;
|
|
1607
|
-
struct P { dt0: f32 }
|
|
1608
|
-
@group(0) @binding(0) var<uniform> p: P;
|
|
1609
|
-
@group(0) @binding(1) var<storage, read_write> d: array<f32>;
|
|
1610
|
-
@group(0) @binding(2) var<storage, read> d0: array<f32>;
|
|
1611
|
-
@group(0) @binding(3) var<storage, read> u: array<f32>;
|
|
1612
|
-
@group(0) @binding(4) var<storage, read> v: array<f32>;
|
|
1613
|
-
@compute @workgroup_size(8, 8)
|
|
1614
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1615
|
-
let i = gid.x + 1u;
|
|
1616
|
-
let j = gid.y + 1u;
|
|
1617
|
-
if (i > N || j > N) { return; }
|
|
1618
|
-
let idx = i + STRIDE * j;
|
|
1619
|
-
var bx = f32(i) - p.dt0 * u[idx];
|
|
1620
|
-
var by = f32(j) - p.dt0 * v[idx];
|
|
1621
|
-
bx = clamp(bx, 0.5, f32(N) + 0.5);
|
|
1622
|
-
by = clamp(by, 0.5, f32(N) + 0.5);
|
|
1623
|
-
let i0 = u32(floor(bx));
|
|
1624
|
-
let j0 = u32(floor(by));
|
|
1625
|
-
let s1 = bx - f32(i0);
|
|
1626
|
-
let s0 = 1.0 - s1;
|
|
1627
|
-
let t1 = by - f32(j0);
|
|
1628
|
-
let t0 = 1.0 - t1;
|
|
1629
|
-
let s = STRIDE;
|
|
1630
|
-
d[idx] = s0 * (t0 * d0[i0 + s * j0] + t1 * d0[i0 + s * (j0 + 1u)])
|
|
1631
|
-
+ s1 * (t0 * d0[(i0 + 1u) + s * j0] + t1 * d0[(i0 + 1u) + s * (j0 + 1u)]);
|
|
1632
|
-
}`
|
|
1633
|
-
);
|
|
1634
|
-
var DIVERGENCE_WGSL = (
|
|
1635
|
-
/* wgsl */
|
|
1636
|
-
`
|
|
1637
|
-
override N: u32;
|
|
1638
|
-
override STRIDE: u32;
|
|
1639
|
-
@group(0) @binding(0) var<storage, read_write> div: array<f32>;
|
|
1640
|
-
@group(0) @binding(1) var<storage, read> u: array<f32>;
|
|
1641
|
-
@group(0) @binding(2) var<storage, read> v: array<f32>;
|
|
1642
|
-
@compute @workgroup_size(8, 8)
|
|
1643
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1644
|
-
let i = gid.x + 1u;
|
|
1645
|
-
let j = gid.y + 1u;
|
|
1646
|
-
if (i > N || j > N) { return; }
|
|
1647
|
-
let idx = i + STRIDE * j;
|
|
1648
|
-
let h = 1.0 / f32(N);
|
|
1649
|
-
div[idx] = -0.5 * h * (u[idx + 1u] - u[idx - 1u] + v[idx + STRIDE] - v[idx - STRIDE]);
|
|
1650
|
-
}`
|
|
1651
|
-
);
|
|
1652
|
-
var GRADIENT_SUB_WGSL = (
|
|
1653
|
-
/* wgsl */
|
|
1654
|
-
`
|
|
1655
|
-
override N: u32;
|
|
1656
|
-
override STRIDE: u32;
|
|
1657
|
-
@group(0) @binding(0) var<storage, read_write> u: array<f32>;
|
|
1658
|
-
@group(0) @binding(1) var<storage, read_write> v: array<f32>;
|
|
1659
|
-
@group(0) @binding(2) var<storage, read> p: array<f32>;
|
|
1660
|
-
@compute @workgroup_size(8, 8)
|
|
1661
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1662
|
-
let i = gid.x + 1u;
|
|
1663
|
-
let j = gid.y + 1u;
|
|
1664
|
-
if (i > N || j > N) { return; }
|
|
1665
|
-
let idx = i + STRIDE * j;
|
|
1666
|
-
u[idx] -= 0.5 * f32(N) * (p[idx + 1u] - p[idx - 1u]);
|
|
1667
|
-
v[idx] -= 0.5 * f32(N) * (p[idx + STRIDE] - p[idx - STRIDE]);
|
|
1668
|
-
}`
|
|
1669
|
-
);
|
|
1670
|
-
var INJECT_WGSL = (
|
|
1671
|
-
/* wgsl */
|
|
1672
|
-
`
|
|
1673
|
-
override N: u32;
|
|
1674
|
-
override STRIDE: u32;
|
|
1675
|
-
struct EmitterData { pos: vec4f, col: vec4f }
|
|
1676
|
-
struct IP {
|
|
1677
|
-
inject_r: f32, dye_rate: f32, force_rate: f32, count: u32,
|
|
1678
|
-
data: array<EmitterData, 4>,
|
|
1679
|
-
}
|
|
1680
|
-
@group(0) @binding(0) var<uniform> p: IP;
|
|
1681
|
-
@group(0) @binding(1) var<storage, read_write> u0: array<f32>;
|
|
1682
|
-
@group(0) @binding(2) var<storage, read_write> v0: array<f32>;
|
|
1683
|
-
@group(0) @binding(3) var<storage, read_write> dr0: array<f32>;
|
|
1684
|
-
@group(0) @binding(4) var<storage, read_write> dg0: array<f32>;
|
|
1685
|
-
@group(0) @binding(5) var<storage, read_write> db0: array<f32>;
|
|
1686
|
-
@compute @workgroup_size(8, 8)
|
|
1687
|
-
fn main(@builtin(global_invocation_id) gid: vec3u) {
|
|
1688
|
-
let i = gid.x;
|
|
1689
|
-
let j = gid.y;
|
|
1690
|
-
if (i >= STRIDE || j >= STRIDE) { return; }
|
|
1691
|
-
let idx = i + STRIDE * j;
|
|
1692
|
-
u0[idx] = 0.0; v0[idx] = 0.0;
|
|
1693
|
-
dr0[idx] = 0.0; dg0[idx] = 0.0; db0[idx] = 0.0;
|
|
1694
|
-
if (i < 1u || i > N || j < 1u || j > N) { return; }
|
|
1695
|
-
let fi = f32(i);
|
|
1696
|
-
let fj = f32(j);
|
|
1697
|
-
for (var e = 0u; e < p.count; e = e + 1u) {
|
|
1698
|
-
let em = p.data[e];
|
|
1699
|
-
let dx = fi - em.pos.x;
|
|
1700
|
-
let dy = fj - em.pos.y;
|
|
1701
|
-
let dist = sqrt(dx * dx + dy * dy);
|
|
1702
|
-
if (dist > p.inject_r) { continue; }
|
|
1703
|
-
let w = 1.0 - dist / p.inject_r;
|
|
1704
|
-
dr0[idx] += em.col.x * p.dye_rate * w;
|
|
1705
|
-
dg0[idx] += em.col.y * p.dye_rate * w;
|
|
1706
|
-
db0[idx] += em.col.z * p.dye_rate * w;
|
|
1707
|
-
u0[idx] += em.pos.z * p.force_rate * w;
|
|
1708
|
-
v0[idx] += em.pos.w * p.force_rate * w;
|
|
1709
|
-
}
|
|
1710
|
-
}`
|
|
1711
|
-
);
|
|
1712
|
-
var RENDER_WGSL = (
|
|
1713
|
-
/* wgsl */
|
|
1714
|
-
`
|
|
1715
|
-
override N: u32;
|
|
1716
|
-
override STRIDE: u32;
|
|
1717
|
-
struct VO { @builtin(position) pos: vec4f, @location(0) uv: vec2f }
|
|
1718
|
-
@vertex fn vs(@builtin(vertex_index) vi: u32) -> VO {
|
|
1719
|
-
var o: VO;
|
|
1720
|
-
let x = f32(i32(vi) / 2) * 4.0 - 1.0;
|
|
1721
|
-
let y = f32(i32(vi) % 2) * 4.0 - 1.0;
|
|
1722
|
-
o.pos = vec4f(x, y, 0.0, 1.0);
|
|
1723
|
-
o.uv = vec2f((x + 1.0) * 0.5, (1.0 - y) * 0.5);
|
|
1724
|
-
return o;
|
|
1725
|
-
}
|
|
1726
|
-
@group(0) @binding(0) var<storage, read> dr: array<f32>;
|
|
1727
|
-
@group(0) @binding(1) var<storage, read> dg: array<f32>;
|
|
1728
|
-
@group(0) @binding(2) var<storage, read> db: array<f32>;
|
|
1729
|
-
@fragment fn fs(@location(0) uv: vec2f) -> @location(0) vec4f {
|
|
1730
|
-
let gx = clamp(uv.x * f32(N) + 0.5, 1.0, f32(N));
|
|
1731
|
-
let gy = clamp(uv.y * f32(N) + 0.5, 1.0, f32(N));
|
|
1732
|
-
let i0 = u32(floor(gx));
|
|
1733
|
-
let j0 = u32(floor(gy));
|
|
1734
|
-
let s1 = gx - f32(i0);
|
|
1735
|
-
let s0 = 1.0 - s1;
|
|
1736
|
-
let t1 = gy - f32(j0);
|
|
1737
|
-
let t0 = 1.0 - t1;
|
|
1738
|
-
let s = STRIDE;
|
|
1739
|
-
let a00 = i0 + s * j0;
|
|
1740
|
-
let a01 = i0 + s * (j0 + 1u);
|
|
1741
|
-
let a10 = (i0 + 1u) + s * j0;
|
|
1742
|
-
let a11 = (i0 + 1u) + s * (j0 + 1u);
|
|
1743
|
-
let r = s0 * (t0 * dr[a00] + t1 * dr[a01]) + s1 * (t0 * dr[a10] + t1 * dr[a11]);
|
|
1744
|
-
let g = s0 * (t0 * dg[a00] + t1 * dg[a01]) + s1 * (t0 * dg[a10] + t1 * dg[a11]);
|
|
1745
|
-
let b = s0 * (t0 * db[a00] + t1 * db[a01]) + s1 * (t0 * db[a10] + t1 * db[a11]);
|
|
1746
|
-
return vec4f(clamp(r / 255.0, 0.0, 1.0), clamp(g / 255.0, 0.0, 1.0),
|
|
1747
|
-
clamp(b / 255.0, 0.0, 1.0), 1.0);
|
|
1748
|
-
}`
|
|
1749
|
-
);
|
|
1750
|
-
var WG1D = Math.ceil(GPU_SZ / 256);
|
|
1751
|
-
var WG2D = Math.ceil(GPU_N / 8);
|
|
1752
|
-
var WG_BND = Math.ceil(GPU_N / 64);
|
|
1753
|
-
var WG_INJ = Math.ceil(GPU_STRIDE / 8);
|
|
1754
|
-
function mkUniform(dev, data) {
|
|
1755
|
-
const size = Math.ceil(Math.max(data.byteLength, 16) / 16) * 16;
|
|
1756
|
-
const buf = dev.createBuffer({
|
|
1757
|
-
size,
|
|
1758
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
1759
|
-
});
|
|
1760
|
-
dev.queue.writeBuffer(buf, 0, data.buffer, data.byteOffset, data.byteLength);
|
|
1761
|
-
return buf;
|
|
1762
|
-
}
|
|
1763
|
-
function mkStorage(dev) {
|
|
1764
|
-
return dev.createBuffer({
|
|
1765
|
-
size: GPU_SZ * 4,
|
|
1766
|
-
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
1767
|
-
});
|
|
1768
|
-
}
|
|
1769
|
-
function mkPipeline(dev, code, constants) {
|
|
1770
|
-
return dev.createComputePipeline({
|
|
1771
|
-
layout: "auto",
|
|
1772
|
-
compute: { module: dev.createShaderModule({ code }), constants }
|
|
1773
|
-
});
|
|
1774
|
-
}
|
|
1775
|
-
var FIELDS = [
|
|
1776
|
-
"u",
|
|
1777
|
-
"v",
|
|
1778
|
-
"dr",
|
|
1779
|
-
"dg",
|
|
1780
|
-
"db",
|
|
1781
|
-
"u0",
|
|
1782
|
-
"v0",
|
|
1783
|
-
"dr0",
|
|
1784
|
-
"dg0",
|
|
1785
|
-
"db0",
|
|
1786
|
-
"sA",
|
|
1787
|
-
"sB",
|
|
1788
|
-
"div",
|
|
1789
|
-
"p"
|
|
1790
|
-
];
|
|
1791
|
-
var FluidGPU = class {
|
|
1792
|
-
device;
|
|
1793
|
-
canvas;
|
|
1794
|
-
gpuCtx;
|
|
1795
|
-
format;
|
|
1796
|
-
dpr;
|
|
1797
|
-
f;
|
|
1798
|
-
copyPL;
|
|
1799
|
-
addSrcPL;
|
|
1800
|
-
scalePL;
|
|
1801
|
-
jacobiPL;
|
|
1802
|
-
bndPL;
|
|
1803
|
-
advectPL;
|
|
1804
|
-
divPL;
|
|
1805
|
-
gradPL;
|
|
1806
|
-
injectPL;
|
|
1807
|
-
renderPL;
|
|
1808
|
-
dtUni;
|
|
1809
|
-
viscUni;
|
|
1810
|
-
diffUni;
|
|
1811
|
-
presUni;
|
|
1812
|
-
densDecUni;
|
|
1813
|
-
velDecUni;
|
|
1814
|
-
zeroUni;
|
|
1815
|
-
bndUni;
|
|
1816
|
-
advUni;
|
|
1817
|
-
emitterUni;
|
|
1818
|
-
emitters;
|
|
1819
|
-
t = 0;
|
|
1820
|
-
w;
|
|
1821
|
-
h;
|
|
1822
|
-
frameId = null;
|
|
1823
|
-
lost = false;
|
|
1824
|
-
constructor(ctx, device) {
|
|
1825
|
-
this.device = device;
|
|
1826
|
-
this.w = ctx.width;
|
|
1827
|
-
this.h = ctx.height;
|
|
1828
|
-
this.dpr = Math.min(ctx.dpr, 2);
|
|
1829
|
-
this.canvas = document.createElement("canvas");
|
|
1830
|
-
this.canvas.style.cssText = "display:block;width:100%;height:100%";
|
|
1831
|
-
this.canvas.setAttribute("aria-hidden", "true");
|
|
1832
|
-
this.canvas.setAttribute("data-fluid-backend", "webgpu");
|
|
1833
|
-
ctx.host.appendChild(this.canvas);
|
|
1834
|
-
this.canvas.width = Math.max(1, Math.round(this.w * this.dpr));
|
|
1835
|
-
this.canvas.height = Math.max(1, Math.round(this.h * this.dpr));
|
|
1836
|
-
this.gpuCtx = this.canvas.getContext("webgpu");
|
|
1837
|
-
this.format = navigator.gpu.getPreferredCanvasFormat();
|
|
1838
|
-
this.gpuCtx.configure({
|
|
1839
|
-
device,
|
|
1840
|
-
format: this.format,
|
|
1841
|
-
alphaMode: "opaque"
|
|
1842
|
-
});
|
|
1843
|
-
this.f = {};
|
|
1844
|
-
for (const n of FIELDS) this.f[n] = mkStorage(device);
|
|
1845
|
-
this.initPipelines();
|
|
1846
|
-
this.initUniforms();
|
|
1847
|
-
this.emitters = buildEmitters(ctx.rng, GPU_N);
|
|
1848
|
-
device.lost.then(() => {
|
|
1849
|
-
this.lost = true;
|
|
1850
|
-
this.stop();
|
|
1851
|
-
});
|
|
1852
|
-
if (ctx.reducedMotion) {
|
|
1853
|
-
this.renderStill();
|
|
1854
|
-
} else {
|
|
1855
|
-
this.start();
|
|
1856
|
-
}
|
|
1857
|
-
}
|
|
1858
|
-
// --- pipeline creation -----------------------------------------------------
|
|
1859
|
-
initPipelines() {
|
|
1860
|
-
const dev = this.device;
|
|
1861
|
-
const ov1 = { SZ: GPU_SZ };
|
|
1862
|
-
const ov2 = { N: GPU_N, STRIDE: GPU_STRIDE };
|
|
1863
|
-
this.copyPL = mkPipeline(dev, COPY_WGSL, ov1);
|
|
1864
|
-
this.addSrcPL = mkPipeline(dev, ADD_SOURCE_WGSL, ov1);
|
|
1865
|
-
this.scalePL = mkPipeline(dev, SCALE_WGSL, ov1);
|
|
1866
|
-
this.jacobiPL = mkPipeline(dev, JACOBI_WGSL, ov2);
|
|
1867
|
-
this.bndPL = mkPipeline(dev, BOUNDARY_WGSL, ov2);
|
|
1868
|
-
this.advectPL = mkPipeline(dev, ADVECT_WGSL, ov2);
|
|
1869
|
-
this.divPL = mkPipeline(dev, DIVERGENCE_WGSL, ov2);
|
|
1870
|
-
this.gradPL = mkPipeline(dev, GRADIENT_SUB_WGSL, ov2);
|
|
1871
|
-
this.injectPL = mkPipeline(dev, INJECT_WGSL, ov2);
|
|
1872
|
-
const renderMod = dev.createShaderModule({ code: RENDER_WGSL });
|
|
1873
|
-
this.renderPL = dev.createRenderPipeline({
|
|
1874
|
-
layout: "auto",
|
|
1875
|
-
vertex: {
|
|
1876
|
-
module: renderMod,
|
|
1877
|
-
entryPoint: "vs",
|
|
1878
|
-
constants: ov2
|
|
1879
|
-
},
|
|
1880
|
-
fragment: {
|
|
1881
|
-
module: renderMod,
|
|
1882
|
-
entryPoint: "fs",
|
|
1883
|
-
targets: [{ format: this.format }],
|
|
1884
|
-
constants: ov2
|
|
1885
|
-
},
|
|
1886
|
-
primitive: { topology: "triangle-list" }
|
|
1887
|
-
});
|
|
1888
|
-
}
|
|
1889
|
-
// --- uniform creation ------------------------------------------------------
|
|
1890
|
-
initUniforms() {
|
|
1891
|
-
const dev = this.device;
|
|
1892
|
-
this.dtUni = mkUniform(dev, new Float32Array([DT]));
|
|
1893
|
-
const aDiff = DT * GPU_DIFF * GPU_N * GPU_N;
|
|
1894
|
-
this.diffUni = mkUniform(dev, new Float32Array([aDiff, 1 + 4 * aDiff]));
|
|
1895
|
-
const aVisc = DT * GPU_VISC * GPU_N * GPU_N;
|
|
1896
|
-
this.viscUni = mkUniform(dev, new Float32Array([aVisc, 1 + 4 * aVisc]));
|
|
1897
|
-
this.presUni = mkUniform(dev, new Float32Array([1, 4]));
|
|
1898
|
-
this.densDecUni = mkUniform(dev, new Float32Array([DENS_DECAY]));
|
|
1899
|
-
this.velDecUni = mkUniform(dev, new Float32Array([VEL_DECAY]));
|
|
1900
|
-
this.zeroUni = mkUniform(dev, new Float32Array([0]));
|
|
1901
|
-
this.bndUni = [
|
|
1902
|
-
mkUniform(dev, new Uint32Array([0])),
|
|
1903
|
-
mkUniform(dev, new Uint32Array([1])),
|
|
1904
|
-
mkUniform(dev, new Uint32Array([2]))
|
|
1905
|
-
];
|
|
1906
|
-
this.advUni = mkUniform(dev, new Float32Array([DT * GPU_N]));
|
|
1907
|
-
this.emitterUni = dev.createBuffer({
|
|
1908
|
-
size: 256,
|
|
1909
|
-
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
1910
|
-
});
|
|
1911
|
-
}
|
|
1912
|
-
// --- bind-group helper -----------------------------------------------------
|
|
1913
|
-
bg(pl, ...bufs) {
|
|
1914
|
-
return this.device.createBindGroup({
|
|
1915
|
-
layout: pl.getBindGroupLayout(0),
|
|
1916
|
-
entries: bufs.map((buffer, binding) => ({ binding, resource: { buffer } }))
|
|
1917
|
-
});
|
|
1918
|
-
}
|
|
1919
|
-
// --- encoding helpers (each creates its own compute pass for barriers) -----
|
|
1920
|
-
eCopy(enc, dst, src) {
|
|
1921
|
-
const p = enc.beginComputePass();
|
|
1922
|
-
p.setPipeline(this.copyPL);
|
|
1923
|
-
p.setBindGroup(0, this.bg(this.copyPL, dst, src));
|
|
1924
|
-
p.dispatchWorkgroups(WG1D);
|
|
1925
|
-
p.end();
|
|
1926
|
-
}
|
|
1927
|
-
eAddSrc(enc, dst, src) {
|
|
1928
|
-
const p = enc.beginComputePass();
|
|
1929
|
-
p.setPipeline(this.addSrcPL);
|
|
1930
|
-
p.setBindGroup(0, this.bg(this.addSrcPL, this.dtUni, dst, src));
|
|
1931
|
-
p.dispatchWorkgroups(WG1D);
|
|
1932
|
-
p.end();
|
|
1933
|
-
}
|
|
1934
|
-
eScale(enc, field, uni) {
|
|
1935
|
-
const p = enc.beginComputePass();
|
|
1936
|
-
p.setPipeline(this.scalePL);
|
|
1937
|
-
p.setBindGroup(0, this.bg(this.scalePL, uni, field));
|
|
1938
|
-
p.dispatchWorkgroups(WG1D);
|
|
1939
|
-
p.end();
|
|
1940
|
-
}
|
|
1941
|
-
eJacobi(enc, dst, x0, prev, uni) {
|
|
1942
|
-
const p = enc.beginComputePass();
|
|
1943
|
-
p.setPipeline(this.jacobiPL);
|
|
1944
|
-
p.setBindGroup(0, this.bg(this.jacobiPL, uni, dst, x0, prev));
|
|
1945
|
-
p.dispatchWorkgroups(WG2D, WG2D);
|
|
1946
|
-
p.end();
|
|
1947
|
-
}
|
|
1948
|
-
eBnd(enc, field, b) {
|
|
1949
|
-
const p = enc.beginComputePass();
|
|
1950
|
-
p.setPipeline(this.bndPL);
|
|
1951
|
-
p.setBindGroup(0, this.bg(this.bndPL, this.bndUni[b], field));
|
|
1952
|
-
p.dispatchWorkgroups(WG_BND);
|
|
1953
|
-
p.end();
|
|
1954
|
-
}
|
|
1955
|
-
eAdvect(enc, d, d0, u, v) {
|
|
1956
|
-
const p = enc.beginComputePass();
|
|
1957
|
-
p.setPipeline(this.advectPL);
|
|
1958
|
-
p.setBindGroup(0, this.bg(this.advectPL, this.advUni, d, d0, u, v));
|
|
1959
|
-
p.dispatchWorkgroups(WG2D, WG2D);
|
|
1960
|
-
p.end();
|
|
1961
|
-
}
|
|
1962
|
-
eDiv(enc) {
|
|
1963
|
-
const p = enc.beginComputePass();
|
|
1964
|
-
p.setPipeline(this.divPL);
|
|
1965
|
-
p.setBindGroup(0, this.bg(this.divPL, this.f.div, this.f.u, this.f.v));
|
|
1966
|
-
p.dispatchWorkgroups(WG2D, WG2D);
|
|
1967
|
-
p.end();
|
|
1968
|
-
}
|
|
1969
|
-
eGrad(enc) {
|
|
1970
|
-
const p = enc.beginComputePass();
|
|
1971
|
-
p.setPipeline(this.gradPL);
|
|
1972
|
-
p.setBindGroup(0, this.bg(this.gradPL, this.f.u, this.f.v, this.f.p));
|
|
1973
|
-
p.dispatchWorkgroups(WG2D, WG2D);
|
|
1974
|
-
p.end();
|
|
1975
|
-
}
|
|
1976
|
-
eInject(enc) {
|
|
1977
|
-
const p = enc.beginComputePass();
|
|
1978
|
-
p.setPipeline(this.injectPL);
|
|
1979
|
-
p.setBindGroup(
|
|
1980
|
-
0,
|
|
1981
|
-
this.bg(this.injectPL, this.emitterUni, this.f.u0, this.f.v0, this.f.dr0, this.f.dg0, this.f.db0)
|
|
1982
|
-
);
|
|
1983
|
-
p.dispatchWorkgroups(WG_INJ, WG_INJ);
|
|
1984
|
-
p.end();
|
|
1985
|
-
}
|
|
1986
|
-
// --- composite steps -------------------------------------------------------
|
|
1987
|
-
encodeDiffuse(enc, field, uni, b) {
|
|
1988
|
-
this.eCopy(enc, this.f.sA, field);
|
|
1989
|
-
this.eCopy(enc, this.f.sB, this.f.sA);
|
|
1990
|
-
for (let k = 0; k < JACOBI_DIFF; k++) {
|
|
1991
|
-
if (k % 2 === 0) {
|
|
1992
|
-
this.eJacobi(enc, field, this.f.sA, this.f.sB, uni);
|
|
1993
|
-
} else {
|
|
1994
|
-
this.eJacobi(enc, this.f.sB, this.f.sA, field, uni);
|
|
1995
|
-
}
|
|
1996
|
-
}
|
|
1997
|
-
if (JACOBI_DIFF % 2 === 0) this.eCopy(enc, field, this.f.sB);
|
|
1998
|
-
this.eBnd(enc, field, b);
|
|
1999
|
-
}
|
|
2000
|
-
encodeProject(enc) {
|
|
2001
|
-
this.eDiv(enc);
|
|
2002
|
-
this.eBnd(enc, this.f.div, 0);
|
|
2003
|
-
this.eScale(enc, this.f.p, this.zeroUni);
|
|
2004
|
-
this.eScale(enc, this.f.sA, this.zeroUni);
|
|
2005
|
-
for (let k = 0; k < JACOBI_PRES; k++) {
|
|
2006
|
-
if (k % 2 === 0) {
|
|
2007
|
-
this.eJacobi(enc, this.f.sA, this.f.div, this.f.p, this.presUni);
|
|
2008
|
-
} else {
|
|
2009
|
-
this.eJacobi(enc, this.f.p, this.f.div, this.f.sA, this.presUni);
|
|
2010
|
-
}
|
|
2011
|
-
}
|
|
2012
|
-
if (JACOBI_PRES % 2 !== 0) this.eCopy(enc, this.f.p, this.f.sA);
|
|
2013
|
-
this.eBnd(enc, this.f.p, 0);
|
|
2014
|
-
this.eGrad(enc);
|
|
2015
|
-
this.eBnd(enc, this.f.u, 1);
|
|
2016
|
-
this.eBnd(enc, this.f.v, 2);
|
|
2017
|
-
}
|
|
2018
|
-
encodeVelStep(enc) {
|
|
2019
|
-
this.eAddSrc(enc, this.f.u, this.f.u0);
|
|
2020
|
-
this.eAddSrc(enc, this.f.v, this.f.v0);
|
|
2021
|
-
this.encodeDiffuse(enc, this.f.u, this.viscUni, 1);
|
|
2022
|
-
this.encodeDiffuse(enc, this.f.v, this.viscUni, 2);
|
|
2023
|
-
this.encodeProject(enc);
|
|
2024
|
-
this.eCopy(enc, this.f.sA, this.f.u);
|
|
2025
|
-
this.eCopy(enc, this.f.sB, this.f.v);
|
|
2026
|
-
this.eAdvect(enc, this.f.u, this.f.sA, this.f.sA, this.f.sB);
|
|
2027
|
-
this.eAdvect(enc, this.f.v, this.f.sB, this.f.sA, this.f.sB);
|
|
2028
|
-
this.eBnd(enc, this.f.u, 1);
|
|
2029
|
-
this.eBnd(enc, this.f.v, 2);
|
|
2030
|
-
this.encodeProject(enc);
|
|
2031
|
-
this.eScale(enc, this.f.u, this.velDecUni);
|
|
2032
|
-
this.eScale(enc, this.f.v, this.velDecUni);
|
|
2033
|
-
}
|
|
2034
|
-
encodeDensStep(enc, d, d0) {
|
|
2035
|
-
this.eAddSrc(enc, this.f[d], this.f[d0]);
|
|
2036
|
-
this.encodeDiffuse(enc, this.f[d], this.diffUni, 0);
|
|
2037
|
-
this.eCopy(enc, this.f.sA, this.f[d]);
|
|
2038
|
-
this.eAdvect(enc, this.f[d], this.f.sA, this.f.u, this.f.v);
|
|
2039
|
-
this.eBnd(enc, this.f[d], 0);
|
|
2040
|
-
this.eScale(enc, this.f[d], this.densDecUni);
|
|
2041
|
-
}
|
|
2042
|
-
// --- emitter upload --------------------------------------------------------
|
|
2043
|
-
writeEmitters() {
|
|
2044
|
-
const frames = stepEmitters(this.t, this.emitters, GPU_N);
|
|
2045
|
-
const buf = new ArrayBuffer(256);
|
|
2046
|
-
const fv = new Float32Array(buf);
|
|
2047
|
-
const uv = new Uint32Array(buf);
|
|
2048
|
-
fv[0] = GPU_INJECT_R;
|
|
2049
|
-
fv[1] = DYE_RATE;
|
|
2050
|
-
fv[2] = GPU_FORCE_RATE;
|
|
2051
|
-
uv[3] = frames.length;
|
|
2052
|
-
for (let i = 0; i < frames.length; i++) {
|
|
2053
|
-
const off = 4 + i * 8;
|
|
2054
|
-
const fr = frames[i];
|
|
2055
|
-
fv[off] = fr.gx;
|
|
2056
|
-
fv[off + 1] = fr.gy;
|
|
2057
|
-
fv[off + 2] = fr.dx;
|
|
2058
|
-
fv[off + 3] = fr.dy;
|
|
2059
|
-
fv[off + 4] = fr.r;
|
|
2060
|
-
fv[off + 5] = fr.g;
|
|
2061
|
-
fv[off + 6] = fr.b;
|
|
2062
|
-
fv[off + 7] = 0;
|
|
2063
|
-
}
|
|
2064
|
-
this.device.queue.writeBuffer(this.emitterUni, 0, buf);
|
|
2065
|
-
}
|
|
2066
|
-
// --- simulation step -------------------------------------------------------
|
|
2067
|
-
simStep() {
|
|
2068
|
-
this.writeEmitters();
|
|
2069
|
-
const enc = this.device.createCommandEncoder();
|
|
2070
|
-
this.eInject(enc);
|
|
2071
|
-
this.encodeVelStep(enc);
|
|
2072
|
-
this.encodeDensStep(enc, "dr", "dr0");
|
|
2073
|
-
this.encodeDensStep(enc, "dg", "dg0");
|
|
2074
|
-
this.encodeDensStep(enc, "db", "db0");
|
|
2075
|
-
this.device.queue.submit([enc.finish()]);
|
|
2076
|
-
}
|
|
2077
|
-
// --- render ----------------------------------------------------------------
|
|
2078
|
-
render() {
|
|
2079
|
-
if (this.lost) return;
|
|
2080
|
-
const tex = this.gpuCtx.getCurrentTexture();
|
|
2081
|
-
const enc = this.device.createCommandEncoder();
|
|
2082
|
-
const pass = enc.beginRenderPass({
|
|
2083
|
-
colorAttachments: [
|
|
2084
|
-
{
|
|
2085
|
-
view: tex.createView(),
|
|
2086
|
-
loadOp: "clear",
|
|
2087
|
-
storeOp: "store",
|
|
2088
|
-
clearValue: { r: 0, g: 0, b: 0, a: 1 }
|
|
2089
|
-
}
|
|
2090
|
-
]
|
|
2091
|
-
});
|
|
2092
|
-
pass.setPipeline(this.renderPL);
|
|
2093
|
-
pass.setBindGroup(0, this.bg(this.renderPL, this.f.dr, this.f.dg, this.f.db));
|
|
2094
|
-
pass.draw(3);
|
|
2095
|
-
pass.end();
|
|
2096
|
-
this.device.queue.submit([enc.finish()]);
|
|
2097
|
-
}
|
|
2098
|
-
// --- animation loop --------------------------------------------------------
|
|
2099
|
-
start() {
|
|
2100
|
-
if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
|
|
2101
|
-
this.loop();
|
|
2102
|
-
}
|
|
2103
|
-
stop() {
|
|
2104
|
-
if (this.frameId !== null) {
|
|
2105
|
-
cancelAnimationFrame(this.frameId);
|
|
2106
|
-
this.frameId = null;
|
|
2107
|
-
}
|
|
2108
|
-
}
|
|
2109
|
-
loop() {
|
|
2110
|
-
this.frameId = requestAnimationFrame(() => this.loop());
|
|
2111
|
-
this.t += DT;
|
|
2112
|
-
this.simStep();
|
|
2113
|
-
this.render();
|
|
2114
|
-
}
|
|
2115
|
-
renderStill() {
|
|
2116
|
-
for (let i = 0; i < 200; i++) {
|
|
2117
|
-
this.t += DT;
|
|
2118
|
-
this.simStep();
|
|
2119
|
-
}
|
|
2120
|
-
this.render();
|
|
2121
|
-
}
|
|
2122
|
-
// --- SaverInstance interface ------------------------------------------------
|
|
2123
|
-
setPaused(paused) {
|
|
2124
|
-
if (paused) this.stop();
|
|
2125
|
-
else this.start();
|
|
2126
|
-
}
|
|
2127
|
-
resize(width, height, dpr) {
|
|
2128
|
-
this.w = width;
|
|
2129
|
-
this.h = height;
|
|
2130
|
-
if (dpr !== void 0) this.dpr = Math.min(dpr, 2);
|
|
2131
|
-
this.canvas.width = Math.max(1, Math.round(this.w * this.dpr));
|
|
2132
|
-
this.canvas.height = Math.max(1, Math.round(this.h * this.dpr));
|
|
2133
|
-
if (!this.lost) {
|
|
2134
|
-
this.gpuCtx.configure({
|
|
2135
|
-
device: this.device,
|
|
2136
|
-
format: this.format,
|
|
2137
|
-
alphaMode: "opaque"
|
|
2138
|
-
});
|
|
2139
|
-
this.render();
|
|
2140
|
-
}
|
|
2141
|
-
}
|
|
2142
|
-
dispose() {
|
|
2143
|
-
this.stop();
|
|
2144
|
-
for (const b of Object.values(this.f)) b.destroy();
|
|
2145
|
-
this.dtUni.destroy();
|
|
2146
|
-
this.diffUni.destroy();
|
|
2147
|
-
this.viscUni.destroy();
|
|
2148
|
-
this.presUni.destroy();
|
|
2149
|
-
this.densDecUni.destroy();
|
|
2150
|
-
this.velDecUni.destroy();
|
|
2151
|
-
this.zeroUni.destroy();
|
|
2152
|
-
for (const b of this.bndUni) b.destroy();
|
|
2153
|
-
this.advUni.destroy();
|
|
2154
|
-
this.emitterUni.destroy();
|
|
2155
|
-
this.canvas.remove();
|
|
2156
|
-
this.device.destroy();
|
|
2157
|
-
}
|
|
2158
|
-
};
|
|
2159
|
-
|
|
2160
|
-
// src/fluid.ts
|
|
2161
|
-
var fluidManifest = {
|
|
2162
|
-
id: "fluid",
|
|
2163
|
-
label: "Fluid",
|
|
2164
|
-
passthrough: false,
|
|
2165
|
-
minBackend: "canvas2d",
|
|
2166
|
-
costTier: "low",
|
|
2167
|
-
motionIntensity: "calm",
|
|
2168
|
-
reducedMotionFallback: "static",
|
|
2169
|
-
a11y: { flashSafe: true }
|
|
2170
|
-
};
|
|
2171
|
-
var N = 96;
|
|
2172
|
-
var SZ = (N + 2) * (N + 2);
|
|
2173
|
-
var DIFF = 3e-5;
|
|
2174
|
-
var VISC = 3e-4;
|
|
2175
|
-
var GS_ITER = 4;
|
|
2176
|
-
var INJECT_R = 4;
|
|
2177
|
-
function IX(i, j) {
|
|
2178
|
-
return i + (N + 2) * j;
|
|
2179
|
-
}
|
|
2180
|
-
var FluidCPU = class {
|
|
2181
|
-
ctxSaver;
|
|
2182
|
-
canvas;
|
|
2183
|
-
gc;
|
|
2184
|
-
buf;
|
|
2185
|
-
bufCtx;
|
|
2186
|
-
img;
|
|
2187
|
-
w = 0;
|
|
2188
|
-
h = 0;
|
|
2189
|
-
u = new Float32Array(SZ);
|
|
2190
|
-
v = new Float32Array(SZ);
|
|
2191
|
-
u0 = new Float32Array(SZ);
|
|
2192
|
-
v0 = new Float32Array(SZ);
|
|
2193
|
-
dr = new Float32Array(SZ);
|
|
2194
|
-
dg = new Float32Array(SZ);
|
|
2195
|
-
db = new Float32Array(SZ);
|
|
2196
|
-
dr0 = new Float32Array(SZ);
|
|
2197
|
-
dg0 = new Float32Array(SZ);
|
|
2198
|
-
db0 = new Float32Array(SZ);
|
|
2199
|
-
tmp = new Float32Array(SZ);
|
|
2200
|
-
emitters;
|
|
2201
|
-
t = 0;
|
|
2202
|
-
frameId = null;
|
|
2203
|
-
paused = false;
|
|
2204
|
-
constructor(ctx) {
|
|
2205
|
-
this.ctxSaver = ctx;
|
|
2206
|
-
this.w = ctx.width;
|
|
2207
|
-
this.h = ctx.height;
|
|
2208
|
-
let canvas;
|
|
2209
|
-
if (ctx.surface) {
|
|
2210
|
-
canvas = ctx.surface;
|
|
2211
|
-
} else {
|
|
2212
|
-
const el = document.createElement("canvas");
|
|
2213
|
-
el.style.cssText = "display:block;width:100%;height:100%";
|
|
2214
|
-
el.setAttribute("aria-hidden", "true");
|
|
2215
|
-
el.setAttribute("data-fluid-backend", "canvas2d");
|
|
2216
|
-
ctx.host.appendChild(el);
|
|
2217
|
-
canvas = el;
|
|
2218
|
-
}
|
|
2219
|
-
this.canvas = canvas;
|
|
2220
|
-
const gc = canvas.getContext("2d", { alpha: false });
|
|
2221
|
-
if (!gc) throw new Error("fluid: no 2d context");
|
|
2222
|
-
this.gc = gc;
|
|
2223
|
-
this.buf = document.createElement("canvas");
|
|
2224
|
-
this.buf.width = N;
|
|
2225
|
-
this.buf.height = N;
|
|
2226
|
-
this.bufCtx = this.buf.getContext("2d");
|
|
2227
|
-
this.img = new ImageData(N, N);
|
|
2228
|
-
this.emitters = this.buildEmitters(ctx.rng);
|
|
2229
|
-
this.sizeCanvas();
|
|
2230
|
-
this.paused = ctx.reducedMotion;
|
|
2231
|
-
if (this.paused) {
|
|
2232
|
-
this.renderStill();
|
|
2233
|
-
} else {
|
|
2234
|
-
this.start();
|
|
2235
|
-
}
|
|
2236
|
-
}
|
|
2237
|
-
buildEmitters(rng) {
|
|
2238
|
-
const out = [];
|
|
2239
|
-
for (let i = 0; i < EMITTER_N; i++) {
|
|
2240
|
-
const cx = N / 2 + 1;
|
|
2241
|
-
out.push({
|
|
2242
|
-
hue: (i * 360 / EMITTER_N + rng.range(0, 40)) % 360,
|
|
2243
|
-
fx: rng.range(0.3, 0.8),
|
|
2244
|
-
fy: rng.range(0.3, 0.8),
|
|
2245
|
-
px: rng.range(0, Math.PI * 2),
|
|
2246
|
-
py: rng.range(0, Math.PI * 2),
|
|
2247
|
-
speed: rng.range(0.5, 1),
|
|
2248
|
-
prevGx: cx,
|
|
2249
|
-
prevGy: cx
|
|
2250
|
-
});
|
|
2251
|
-
}
|
|
2252
|
-
return out;
|
|
2253
|
-
}
|
|
2254
|
-
sizeCanvas() {
|
|
2255
|
-
const dpr = Math.min(this.ctxSaver.dpr, 2);
|
|
2256
|
-
this.canvas.width = Math.max(1, Math.round(this.w * dpr));
|
|
2257
|
-
this.canvas.height = Math.max(1, Math.round(this.h * dpr));
|
|
2258
|
-
this.gc.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
2259
|
-
}
|
|
2260
|
-
// --- Stam stable-fluids solver ---
|
|
2261
|
-
setBnd(b, x) {
|
|
2262
|
-
for (let i = 1; i <= N; i++) {
|
|
2263
|
-
x[IX(0, i)] = b === 1 ? -x[IX(1, i)] : x[IX(1, i)];
|
|
2264
|
-
x[IX(N + 1, i)] = b === 1 ? -x[IX(N, i)] : x[IX(N, i)];
|
|
2265
|
-
x[IX(i, 0)] = b === 2 ? -x[IX(i, 1)] : x[IX(i, 1)];
|
|
2266
|
-
x[IX(i, N + 1)] = b === 2 ? -x[IX(i, N)] : x[IX(i, N)];
|
|
2267
|
-
}
|
|
2268
|
-
x[IX(0, 0)] = 0.5 * (x[IX(1, 0)] + x[IX(0, 1)]);
|
|
2269
|
-
x[IX(0, N + 1)] = 0.5 * (x[IX(1, N + 1)] + x[IX(0, N)]);
|
|
2270
|
-
x[IX(N + 1, 0)] = 0.5 * (x[IX(N, 0)] + x[IX(N + 1, 1)]);
|
|
2271
|
-
x[IX(N + 1, N + 1)] = 0.5 * (x[IX(N, N + 1)] + x[IX(N + 1, N)]);
|
|
2272
|
-
}
|
|
2273
|
-
diffuse(b, x, x0, diff) {
|
|
2274
|
-
const a = DT * diff * N * N;
|
|
2275
|
-
if (a === 0) {
|
|
2276
|
-
x.set(x0);
|
|
2277
|
-
return;
|
|
2278
|
-
}
|
|
2279
|
-
const denom = 1 + 4 * a;
|
|
2280
|
-
for (let k = 0; k < GS_ITER; k++) {
|
|
2281
|
-
for (let j = 1; j <= N; j++) {
|
|
2282
|
-
for (let i = 1; i <= N; i++) {
|
|
2283
|
-
const idx = IX(i, j);
|
|
2284
|
-
x[idx] = (x0[idx] + a * (x[IX(i - 1, j)] + x[IX(i + 1, j)] + x[IX(i, j - 1)] + x[IX(i, j + 1)])) / denom;
|
|
2285
|
-
}
|
|
2286
|
-
}
|
|
2287
|
-
this.setBnd(b, x);
|
|
2288
|
-
}
|
|
2289
|
-
}
|
|
2290
|
-
advect(b, d, d0, u, v) {
|
|
2291
|
-
const dt0 = DT * N;
|
|
2292
|
-
for (let j = 1; j <= N; j++) {
|
|
2293
|
-
for (let i = 1; i <= N; i++) {
|
|
2294
|
-
const idx = IX(i, j);
|
|
2295
|
-
let x = i - dt0 * u[idx];
|
|
2296
|
-
let y = j - dt0 * v[idx];
|
|
2297
|
-
if (x < 0.5) x = 0.5;
|
|
2298
|
-
if (x > N + 0.5) x = N + 0.5;
|
|
2299
|
-
if (y < 0.5) y = 0.5;
|
|
2300
|
-
if (y > N + 0.5) y = N + 0.5;
|
|
2301
|
-
const i0 = Math.floor(x);
|
|
2302
|
-
const j0 = Math.floor(y);
|
|
2303
|
-
const s1 = x - i0;
|
|
2304
|
-
const s0 = 1 - s1;
|
|
2305
|
-
const t1 = y - j0;
|
|
2306
|
-
const t0 = 1 - t1;
|
|
2307
|
-
d[idx] = s0 * (t0 * d0[IX(i0, j0)] + t1 * d0[IX(i0, j0 + 1)]) + s1 * (t0 * d0[IX(i0 + 1, j0)] + t1 * d0[IX(i0 + 1, j0 + 1)]);
|
|
2308
|
-
}
|
|
2309
|
-
}
|
|
2310
|
-
this.setBnd(b, d);
|
|
2311
|
-
}
|
|
2312
|
-
project(u, v, p, div) {
|
|
2313
|
-
const h = 1 / N;
|
|
2314
|
-
for (let j = 1; j <= N; j++) {
|
|
2315
|
-
for (let i = 1; i <= N; i++) {
|
|
2316
|
-
const idx = IX(i, j);
|
|
2317
|
-
div[idx] = -0.5 * h * (u[IX(i + 1, j)] - u[IX(i - 1, j)] + v[IX(i, j + 1)] - v[IX(i, j - 1)]);
|
|
2318
|
-
p[idx] = 0;
|
|
2319
|
-
}
|
|
2320
|
-
}
|
|
2321
|
-
this.setBnd(0, div);
|
|
2322
|
-
this.setBnd(0, p);
|
|
2323
|
-
for (let k = 0; k < GS_ITER; k++) {
|
|
2324
|
-
for (let j = 1; j <= N; j++) {
|
|
2325
|
-
for (let i = 1; i <= N; i++) {
|
|
2326
|
-
const idx = IX(i, j);
|
|
2327
|
-
p[idx] = (div[idx] + p[IX(i - 1, j)] + p[IX(i + 1, j)] + p[IX(i, j - 1)] + p[IX(i, j + 1)]) / 4;
|
|
2328
|
-
}
|
|
2329
|
-
}
|
|
2330
|
-
this.setBnd(0, p);
|
|
2331
|
-
}
|
|
2332
|
-
for (let j = 1; j <= N; j++) {
|
|
2333
|
-
for (let i = 1; i <= N; i++) {
|
|
2334
|
-
const idx = IX(i, j);
|
|
2335
|
-
u[idx] -= 0.5 * N * (p[IX(i + 1, j)] - p[IX(i - 1, j)]);
|
|
2336
|
-
v[idx] -= 0.5 * N * (p[IX(i, j + 1)] - p[IX(i, j - 1)]);
|
|
2337
|
-
}
|
|
2338
|
-
}
|
|
2339
|
-
this.setBnd(1, u);
|
|
2340
|
-
this.setBnd(2, v);
|
|
2341
|
-
}
|
|
2342
|
-
velStep() {
|
|
2343
|
-
for (let i = 0; i < SZ; i++) {
|
|
2344
|
-
this.u[i] += DT * this.u0[i];
|
|
2345
|
-
this.v[i] += DT * this.v0[i];
|
|
2346
|
-
}
|
|
2347
|
-
[this.u, this.u0] = [this.u0, this.u];
|
|
2348
|
-
this.diffuse(1, this.u, this.u0, VISC);
|
|
2349
|
-
[this.v, this.v0] = [this.v0, this.v];
|
|
2350
|
-
this.diffuse(2, this.v, this.v0, VISC);
|
|
2351
|
-
this.project(this.u, this.v, this.u0, this.v0);
|
|
2352
|
-
[this.u, this.u0] = [this.u0, this.u];
|
|
2353
|
-
[this.v, this.v0] = [this.v0, this.v];
|
|
2354
|
-
this.advect(1, this.u, this.u0, this.u0, this.v0);
|
|
2355
|
-
this.advect(2, this.v, this.v0, this.u0, this.v0);
|
|
2356
|
-
this.project(this.u, this.v, this.u0, this.v0);
|
|
2357
|
-
for (let i = 0; i < SZ; i++) {
|
|
2358
|
-
this.u[i] *= VEL_DECAY;
|
|
2359
|
-
this.v[i] *= VEL_DECAY;
|
|
2360
|
-
}
|
|
2361
|
-
}
|
|
2362
|
-
densStep(d, d0) {
|
|
2363
|
-
for (let i = 0; i < SZ; i++) d[i] += DT * d0[i];
|
|
2364
|
-
this.tmp.set(d);
|
|
2365
|
-
this.diffuse(0, d, this.tmp, DIFF);
|
|
2366
|
-
this.tmp.set(d);
|
|
2367
|
-
this.advect(0, d, this.tmp, this.u, this.v);
|
|
2368
|
-
for (let i = 0; i < SZ; i++) d[i] *= DENS_DECAY;
|
|
2369
|
-
}
|
|
2370
|
-
// --- Source injection ---
|
|
2371
|
-
injectSources() {
|
|
2372
|
-
this.u0.fill(0);
|
|
2373
|
-
this.v0.fill(0);
|
|
2374
|
-
this.dr0.fill(0);
|
|
2375
|
-
this.dg0.fill(0);
|
|
2376
|
-
this.db0.fill(0);
|
|
2377
|
-
for (const e of this.emitters) {
|
|
2378
|
-
const st = this.t * e.speed;
|
|
2379
|
-
const gx = (Math.sin(st * e.fx + e.px) * 0.35 + 0.5) * N + 1;
|
|
2380
|
-
const gy = (Math.cos(st * e.fy + e.py) * 0.35 + 0.5) * N + 1;
|
|
2381
|
-
const dx = gx - e.prevGx;
|
|
2382
|
-
const dy = gy - e.prevGy;
|
|
2383
|
-
const ci = Math.round(gx);
|
|
2384
|
-
const cj = Math.round(gy);
|
|
2385
|
-
const [cr, cg, cb] = hue2rgb(e.hue + this.t * 15);
|
|
2386
|
-
for (let di = -INJECT_R; di <= INJECT_R; di++) {
|
|
2387
|
-
for (let dj = -INJECT_R; dj <= INJECT_R; dj++) {
|
|
2388
|
-
const ii = ci + di;
|
|
2389
|
-
const jj = cj + dj;
|
|
2390
|
-
if (ii < 1 || ii > N || jj < 1 || jj > N) continue;
|
|
2391
|
-
const dist = Math.sqrt(di * di + dj * dj);
|
|
2392
|
-
if (dist > INJECT_R) continue;
|
|
2393
|
-
const s = 1 - dist / INJECT_R;
|
|
2394
|
-
const idx = IX(ii, jj);
|
|
2395
|
-
this.dr0[idx] += cr * DYE_RATE * s;
|
|
2396
|
-
this.dg0[idx] += cg * DYE_RATE * s;
|
|
2397
|
-
this.db0[idx] += cb * DYE_RATE * s;
|
|
2398
|
-
this.u0[idx] += dx * FORCE_RATE * s;
|
|
2399
|
-
this.v0[idx] += dy * FORCE_RATE * s;
|
|
2400
|
-
}
|
|
2401
|
-
}
|
|
2402
|
-
e.prevGx = gx;
|
|
2403
|
-
e.prevGy = gy;
|
|
2404
|
-
}
|
|
2405
|
-
}
|
|
2406
|
-
// --- Simulation step ---
|
|
2407
|
-
simStep() {
|
|
2408
|
-
this.injectSources();
|
|
2409
|
-
this.velStep();
|
|
2410
|
-
this.densStep(this.dr, this.dr0);
|
|
2411
|
-
this.densStep(this.dg, this.dg0);
|
|
2412
|
-
this.densStep(this.db, this.db0);
|
|
2413
|
-
}
|
|
2414
|
-
// --- Rendering ---
|
|
2415
|
-
render() {
|
|
2416
|
-
const data = this.img.data;
|
|
2417
|
-
for (let j = 0; j < N; j++) {
|
|
2418
|
-
for (let i = 0; i < N; i++) {
|
|
2419
|
-
const si = IX(i + 1, j + 1);
|
|
2420
|
-
const pi = (j * N + i) * 4;
|
|
2421
|
-
data[pi] = Math.min(255, this.dr[si] | 0);
|
|
2422
|
-
data[pi + 1] = Math.min(255, this.dg[si] | 0);
|
|
2423
|
-
data[pi + 2] = Math.min(255, this.db[si] | 0);
|
|
2424
|
-
data[pi + 3] = 255;
|
|
2425
|
-
}
|
|
2426
|
-
}
|
|
2427
|
-
this.bufCtx.putImageData(this.img, 0, 0);
|
|
2428
|
-
this.gc.imageSmoothingEnabled = true;
|
|
2429
|
-
this.gc.drawImage(this.buf, 0, 0, this.w, this.h);
|
|
2430
|
-
}
|
|
2431
|
-
// --- Animation ---
|
|
2432
|
-
start() {
|
|
2433
|
-
if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
|
|
2434
|
-
this.loop();
|
|
2435
|
-
}
|
|
2436
|
-
stop() {
|
|
2437
|
-
if (this.frameId !== null) {
|
|
2438
|
-
cancelAnimationFrame(this.frameId);
|
|
2439
|
-
this.frameId = null;
|
|
2440
|
-
}
|
|
2441
|
-
}
|
|
2442
|
-
loop() {
|
|
2443
|
-
this.frameId = requestAnimationFrame(() => this.loop());
|
|
2444
|
-
this.t += DT;
|
|
2445
|
-
this.simStep();
|
|
2446
|
-
this.render();
|
|
2447
|
-
}
|
|
2448
|
-
renderStill() {
|
|
2449
|
-
for (let i = 0; i < 200; i++) {
|
|
2450
|
-
this.t += DT;
|
|
2451
|
-
this.simStep();
|
|
2452
|
-
}
|
|
2453
|
-
this.render();
|
|
2454
|
-
}
|
|
2455
|
-
setPaused(paused) {
|
|
2456
|
-
this.paused = paused;
|
|
2457
|
-
if (paused) this.stop();
|
|
2458
|
-
else this.start();
|
|
2459
|
-
}
|
|
2460
|
-
resize(width, height, dpr) {
|
|
2461
|
-
this.w = width;
|
|
2462
|
-
this.h = height;
|
|
2463
|
-
if (dpr !== void 0) this.ctxSaver.dpr = dpr;
|
|
2464
|
-
this.sizeCanvas();
|
|
2465
|
-
this.render();
|
|
2466
|
-
}
|
|
2467
|
-
dispose() {
|
|
2468
|
-
this.stop();
|
|
2469
|
-
if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
|
|
2470
|
-
}
|
|
2471
|
-
};
|
|
2472
|
-
var fluid = {
|
|
2473
|
-
manifest: fluidManifest,
|
|
2474
|
-
async mount(ctx) {
|
|
2475
|
-
try {
|
|
2476
|
-
const adapter = await navigator.gpu?.requestAdapter();
|
|
2477
|
-
if (adapter) {
|
|
2478
|
-
const device = await adapter.requestDevice();
|
|
2479
|
-
return new FluidGPU(ctx, device);
|
|
2480
|
-
}
|
|
2481
|
-
} catch {
|
|
2482
|
-
}
|
|
2483
|
-
return new FluidCPU(ctx);
|
|
2484
|
-
}
|
|
2485
|
-
};
|
|
2486
|
-
|
|
2487
|
-
// src/reaction-diffusion-shared.ts
|
|
2488
|
-
var RD_DU = 0.16;
|
|
2489
|
-
var RD_DV = 0.08;
|
|
2490
|
-
var RD_F = 0.055;
|
|
2491
|
-
var RD_K = 0.062;
|
|
2492
|
-
var RD_DT = 1;
|
|
2493
|
-
var SEED_R = 5;
|
|
2494
|
-
var SEED_COUNT = 15;
|
|
2495
|
-
var RESEED_INTERVAL = 360;
|
|
2496
|
-
var RESEED_BATCH = 3;
|
|
2497
|
-
function generateSeeds(rng, N3, count) {
|
|
2498
|
-
const seeds = [];
|
|
2499
|
-
for (let i = 0; i < count; i++) {
|
|
2500
|
-
seeds.push({
|
|
2501
|
-
x: rng.int(SEED_R + 1, N3 - SEED_R - 2),
|
|
2502
|
-
y: rng.int(SEED_R + 1, N3 - SEED_R - 2)
|
|
2503
|
-
});
|
|
2504
|
-
}
|
|
2505
|
-
return seeds;
|
|
2506
|
-
}
|
|
2507
|
-
function applySeedsCPU(u, v, N3, seeds, r) {
|
|
2508
|
-
for (const s of seeds) {
|
|
2509
|
-
for (let di = -r; di <= r; di++) {
|
|
2510
|
-
for (let dj = -r; dj <= r; dj++) {
|
|
2511
|
-
if (di * di + dj * dj > r * r) continue;
|
|
2512
|
-
const x = (s.x + di + N3) % N3;
|
|
2513
|
-
const y = (s.y + dj + N3) % N3;
|
|
2514
|
-
const idx = x + y * N3;
|
|
2515
|
-
u[idx] = 0.5;
|
|
2516
|
-
v[idx] = 0.25;
|
|
2517
|
-
}
|
|
2518
|
-
}
|
|
2519
|
-
}
|
|
2520
|
-
}
|
|
2521
|
-
var LUT = buildColorLUT();
|
|
2522
|
-
function buildColorLUT() {
|
|
2523
|
-
const lut = new Uint8Array(256 * 3);
|
|
2524
|
-
for (let i = 0; i < 256; i++) {
|
|
2525
|
-
const t = i / 255;
|
|
2526
|
-
let r, g, b;
|
|
2527
|
-
if (t < 0.33) {
|
|
2528
|
-
const s = t / 0.33;
|
|
2529
|
-
r = 10 + s * 3;
|
|
2530
|
-
g = 10 + s * 67;
|
|
2531
|
-
b = 46 + s * 56;
|
|
2532
|
-
} else if (t < 0.66) {
|
|
2533
|
-
const s = (t - 0.33) / 0.33;
|
|
2534
|
-
r = 13 + s * 211;
|
|
2535
|
-
g = 77 + s * 35;
|
|
2536
|
-
b = 102 - s * 38;
|
|
2537
|
-
} else {
|
|
2538
|
-
const s = (t - 0.66) / 0.34;
|
|
2539
|
-
r = 224 + s * 16;
|
|
2540
|
-
g = 112 + s * 97;
|
|
2541
|
-
b = 64 + s * 96;
|
|
2542
|
-
}
|
|
2543
|
-
lut[i * 3] = Math.round(r);
|
|
2544
|
-
lut[i * 3 + 1] = Math.round(g);
|
|
2545
|
-
lut[i * 3 + 2] = Math.round(b);
|
|
2546
|
-
}
|
|
2547
|
-
return lut;
|
|
2548
|
-
}
|
|
2549
|
-
function colorV(v) {
|
|
2550
|
-
const i = Math.min(255, Math.max(0, v * 768 | 0));
|
|
2551
|
-
return [LUT[i * 3], LUT[i * 3 + 1], LUT[i * 3 + 2]];
|
|
2552
|
-
}
|
|
2553
|
-
|
|
2554
|
-
// src/reaction-diffusion-gpu.ts
|
|
2555
|
-
var GPU_N2 = 512;
|
|
2556
|
-
var GPU_SEED_R = Math.round(SEED_R * (GPU_N2 / 256));
|
|
2557
|
-
var GPU_STEPS = 32;
|
|
2558
|
-
var WG = 8;
|
|
2559
|
-
var SIM_WGSL = (
|
|
2560
|
-
/* wgsl */
|
|
2561
|
-
`
|
|
2562
|
-
override N: u32;
|
|
2563
|
-
|
|
2564
|
-
const Du = ${RD_DU};
|
|
2565
|
-
const Dv = ${RD_DV};
|
|
2566
|
-
const f = ${RD_F};
|
|
2567
|
-
const k = ${RD_K};
|
|
2568
|
-
const dt = ${RD_DT};
|
|
2569
|
-
|
|
2570
|
-
@group(0) @binding(0) var<storage, read> src: array<vec2f>;
|
|
2571
|
-
@group(0) @binding(1) var<storage, read_write> dst: array<vec2f>;
|
|
2572
|
-
|
|
2573
|
-
@compute @workgroup_size(${WG}, ${WG})
|
|
2574
|
-
fn main(@builtin(global_invocation_id) id: vec3u) {
|
|
2575
|
-
let x = id.x;
|
|
2576
|
-
let y = id.y;
|
|
2577
|
-
if (x >= N || y >= N) { return; }
|
|
2578
|
-
|
|
2579
|
-
let idx = x + y * N;
|
|
2580
|
-
let c = src[idx];
|
|
2581
|
-
|
|
2582
|
-
let lx = ((x + N - 1u) % N) + y * N;
|
|
2583
|
-
let rx = ((x + 1u) % N) + y * N;
|
|
2584
|
-
let uy = x + ((y + N - 1u) % N) * N;
|
|
2585
|
-
let dy = x + ((y + 1u) % N) * N;
|
|
2586
|
-
|
|
2587
|
-
let lap = src[lx] + src[rx] + src[uy] + src[dy] - 4.0 * c;
|
|
2588
|
-
|
|
2589
|
-
let uvv = c.x * c.y * c.y;
|
|
2590
|
-
let nu = c.x + dt * (Du * lap.x - uvv + f * (1.0 - c.x));
|
|
2591
|
-
let nv = c.y + dt * (Dv * lap.y + uvv - (f + k) * c.y);
|
|
2592
|
-
|
|
2593
|
-
dst[idx] = clamp(vec2f(nu, nv), vec2f(0.0), vec2f(1.0));
|
|
2594
|
-
}`
|
|
2595
|
-
);
|
|
2596
|
-
var RENDER_WGSL2 = (
|
|
2597
|
-
/* wgsl */
|
|
2598
|
-
`
|
|
2599
|
-
override N: u32;
|
|
2600
|
-
|
|
2601
|
-
@group(0) @binding(0) var<storage, read> field: array<vec2f>;
|
|
2602
|
-
|
|
2603
|
-
struct VOut {
|
|
2604
|
-
@builtin(position) pos: vec4f,
|
|
2605
|
-
@location(0) uv: vec2f,
|
|
2606
|
-
}
|
|
2607
|
-
|
|
2608
|
-
@vertex
|
|
2609
|
-
fn vs(@builtin(vertex_index) i: u32) -> VOut {
|
|
2610
|
-
var p = array<vec2f, 3>(vec2f(-1,-1), vec2f(3,-1), vec2f(-1,3));
|
|
2611
|
-
var o: VOut;
|
|
2612
|
-
o.pos = vec4f(p[i], 0, 1);
|
|
2613
|
-
o.uv = (p[i] + 1.0) * 0.5;
|
|
2614
|
-
o.uv.y = 1.0 - o.uv.y;
|
|
2615
|
-
return o;
|
|
2616
|
-
}
|
|
2617
|
-
|
|
2618
|
-
@fragment
|
|
2619
|
-
fn fs(v: VOut) -> @location(0) vec4f {
|
|
2620
|
-
let x = min(u32(v.uv.x * f32(N)), N - 1u);
|
|
2621
|
-
let y = min(u32(v.uv.y * f32(N)), N - 1u);
|
|
2622
|
-
let val = field[x + y * N];
|
|
2623
|
-
|
|
2624
|
-
let t = clamp(val.y * 3.0, 0.0, 1.0);
|
|
2625
|
-
|
|
2626
|
-
let c0 = vec3f(0.039, 0.039, 0.180);
|
|
2627
|
-
let c1 = vec3f(0.051, 0.302, 0.400);
|
|
2628
|
-
let c2 = vec3f(0.878, 0.439, 0.251);
|
|
2629
|
-
let c3 = vec3f(0.941, 0.820, 0.627);
|
|
2630
|
-
|
|
2631
|
-
var color: vec3f;
|
|
2632
|
-
if (t < 0.33) {
|
|
2633
|
-
color = mix(c0, c1, t / 0.33);
|
|
2634
|
-
} else if (t < 0.66) {
|
|
2635
|
-
color = mix(c1, c2, (t - 0.33) / 0.33);
|
|
2636
|
-
} else {
|
|
2637
|
-
color = mix(c2, c3, (t - 0.66) / 0.34);
|
|
2638
|
-
}
|
|
2639
|
-
|
|
2640
|
-
return vec4f(color, 1.0);
|
|
2641
|
-
}`
|
|
2642
|
-
);
|
|
2643
|
-
var ReactionDiffusionGPU = class {
|
|
2644
|
-
device;
|
|
2645
|
-
canvas;
|
|
2646
|
-
gpuCtx;
|
|
2647
|
-
format;
|
|
2648
|
-
dpr;
|
|
2649
|
-
w;
|
|
2650
|
-
h;
|
|
2651
|
-
bufA;
|
|
2652
|
-
bufB;
|
|
2653
|
-
simPL;
|
|
2654
|
-
renderPL;
|
|
2655
|
-
simBG_AB;
|
|
2656
|
-
simBG_BA;
|
|
2657
|
-
renderBG_A;
|
|
2658
|
-
renderBG_B;
|
|
2659
|
-
parity = 0;
|
|
2660
|
-
frameId = null;
|
|
2661
|
-
frameCount = 0;
|
|
2662
|
-
lost = false;
|
|
2663
|
-
rng;
|
|
2664
|
-
constructor(ctx, device) {
|
|
2665
|
-
this.device = device;
|
|
2666
|
-
this.w = ctx.width;
|
|
2667
|
-
this.h = ctx.height;
|
|
2668
|
-
this.dpr = Math.min(ctx.dpr, 2);
|
|
2669
|
-
this.rng = ctx.rng;
|
|
2670
|
-
this.canvas = document.createElement("canvas");
|
|
2671
|
-
this.canvas.style.cssText = "display:block;width:100%;height:100%";
|
|
2672
|
-
this.canvas.setAttribute("aria-hidden", "true");
|
|
2673
|
-
this.canvas.setAttribute("data-rd-backend", "webgpu");
|
|
2674
|
-
ctx.host.appendChild(this.canvas);
|
|
2675
|
-
this.sizeCanvas();
|
|
2676
|
-
this.gpuCtx = this.canvas.getContext("webgpu");
|
|
2677
|
-
this.format = navigator.gpu.getPreferredCanvasFormat();
|
|
2678
|
-
this.gpuCtx.configure({ device, format: this.format, alphaMode: "opaque" });
|
|
2679
|
-
const sz = GPU_N2 * GPU_N2 * 8;
|
|
2680
|
-
const usage = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST;
|
|
2681
|
-
this.bufA = device.createBuffer({ size: sz, usage });
|
|
2682
|
-
this.bufB = device.createBuffer({ size: sz, usage });
|
|
2683
|
-
this.initField();
|
|
2684
|
-
const simLayout = device.createBindGroupLayout({
|
|
2685
|
-
entries: [
|
|
2686
|
-
{ binding: 0, visibility: GPUShaderStage.COMPUTE, buffer: { type: "read-only-storage" } },
|
|
2687
|
-
{ binding: 1, visibility: GPUShaderStage.COMPUTE, buffer: { type: "storage" } }
|
|
2688
|
-
]
|
|
2689
|
-
});
|
|
2690
|
-
const simModule = device.createShaderModule({ code: SIM_WGSL });
|
|
2691
|
-
this.simPL = device.createComputePipeline({
|
|
2692
|
-
layout: device.createPipelineLayout({ bindGroupLayouts: [simLayout] }),
|
|
2693
|
-
compute: { module: simModule, entryPoint: "main", constants: { N: GPU_N2 } }
|
|
2694
|
-
});
|
|
2695
|
-
this.simBG_AB = device.createBindGroup({
|
|
2696
|
-
layout: simLayout,
|
|
2697
|
-
entries: [
|
|
2698
|
-
{ binding: 0, resource: { buffer: this.bufA } },
|
|
2699
|
-
{ binding: 1, resource: { buffer: this.bufB } }
|
|
2700
|
-
]
|
|
2701
|
-
});
|
|
2702
|
-
this.simBG_BA = device.createBindGroup({
|
|
2703
|
-
layout: simLayout,
|
|
2704
|
-
entries: [
|
|
2705
|
-
{ binding: 0, resource: { buffer: this.bufB } },
|
|
2706
|
-
{ binding: 1, resource: { buffer: this.bufA } }
|
|
2707
|
-
]
|
|
2708
|
-
});
|
|
2709
|
-
const renderLayout = device.createBindGroupLayout({
|
|
2710
|
-
entries: [
|
|
2711
|
-
{ binding: 0, visibility: GPUShaderStage.FRAGMENT, buffer: { type: "read-only-storage" } }
|
|
2712
|
-
]
|
|
2713
|
-
});
|
|
2714
|
-
const renderModule = device.createShaderModule({ code: RENDER_WGSL2 });
|
|
2715
|
-
this.renderPL = device.createRenderPipeline({
|
|
2716
|
-
layout: device.createPipelineLayout({ bindGroupLayouts: [renderLayout] }),
|
|
2717
|
-
vertex: { module: renderModule, entryPoint: "vs" },
|
|
2718
|
-
fragment: {
|
|
2719
|
-
module: renderModule,
|
|
2720
|
-
entryPoint: "fs",
|
|
2721
|
-
targets: [{ format: this.format }],
|
|
2722
|
-
constants: { N: GPU_N2 }
|
|
2723
|
-
}
|
|
2724
|
-
});
|
|
2725
|
-
this.renderBG_A = device.createBindGroup({
|
|
2726
|
-
layout: renderLayout,
|
|
2727
|
-
entries: [{ binding: 0, resource: { buffer: this.bufA } }]
|
|
2728
|
-
});
|
|
2729
|
-
this.renderBG_B = device.createBindGroup({
|
|
2730
|
-
layout: renderLayout,
|
|
2731
|
-
entries: [{ binding: 0, resource: { buffer: this.bufB } }]
|
|
2732
|
-
});
|
|
2733
|
-
device.lost.then(() => {
|
|
2734
|
-
this.lost = true;
|
|
2735
|
-
this.stop();
|
|
2736
|
-
});
|
|
2737
|
-
if (ctx.reducedMotion) {
|
|
2738
|
-
this.renderStill();
|
|
2739
|
-
} else {
|
|
2740
|
-
this.start();
|
|
2741
|
-
}
|
|
2742
|
-
}
|
|
2743
|
-
initField() {
|
|
2744
|
-
const data = new Float32Array(GPU_N2 * GPU_N2 * 2);
|
|
2745
|
-
for (let i = 0; i < GPU_N2 * GPU_N2; i++) {
|
|
2746
|
-
data[i * 2] = 1;
|
|
2747
|
-
data[i * 2 + 1] = 0;
|
|
2748
|
-
}
|
|
2749
|
-
this.seedInto(data, SEED_COUNT);
|
|
2750
|
-
this.device.queue.writeBuffer(this.bufA, 0, data);
|
|
2751
|
-
this.device.queue.writeBuffer(this.bufB, 0, data);
|
|
2752
|
-
this.parity = 0;
|
|
2753
|
-
}
|
|
2754
|
-
seedInto(data, count) {
|
|
2755
|
-
const seeds = generateSeeds(this.rng, GPU_N2, count);
|
|
2756
|
-
for (const s of seeds) {
|
|
2757
|
-
for (let di = -GPU_SEED_R; di <= GPU_SEED_R; di++) {
|
|
2758
|
-
for (let dj = -GPU_SEED_R; dj <= GPU_SEED_R; dj++) {
|
|
2759
|
-
if (di * di + dj * dj > GPU_SEED_R * GPU_SEED_R) continue;
|
|
2760
|
-
const x = (s.x + di + GPU_N2) % GPU_N2;
|
|
2761
|
-
const y = (s.y + dj + GPU_N2) % GPU_N2;
|
|
2762
|
-
const idx = (x + y * GPU_N2) * 2;
|
|
2763
|
-
data[idx] = 0.5;
|
|
2764
|
-
data[idx + 1] = 0.25;
|
|
2765
|
-
}
|
|
2766
|
-
}
|
|
2767
|
-
}
|
|
2768
|
-
}
|
|
2769
|
-
reseed() {
|
|
2770
|
-
const seeds = generateSeeds(this.rng, GPU_N2, RESEED_BATCH);
|
|
2771
|
-
const buf = this.parity === 0 ? this.bufA : this.bufB;
|
|
2772
|
-
const cell = new Float32Array([0.5, 0.25]);
|
|
2773
|
-
for (const s of seeds) {
|
|
2774
|
-
for (let di = -GPU_SEED_R; di <= GPU_SEED_R; di++) {
|
|
2775
|
-
for (let dj = -GPU_SEED_R; dj <= GPU_SEED_R; dj++) {
|
|
2776
|
-
if (di * di + dj * dj > GPU_SEED_R * GPU_SEED_R) continue;
|
|
2777
|
-
const x = (s.x + di + GPU_N2) % GPU_N2;
|
|
2778
|
-
const y = (s.y + dj + GPU_N2) % GPU_N2;
|
|
2779
|
-
this.device.queue.writeBuffer(buf, (x + y * GPU_N2) * 8, cell);
|
|
2780
|
-
}
|
|
2781
|
-
}
|
|
2782
|
-
}
|
|
2783
|
-
}
|
|
2784
|
-
sizeCanvas() {
|
|
2785
|
-
this.canvas.width = Math.max(1, Math.round(this.w * this.dpr));
|
|
2786
|
-
this.canvas.height = Math.max(1, Math.round(this.h * this.dpr));
|
|
2787
|
-
}
|
|
2788
|
-
simStep() {
|
|
2789
|
-
if (this.lost) return;
|
|
2790
|
-
const wgCount = Math.ceil(GPU_N2 / WG);
|
|
2791
|
-
const enc = this.device.createCommandEncoder();
|
|
2792
|
-
for (let i = 0; i < GPU_STEPS; i++) {
|
|
2793
|
-
const bg = this.parity === 0 ? this.simBG_AB : this.simBG_BA;
|
|
2794
|
-
const pass = enc.beginComputePass();
|
|
2795
|
-
pass.setPipeline(this.simPL);
|
|
2796
|
-
pass.setBindGroup(0, bg);
|
|
2797
|
-
pass.dispatchWorkgroups(wgCount, wgCount);
|
|
2798
|
-
pass.end();
|
|
2799
|
-
this.parity ^= 1;
|
|
2800
|
-
}
|
|
2801
|
-
this.device.queue.submit([enc.finish()]);
|
|
2802
|
-
}
|
|
2803
|
-
render() {
|
|
2804
|
-
if (this.lost) return;
|
|
2805
|
-
const tex = this.gpuCtx.getCurrentTexture();
|
|
2806
|
-
const enc = this.device.createCommandEncoder();
|
|
2807
|
-
const pass = enc.beginRenderPass({
|
|
2808
|
-
colorAttachments: [{
|
|
2809
|
-
view: tex.createView(),
|
|
2810
|
-
loadOp: "clear",
|
|
2811
|
-
storeOp: "store",
|
|
2812
|
-
clearValue: { r: 0, g: 0, b: 0, a: 1 }
|
|
2813
|
-
}]
|
|
2814
|
-
});
|
|
2815
|
-
pass.setPipeline(this.renderPL);
|
|
2816
|
-
pass.setBindGroup(0, this.parity === 0 ? this.renderBG_A : this.renderBG_B);
|
|
2817
|
-
pass.draw(3);
|
|
2818
|
-
pass.end();
|
|
2819
|
-
this.device.queue.submit([enc.finish()]);
|
|
2820
|
-
}
|
|
2821
|
-
start() {
|
|
2822
|
-
if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
|
|
2823
|
-
this.loop();
|
|
2824
|
-
}
|
|
2825
|
-
stop() {
|
|
2826
|
-
if (this.frameId !== null) {
|
|
2827
|
-
cancelAnimationFrame(this.frameId);
|
|
2828
|
-
this.frameId = null;
|
|
2829
|
-
}
|
|
2830
|
-
}
|
|
2831
|
-
loop() {
|
|
2832
|
-
this.frameId = requestAnimationFrame(() => this.loop());
|
|
2833
|
-
this.frameCount++;
|
|
2834
|
-
if (this.frameCount % RESEED_INTERVAL === 0) this.reseed();
|
|
2835
|
-
this.simStep();
|
|
2836
|
-
this.render();
|
|
2837
|
-
}
|
|
2838
|
-
renderStill() {
|
|
2839
|
-
for (let i = 0; i < 200; i++) {
|
|
2840
|
-
this.simStep();
|
|
2841
|
-
}
|
|
2842
|
-
this.render();
|
|
2843
|
-
}
|
|
2844
|
-
setPaused(paused) {
|
|
2845
|
-
if (paused) this.stop();
|
|
2846
|
-
else this.start();
|
|
2847
|
-
}
|
|
2848
|
-
resize(width, height, dpr) {
|
|
2849
|
-
this.w = width;
|
|
2850
|
-
this.h = height;
|
|
2851
|
-
if (dpr !== void 0) this.dpr = Math.min(dpr, 2);
|
|
2852
|
-
this.sizeCanvas();
|
|
2853
|
-
if (!this.lost) {
|
|
2854
|
-
this.gpuCtx.configure({
|
|
2855
|
-
device: this.device,
|
|
2856
|
-
format: this.format,
|
|
2857
|
-
alphaMode: "opaque"
|
|
2858
|
-
});
|
|
2859
|
-
this.render();
|
|
2860
|
-
}
|
|
2861
|
-
}
|
|
2862
|
-
dispose() {
|
|
2863
|
-
this.stop();
|
|
2864
|
-
this.bufA.destroy();
|
|
2865
|
-
this.bufB.destroy();
|
|
2866
|
-
this.canvas.remove();
|
|
2867
|
-
this.device.destroy();
|
|
2868
|
-
}
|
|
2869
|
-
};
|
|
2870
|
-
|
|
2871
|
-
// src/reaction-diffusion.ts
|
|
2872
|
-
var reactionDiffusionManifest = {
|
|
2873
|
-
id: "reaction-diffusion",
|
|
2874
|
-
label: "Reaction Diffusion",
|
|
2875
|
-
passthrough: false,
|
|
2876
|
-
minBackend: "canvas2d",
|
|
2877
|
-
costTier: "low",
|
|
2878
|
-
motionIntensity: "calm",
|
|
2879
|
-
reducedMotionFallback: "static",
|
|
2880
|
-
a11y: { flashSafe: true }
|
|
2881
|
-
};
|
|
2882
|
-
var N2 = 256;
|
|
2883
|
-
var STEPS = 32;
|
|
2884
|
-
var ReactionDiffusionCPU = class {
|
|
2885
|
-
ctxSaver;
|
|
2886
|
-
canvas;
|
|
2887
|
-
gc;
|
|
2888
|
-
buf;
|
|
2889
|
-
bufCtx;
|
|
2890
|
-
img;
|
|
2891
|
-
w;
|
|
2892
|
-
h;
|
|
2893
|
-
u = new Float32Array(N2 * N2);
|
|
2894
|
-
v = new Float32Array(N2 * N2);
|
|
2895
|
-
uN = new Float32Array(N2 * N2);
|
|
2896
|
-
vN = new Float32Array(N2 * N2);
|
|
2897
|
-
rng;
|
|
2898
|
-
frameId = null;
|
|
2899
|
-
frameCount = 0;
|
|
2900
|
-
constructor(ctx) {
|
|
2901
|
-
this.ctxSaver = ctx;
|
|
2902
|
-
this.w = ctx.width;
|
|
2903
|
-
this.h = ctx.height;
|
|
2904
|
-
this.rng = ctx.rng;
|
|
2905
|
-
let canvas;
|
|
2906
|
-
if (ctx.surface) {
|
|
2907
|
-
canvas = ctx.surface;
|
|
2908
|
-
} else {
|
|
2909
|
-
const el = document.createElement("canvas");
|
|
2910
|
-
el.style.cssText = "display:block;width:100%;height:100%";
|
|
2911
|
-
el.setAttribute("aria-hidden", "true");
|
|
2912
|
-
el.setAttribute("data-rd-backend", "canvas2d");
|
|
2913
|
-
ctx.host.appendChild(el);
|
|
2914
|
-
canvas = el;
|
|
2915
|
-
}
|
|
2916
|
-
this.canvas = canvas;
|
|
2917
|
-
const gc = canvas.getContext("2d", { alpha: false });
|
|
2918
|
-
if (!gc) throw new Error("reaction-diffusion: no 2d context");
|
|
2919
|
-
this.gc = gc;
|
|
2920
|
-
this.buf = document.createElement("canvas");
|
|
2921
|
-
this.buf.width = N2;
|
|
2922
|
-
this.buf.height = N2;
|
|
2923
|
-
this.bufCtx = this.buf.getContext("2d");
|
|
2924
|
-
this.img = new ImageData(N2, N2);
|
|
2925
|
-
this.u.fill(1);
|
|
2926
|
-
applySeedsCPU(this.u, this.v, N2, generateSeeds(this.rng, N2, SEED_COUNT), SEED_R);
|
|
2927
|
-
this.sizeCanvas();
|
|
2928
|
-
if (ctx.reducedMotion) {
|
|
2929
|
-
this.renderStill();
|
|
2930
|
-
} else {
|
|
2931
|
-
this.start();
|
|
2932
|
-
}
|
|
2933
|
-
}
|
|
2934
|
-
sizeCanvas() {
|
|
2935
|
-
const dpr = Math.min(this.ctxSaver.dpr, 2);
|
|
2936
|
-
this.canvas.width = Math.max(1, Math.round(this.w * dpr));
|
|
2937
|
-
this.canvas.height = Math.max(1, Math.round(this.h * dpr));
|
|
2938
|
-
this.gc.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
2939
|
-
}
|
|
2940
|
-
simStep() {
|
|
2941
|
-
const { u, v, uN, vN } = this;
|
|
2942
|
-
for (let y = 0; y < N2; y++) {
|
|
2943
|
-
for (let x = 0; x < N2; x++) {
|
|
2944
|
-
const idx = x + y * N2;
|
|
2945
|
-
const lx = (x - 1 + N2) % N2 + y * N2;
|
|
2946
|
-
const rx = (x + 1) % N2 + y * N2;
|
|
2947
|
-
const uy = x + (y - 1 + N2) % N2 * N2;
|
|
2948
|
-
const dy = x + (y + 1) % N2 * N2;
|
|
2949
|
-
const lapU = u[lx] + u[rx] + u[uy] + u[dy] - 4 * u[idx];
|
|
2950
|
-
const lapV = v[lx] + v[rx] + v[uy] + v[dy] - 4 * v[idx];
|
|
2951
|
-
const uvv = u[idx] * v[idx] * v[idx];
|
|
2952
|
-
uN[idx] = Math.max(0, Math.min(
|
|
2953
|
-
1,
|
|
2954
|
-
u[idx] + RD_DT * (RD_DU * lapU - uvv + RD_F * (1 - u[idx]))
|
|
2955
|
-
));
|
|
2956
|
-
vN[idx] = Math.max(0, Math.min(
|
|
2957
|
-
1,
|
|
2958
|
-
v[idx] + RD_DT * (RD_DV * lapV + uvv - (RD_F + RD_K) * v[idx])
|
|
2959
|
-
));
|
|
2960
|
-
}
|
|
2961
|
-
}
|
|
2962
|
-
[this.u, this.uN] = [this.uN, this.u];
|
|
2963
|
-
[this.v, this.vN] = [this.vN, this.v];
|
|
2964
|
-
}
|
|
2965
|
-
render() {
|
|
2966
|
-
const data = this.img.data;
|
|
2967
|
-
for (let i = 0; i < N2 * N2; i++) {
|
|
2968
|
-
const [r, g, b] = colorV(this.v[i]);
|
|
2969
|
-
const pi = i * 4;
|
|
2970
|
-
data[pi] = r;
|
|
2971
|
-
data[pi + 1] = g;
|
|
2972
|
-
data[pi + 2] = b;
|
|
2973
|
-
data[pi + 3] = 255;
|
|
2974
|
-
}
|
|
2975
|
-
this.bufCtx.putImageData(this.img, 0, 0);
|
|
2976
|
-
this.gc.imageSmoothingEnabled = true;
|
|
2977
|
-
this.gc.drawImage(this.buf, 0, 0, this.w, this.h);
|
|
2978
|
-
}
|
|
2979
|
-
start() {
|
|
2980
|
-
if (this.frameId !== null || typeof requestAnimationFrame === "undefined") return;
|
|
2981
|
-
this.loop();
|
|
2982
|
-
}
|
|
2983
|
-
stop() {
|
|
2984
|
-
if (this.frameId !== null) {
|
|
2985
|
-
cancelAnimationFrame(this.frameId);
|
|
2986
|
-
this.frameId = null;
|
|
2987
|
-
}
|
|
2988
|
-
}
|
|
2989
|
-
loop() {
|
|
2990
|
-
this.frameId = requestAnimationFrame(() => this.loop());
|
|
2991
|
-
this.frameCount++;
|
|
2992
|
-
if (this.frameCount % RESEED_INTERVAL === 0) {
|
|
2993
|
-
applySeedsCPU(
|
|
2994
|
-
this.u,
|
|
2995
|
-
this.v,
|
|
2996
|
-
N2,
|
|
2997
|
-
generateSeeds(this.rng, N2, RESEED_BATCH),
|
|
2998
|
-
SEED_R
|
|
2999
|
-
);
|
|
3000
|
-
}
|
|
3001
|
-
for (let i = 0; i < STEPS; i++) this.simStep();
|
|
3002
|
-
this.render();
|
|
3003
|
-
}
|
|
3004
|
-
renderStill() {
|
|
3005
|
-
for (let i = 0; i < 200; i++) this.simStep();
|
|
3006
|
-
this.render();
|
|
3007
|
-
}
|
|
3008
|
-
setPaused(paused) {
|
|
3009
|
-
if (paused) this.stop();
|
|
3010
|
-
else this.start();
|
|
3011
|
-
}
|
|
3012
|
-
resize(width, height, dpr) {
|
|
3013
|
-
this.w = width;
|
|
3014
|
-
this.h = height;
|
|
3015
|
-
if (dpr !== void 0) this.ctxSaver.dpr = dpr;
|
|
3016
|
-
this.sizeCanvas();
|
|
3017
|
-
this.render();
|
|
3018
|
-
}
|
|
3019
|
-
dispose() {
|
|
3020
|
-
this.stop();
|
|
3021
|
-
if (typeof HTMLCanvasElement !== "undefined" && this.canvas instanceof HTMLCanvasElement) this.canvas.remove();
|
|
3022
|
-
}
|
|
3023
|
-
};
|
|
3024
|
-
var reactionDiffusion = {
|
|
3025
|
-
manifest: reactionDiffusionManifest,
|
|
3026
|
-
async mount(ctx) {
|
|
3027
|
-
try {
|
|
3028
|
-
const adapter = await navigator.gpu?.requestAdapter();
|
|
3029
|
-
if (adapter) {
|
|
3030
|
-
const device = await adapter.requestDevice();
|
|
3031
|
-
return new ReactionDiffusionGPU(ctx, device);
|
|
3032
|
-
}
|
|
3033
|
-
} catch {
|
|
3034
|
-
}
|
|
3035
|
-
return new ReactionDiffusionCPU(ctx);
|
|
3036
|
-
}
|
|
3037
|
-
};
|
|
3038
|
-
|
|
3039
1075
|
// src/index.ts
|
|
3040
1076
|
var CLASSIC_SAVERS = [
|
|
3041
1077
|
toasters,
|
|
@@ -3055,7 +1091,8 @@ var CLASSIC_SAVERS = [
|
|
|
3055
1091
|
bsod,
|
|
3056
1092
|
flurry,
|
|
3057
1093
|
fluid,
|
|
3058
|
-
reactionDiffusion
|
|
1094
|
+
reactionDiffusion,
|
|
1095
|
+
mystify
|
|
3059
1096
|
];
|
|
3060
1097
|
export {
|
|
3061
1098
|
CLASSIC_SAVERS,
|
|
@@ -3071,6 +1108,7 @@ export {
|
|
|
3071
1108
|
logo,
|
|
3072
1109
|
messages,
|
|
3073
1110
|
messages2,
|
|
1111
|
+
mystify,
|
|
3074
1112
|
pipes,
|
|
3075
1113
|
rainstorm,
|
|
3076
1114
|
reactionDiffusion,
|