xxxxxxxxxx
32
let grainShader;
let imageToGrain;
function preload() {
grainShader = loadShader("grain.vert", "grain.frag");
}
function setup() {
createCanvas(400, 400, WEBGL);
pixelDensity(5);
// Just create an image to pass into the shader
// This is just grey, but will be your artwork
imageToGrain = createGraphics(width, height);
imageToGrain.pixelDensity(pixelDensity());
imageToGrain.background(127);
// Set the image into the shader memory
shader(grainShader);
grainShader.setUniform("image", imageToGrain);
grainShader.setUniform("pixD", pixelDensity());
grainShader.setUniform("seed", random(100));
}
function draw() {
// Update how much variance in the grain based on mouse x pos
grainShader.setUniform("grainVary", mouseX/width);
// Draw geometry to use the shader
rect(-width/2, height/2, width, height);
}