xxxxxxxxxx
//Perspective
let cam;
// Global Variables
var zdist; // Variable for camera distance
// Sphere Variables
var xOffS = 0;
var yOffS = 1;
var zOffS = 2;
// Audio Variables
var music; // Sound file
var fft; // Fast Fourier Transform for audio analysis
// 3D Model Variables
var rabbit; // 3D model of a rabbit
// Preload function to load external assets
function preload(){
// Preload for Music and Audio Visualization
music = loadSound("Lilith.mp3"); // Load the sound file
fft = new p5.FFT(); // Initialize the FFT for audio analysis
// Preload for 3D Model
rabbit = loadModel("Rabbit.obj", true); // Load 3D model (rabbit)
}
// Setup function to initialize the canvas
function setup() {
// Setup canvas
createCanvas(windowWidth, windowHeight, WEBGL); // Create a 3D canvas
angleMode(DEGREES); // Use degrees for rotation angles
}
// Draw function, continuously executed to render graphics
function draw() {
background(0); // Set background color to black
// Variables
zdist = height/2; // Set the camera distance to half of the canvas height
// Audio Analysis
var wave = fft.waveform(); // Get the waveform of the audio signal
fft.analyze(); // Analyze the audio spectrum
amp = fft.getEnergy(20,200); // Get energy from the audio spectrum in a specific range
//Perspective
var fov = 760 + amp*0.15;
perspective(fov, width / height, 80, fov*10);
// Audio Visualization #1
push();
translate(0, 0,(-500-height/4)-zdist);
for(k=-1; k<=1; k+=2){
beginShape();
stroke(0,255,0); // Set stroke color to green
strokeWeight(4+amp*0.005); // Set stroke weight
for(k2=0; k2<=180; k2++){
var ws = 1000;
var index = floor(map(k2,0,180,0,wave.length-1));
var wr = map(wave[index], -1, 1, 140, 800+amp*0.4);
var wx = wr*sin(k2)*k;
var wy = wr*cos(k2);
noFill();
vertex(wx,wy);
}
endShape();
}
pop();
// Audio Visualization #2
push();
translate(0, 0,(-500-height/4)-zdist);
for(k=-1; k<=1; k+=2){
beginShape();
noStroke();
for(k2=0; k2<=180; k2++){
var ws = 1000;
var index = floor(map(k2,0,180,0,wave.length-1));
var wr = map(wave[index], -1, 1, 140, 400);
var wx = wr*sin(k2)*k;
var wy = wr*cos(k2);
fill(255,0,0,95);
vertex(wx,wy);
}
endShape();
}
pop();
// Sphere
for(i=-1; i<=0; i++){
push();
noFill();
strokeWeight(3);
translate(0,0,-zdist);
rotateX(millis()/50);
rotateY(millis()/50);
if(i==-1){
stroke(0,0,255); // Set stroke color to blue for the first sphere
}else{
stroke(255,0,0); // Set stroke color to red for the second sphere
}
sphere(((height/2)*0.8)-(i*40)+amp*-0.4);
pop();
}
// Exosphere
push();
noFill();
strokeWeight(2);
translate(0,0,-zdist);
rotateX(millis()/50*-1);
rotateY(millis()/50);
stroke(0,0,255);
sphere(((height/2)*0.8)*20);
rotateX(millis()/50);
rotateY(millis()/50*-1);
stroke(255,0,0);
sphere(((height/2)*0.8)*24);
pop();
// 3D Model
push();
stroke(255);
noFill();
//directionalLight(255,255,255,0.5,0.5,-4); // Add directional light
//ambientMaterial(255,255,255); // Set ambient material color
//shininess(80); // Set shininess
translate(0,0,-zdist);
rotate(180);
var rotspeed = 10;
rotateX(frameCount+20);
rotateY(frameCount+20);
model(rabbit); // Display the 3D rabbit model
pop();
}
// Function triggered on mouse click to play/pause music and animation
function mouseClicked(){
if(music.isPlaying()){
music.pause(); // Pause the music
noLoop(); // Stop the animation
}else{
music.play(); // Play the music
loop(); // Resume the animation
}
}
function keyPressed(){
if(key === "s"){
saveCanvas("rabbit_hole.jpg");
}
}