consider this damn slow and unoptimized p5.js code snippet. It outputs a self painting…
- User
consider this damn slow and unoptimized p5.js code snippet. It outputs a self painting watercolor effect with ink expanding on canvas. I want you to optimize it, write the full p5.js source code. It must not refer to external assets or files, the source must be self- contained. I want you to use webgl and pixel shaders where possible.
// *** forked and modded from here: // *** https://editor.p5js.org/StevesMakerspace/sketches/d0lPUJt8T
let xyoffAdd = 0.18; // 0.08 default - lower # = straighter lines; changing Perlin noise variables let coloroffAdd = 5.5; // 0.5 default let pixelSkip = 12; // 12 default let colorSkip = 7; // 7 default let dryTime = 0.0012; //0.0012 default //let startWidth = 40; let runnyColors = false; let canvasSmall = true; // for true, set width & height below. A smaller canvas will run faster. Setting to false will fill the available space. let canvWidth = 480; let canvHeight = 270; let paintDrop = 50; // fixed brush width let paint = []; let tempPaint1 = []; let tempPaint2 = []; let posX, posY, prevPosX, prevPosY; let p = 1; let xoff, yoff, roff, goff, boff, poff; let randR, randG, randB; function setup() { //let fs = fullscreen(); // fullscreen(!fs); randR = random(255); randG = random(255); randB = random(255); xoff = random(40000); // Perlin noise seed starts yoff = random(40000); roff = random(40000); goff = random(40000); boff = random(40000); poff = random(40000); pixelDensity(1);
if (canvasSmall ==true){ createCanvas(canvWidth,canvHeight) } else { createCanvas(round(windowWidth*0.98) , round(windowHeight*0.93)); } background(220); posX = prevPosX = round(random(width)); posY = prevPosY = round(random(height)); // fill the arrays with black color for (let x = 0; x < width; x++) { for (let y = 0; y < height; y++) { paint.push(250, 240, 220, 0); } } tempPaint1 = paint; tempPaint2 = paint; }
function draw() { if (p > 0) { addPaint(); updater(); render(); } }
//function windowResized() { // resizeCanvas(canvWidth, canvHeight); //} // paint new color function addPaint() { xoff += xyoffAdd; yoff += xyoffAdd; roff += coloroffAdd; goff += coloroffAdd; boff += coloroffAdd; // using Perlin noise to smoothly change position and color posX += round((noise(xoff) - 0.5) * pixelSkip); posY += round((noise(yoff) - 0.5) * pixelSkip); randR += round((noise(roff) - 0.5) * colorSkip); randG += round((noise(goff) - 0.5) * colorSkip); randB += round((noise(boff) - 0.5) * colorSkip);
// wrap around edges and colors if (posX > width) { posX = 0; } if (posX < 0) { posX = width; } if (posY > height) { posY = 0; } if (posY < 0) { posY = height; } if (randR > 255) { randR = 0; } if (randR < 0) { randR = 255; } if (randG > 255) { randG = 0; } if (randG < 0) { randG = 255; } if (randB > 255) { randB = 0; } if (randB < 0) { randB = 255; } let distance = dist(prevPosX, prevPosY, posX, posY); if (distance < width * 0.9 && distance < height *0.9){ let numPoints = floor(distance / 1); // larger number = more gaps and fewer points; these two lines from George Profenza, noted below. drawLinePoints(prevPosX, prevPosY, posX, posY, numPoints); } prevPosX = posX; prevPosY = posY; }
- Assistant
- User
please continue from here
shader.setUniform('posY', posY); shader.setUniform('prevPosX', prevPosX); shader.setUniform('prevPosY', prevPosY); shader.setUniform
- Assistant
('time', time);
The above code sets four uniform variables in the shader: 'posY', 'prevPosX', 'prevPosY', and 'time'.
'posY' is the current Y position of the mouse cursor, which is passed as a parameter to the function.
'prevPosX' and 'prevPosY' are the previous X and Y positions of the mouse cursor, respectively, which are stored in the 'prevPosX' and 'prevPosY' variables.
'time' is a variable that stores the current time in milliseconds since the program started running.
These uniform variables can be used in the shader code to modify the behavior of the program based on the current position of the mouse and the time that has elapsed since the program started. For example, the shader code might use the position of