xxxxxxxxxx
266
var sketch = function(p) {
// An array with nodes
var nodes = [];
var nodeCount = 200;
p.setup = function() {
p.createCanvas(p.windowWidth, p.windowHeight);
p.noStroke();
// Create nodes
createNodes();
};
p.draw = function() {
p.fill(255, 20);
p.rect(0, 0, p.width, p.height);
p.fill(0);
for (var i = 0; i < nodes.length; i++) {
// Let all nodes repel each other
nodes[i].attractNodes(nodes);
// Apply velocity vector and update position
nodes[i].update();
// Draw node
p.ellipse(nodes[i].x, nodes[i].y, 10, 10);
}
};
p.keyPressed = function() {
if (p.key == 's' || p.key == 'S') p.saveCanvas(gd.timestamp(), 'png');
if (p.key == 'r' || p.key == 'R') {
p.background(255);
createNodes();
}
};
function createNodes() {
nodes = [];
for (var i = 0; i < nodeCount; i++) {
nodes.push(new Node(
p.width / 2 + p.random(-1, 1),
p.height / 2 + p.random(-1, 1),
5,
p.width - 5,
5,
p.height - 5
));
}
}
};
var myp5 = new p5(sketch);
let mic;
let fft;
let boxSz;
let gridSz;
let dropzone;
let pauseButton;
let playButton;
let maxBoxSz;
let minBoxSz;
let zTranslate;
let objRotateX = 0;
let objRotateY = 0;
let objRotateZ = 0;
let boomboom = 0;
let pathPoints = []
let w;
/*
function preload() {
song = loadSound('music/orangeevening.mp3');
}
*/
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
//background(255);
colorMode(HSB);
w = windowWidth/128 - 2;
mic = new p5.AudioIn();
mic.start();
//starts up audio analyzer
amplitude = new p5.Amplitude();
//0.9 smoothing and 64 frequency bands
fft = new p5.FFT(0.9, 128);
fft.setInput(mic);
}
//Draws the voxelized polygon
function draw() {
analyzer();
drawStuff();
}
function drawStuff(){
//Amount of voxels
let spectrum = fft.analyze();
//Gets level of amplitude
let level = amplitude.getLevel();
//Size of voxel sphere
boxSz = 64 + spectrum[0];
//console.log(level);
console.log(spectrum[0]);
//Changes amount of voxels depending on the frequency #
ambientLight(spectrum[0]);
for (let i = 0; i < spectrum.length; i++) {
if (spectrum[0] == 255) {
gridSz = 255 / 8;
} else if (spectrum[0] == 253) {
gridSz = 253 / 7;
} else if (spectrum[0] == 251) {
gridSz = 251 / 6;
} else if (spectrum[0] == 249) {
gridSz = 249 / 5;
} else if (spectrum[0] == 216) {
gridSz = 216 / 4;
} else if (spectrum[0] == 180) {
gridSz = 180 / 3;
} else if (spectrum[0] == 160) {
gridSz = 160 / 2;
} else if (spectrum[0] == 0) {
gridSz = 180/ 1;
}
maxBoxSz = spectrum[0];
minBoxSz = (spectrum[0])/16;
//boxSz = map(sin(frameCount * 0.025), -1, 1, minBoxSz, maxBoxSz);
}
zTranslate = -boxSz;
background(spectrum[90]-1, spectrum[60]-1, spectrum[30]-1);
translate(0, 0, zTranslate);
push();
//Speed of rotation
//Increase decimals to make shape spin faster
rotateY(frameCount * 0.015);
rotateZ(spectrum[0] * 0.0012);
rotateX(frameCount * 0.015);
let radius = boxSz - boxSz / 10;
//Sets up voxels
for (let x = -boxSz + gridSz; x <= boxSz - gridSz; x += gridSz) {
for (let y = -boxSz + gridSz; y <= boxSz - gridSz; y += gridSz) {
for (let z = -boxSz + gridSz; z <= boxSz - gridSz; z += gridSz) {
let d = dist(0, 0, 0, x, y, z);
if (d > radius - gridSz && d < radius) {
push();
translate(x, y, z);
normalMaterial();
box(gridSz);
pop();
}
}
}
}
pop();
push();
drawModel();
doAnimate();
objRotate();
pop();
boomboom = sin(frameCount*0.005);
}
function drawModel() {
let spectrum = fft.analyze();
let level = amplitude.getLevel();
var groupRadius = 2*((height/2) - (height/10));
var sphereCount = 512;
var sphereMaxR = 6;
// big sphere
//noFill(); // hmmm, seems like noFill has no effect on 3d yet
//stroke(255); // hmmm, seems like stroke has no effect on 3d yet
//basicMaterial(255,255,255);
//sphere(groupRadius, 20);
// smaller spheres
normalMaterial(32);
ambientLight(spectrum[0]);
for (var s=0; s < sphereCount; s++) {
var noiseCoord = s;
var rnd = lerp(-1,1,noise(noiseCoord));
//push();
// translate
rotateY(PI*rnd);
rotateZ(PI*rnd);
translate(groupRadius,0,0);
fill(random(255), random(255), random(255))
sphere(rnd * sphereMaxR, 40);
//reset translation
translate(-groupRadius,0,50*boomboom);
rotateY(-(PI*rnd));
rotateZ(-(PI*rnd));
//pop();
}
}
function doAnimate() {
//increment animation variables
let spectrum = fft.analyze();
let level = amplitude.getLevel();
objRotateX -= 0.1;
objRotateY -= 0.1;
objRotateZ -= 0.1;
}
function objRotate() {
rotateX(radians(objRotateX));
rotateY(radians(objRotateY));
rotateZ(radians(objRotateZ));
}
function analyzer(){
let spectrum = fft.analyze();
let level = amplitude.getLevel();
push();
stroke(255);
translate(0, 0, zTranslate);
for(var i = 0; i < spectrum.length; i++){
let amp1 = spectrum[i];
let y4 = map(amp1, 0, 256, windowHeight, 0);
rectMode(CENTER);
rect(i*w, y4, i*w, windowHeight - y4);
}
pop();
}