ADVERTISEMENT

Three.js Split-Screen Ski Race: One Keyboard, Two Cameras

Learn how to build a local split-screen ski race in Three.js using dual cameras, viewport management, scissor tests, and shared physics.
Three.js Split-Screen Ski Race: One Keyboard, Two Cameras
Official GAMEHTML5 editorial documentation: Three.js Split-Screen Ski Race: One Keyboard, Two Cameras

GAMEHTML5 — Implementing local multiplayer in browser-based WebGL experiences often presents unique rendering and input challenges. When developing Northbound, our Christmas browser game featuring a local downhill ski race, we needed a seamless way to let two players share a single keyboard—Santa using WASD and Holly using the arrow keys—while racing down the same snowy mountain in real time. Rather than rendering separate browser instances or sacrificing screen estate, we leveraged Three.js to render two distinct camera viewports into a single WebGL canvas every frame.

Table of Contents

Dual-Camera & Scissor Rendering Architecture

The core mechanic relies on a single Three.js scene populated with terrain, trees, obstacles, and shared collectibles. Instead of clearing and rendering the entire scene once, the animation loop updates both skiers, repositions their respective tracking cameras, and executes two distinct render passes per frame.

GAMEHTML5 photo - Three.js Split-Screen Ski Race: One Keyboard, Two Cameras - Frame 2
Trackside action & vehicle telemetry: Three.js Split-Screen Ski Race: One Keyboard, Two Cameras

To divide the canvas cleanly without distortion, we utilize Three.js's renderer.setViewport() and renderer.setScissor() methods. Furthermore, updating each camera's aspect ratio dynamically based on the width of its assigned half ensures that proportions remain pristine during browser window resizing.

Read also:HTML5 Game Programming Fundamentals: Canvas 2D vs WebGL & Delta-Time Game Loops
// Example: Dual Camera Split-Screen Render Loop in Three.js
function renderSplitScreen(renderer, scene, camera1, camera2, width, height) {
  // Disable automatic clearing to allow multi-pass rendering
  renderer.autoClear = false;
  renderer.clear();

  // Left Viewport (Player 1: Santa)
  renderer.setViewport(0, 0, width / 2, height);
  renderer.setScissor(0, 0, width / 2, height);
  renderer.setScissorTest(true);
  camera1.aspect = (width / 2) / height;
  camera1.updateProjectionMatrix();
  renderer.render(scene, camera1);

  // Right Viewport (Player 2: Holly)
  renderer.setViewport(width / 2, 0, width / 2, height);
  renderer.setScissor(width / 2, 0, width / 2, height);
  renderer.setScissorTest(true);
  camera2.aspect = (width / 2) / height;
  camera2.updateProjectionMatrix();
  renderer.render(scene, camera2);
}

Local Multi-Input Management

Designing cooperative or competitive same-device multiplayer requires careful handling of JavaScript key events to prevent key-blocking or ghosting. By maintaining a persistent input state object, both players can execute simultaneous inputs without hardware conflict.

ADVERTISEMENT
const inputState = {
  p1: { up: false, down: false, left: false, right: false },
  p2: { up: false, down: false, left: false, right: false }
};

window.addEventListener('keydown', (e) => {
  switch(e.code) {
    case 'KeyW': inputState.p1.up = true; break;
    case 'KeyS': inputState.p1.down = true; break;
    case 'KeyA': inputState.p1.left = true; break;
    case 'KeyD': inputState.p1.right = true; break;
    case 'ArrowUp': inputState.p2.up = true; break;
    case 'ArrowDown': inputState.p2.down = true; break;
    case 'ArrowLeft': inputState.p2.left = true; break;
    case 'ArrowRight': inputState.p2.right = true; break;
  }
});

Shared Game State & Gift Collision Logic

Managing collectibles in a split-screen racing environment introduces interesting edge cases. In Northbound, gifts are placed along the shared downhill track. The race engine updates both player positions first, then evaluates where each skier’s movement path intersected the gift’s pickup radius during that frame.

The skier with the earlier contact timestamp claims the gift. In the rare event of an exact tie in both contact time and distance, priority alternates based on the gift's spatial coordinate index. Once claimed, the gift immediately disappears from both viewport renders.

Web Publisher SDK Integration

When preparing HTML5 games for leading distribution networks such as GameDistribution, CrazyGames Developer Portal, or Poki Developers, developers must ensure that local pause states correctly suspend audio and input listening when advertisement overlays or menu modals appear.

“Building split-screen WebGL games unlocks unique social dynamics on browser portals, merging arcade nostalgia with modern hardware acceleration.” — GAMEHTML5 Engineering Bureau

Frequently Asked Questions
How do you handle window resizing in a Three.js split-screen setup?
You must update the renderer size to match the canvas dimensions, then recalculate each camera's aspect ratio using half the total window width divided by the full height before calling updateProjectionMatrix().
Can three or four players share a single keyboard in WebGL games?
While technically possible, standard keyboards suffer from hardware 'ghosting' where simultaneous key presses in close proximity fail to register. For 3+ players, integrating Gamepad API support alongside keyboard controls is strongly recommended.
Enthusiast Discussion

What are your thoughts on implementing multi-camera viewports in WebGL? Share your technical critique in the comments below!

This comprehensive technical dossier was independently compiled, verified, and published by the GAMEHTML5 Global Editorial Bureau.

Source: dev.to
Notice an outdated SDK snippet or want to suggest a technical update for this guide?
Suggest Edit
ADVERTISEMENT

Discussion & Comments 1

Please share your perspective constructively and ethically. All reader contributions are moderated pursuant to the Motoring Editorial Standards & Discussion Policy.

Leave Your Perspective

Constructive public perspectives enrich democratic journalism.

0 / 2000 characters
W
WebGl_Wizard
3 days ago
Using scissor tests instead of creating two separate renderer instances is such a performance win. Brilliant breakdown!
ADVERTISEMENT
ADVERTISEMENT