-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-fluid-simulation-202603250302.html
More file actions
423 lines (355 loc) · 14.1 KB
/
Copy pathai-fluid-simulation-202603250302.html
File metadata and controls
423 lines (355 loc) · 14.1 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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>流体模拟 - Fluid Simulation</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow: hidden;
background: #000;
font-family: 'Arial', sans-serif;
}
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
}
.info {
position: fixed;
bottom: 20px;
left: 20px;
color: rgba(100, 200, 255, 0.8);
font-size: 14px;
z-index: 100;
pointer-events: none;
text-shadow: 0 0 15px rgba(100, 200, 255, 0.6);
}
.title {
position: fixed;
top: 30px;
left: 50%;
transform: translateX(-50%);
color: rgba(100, 200, 255, 0.9);
font-size: 32px;
font-weight: bold;
z-index: 100;
text-shadow: 0 0 30px rgba(100, 200, 255, 0.8);
letter-spacing: 5px;
}
.fps {
position: fixed;
top: 20px;
right: 20px;
color: rgba(100, 200, 255, 0.6);
font-size: 12px;
font-family: monospace;
z-index: 100;
}
@media (max-width: 768px) {
.info {
font-size: 11px;
bottom: 10px;
left: 10px;
}
.title {
font-size: 20px;
top: 15px;
}
}
</style>
</head>
<body>
<div class="title">🌊 流体模拟</div>
<canvas id="fluid"></canvas>
<div class="info">
<div>🖱️ 拖动鼠标 - 产生流动</div>
<div>👆 点击 - 产生涟漪</div>
</div>
<div class="fps" id="fps">FPS: 60</div>
<script>
// 性能监控
const fpsElement = document.getElementById('fps');
let lastTime = performance.now();
let frameCount = 0;
// Canvas 设置
const canvas = document.getElementById('fluid');
const ctx = canvas.getContext('2d');
let width, height;
let mouseX = 0, mouseY = 0;
let isMouseDown = false;
// 流体网格
const GRID_SCALE = 5;
let gridWidth, gridHeight;
let density, densityPrev;
let velocityX, velocityY, velocityXPrev, velocityYPrev;
// 参数
const DIFFUSION = 0.0001;
const VISCOSITY = 0.0000001;
const DT = 0.1;
const ITERATIONS = 4;
// 颜色配置
const COLORS = {
low: { r: 0, g: 50, b: 100 },
high: { r: 100, g: 200, b: 255 },
};
// 响应式调整
function resize() {
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
initFluid();
}
// 初始化流体
function initFluid() {
gridWidth = Math.ceil(width / GRID_SCALE);
gridHeight = Math.ceil(height / GRID_SCALE);
const size = gridWidth * gridHeight;
density = new Float32Array(size);
densityPrev = new Float32Array(size);
velocityX = new Float32Array(size);
velocityY = new Float32Array(size);
velocityXPrev = new Float32Array(size);
velocityYPrev = new Float32Array(size);
}
// 获取网格索引
function IX(x, y) {
x = Math.max(0, Math.min(gridWidth - 1, x));
y = Math.max(0, Math.min(gridHeight - 1, y));
return x + y * gridWidth;
}
// 设置边界条件
function setBnd(b, x) {
for (let i = 1; i < gridWidth - 1; i++) {
x[IX(i, 0)] = b === 2 ? -x[IX(i, 1)] : x[IX(i, 1)];
x[IX(i, gridHeight - 1)] = b === 2 ? -x[IX(i, gridHeight - 2)] : x[IX(i, gridHeight - 2)];
}
for (let j = 1; j < gridHeight - 1; j++) {
x[IX(0, j)] = b === 1 ? -x[IX(1, j)] : x[IX(1, j)];
x[IX(gridWidth - 1, j)] = b === 1 ? -x[IX(gridWidth - 2, j)] : x[IX(gridWidth - 2, j)];
}
x[IX(0, 0)] = 0.5 * (x[IX(1, 0)] + x[IX(0, 1)]);
x[IX(0, gridHeight - 1)] = 0.5 * (x[IX(1, gridHeight - 1)] + x[IX(0, gridHeight - 2)]);
x[IX(gridWidth - 1, 0)] = 0.5 * (x[IX(gridWidth - 2, 0)] + x[IX(gridWidth - 1, 1)]);
x[IX(gridWidth - 1, gridHeight - 1)] = 0.5 * (x[IX(gridWidth - 2, gridHeight - 1)] + x[IX(gridWidth - 1, gridHeight - 2)]);
}
// 扩散
function diffuse(b, x, x0, diff) {
const a = DT * diff * (gridWidth - 2) * (gridHeight - 2);
linearSolve(b, x, x0, a, 1 + 6 * a);
}
// 线性求解
function linearSolve(b, x, x0, a, c) {
const cRecip = 1.0 / c;
for (let k = 0; k < ITERATIONS; k++) {
for (let j = 1; j < gridHeight - 1; j++) {
for (let i = 1; i < gridWidth - 1; i++) {
x[IX(i, j)] = (x0[IX(i, j)] + a * (
x[IX(i + 1, j)] + x[IX(i - 1, j)] +
x[IX(i, j + 1)] + x[IX(i, j - 1)]
)) * cRecip;
}
}
setBnd(b, x);
}
}
// 平流
function advect(b, d, d0, velocX, velocY) {
let i0, i1, j0, j1;
const dtx = DT * (gridWidth - 2);
const dty = DT * (gridHeight - 2);
let s0, s1, t0, t1;
let tmp1, tmp2, x, y;
for (let j = 1; j < gridHeight - 1; j++) {
for (let i = 1; i < gridWidth - 1; i++) {
tmp1 = dtx * velocX[IX(i, j)];
tmp2 = dty * velocY[IX(i, j)];
x = i - tmp1;
y = j - tmp2;
if (x < 0.5) x = 0.5;
if (x > gridWidth + 0.5) x = gridWidth + 0.5;
i0 = Math.floor(x);
i1 = i0 + 1;
if (y < 0.5) y = 0.5;
if (y > gridHeight + 0.5) y = gridHeight + 0.5;
j0 = Math.floor(y);
j1 = j0 + 1;
s1 = x - i0;
s0 = 1 - s1;
t1 = y - j0;
t0 = 1 - t1;
const i0i = Math.floor(i0);
const i1i = Math.floor(i1);
const j0i = Math.floor(j0);
const j1i = Math.floor(j1);
d[IX(i, j)] =
s0 * (t0 * d0[IX(i0i, j0i)] + t1 * d0[IX(i0i, j1i)]) +
s1 * (t0 * d0[IX(i1i, j0i)] + t1 * d0[IX(i1i, j1i)]);
}
}
setBnd(b, d);
}
// 投影
function project(velocX, velocY, p, div) {
for (let j = 1; j < gridHeight - 1; j++) {
for (let i = 1; i < gridWidth - 1; i++) {
div[IX(i, j)] = -0.5 * (
velocX[IX(i + 1, j)] - velocX[IX(i - 1, j)] +
velocY[IX(i, j + 1)] - velocY[IX(i, j - 1)]
) / gridWidth;
p[IX(i, j)] = 0;
}
}
setBnd(0, div);
setBnd(0, p);
linearSolve(0, p, div, 1, 6);
for (let j = 1; j < gridHeight - 1; j++) {
for (let i = 1; i < gridWidth - 1; i++) {
velocX[IX(i, j)] -= 0.5 * (p[IX(i + 1, j)] - p[IX(i - 1, j)]) * gridWidth;
velocY[IX(i, j)] -= 0.5 * (p[IX(i, j + 1)] - p[IX(i, j - 1)]) * gridHeight;
}
}
setBnd(1, velocX);
setBnd(2, velocY);
}
// 更新流体
function updateFluid() {
// 扩散速度
diffuse(1, velocityXPrev, velocityX, VISCOSITY);
diffuse(2, velocityYPrev, velocityY, VISCOSITY);
// 投影
project(velocityXPrev, velocityYPrev, velocityX, velocityY);
// 平流速度
advect(1, velocityX, velocityXPrev, velocityXPrev, velocityYPrev);
advect(2, velocityY, velocityYPrev, velocityXPrev, velocityYPrev);
// 投影
project(velocityX, velocityY, velocityXPrev, velocityYPrev);
// 扩散密度
diffuse(0, densityPrev, density, DIFFUSION);
// 平流密度
advect(0, density, densityPrev, velocityX, velocityY);
// 衰减
for (let i = 0; i < density.length; i++) {
density[i] *= 0.99;
}
}
// 添加密度和速度
function addDensity(x, y, amount) {
const gridX = Math.floor(x / GRID_SCALE);
const gridY = Math.floor(y / GRID_SCALE);
density[IX(gridX, gridY)] += amount;
}
function addVelocity(x, y, amountX, amountY) {
const gridX = Math.floor(x / GRID_SCALE);
const gridY = Math.floor(y / GRID_SCALE);
velocityX[IX(gridX, gridY)] += amountX;
velocityY[IX(gridX, gridY)] += amountY;
}
// 绘制流体
function drawFluid() {
const imageData = ctx.createImageData(width, height);
for (let j = 0; j < gridHeight; j++) {
for (let i = 0; i < gridWidth; i++) {
const d = density[IX(i, j)];
const r = COLORS.low.r + (COLORS.high.r - COLORS.low.r) * d;
const g = COLORS.low.g + (COLORS.high.g - COLORS.low.g) * d;
const b = COLORS.low.b + (COLORS.high.b - COLORS.low.b) * d;
// 填充网格区域
for (let py = 0; py < GRID_SCALE; py++) {
for (let px = 0; px < GRID_SCALE; px++) {
const pixelX = i * GRID_SCALE + px;
const pixelY = j * GRID_SCALE + py;
if (pixelX < width && pixelY < height) {
const idx = (pixelY * width + pixelX) * 4;
imageData.data[idx] = r;
imageData.data[idx + 1] = g;
imageData.data[idx + 2] = b;
imageData.data[idx + 3] = 255;
}
}
}
}
}
ctx.putImageData(imageData, 0, 0);
}
// 主动画循环
function animate(currentTime) {
// 更新流体
updateFluid();
// 绘制流体
drawFluid();
// FPS 计算
frameCount++;
if (frameCount >= 30) {
const fps = Math.round(30 / ((currentTime - lastTime) / 1000 * 30));
fpsElement.textContent = `FPS: ${Math.min(fps, 60)}`;
frameCount = 0;
}
lastTime = currentTime;
requestAnimationFrame(animate);
}
// 事件监听
window.addEventListener('resize', resize);
canvas.addEventListener('mousemove', (e) => {
const prevMouseX = mouseX;
const prevMouseY = mouseY;
mouseX = e.clientX;
mouseY = e.clientY;
if (isMouseDown) {
const dx = mouseX - prevMouseX;
const dy = mouseY - prevMouseY;
addDensity(mouseX, mouseY, 10);
addVelocity(mouseX, mouseY, dx * 5, dy * 5);
}
});
canvas.addEventListener('mousedown', () => {
isMouseDown = true;
});
canvas.addEventListener('mouseup', () => {
isMouseDown = false;
});
canvas.addEventListener('click', (e) => {
// 产生涟漪
for (let i = -2; i <= 2; i++) {
for (let j = -2; j <= 2; j++) {
addDensity(e.clientX + i * 10, e.clientY + j * 10, 50);
addVelocity(e.clientX + i * 10, e.clientY + j * 10, i * 20, j * 20);
}
}
});
canvas.addEventListener('touchmove', (e) => {
e.preventDefault();
const touch = e.touches[0];
const dx = touch.clientX - mouseX;
const dy = touch.clientY - mouseY;
mouseX = touch.clientX;
mouseY = touch.clientY;
addDensity(mouseX, mouseY, 10);
addVelocity(mouseX, mouseY, dx * 5, dy * 5);
});
canvas.addEventListener('touchstart', (e) => {
e.preventDefault();
const touch = e.touches[0];
mouseX = touch.clientX;
mouseY = touch.clientY;
// 产生涟漪
for (let i = -2; i <= 2; i++) {
for (let j = -2; j <= 2; j++) {
addDensity(mouseX + i * 10, mouseY + j * 10, 50);
addVelocity(mouseX + i * 10, mouseY + j * 10, i * 20, j * 20);
}
}
});
// 初始化
resize();
mouseX = width / 2;
mouseY = height / 2;
requestAnimationFrame(animate);
</script>
</body>
</html>