xxxxxxxxxx
117
/*
☐☐☐☐☐☐☐☐
☐Rubik-Ception☐
☐☐☐☐☐☐☐☐
by Danny Baghdasarians
*/
// Declaring our Variables
let boxSize;
let arraySize;
let spacingSize;
let rotateAmount;
let cubeTexture;
let camZoom;
let camZAxis;
let cam;
let mouseLock;
function preload() {
cubeTexture = loadImage("Rubiks.png");
}
function setup() {
createCanvas(500, 500, WEBGL);
noStroke();
// Centering our Array
translate(
0 - arraySize * boxSize + spacingSize * 2,
0 - arraySize * boxSize + spacingSize * 2,
0
);
boxSize = 0;
arraySize = 4;
spacingSize = 40;
rotateAmount = 0.00005;
camZoom = 0;
camZAxis = 0;
cam = createCamera();
mouseLock = false;
}
function draw() {
zoomCamera();
setupLights();
createArray();
}
//
// Creating our Array with 3 Nested For Loops
//
function createArray() {
// Rotating our Array
rotateX((frameCount * rotateAmount) / 0.005);
rotateY((frameCount * rotateAmount) / 0.005);
// Increase our Box Size
if (boxSize <= 15) {
boxSize += 0.025;
}
// Actual Creation of our Array
for (let x = 0; x < arraySize; x += 0.5) {
rotateX(frameCount * x * rotateAmount);
for (let y = 0; y < arraySize; y += 0.5) {
rotateY(frameCount * y * rotateAmount);
for (let z = 0; z < arraySize; z += 0.5) {
push();
specularMaterial(250);
shininess(50);
texture(cubeTexture);
translate(x * spacingSize, y * spacingSize, z * spacingSize);
rotateX((frameCount * x * rotateAmount) / 10);
rotateY((frameCount * z * rotateAmount) / 10);
box(boxSize);
pop();
}
}
}
}
//
// Creating RGB Point Lighting and Ambient Light
//
function setupLights() {
let r = random(0, 255);
let g = random(0, 255);
let b = random(0, 255);
ambientLight(150);
pointLight(r, g, b, 0, 0, 50);
}
//
// Zooming our Camera Towards center of Array
//
function zoomCamera() {
cam.move(0, 0, frameCount * camZoom);
camZAxis = frameCount * camZoom;
if (camZAxis >= 0) {
camZoom = -0.0005;
} else if (camZAxis <= -0.4) {
camZoom = 0.0005;
}
}