xxxxxxxxxx
86
// a shader variable
let theShader;
const MAX_POINTS = 150;
let points = [];
function preload(){
// load the shader
theShader = loadShader('worley.vert', 'worley.frag');
}
function setup() {
// shaders require WEBGL mode to work
createCanvas(800, 800, WEBGL);
colorMode(HSB, 100);
for(let i = 0; i < MAX_POINTS; i++) {
points[i] = {
vector: createVector(random(), random(), random()),
color: color(random(100), random(100), 100)
};
}
//console.log(points.map(v => [v.x, v.y]).flat());
}
function squad(sl, st, sr, sb) {
let z = -1;
if ((sl > 6) || (st > 6) || (sr >6) || (sb > 6)) return;
if ((sl == 0) && (st == 0) && (sr == 0) && (sb == 0)) {
z =-0.5;
}
sl = constrain(sl, 0, 1);
st = constrain(st, 0, 1);
sr = constrain(sr, 0, 1);
sb = constrain(sb, 0, 1);
quad(-sl, -sb, z, sr, -sb, z, sr, st, z, -sl, st, z);
}
function draw() {
background(255);
noStroke();
// shader() sets the active shader with our shader
shader(theShader);
theShader.setUniform('points', points.map(v => [v.vector.x, v.vector.y, 0.0]).flat());
theShader.setUniform('colors', points.map(v => [1, 1, 1]).flat());
theShader.setUniform('z', 0.0);
theShader.setUniform('reverse', false);
let scale = (360 - frameCount) / 60;
squad(1, 1, scale, scale, -1);
theShader.setUniform('points', points.map(v => [v.vector.x, v.vector.y, v.vector.z]).flat());
theShader.setUniform('z', (sin(frameCount * 0.01) + 1) * 0.5);
scale = (800 - frameCount) / 60;
squad(scale, 1, 1, scale, -0.9);
theShader.setUniform('points', points.map(v => [v.vector.x, v.vector.y, 0.0]).flat());
colorMode(RGB, 1);
theShader.setUniform('colors', points.map(v => [v.color._getRed(), v.color._getGreen(), v.color._getBlue()]).flat());
theShader.setUniform('z', 0.0);
theShader.setUniform('reverse', true);
scale = (1240 - frameCount) / 60;
squad(1, scale, scale, 1, -0.8);
theShader.setUniform('points', points.map(v => [v.vector.x, v.vector.y, v.vector.z]).flat());
theShader.setUniform('z', (sin(frameCount * 0.01) + 1) * 0.5);
scale = (1680 - frameCount) / 60;
squad(scale, scale, 1, 1, -0.7);
for(let v of points) {
//stroke(0, 255, 0);
//strokeWeight(8);
//point(v.x * width - width / 2, -(v.y * height - height / 2));
v.vector.x += random(-2, 2) / width;
v.vector.y += random(-2, 2) / height;
v.vector.z += random(-2, 2) / height;
}
// print out the framerate
// print(frameRate());
}