setup()
setup() runs once when the sketch starts. Use it to initialize your canvas, set default styles, and call any scene initialization functions. In this game, setup() prepares both the p5.js UI layer and the three.js 3D graphics layer.
function setup() {
// Create p5.js canvas for 2D UI overlay
createCanvas(800, 600);
frameRate(60);
textFont("Arial");
textAlign(CENTER, CENTER);
// Initialize three.js scene and objects
setup3D();
// User must interact to start audio
userStartAudio(); // Required for p5.sound to work in modern browsers
}
Line-by-line explanation (6 lines)
🔧 Subcomponents:
createCanvas(800, 600);
Creates an 800x600 pixel p5.js canvas that will sit on top of the three.js renderer
setup3D();
Calls the setup3D function to initialize the three.js scene, cameras, lights, and 3D objects
createCanvas(800, 600);- Creates an 800×600 pixel canvas managed by p5.js. This canvas will float above the three.js rendering canvas to draw UI elements.
frameRate(60);- Locks the frame rate to 60 FPS, ensuring consistent game updates and smooth animation.
textFont("Arial");- Sets the font for all text drawn by p5.js to Arial, keeping the UI consistent.
textAlign(CENTER, CENTER);- Centers all text horizontally and vertically by default, making it easier to position UI elements.
setup3D();- Calls the setup3D() function, which initializes the three.js scene, renderer, cameras, lights, and all 3D objects (office, doors, lights, animatronics).
userStartAudio();- A p5.sound function required by modern browsers—allows audio to play only after user interaction, satisfying browser security policies.