Advertisement

Interactive 3D Workspace Diorama

| by Vladimir | 2 min read | code by Ricardo Oliva Alonso
Intermediate

Tech & Dependencies

HTML SCSS JavaScript
three.js gltf-loader orbit-controls

Features

  • Baked Lighting
  • Constrained Orbit
  • GLTF Loader
  • Performance Optimized

Browser Support

Chrome 60+ Edge 79+ Firefox 55+ Safari 11+

Core

We often treat web content as flat information, devoid of spatial context. This component creates a digital sanctuary. By combining soft, pre-calculated lighting with a constrained interactive camera, we invite the user to inhabit a space, not just view it.

Core Technique

The visual richness here does not stem from computationally expensive real-time lights. It creates an illusion using Baked Textures and Geometric Constraints.

  • The Baked Texture Strategy: The scene contains zero active light sources (AmbientLight, DirectionalLight, etc.). Instead, all shadows, bounces, and soft gradients are painted directly onto a single texture map (baked.jpg). We map this onto the geometry using MeshBasicMaterial. This approach looks expensive but renders as cheaply as a simple image, maintaining 60 FPS on almost any device.
  • Curated Camera Angles: Unlike standard 3D viewers that let you get lost, this camera is disciplined. By setting minAzimuthAngle, maxAzimuthAngle, and maxPolarAngle, we lock the user’s perspective to the “fourth wall,” ensuring the composition always looks perfect and preventing them from clipping through the floor.

Customization

Because the lighting is baked, changing the “mood” implies changing the texture. However, the feel of the interaction is entirely code-driven.

Adjusting the Viewing Angles. You can widen the freedom of movement or lock it down further to a specific diorama view.

// Controls configuration
controls.enableDamping = true; 
controls.minDistance = 5;  // Allow closer zoom
controls.maxDistance = 30; // Allow further zoom

// Unlock rotation (remove limits to spin 360)
// controls.minAzimuthAngle = -Infinity; 
// controls.maxAzimuthAngle = Infinity;

Changing the Background. Match the canvas background to your site’s theme for seamless integration.

body {
    /* Use a hex code or CSS variable */
    background-color: #2a2a2a; 
}

Browser Support

This relies on WebGL via Three.js. It is supported by all modern browsers.

Advertisement