xxxxxxxxxx
290
let canvas;
let flowfield;
let cellSize = 20;
let noiseScale = 0.0035 * 2;
const FORWARD_ONLY = true;
const SAVE = false;
const maxFrames = 240;
const EPS = 0.001;
let lines = [];
let reset = false;
let s;
let vx, vy;
let circuit, sine_waves, forward, boxy;
let bg;
let a = [0.5, 0.5, 0.5];
let c = [1.0, 1.0, 1.0];
let d = [0.6, 0.85, 1.0];
let BLACK;
function palette(t, a, b, c, d) {
return a+b*cos((t*c+d)*TAU);
}
function getColor(t) {
let r = palette(t, 1-a[0], a[0], c[0], d[0])*255;
let g = palette(t, 1-a[1], a[1], c[1], d[1])*255;
let b = palette(t, 1-a[2], a[2], c[2], d[2])*255;
return color(r, g, b);
}
function randColor() {
//if (RAINBOW_PALETTE)
// return color(random(255), random(255), random(255));
let t = random();
return getColor(t);
}
function smoothstep(edge0, edge1, x) {
if (x < 0)
return edge0;
if (x > 1)
return edge1;
return lerp(edge0, edge1, x * x * (3 - 2 * x));
}
function rand(co){
return fract(sin(co.x * 1718.9898 * 3131.313, co.y * 78.233 * 3313.313) * 43758.5453);
}
let p3x, p3y, p3z, pd;
function hash12(p)
{
p3x = fract(p.x * 0.1031);
p3y = fract(p.y * 0.1031);
p3z = fract(p.x * 0.1031);
pd = p3x * (p3y + 33.33) +
p3y * (p3z + 33.33) +
p3z * (p3x + 33.33);
p3x += pd;
p3y += pd;
p3z += pd;
return fract((p3x + p3y) * p3z);
}
function fbm(x, y, s=4) {
let qx = noise(x, y);
let qy = noise(x + 13.3, y + 52.3);
let rx = noise(x + s*qx + 32.3, y + s*qy - 12.2);
let ry = noise(x + s*qx + 82.3, y + s*qy - 32.2);
return noise(x + s*rx, y + s*ry);
}
function setup() {
createCanvas(600, 600).id("myCanvas");
bg = color(15);
background(bg);
if (SAVE)
frameRate(5);
// randomSeed(3);
// noiseSeed(4);
canvas = document.getElementById("myCanvas");
flowfield = new Flowfield(
floor(width / cellSize),
floor(height / cellSize), noiseScale);
BLACK = color(0, 0, 0);
a = [random(0.2, 0.8), random(0.2, 0.8), random(0.2, 0.8)];
c = [random(0.5, 1.0), random(0.5, 1.0), random(0.5, 1.0)];
d = [random(0.0, 1.0), random(0.0, 1.0), random(0.0, 1.0)];
noiseScale *= random(0.1, 2);
circuit = random(0, 1) < 0.4;
forward = FORWARD_ONLY ? true : random([true, false]);
sine_waves = random([true, false]);
boxy = random(0, 1) < 0.2;
let l = 8;
vx = 0.5;//random(0.2, 0.8);
vy = 0.5;//random(0.2, 0.8);
let s0 = random(1, 3);
let s1 = random(1, 3);
let s2 = random(3, 25);
let s3 = random(3, 25);
let d0 = random(0.12, 0.28);
let d1 = d0 + random(0.05, 0.12);
let d2 = d1 + random(0.05, 0.12);
let d3 = d2 + random(0.05, 0.12);
let rr = random(0, 500);
let w = 15;
let v = createVector();
s = streamlines({
boundingBox: {
left: 0, top: 0, width: width, height: height
},
seed: { x: vx*width, y: vy*height },
vectorField(p) {
//let v = flowfield.getFieldInterp(p.x, p.y);
//return { x: v.x, y: v.y };
let s = 0.015;
let nx = (p.x + (noise(p.x*s, p.y*s) - 0.5) * rr) / width - vx;
let ny = (p.y - (noise(p.y*s, p.x*s) - 0.5) * rr) / height - vy;
let px = p.x * noiseScale;
let py = p.y * noiseScale;
let x = p.x / width - vx;
let y = p.y / height - vy;
let dx = (noise(px - EPS, py) - noise(px + EPS, py)) / EPS;
let dy = (noise(px, py - EPS) - noise(px, py + EPS)) / EPS;
// let f = fbm(px * 3, py * 3, 0) * TAU;
// let dx = cos(f);
// let dy = sin(f);
// px *= 0.3;
// py *= 0.3;
// let dx = (fbm(px - EPS, py) - fbm(px + EPS, py)) / EPS;
// let dy = (fbm(px, py - EPS) - fbm(px, py + EPS)) / EPS;
// return { x: -dy, y: dx };
if (true) {
let temp = dx;
dx = -dy;
dy = temp;
}
let d = dist(x, y, 0, 0);
let a = atan2(y, x);
let a1 = x / d;
let a2 = y / d;
let na = noise(a1 * 5, a2 * 5);
let na2 = noise(a1 * 8 + 32.32, a2 * 8 + 23.32);
let na3 = noise(a1 * 12 - 22.32, a2 * 12 + 32.32);
if (d > d0) {
if (d > d1 || na > 0.5) {
dx = nx;
dy = ny;
} else {
dx = -ny;
dy = nx;
}
if (abs(d - d2) < 0.03 && na2 > 0.5) {
dx = -ny;
dy = nx;
} else if (abs(d - d3) < 0.05 && na3 > 0.5) {
dx = -ny;
dy = nx;
}
}
return { x: dx, y: dy };
// v.x = p.x;
// v.y = p.y;
// if (boxy) {
// v.x = floor(p.x / w) * w;
// v.y = floor(p.y / w) * w;
// }
// let x = v.x / width - vx;
// let y = v.y / height - vy;
// let ns = noiseScale / (dist(x, y, 0, 0) + 0.01);
// let nx = v.x * ns;
// let ny = v.y * ns;
// let n = constrain(
// (noise(nx, ny)-0.2) / 0.8, 0, 1);
// if (sine_waves) {
// ns = lerp(noiseScale / (dist(x, y, 0, 0) + 0.01), noiseScale, 0.95);
// ns *= 0.5;
// nx = v.x * ns;
// ny = v.y * ns;
// n = sin(nx * s0) + sin(ny * s1) +
// sin(nx * s2) + sin(ny * s3);
// }
if (circuit)
n = floor(n * l) / l;
n *= TAU;
return { x: cos(n), y: sin(n) };
},
dSep: 3.5,
dTest: 3,
timeStep: 3,
//onPointAdded:
// streamlines.renderTo(canvas),
onStreamlineAdded: onStreamlineAdded
});
s.run();
}
let i = 0;
function onStreamlineAdded(points) {
lines.push(new Streamline(points, i, frameCount));
i++;
}
let resetTime = 0;
function draw() {
for (let i = 0; i < lines.length; i++) {
if (frameCount - resetTime >= lines[i].frameTime) {
lines[i].update();
lines[i].draw();
}
/*
//
if (i == 0) {
lines[i].update();
lines[i].draw();
} else if (lines[i-1].finished) {
lines[i].update();
lines[i].draw();
}
*/
}
if (reset) {
for (let i = 0; i < lines.length; i++) {
lines[i].reset();
background(bg);
}
reset = false;
}
if (SAVE && frameCount <= maxFrames) {
let filename = nf(frameCount, 4, 0) + ".png";
save(filename);
}
}
function mousePressed() {
if (mouseButton == LEFT) {
reset = true;
resetTime = frameCount;
}
}