setup()
setup() runs once when the sketch starts and is where you initialize everything: create canvases, load assets, set up the 3D scene, and register event listeners. In this game, setup() bridges p5.js and three.js by creating both rendering systems and connecting them to the same container.
function setup() {
p5Canvas = createCanvas(windowWidth, windowHeight);
p5Canvas.parent('three-canvas-container');
p5Canvas.style('position', 'absolute');
p5Canvas.style('top', '0');
p5Canvas.style('left', '0');
p5Canvas.style('z-index', '1');
shootSound = new p5.Oscillator();
shootSound.setType('sawtooth');
shootSound.freq(300);
shootSound.amp(0);
shootSound.start();
initThreeJS();
document.addEventListener('keydown', onKeyDown, false);
document.addEventListener('keyup', onKeyUp, false);
document.addEventListener('click', onDocumentClick, false);
document.addEventListener('pointerlockchange', onPointerLockChange, false);
document.addEventListener('mozpointerlockchange', onPointerLockChange, false);
document.addEventListener('webkitpointerlockchange', onPointerLockChange, false);
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
p5Canvas = createCanvas(windowWidth, windowHeight);
Creates a transparent p5.js canvas that overlays the three.js 3D scene for rendering UI elements
p5Canvas.style('position', 'absolute');
Positions the p5 canvas absolutely so it sits directly on top of the three.js renderer without affecting layout
shootSound = new p5.Oscillator();
Creates a sawtooth-wave oscillator that will play a harsh sound when the player shoots
document.addEventListener('keydown', onKeyDown, false);
Registers keyboard and mouse input handlers to detect player movement (WASD) and shooting (click)
p5Canvas = createCanvas(windowWidth, windowHeight);- Creates a p5.js canvas that fills the entire window; this will be used for drawing UI elements (score, crosshair, etc.)
p5Canvas.parent('three-canvas-container');- Attaches the p5 canvas to the same DOM container as the three.js renderer, allowing both to share the same space
p5Canvas.style('position', 'absolute');- Makes the p5 canvas positioned absolutely, so it overlays the three.js canvas without affecting page layout
p5Canvas.style('z-index', '1');- Sets the p5 canvas to appear on top of the three.js renderer (which has z-index 0), so UI is visible over the 3D game
shootSound = new p5.Oscillator();- Creates an oscillator object that will generate sound when the player shoots
shootSound.setType('sawtooth');- Sets the oscillator to a sawtooth waveform, which produces a harsh, buzzy sound suitable for a shooter game
shootSound.freq(300);- Sets the oscillator frequency to 300 Hz, which is a low-mid pitch for the shooting sound
shootSound.amp(0);- Starts the oscillator at zero volume (silent) so it's ready to play but not audible until needed
shootSound.start();- Starts the oscillator running; it will remain silent until amp() is changed to a positive value
initThreeJS();- Calls the function that sets up the three.js scene, camera, renderer, lights, floor, and controls
document.addEventListener('keydown', onKeyDown, false);- Registers a listener for keyboard key-down events so movement keys (WASD) can be detected
document.addEventListener('click', onDocumentClick, false);- Registers a listener for mouse clicks, which trigger both shooting (if in-game) and pointer lock requests (to start the game)
document.addEventListener('pointerlockchange', onPointerLockChange, false);- Detects when pointer lock is gained or lost, allowing the game to respond to pointer lock state changes (e.g., pressing Escape)