|
| 1 | +const cubeSize = 50; // Size of each small cube |
| 2 | + |
| 3 | +function setup() { |
| 4 | + createCanvas(windowWidth, windowHeight, WEBGL); |
| 5 | + textSize(40); |
| 6 | + frameRate(60); |
| 7 | + |
| 8 | + createEasyCam( |
| 9 | + { |
| 10 | + distance: 400, |
| 11 | + // rotation: [0.2, -1, 0, -0.25], |
| 12 | + rotation: [0.9, -0.185, -0.365, 0.068], |
| 13 | + }, |
| 14 | + 500 |
| 15 | + ); |
| 16 | + |
| 17 | + // Suppress right-click context menu |
| 18 | + document.oncontextmenu = function () { |
| 19 | + return false; |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +function windowResized() { |
| 24 | + resizeCanvas(windowWidth, windowHeight); |
| 25 | +} |
| 26 | + |
| 27 | +function draw() { |
| 28 | + background(21); |
| 29 | + lights(); |
| 30 | + |
| 31 | + const overlayText = document.getElementById("text-overlay"); |
| 32 | + overlayText.textContent = `FPS: ${Number(frameRate()).toFixed(4)}`; |
| 33 | + |
| 34 | + for (let x = -1; x <= 1; x++) { |
| 35 | + for (let y = -1; y <= 1; y++) { |
| 36 | + for (let z = -1; z <= 1; z++) { |
| 37 | + push(); |
| 38 | + translate(x * cubeSize, y * cubeSize, z * cubeSize); |
| 39 | + draw3DCube(x, y, z); |
| 40 | + pop(); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function draw3DCube(x, y, z) { |
| 47 | + noStroke(); // Disable edges for the planes |
| 48 | + drawFace(0, 0, cubeSize / 2, [255, 0, 0], 0, 0); // Front face (red) |
| 49 | + drawFace(0, 0, -cubeSize / 2, [255, 165, 0], 0, PI); // Back face (orange) |
| 50 | + drawFace(cubeSize / 2, 0, 0, [255, 255, 0], HALF_PI, 0); // Right face (yellow) |
| 51 | + drawFace(-cubeSize / 2, 0, 0, [255, 255, 255], -HALF_PI, 0); // Left face (white) |
| 52 | + drawFace(0, -cubeSize / 2, 0, [0, 0, 255], 0, -HALF_PI); // Top face (blue) |
| 53 | + drawFace(0, cubeSize / 2, 0, [0, 255, 0], 0, HALF_PI); // Bottom face (green) |
| 54 | + |
| 55 | + drawCubeOutline(); // Draw the black outline for the entire cube |
| 56 | +} |
| 57 | + |
| 58 | +function drawFace(tx, ty, tz, faceColor, rotY, rotX) { |
| 59 | + push(); |
| 60 | + translate(tx, ty, tz); |
| 61 | + rotateY(rotY); |
| 62 | + rotateX(rotX); |
| 63 | + fill(faceColor); |
| 64 | + beginShape(); |
| 65 | + vertex(-cubeSize / 2, -cubeSize / 2, 0); |
| 66 | + vertex(cubeSize / 2, -cubeSize / 2, 0); |
| 67 | + vertex(cubeSize / 2, cubeSize / 2, 0); |
| 68 | + vertex(-cubeSize / 2, cubeSize / 2, 0); |
| 69 | + endShape(CLOSE); |
| 70 | + pop(); |
| 71 | +} |
| 72 | + |
| 73 | +function drawCubeOutline() { |
| 74 | + stroke(0); // Black outline |
| 75 | + strokeWeight(2); |
| 76 | + noFill(); |
| 77 | + box(cubeSize); // Use `box()` to draw the outline of the cube |
| 78 | +} |
0 commit comments