xxxxxxxxxx
375
let angle = 0;
let cam;
let bubbles = [];
var song;
var buttonShowTime;
var button;
var fft, peakDetect,ellipseWidth;
let pulseSize = 0;
let detailX=3;
let detailXlist, detailXval;
let starcolor = [[255,255,255],[204,153,255],[102,178,255],[255,153,204]]
let colorCount = 0
let bg = 0
var volhistory = [];
let colors = [];
// black hole
let blackHoleRadius=5;
//particle
var num = 2000;
var noiseScale=500, noiseStrength=1;
var particles = [num];
function preload() {
song = loadSound('adagio_cut2.mp3')
}
function setup() {
createCanvas(800, 800, WEBGL);
gravity = createVector(0, 0.2);
button = createButton("Play");
button.mousePressed(togglePlay);
fft = new p5.FFT(0.9,256);
peakDetect = new p5.PeakDetect();
song.play();
console.log(starcolor)
for (let x =0; x < 11; x ++) {
bubbles.push(new Bubble(cos(x*36)*200,sin(x*36)*200,-800,5,starcolor[colorCount],1))
colorCount = (colorCount +1)%4;
}
// phase1
detailXlist = [11,11.1,11.2,11.3,43,43.1,43.2,43.3]
detailXval = [1,2,3,4,5,6,7,8]
for (let i = 0; i < detailXlist.length; i += 1) {
song.addCue(detailXlist[i],changeDetailX,detailXval[i]);
}
amp = new p5.Amplitude()
noStroke()
for (let i=0; i<num; i++) {
//x value start slightly outside the right of canvas, z value how close to viewer
var loc = createVector(random(width*1.2), random(height), 2);
var angle = 0; //any value to initialize
var dir = createVector(cos(angle), sin(angle));
var speed = random(0.5,2);
// var speed = random(5,map(mouseX,0,width,5,20)); // faster
particles[i]= new Particle(loc, dir, speed);
}
}
function changeDetailX(val){
console.log(val);
bubbles.push(new Bubble(cos(val)*200,sin(val)*200,-800,5,255,2))
}
function togglePlay(){
if (!song.isPlaying()){
song.play();
button.html("Play");
} else {
song.pause();
button.html("Pause")
}
}
function createPulse(){
pulseSize = 250;
}
class Bubble {
constructor(_x,_y,_z,_sz,_color,_mark){
this.x = _x+random(-60,60);
this.y = _y+random(-60,60);
this.z = _z+random(-60,60);
this.sz = _sz;
this.angle = 0;
this.color = _color;
this.oricolor = _color;
this.mark = _mark;
}
display() {
push();
noStroke();
translate(this.x,this.y,this.z);
rotateX(this.angle);
rotateY(this.angle);
rotateZ(this.angle * 0.75);
// normalMaterial();
ambientLight(0);
emissiveMaterial(this.color);
sphere(this.sz)
this.angle += 0.025
pop();
}
display2() {
push();
noStroke();
translate(this.x,this.y,this.z);
rotateX(7.5);
// normalMaterial();
ambientLight(0);
emissiveMaterial(255,51,51);
cone(20,40)
this.angle += 0.025
pop();
}
move(){
// this.x += random(-5,5);
// this.y += random(-5,5);
if (frameCount%20==0){
this.z += 50;
} else {
this.z+= 10;
}
this.sz+= random(-2,2)
}
move2(){
// this.x += random(-5,5);
// this.y += random(-5,5);
this.z += 100;
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
camera(0, 0, (height/2) / tan(PI/6), 0, 0, 0, 0, 1, 0)
// phase1
if (song.currentTime() <= 67) {
background(bg);
push();
var spectrum = fft.analyze()
// rotateX(millis() / 1000);
rotateX(millis() / 1000);
fill(0)
stroke(255)
sphere(blackHoleRadius,10,10);
if (song.currentTime() <= 60) {
blackHoleRadius += 0.1;
} else {
blackHoleRadius += 1
}
// console.log(spectrum)
pop();
push();
rotateX(millis() / 1000);
rotateZ(millis() / 1000);
beginShape();
// fill(255)
noFill()
stroke(255)
// vertex(cos(0),sin(0))
for (let x = 0; x < spectrum.length; x++){
var ampspe = spectrum[x]
var i = map(x,0,spectrum.length,0,6.28319)
var y = map(ampspe,0,256,blackHoleRadius,blackHoleRadius+50)
var y2 = map(ampspe,0,256,blackHoleRadius,blackHoleRadius+100)
vertex(cos(i)*y,sin(i)*y);
vertex(cos(i)*(y2),sin(i)*(y2));
}
vertex(cos(6.28319),sin(6.28319))
endShape()
pop();
// adding new objects
if (frameCount%30==0){
for (let x =0; x < 11; x ++) {
bubbles.push(new Bubble(cos(x*36)*200,sin(x*36)*200,-800,5,starcolor[colorCount],1))
colorCount = (colorCount +1)%4;
}
}
// display and move
for (let x =0; x < bubbles.length; x ++) {
if (bubbles[x].mark == 1){
bubbles[x].display();
bubbles[x].move()
} else{
bubbles[x].display2();
bubbles[x].move2()
}
}
if (bubbles.length>200){
bubbles.splice(0,10)
}
fft.analyze();
peakDetect.update(fft);
if ( peakDetect.isDetected ) {
ellipseWidth = 50;
} else {
ellipseWidth *= 0.95;
}
// sphere(ellipseWidth);
// push();
// // update torus radius
// // draw torus
// noStroke();
// rotateY(millis() / 1000);
// rotateX(millis() / 1000);
// ambientLight(60);
// // add point light to showcase specular material
// let locX = mouseX - width / 2;
// let locY = mouseY - height / 2;
// pointLight(0, 0, 255, locX, locY, 50);
// pointLight(255, 0, 0, -locX, -locY, 50);
// ambientMaterial(255);
// shininess(255);
// // var vol = amp.getLevel();
// // torus(vol*100, 10+vol*10, detailX, 10);
// pop();
} else {
// phase 2
if (67<song.currentTime() < 74){
for (let x =0; x < bubbles.length; x ++) {
bubbles[x].display();
bubbles[x].move()
}
if (bubbles.length>200){
bubbles.splice(0,10)
}
else{
translate(-400,-400)
push();
// var vol = amp.getLevel();
var spectrum = fft.analyze()
stroke(255);
noFill();
beginShape();
maxP = max(spectrum)
volhistory.push(maxP)
for (let i = 0; i < volhistory.length; i++) {
var h = map(volhistory[i],0,255,height,0);
vertex(i,h);
}
endShape();
if (volhistory.length>200){
volhistory.splice(0,1)
}
pop();
push();
fill(0, 10);
noStroke();
rect(0, 0, width, height);
for (let i=0; i<particles.length; i++) {
particles[i].run();
}
pop();
}
}
}
}
class Particle{
constructor(_loc,_dir,_speed){
this.loc = _loc;
this.dir = _dir;
this.speed = _speed;
// var col;
}
run() {
this.move();
this.checkEdges();
this.update();
}
move(){
// let angle=noise(this.loc.x/noiseScale, this.loc.y/noiseScale, frameCount/noiseScale)*TWO_PI*noiseStrength; //0-2PI
let angle=noise(this.loc.x/noiseScale, this.loc.y/noiseScale, frameCount/noiseScale)*TWO_PI*noiseStrength; //0-2PI
this.dir.x = cos(angle);
this.dir.y = sin(angle);
var vel = this.dir.copy();
var d =1; //direction change
vel.mult(this.speed*d); //vel = vel * (speed*d)
this.loc.add(vel); //loc = loc + vel
}
checkEdges(){
//float distance = dist(width/2, height/2, loc.x, loc.y);
//if (distance>150) {
if (this.loc.x<0 || this.loc.x>800 || this.loc.y<0 || this.loc.y>800) {
this.loc.x = random(800*1.2);
this.loc.y = random(800);
}
}
update(){
fill(255);
ellipse(this.loc.x, this.loc.y, this.loc.z);
}
}