https://download.csdn.net/download/weixinyi21cn/81313709
参考: https://thecodingtrain.com/CodingChallenges/102-2d-water-ripple.html https://web.archive.org/web/20160418004149/http://freespace.virgin.net/hugo.elias/graphics/x_water.htm
参考核心代码
let cols;
let rows;
let current;
let previous;
let dampening = 0.99;
function setup() {
pixelDensity(1);
createCanvas(600, 400);
cols = width;
rows = height;
current = new Array(cols).fill(0).map(n => new Array(rows).fill(0));
previous = new Array(cols).fill(0).map(n => new Array(rows).fill(0));
}
function mouseDragged() {
previous[mouseX][mouseY] = 2500;
}
function draw() {
background(0);
loadPixels();
for (let i = 1; i < cols - 1; i++) {
for (let j = 1; j < rows - 1; j++) {
current[i][j] =
(previous[i - 1][j] +
previous[i + 1][j] +
previous[i][j - 1] +
previous[i][j + 1]) /
2 -
current[i][j];
current[i][j] = current[i][j] * dampening;
let index = (i + j * cols) * 4;
pixels[index + 0] = current[i][j];
pixels[index + 1] = current[i][j];
pixels[index + 2] = current[i][j];
}
}
updatePixels();
let temp = previous;
previous = current;
current = temp;
}
|