Prompt specification
Build or customize the Three.js 3D scene "Resource-substitution story sandbox" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Resource-substitution story sandbox
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/resource-substitution-story-sandbox-x3900
Scene Description Prompt:
Build a voxel home story where a power interruption prompts switching from electric activities to available independent alternatives. Three.js from cdnjs, one HTML file.
Working Three.js Starter (resource-substitution-story-sandbox-x3900):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>When the Lights Go Out — A Voxel Home Story</title>
<style>
:root {
--ink: #203039;
--paper: #fff7e9;
--orange: #ed754d;
--orange-dark: #b64d32;
--teal: #74c7c8;
--green: #8ec38b;
--panel: rgba(26, 38, 44, 0.92);
--muted: rgba(255, 247, 233, 0.68);
--border: rgba(255, 255, 255, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe8e5;
color: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
}
.grain {
position: fixed;
inset: 0;
pointer-events: none;
opacity: 0.16;
z-index: 2;
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 160 160' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.14'/%3E%3C/svg%3E");
mix-blend-mode: soft-light;
}
.topbar {
position: fixed;
inset: 0 0 auto 0;
z-index: 10;
display: flex;
justify-content: space-between;
align-items: flex-start;
padding: 24px 28px;
pointer-events: none;
}
.title-block {
color: var(--ink);
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.55);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 25px;
height: 3px;
background: var(--orange);
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.45);
}
h1 {
margin: 0;
max-width: 480px;
font-size: clamp(25px, 3.1vw, 47px);
line-height: 0.98;
letter-spacing: -0.055em;
font-weight: 850;
}
.subtitle {
margin-top: 9px;
color: rgba(32, 48, 57, 0.68);
font-size: 12px;
font-weight: 650;
letter-spacing: 0.025em;
}
.status-card {
pointer-events: auto;
width: 214px;
padding: 13px;
border: 1px solid rgba(255, 255, 255, 0.68);
border-radius: 18px;
color: var(--ink);
background: rgba(255, 250, 239, 0.79);
box-shadow: 0 14px 40px rgba(41, 56, 61, 0.13);
backdrop-filter: blur(14px);
}
.status-row {
display: flex;
align-items: center;
gap: 10px;
}
.status-dot {
width: 11px;
height: 11px;
flex: 0 0 auto;
border-radius: 50%;
background: #63b16c;
box-shadow: 0 0 0 5px rgba(99, 177, 108, 0.14);
transition: 0.4s ease;
}
body.is-dark .status-dot {
background: #df624b;
box-shadow: 0 0 0 5px rgba(223, 98, 75, 0.14);
}
.status-copy {
min-width: 0;
line-height: 1.15;
}
.status-copy strong {
display: block;
font-size: 13px;
}
.status-copy small {
display: block;
margin-top: 3px;
color: rgba(32, 48, 57, 0.58);
font-size: 10px;
white-space: nowrap;
}
#progress {
margin: 10px 0 9px;
padding-top: 9px;
border-top: 1px solid rgba(32, 48, 57, 0.1);
color: rgba(32, 48, 57, 0.66);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
#powerBtn {
width: 100%;
border: 0;
border-radius: 11px;
padding: 10px 12px;
color: #fffaf0;
background: var(--orange);
box-shadow: 0 5px 0 var(--orange-dark);
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
transition: transform 0.15s ease, opacity 0.2s ease;
}
#powerBtn:hover:not(:disabled) {
transform: translateY(-2px);
}
#powerBtn:active:not(:disabled) {
transform: translateY(2px);
box-shadow: 0 2px 0 var(--orange-dark);
}
#powerBtn:disabled {
opacity: 0.55;
cursor: default;
box-shadow: 0 3px 0 rgba(32, 48, 57, 0.22);
}
#phaseRail {
position: fixed;
left: 28px;
top: 132px;
z-index: 9;
display: flex;
gap: 7px;
pointer-events: none;
}
.phase-step {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.52);
border-radius: 999px;
color: rgba(32, 48, 57, 0.47);
background: rgba(255, 250, 239, 0.56);
backdrop-filter: blur(8px);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
transition: 0.25s ease;
}
.phase-step b {
display: grid;
width: 17px;
height: 17px;
place-items: center;
border-radius: 50%;
background: rgba(32, 48, 57, 0.1);
font-size: 9px;
}
.phase-step.current {
color: #fffaf0;
border-color: transparent;
background: var(--ink);
}
.phase-step.current b {
color: var(--ink);
background: var(--orange);
}
.phase-step.complete {
color: var(--ink);
border-color: rgba(99, 177, 108, 0.4);
background: rgba(220, 240, 217, 0.75);
}
#hotspotLayer {
position: fixed;
inset: 0;
z-index: 6;
pointer-events: none;
}
.hotspot {
position: absolute;
display: none;
align-items: center;
gap: 7px;
padding: 7px 10px;
transform: translate(-50%, -50%);
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 999px;
color: var(--ink);
background: rgba(255, 250, 239, 0.89);
box-shadow: 0 7px 22px rgba(31, 45, 51, 0.18);
backdrop-filter: blur(8px);
cursor: pointer;
pointer-events: auto;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.04em;
text-transform: uppercase;
white-space: nowrap;
}
.hotspot::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(116, 199, 200, 0.2);
}
.hotspot.analog::before {
background: var(--orange);
box-shadow: 0 0 0 4px rgba(237, 117, 77, 0.2);
}
.hotspot.done {
opacity: 0.68;
}
.hotspot.done::before {
background: var(--green);
}
#storyPanel {
position: fixed;
z-index: 12;
left: 50%;
bottom: 22px;
width: min(970px, calc(100% - 32px));
transform: translateX(-50%);
display: grid;
grid-template-columns: 96px minmax(250px, 1fr) minmax(350px, auto);
gap: 18px;
align-items: center;
padding: 16px 18px;
border: 1px solid var(--border);
border-radius: 22px;
color: var(--paper);
background: var(--panel);
box-shadow: 0 22px 70px rgba(20, 29, 33, 0.3);
backdrop-filter: blur(18px);
}
.speaker {
display: flex;
flex-direction: column;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.13em;
}
.avatar {
position: relative;
display: grid;
width: 50px;
height: 50px;
place-items: center;
border: 2px solid rgba(255, 255, 255, 0.18);
border-radius: 15px;
color: #fff7e9;
background: var(--orange);
box-shadow: 0 6px 0 rgba(0, 0, 0, 0.18);
font-size: 20px;
font-weight: 900;
}
.avatar::after {
content: "";
position: absolute;
right: -4px;
bottom: -4px;
width: 11px;
height: 11px;
border: 3px solid var(--panel);
border-radius: 50%;
background: #75c579;
}
#chapter {
margin-bottom: 4px;
color: var(--orange);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#storyText {
margin: 0;
color: rgba(255, 247, 233, 0.9);
font-size: 13px;
line-height: 1.48;
}
.choice-wrap {
min-width: 0;
}
#choiceTitle {
display: block;
margin: 0 0 8px 2px;
color: var(--muted);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#choices {
display: flex;
gap: 7px;
}
.choice {
min-width: 0;
flex: 1;
display: flex;
align-items: center;
gap: 8px;
padding: 9px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 12px;
color: var(--paper);
background: rgba(255, 255, 255, 0.07);
cursor: pointer;
text-align: left;
transition: transform 0.18s ease, background 0.18s ease, border-color 0.18s ease;
}
.choice:hover:not(:disabled) {
transform: translateY(-2px);
border-color: rgba(237, 117, 77, 0.7);
background: rgba(237, 117, 77, 0.12);
}
.choice.active {
border-color: var(--orange);
background: rgba(237, 117, 77, 0.16);
}
.choice.done {
border-color: rgba(142, 195, 139, 0.45);
background: rgba(142, 195, 139, 0.12);
}
.choice:disabled {
cursor: default;
opacity: 0.72;
}
.choice .key {
display: grid;
width: 22px;
height: 22px;
flex: 0 0 auto;
place-items: center;
border-radius: 7px;
color: var(--ink);
background: var(--paper);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
font-weight: 900;
}
.choice strong,
.choice small {
display: block;
white-space: nowrap;
}
.choice strong {
font-size: 10px;
line-height: 1.2;
}
.choice small {
margin-top: 2px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.final-card {
position: fixed;
z-index: 30;
left: 50%;
top: 47%;
width: min(410px, calc(100% - 34px));
padding: 27px;
transform: translate(-50%, -45%) scale(0.92);
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 25px;
color: var(--ink);
background: rgba(255, 250, 239, 0.94);
box-shadow: 0 30px 100px rgba(10, 18, 22, 0.38);
text-align: center;
opacity: 0;
visibility: hidden;
backdrop-filter: blur(18px);
transition: 0.45s cubic-bezier(0.2, 0.9, 0.2, 1);
}
.final-card.show {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
visibility: visible;
}
.final-icon {
display: grid;
width: 58px;
height: 58px;
margin: 0 auto 13px;
place-items: center;
border-radius: 18px;
color: #fff7e9;
background: var(--orange);
box-shadow: 0 7px 0 var(--orange-dark);
font-size: 26px;
}
.final-card h2 {
margin: 0;
font-size: 27px;
letter-spacing: -0.04em;
}
.final-card p {
margin: 11px auto 18px;
max-width: 330px;
color: rgba(32, 48, 57, 0.68);
font-size: 13px;
line-height: 1.55;
}
.final-tags {
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 6px;
margin-bottom: 20px;
}
.final-tags span {
padding: 6px 9px;
border-radius: 999px;
color: #365a49;
background: rgba(99, 177, 108, 0.15);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.04em;
text-transform: uppercase;
}
#replayBtn {
border: 0;
border-radius: 12px;
padding: 12px 20px;
color: #fff7e9;
background: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.09em;
text-transform: uppercase;
}
@media (max-width: 900px) {
#storyPanel {
grid-template-columns: 72px 1fr;
gap: 12px;
}
.choice-wrap {
grid-column: 1 / -1;
}
.speaker .avatar {
width: 44px;
height: 44px;
}
}
@media (max-width: 620px) {
.topbar {
padding: 15px;
}
h1 {
max-width: 220px;
font-size: 26px;
}
.subtitle,
#phaseRail {
display: none;
}
.status-card {
width: 151px;
padding: 10px;
border-radius: 15px;
}
.status-copy small {
max-width: 90px;
overflow: hidden;
text-overflow: ellipsis;
}
#progress {
margin: 7px 0;
padding-top: 7px;
}
#storyPanel {
bottom: 10px;
width: calc(100% - 20px);
grid-template-columns: 1fr;
gap: 9px;
padding: 13px;
border-radius: 18px;
}
.speaker {
display: none;
}
#storyText {
font-size: 11px;
line-height: 1.4;
}
#choices {
gap: 5px;
}
.choice {
padding: 7px 6px;
}
.choice .key {
display: none;
}
.choice strong {
font-size: 9px;
}
.choice small {
font-size: 7px;
}
.hotspot {
padding: 6px 8px;
font-size: 8px;
}
}
@media (max-height: 650px) {
#phaseRail {
display: none;
}
#storyPanel {
bottom: 9px;
padding: 10px 14px;
}
.speaker {
display: none;
}
#storyPanel {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel home during a power interruption"></canvas>
<div class="grain"></div>
<header class="topbar">
<div class="title-block">
<div class="eyebrow">A small home story</div>
<h1>When the Lights<br />Go Out</h1>
<div class="subtitle">Drag to orbit · Scroll to zoom · Select a choice</div>
</div>
<aside class="status-card" aria-live="polite">
<div class="status-row">
<span class="status-dot"></span>
<div class="status-copy">
<strong id="statusText">Connected</strong>
<small id="statusSub">Grid power is stable</small>
</div>
</div>
<div id="progress">0 / 3 non-grid options ready</div>
<button id="powerBtn" type="button">Pull the plug</button>
</aside>
</header>
<nav id="phaseRail" aria-label="Story progress">
<span class="phase-step current" data-phase="electric"><b>1</b> Plugged in</span>
<span class="phase-step" data-phase="outage"><b>2</b> Switch</span>
<span class="phase-step" data-phase="independent"><b>3</b> Together</span>
</nav>
<div id="hotspotLayer" aria-label="Objects in the home">
<button class="hotspot" type="button" data-action="tv">TV</button>
<button class="hotspot" type="button" data-action="laptop">Laptop</button>
<button class="hotspot" type="button" data-action="lamp">Reading lamp</button>
<button class="hotspot analog" type="button" data-action="board">Board game</button>
<button class="hotspot analog" type="button" data-action="puzzle">Paper puzzle</button>
<button class="hotspot analog" type="button" data-action="story">Storybook</button>
</div>
<section id="storyPanel" aria-live="polite">
<div class="speaker">
<div class="avatar">M</div>
<span>MAYA</span>
</div>
<div class="narrative">
<div id="chapter">01 / Plugged in</div>
<p id="storyText"></p>
</div>
<div class="choice-wrap">
<span id="choiceTitle">Choose an electric activity</span>
<div id="choices"></div>
</div>
</section>
<div class="final-card" id="finalCard" aria-hidden="true">
<div class="final-icon">✦</div>
<h2>The evening goes on.</h2>
<p>
The screens are dark, but nobody is stuck. Maya keeps the company of a game,
a puzzle, and a story — activities that never needed the grid.
</p>
<div class="final-tags">
<span>Board game</span>
<span>Paper puzzle</span>
<span>Candle-light story</span>
</div>
<button id="replayBtn" type="button">Replay the evening</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const $ = (selector) => document.querySelector(selector);
const canvas = $("#scene");
const statusText = $("#statusText");
const statusSub = $("#statusSub");
const progress = $("#progress");
const powerBtn = $("#powerBtn");
const chapter = $("#chapter");
const storyText = $("#storyText");
const choiceTitle = $("#choiceTitle");
const choices = $("#choices");
const finalCard = $("#finalCard");
const replayBtn = $("#replayBtn");
const hotspotButtons = [...document.querySelectorAll(".hotspot")];
const phaseSteps = [...document.querySelectorAll(".phase-step")];
const ui = {
statusText,
statusSub,
progress,
powerBtn,
chapter,
storyText,
choiceTitle,
choices,
finalCard
};
// -------------------------------------------------------------------------
// Renderer and camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
const dayColor = new THREE.Color("#d8e9e5");
const nightColor = new THREE.Color("#1d2d39");
scene.background = dayColor.clone();
scene.fog = new THREE.Fog(dayColor.clone(), 18, 38);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.05, 0);
const controls = {
azimuth: 0.68,
elevation: 0.68,
zoom: 1,
targetAzimuth: 0.68,
targetElevation: 0.68,
targetZoom: 1,
distance: 16
};
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const verticalSize = Math.max(9.2, 12.4 / aspect);
renderer.setSize(width, height, false);
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
function dampAngle(current, target, smoothing, dt) {
let difference = ((target - current + Math.PI) % (Math.PI * 2) + Math.PI * 2)
% (Math.PI * 2) - Math.PI;
return current + difference * (1 - Math.exp(-smoothing * dt));
}
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-8 * dt);
controls.azimuth = THREE.MathUtils.lerp(
controls.azimuth,
controls.targetAzimuth,
smoothing
);
controls.elevation = THREE.MathUtils.lerp(
controls.elevation,
controls.targetElevation,
smoothing
);
controls.zoom = THREE.MathUtils.lerp(
controls.zoom,
controls.targetZoom,
smoothing
);
const horizontal = Math.cos(controls.elevation) * controls.distance;
camera.position.set(
cameraTarget.x + Math.sin(controls.azimuth) * horizontal,
cameraTarget.y + Math.sin(controls.elevation) * controls.distance,
cameraTarget.z + Math.cos(controls.azimuth) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
}
// -------------------------------------------------------------------------
// Materials and voxel helpers
// -------------------------------------------------------------------------
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.8,
metalness: 0,
...options
});
}
const M = {
wall: makeMaterial("#f0e7d8"),
wallSide: makeMaterial("#e4d8c6"),
trim: makeMaterial("#fff6e5"),
floorA: makeMaterial("#dec29d"),
floorB: makeMaterial("#ead0ae"),
wood: makeMaterial("#915a3e"),
woodLight: makeMaterial("#bc7c51"),
blue: makeMaterial("#467786"),
blueDark: makeMaterial("#284d59"),
orange: makeMaterial("#e8754d"),
red: makeMaterial("#cc5147"),
green: makeMaterial("#648e64"),
leaf: makeMaterial("#3f704e"),
cream: makeMaterial("#fff0c9"),
metal: makeMaterial("#8a989c", { metalness: 0.35, roughness: 0.48 }),
dark: makeMaterial("#26343a"),
black: makeMaterial("#172126"),
skin: makeMaterial("#d99a73"),
hair: makeMaterial("#392724"),
rug: makeMaterial("#b96e59"),
paper: makeMaterial("#f5e8ca"),
grass: makeMaterial("#789a67"),
stone: makeMaterial("#b7b3a5")
};
function box(parent, size, position, colorOrMaterial, options = {}) {
const material = colorOrMaterial && colorOrMaterial.isMaterial
? colorOrMaterial
: makeMaterial(colorOrMaterial, options);
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const actionRoots = [];
function actionGroup(action) {
const group = new THREE.Group();
group.userData.action = action;
actionRoots.push(group);
scene.add(group);
return group;
}
// -------------------------------------------------------------------------
// House shell
// -------------------------------------------------------------------------
box(scene, [12.5, 0.3, 9.4], [0, -0.66, 0], M.grass);
box(scene, [11.6, 0.42, 8.7], [0, -0.35, 0], M.stone);
box(scene, [11.1, 0.22, 8.15], [0, -0.08, 0], M.woodLight);
for (let x = 0; x < 10; x++) {
for (let z = 0; z < 8; z++) {
box(
scene,
[0.96, 0.035, 0.96],
[-4.5 + x, 0.035, -3.5 + z],
(x + z) % 2 === 0 ? M.floorA : M.floorB
);
}
}
// Back and left walls form a readable cutaway interior.
box(scene, [11.2, 3.5, 0.32], [0, 1.75, -4.05], M.wall);
box(scene, [0.32, 3.5, 8.2], [-5.55, 1.75, 0], M.wallSide);
box(scene, [0.28, 1.05, 3.45], [5.55, 0.525, -2.35], M.wallSide);
// Roof-edge trim.
box(scene, [11.35, 0.25, 0.43], [0, 3.56, -4.03], M.wood);
box(scene, [0.43, 0.25, 8.25], [-5.55, 3.56, 0], M.wood);
// Baseboards.
box(scene, [10.9, 0.18, 0.12], [0, 0.16, -3.85], M.trim);
box(scene, [0.12, 0.18, 7.9], [-5.36, 0.16, 0], M.trim);
// Door on the left wall.
box(scene, [0.11, 2.55, 1.35], [-5.35, 1.38, 1.7], M.blueDark);
box(scene, [0.08, 0.17, 1.05], [-5.27, 1.55, 1.7], M.blue);
box(scene, [0.15, 0.14, 0.14], [-5.2, 1.25, 1.15], M.metal);
// Window, frame, and sill.
const skyMaterial = makeMaterial("#8bc8dc", {
emissive: "#4f91a5",
emissiveIntensity: 0.45,
roughness: 0.45
});
box(scene, [2.75, 1.65, 0.08], [0.25, 2.04, -3.84], skyMaterial);
box(scene, [2.95, 0.12, 0.18], [0.25, 2.91, -3.75], M.trim);
box(scene, [2.95, 0.12, 0.18], [0.25, 1.17, -3.75], M.trim);
box(scene, [0.12, 1.84, 0.18], [-1.25, 2.04, -3.75], M.trim);
box(scene, [0.12, 1.84, 0.18], [1.75, 2.04, -3.75], M.trim);
box(scene, [0.1, 1.7, 0.18], [0.25, 2.04, -3.75], M.trim);
box(scene, [3.05, 0.13, 0.38], [0.25, 1.08, -3.68], M.woodLight);
// Voxel moon and stars appear after the outage.
const nightDecor = [];
const moon = new THREE.Group();
box(moon, [0.28, 0.28, 0.04], [0, 0, 0], M.cream);
box(moon, [0.12, 0.12, 0.03], [-0.07, 0.05, 0.025], M.wallSide);
moon.position.set(1.05, 2.5, -3.72);
scene.add(moon);
nightDecor.push(moon);
[
[-0.8, 2.45], [-0.45, 2.7], [0.5, 2.55], [-0.15, 1.7],
[0.75, 1.55], [-0.92, 1.85], [1.38, 2.18]
].forEach(([x, y], index) => {
const star = box(
scene,
[0.055, 0.055, 0.025],
[x, y, -3.72],
makeMaterial("#fff4cb", { emissive: "#fff0aa", emissiveIntensity: 0.7 })
);
star.userData.starPhase = index * 0.7;
nightDecor.push(star);
});
// Wall clock.
const clockGroup = new THREE.Group();
clockGroup.position.set(2.35, 2.55, -3.78);
scene.add(clockGroup);
box(clockGroup, [0.58, 0.58, 0.12], [0, 0, 0], M.wood);
box(clockGroup, [0.43, 0.43, 0.06], [0, 0, 0.08], M.cream);
box(clockGroup, [0.04, 0.17, 0.04], [0, 0.07, 0.12], M.dark);
box(clockGroup, [0.14, 0.04, 0.04], [0.06, 0, 0.12], M.dark);
// -------------------------------------------------------------------------
// Kitchen
// -------------------------------------------------------------------------
box(scene, [2.55, 1.02, 0.82], [3.05, 0.52, -3.46], M.blue);
box(scene, [2.7, 0.13, 0.92], [3.05, 1.09, -3.45], M.cream);
for (let i = 0; i < 3; i++) {
box(scene, [0.72, 0.72, 0.05], [2.23 + i * 0.82, 0.55, -3.03], M.blueDark);
box(scene, [0.22, 0.05, 0.06], [2.23 + i * 0.82, 0.57, -2.99], M.metal);
}
// Sink.
box(scene, [0.72, 0.07, 0.5], [3.5, 1.17, -3.45], M.metal);
box(scene, [0.48, 0.08, 0.3], [3.5, 1.2, -3.45], M.dark);
box(scene, [0.06, 0.35, 0.06], [3.75, 1.36, -3.64], M.metal);
// Stove.
box(scene, [1.05, 0.055, 0.66], [2.25, 1.18, -3.43], M.black);
for (let x = 0; x < 2; x++) {
for (let z = 0; z < 2; z++) {
box(
scene,
[0.24, 0.025, 0.24],
[2.0 + x * 0.5, 1.22, -3.62 + z * 0.38],
M.metal
);
}
}
// Fridge.
box(scene, [1.1, 2.28, 1.05], [4.72, 1.15, -3.42], M.trim);
box(scene, [0.88, 0.05, 0.04], [4.72, 1.6, -2.88], M.stone);
box(scene, [0.08, 0.55, 0.09], [4.38, 1.05, -2.85], M.metal);
box(scene, [0.08, 0.38, 0.09], [4.38, 1.82, -2.85], M.metal);
// Upper cupboards and shelf.
box(scene, [2.35, 0.65, 0.42], [3.2, 2.5, -3.75], M.woodLight);
for (let i = 0; i < 3; i++) {
box(scene, [0.68, 0.54, 0.04], [2.42 + i * 0.78, 2.5, -3.52], M.wood);
box(scene, [0.05, 0.15, 0.06], [2.42 + i * 0.78, 2.5, -3.47], M.metal);
}
// Countertop objects.
box(scene, [0.28, 0.35, 0.28], [4.02, 1.34, -3.45], M.orange);
box(scene, [0.14, 0.13, 0.14], [4.02, 1.58, -3.45], M.cream);
// -------------------------------------------------------------------------
// Living room
// -------------------------------------------------------------------------
box(scene, [4.1, 0.025, 2.35], [-2.55, 0.066, 0.08], M.rug);
box(scene, [3.65, 0.018, 1.9], [-2.55, 0.082, 0.08], M.orange);
// Sofa faces the TV.
box(scene, [2.85, 0.48, 1.2], [-2.65, 0.56, -1.42], M.blue);
box(scene, [2.85, 1.05, 0.24], [-2.65, 1.08, -0.88], M.blueDark);
box(scene, [0.28, 0.75, 1.25], [-4.02, 0.8, -1.42], M.blueDark);
box(scene, [0.28, 0.75, 1.25], [-1.28, 0.8, -1.42], M.blueDark);
box(scene, [1.22, 0.16, 0.9], [-3.28, 0.88, -1.42], M.cream);
box(scene, [1.22, 0.16, 0.9], [-2.02, 0.88, -1.42], M.cream);
// TV and media console.
const tvRoot = actionGroup("tv");
tvRoot.position.set(-2.65, 0, -3.43);
box(tvRoot, [3.05, 0.58, 0.56], [0, 0.32, 0], M.wood);
box(tvRoot, [0.2, 0.48, 0.2], [-1.35, 0.25, 0], M.dark);
box(tvRoot, [0.2, 0.48, 0.2], [1.35, 0.25, 0], M.dark);
const tvScreenMaterial = makeMaterial("#59c9d2", {
emissive: "#126f7b",
emissiveIntensity: 1.25,
roughness: 0.35
});
box(tvRoot, [2.35, 1.27, 0.14], [0, 1.62, 0], M.black);
box(tvRoot, [2.17, 1.09, 0.08], [0, 1.62, 0.09], tvScreenMaterial);
const tvPixels = [];
const pixelColors = ["#f4cf69", "#f28d68", "#79d0ca", "#e8f3dc"];
for (let i = 0; i < 12; i++) {
const column = i % 4;
const row = Math.floor(i / 4);
const pixel = box(
tvRoot,
[0.27, 0.12, 0.035],
[-0.72 + column * 0.48, 1.92 - row * 0.31, 0.15],
pixelColors[i % pixelColors.length]
);
tvPixels.push({
mesh: pixel,
x: pixel.position.x,
y: pixel.position.y,
phase: i * 0.47
});
}
// Coffee table with the board game.
box(scene, [2.25, 0.16, 1.3], [-2.5, 0.72, 0.48], M.woodLight);
box(scene, [0.16, 0.66, 0.16], [-3.37, 0.36, 0.03], M.wood);
box(scene, [0.16, 0.66, 0.16], [-1.63, 0.36, 0.03], M.wood);
box(scene, [0.16, 0.66, 0.16], [-3.37, 0.36, 0.93], M.wood);
box(scene, [0.16, 0.66, 0.16], [-1.63, 0.36, 0.93], M.wood);
// -------------------------------------------------------------------------
// Candles used by the non-grid activities
// -------------------------------------------------------------------------
const candles = [];
function addCandle(parent, x, y, z) {
const candle = new THREE.Group();
candle.position.set(x, y, z);
parent.add(candle);
box(candle, [0.25, 0.08, 0.25], [0, 0.04, 0], M.cream);
box(candle, [0.12, 0.32, 0.12], [0, 0.23, 0], M.paper);
const flameMaterial = makeMaterial("#ffb13b", {
emissive: "#ff7b18",
emissiveIntensity: 2.5,
roughness: 0.4
});
const flame = box(candle, [0.13, 0.22, 0.13], [0, 0.48, 0], flameMaterial);
const light = new THREE.PointLight("#ffad55", 0, 5.5, 2);
light.position.set(0, 0.58, 0);
candle.add(light);
candles.push({ flame, light, phase: candles.length * 1.7 });
return candle;
}
// -------------------------------------------------------------------------
// Board game
// -------------------------------------------------------------------------
const boardRoot = actionGroup("board");
boardRoot.position.set(-2.5, 0.82, 0.48);
box(boardRoot, [1.45, 0.08, 0.92], [0, 0, 0], M.cream);
box(boardRoot, [1.34, 0.035, 0.81], [0, 0.055, 0], M.paper);
const boardTiles = [];
for (let x = 0; x < 5; x++) {
for (let z = 0; z < 4; z++) {
boardTiles.push(
box(
boardRoot,
[0.22, 0.025, 0.16],
[-0.54 + x * 0.27, 0.086, -0.3 + z * 0.2],
(x + z) % 2 === 0 ? M.blue : M.orange
)
);
}
}
const boardPieces = [];
for (let i = 0; i < 5; i++) {
const piece = box(
boardRoot,
[0.14, 0.14, 0.14],
[-0.38 + i * 0.19, 0.15, -0.1 + (i % 2) * 0.23],
i % 2 === 0 ? M.red : M.green
);
piece.userData.baseY = piece.position.y;
boardPieces.push(piece);
}
addCandle(boardRoot, 0.93, 0.06, -0.32);
// -------------------------------------------------------------------------
// Paper puzzle
// -------------------------------------------------------------------------
box(scene, [1.45, 0.14, 1.0], [2.18, 0.75, 0.35], M.woodLight);
box(scene, [0.14, 0.7, 0.14], [1.59, 0.38, 0.02], M.wood);
box(scene, [0.14, 0.7, 0.14], [2.77, 0.38, 0.02], M.wood);
box(scene, [0.14, 0.7, 0.14], [1.59, 0.38, 0.68], M.wood);
box(scene, [0.14, 0.7, 0.14], [2.77, 0.38, 0.68], M.wood);
const puzzleRoot = actionGroup("puzzle");
puzzleRoot.position.set(2.18, 0.84, 0.35);
box(puzzleRoot, [1.08, 0.035, 0.76], [0, 0, 0], M.paper);
const puzzleTiles = [];
const puzzleColors = ["#e87950", "#5c9090", "#e4bd61", "#6f9769"];
for (let x = 0; x < 4; x++) {
for (let z = 0; z < 3; z++) {
const tile = box(
puzzleRoot,
[0.22, 0.035, 0.19],
[-0.34 + x * 0.23, 0.04, -0.25 + z * 0.25],
puzzleColors[(x + z) % puzzleColors.length]
);
tile.userData.baseX = tile.position.x;
tile.userData.baseZ = tile.position.z;
tile.userData.phase = x * 0.6 + z;
puzzleTiles.push(tile);
}
}
box(puzzleRoot, [0.035, 0.035, 0.55], [0.03, 0.07, 0], M.dark);
box(puzzleRoot, [0.35, 0.035, 0.035], [-0.13, 0.07, 0], M.dark);
addCandle(puzzleRoot, 0.72, 0.03, -0.23);
// -------------------------------------------------------------------------
// Reading nook and storybook
// -------------------------------------------------------------------------
box(scene, [1.25, 0.44, 1.15], [-4.08, 0.55, 1.25], M.green);
box(scene, [1.25, 0.95, 0.22], [-4.08, 1.0, 1.7], M.green);
box(scene, [0.22, 0.75, 1.15], [-4.63, 0.74, 1.25], M.green);
box(scene, [0.22, 0.75, 1.15], [-3.53, 0.74, 1.25], M.green);
box(scene, [0.9, 0.14, 0.8], [-4.08, 0.85, 1.24], M.cream);
box(scene, [1.12, 0.14, 0.75], [-3.72, 0.78, 2.45], M.woodLight);
box(scene, [0.14, 0.72, 0.14], [-4.13, 0.39, 2.21], M.wood);
box(scene, [0.14, 0.72, 0.14], [-3.31, 0.39, 2.21], M.wood);
box(scene, [0.14, 0.72, 0.14], [-4.13, 0.39, 2.69], M.wood);
box(scene, [0.14, 0.72, 0.14], [-3.31, 0.39, 2.69], M.wood);
const storyRoot = actionGroup("story");
storyRoot.position.set(-3.72, 0.87, 2.45);
const storyBook = box(storyRoot, [0.72, 0.09, 0.5], [-0.12, 0.06, 0], M.red);
storyBook.userData.baseY = storyBook.position.y;
const bookPages = box(storyRoot, [0.6, 0.055, 0.4], [-0.1, 0.115, 0], M.paper);
box(storyRoot, [0.03, 0.03, 0.38], [-0.1, 0.15, 0], M.wood);
addCandle(storyRoot, 0.53, 0.03, -0.23);
// -------------------------------------------------------------------------
// Reading lamp and laptop
// -------------------------------------------------------------------------
const lampRoot = actionGroup("lamp");
lampRoot.position.set(-4.78, 0, 2.65);
box(lampRoot, [0.65, 0.1, 0.65], [0, 0.08, 0], M.dark);
box(lampRoot, [0.09, 2.12, 0.09], [0, 1.1, 0], M.metal);
box(lampRoot, [0.78, 0.22, 0.78], [0, 2.05, 0], M.orange);
box(lampRoot, [0.56, 0.2, 0.56], [0, 2.23, 0], M.orange);
const lampBulbMaterial = makeMaterial("#fff0b1", {
emissive: "#ffc85c",
emissiveIntensity: 2.2
});
box(lampRoot, [0.18, 0.16, 0.18], [0, 1.96, 0], lampBulbMaterial);
// Desk and laptop.
box(scene, [2.45, 0.16, 1.12], [3.35, 0.9, 2.05], M.woodLight);
box(scene, [0.17, 0.84, 0.17], [2.3, 0.46, 1.66], M.wood);
box(scene, [0.17, 0.84, 0.17], [4.4, 0.46, 1.66], M.wood);
box(scene, [0.17, 0.84, 0.17], [2.3, 0.46, 2.44], M.wood);
box(scene, [0.17, 0.84, 0.17], [4.4, 0.46, 2.44], M.wood);
const laptopRoot = actionGroup("laptop");
laptopRoot.position.set(3.35, 1.01, 2.02);
box(laptopRoot, [1.0, 0.07, 0.68], [0, 0, 0], M.metal);
const laptopScreenMaterial = makeMaterial("#71cfd3", {
emissive: "#176c78",
emissiveIntensity: 1.15,
roughness: 0.4
});
box(laptopRoot, [1.03, 0.68, 0.09], [0, 0.39, -0.25], M.dark);
box(laptopRoot, [0.92, 0.55, 0.05], [0, 0.4, -0.19], laptopScreenMaterial);
box(laptopRoot, [0.42, 0.025, 0.25], [0, 0.05, 0.08], M.dark);
const laptopPixels = [];
for (let i = 0; i < 6; i++) {
const pixel = box(
laptopRoot,
[0.2, 0.07, 0.025],
[-0.25 + (i % 3) * 0.25,
0.57 - Math.floor(i / 3) * 0.25,
-0.15],
i % 2 ? M.cream : M.orange
);
laptopPixels.push({
mesh: pixel,
x: pixel.position.x,
y: pixel.position.y,
phase: i * 0.63
});
}
// A few decorative plants and books.
function plant(x, z, scale = 1) {
box(scene, [0.42 * scale, 0.42 * scale, 0.42 * scale], [x, 0.22 * scale, z], M.orange);
box(scene, [0.11 * scale, 0.8 * scale, 0.11 * scale], [x, 0.7 * scale, z], M.wood);
box(scene, [0.48 * scale, 0.3 * scale, 0.45 * scale], [x, 1.0 * scale, z], M.leaf);
box(scene, [0.35 * scale, 0.34 * scale, 0.37 * scale], [x + 0.18 * scale, 1.25 * scale, z - 0.04], M.leaf);
}
plant(5.05, 3.25, 1.15);
plant(1.68, -3.35, 0.7);
// Shelf with colorful books.
box(scene, [1.85, 0.13, 0.38], [-3.25, 2.78, -3.75], M.wood);
const bookColors = ["#e77b55", "#4d8490", "#e4bd62", "#6f9869", "#a86455"];
for (let i = 0; i < 7; i++) {
box(
scene,
[0.16, 0.55 + (i % 3) * 0.07, 0.25],
[-3.9 + i * 0.2, 3.1, -3.72],
makeMaterial(bookColors[i % bookColors.length])
);
}
// -------------------------------------------------------------------------
// Character
// -------------------------------------------------------------------------
const character = new THREE.Group();
character.position.set(0.2, 0.05, 1.55);
character.rotation.y = 2.65;
scene.add(character);
const body = box(character, [0.68, 0.72, 0.43], [0, 1.02, 0], M.orange);
box(character, [0.54, 0.55, 0.5], [0, 1.58, 0], M.skin);
box(character, [0.58, 0.22, 0.54], [0, 1.83, -0.02], M.hair);
box(character, [0.57, 0.16, 0.13], [0, 1.76, 0.25], M.hair);
box(character, [0.08, 0.08, 0.045], [-0.13, 1.62, 0.27], M.black);
box(character, [0.08, 0.08, 0.045], [0.13, 1.62, 0.27], M.black);
box(character, [0.14, 0.04, 0.04], [0, 1.47, 0.28], M.red);
const leftArm = new THREE.Group();
const rightArm = new THREE.Group();
leftArm.position.set(-0.43, 1.28, 0);
rightArm.position.set(0.43, 1.28, 0);
character.add(leftArm, rightArm);
box(leftArm, [0.2, 0.6, 0.22], [0, -0.25, 0], M.orange);
box(rightArm, [0.2, 0.6, 0.22], [0, -0.25, 0], M.orange);
box(leftArm, [0.18, 0.17, 0.2], [0, -0.62, 0], M.skin);
box(rightArm, [0.18, 0.17, 0.2], [0, -0.62, 0], M.skin);
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.18, 0.69, 0);
rightLeg.position.set(0.18, 0.69, 0);
character.add(leftLeg, rightLeg);
box(leftLeg, [0.25, 0.58, 0.27], [0, -0.26, 0], M.blueDark);
box(rightLeg, [0.25, 0.58, 0.27], [0, -0.26, 0], M.blueDark);
box(leftLeg, [0.27, 0.14, 0.4], [0, -0.57, 0.08], M.dark);
box(rightLeg, [0.27, 0.14, 0.4], [0, -0.57, 0.08], M.dark);
const characterTarget = new THREE.Vector3(0.2, 0, 1.55);
let characterTargetYaw = 2.65;
let characterMoving = false;
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight("#fff4d6", "#52646a", 1.6);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff1d2", 2.5);
sun.position.set(-5, 10, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0005;
scene.add(sun);
const ceilingLight = new THREE.PointLight("#ffd79a", 28, 12, 2);
ceilingLight.position.set(0, 3.05, -0.15);
scene.add(ceilingLight);
const kitchenLight = new THREE.PointLight("#fff1c5", 12, 7, 2);
kitchenLight.position.set(3.2, 2.6, -2.7);
scene.add(kitchenLight);
const electricLampLight = new THREE.PointLight("#ffd076", 8, 5, 2);
electricLampLight.position.set(-4.78, 2.0, 2.65);
scene.add(electricLampLight);
// -------------------------------------------------------------------------
// Story state
// -------------------------------------------------------------------------
let phase = "electric";
let selectedElectric = null;
let analogActive = null;
let powerLevel = 1;
let powerTarget = 1;
const completed = new Set();
const analogLit = new Set();
const electricDestinations = {
tv: new THREE.Vector3(-2.5, 0, -0.15),
laptop: new THREE.Vector3(2.45, 0, 1.0),
lamp: new THREE.Vector3(-3.65, 0, 1.55)
};
const analogDestinations = {
board: new THREE.Vector3(-1.45, 0, 0.85),
puzzle: new THREE.Vector3(1.18, 0, 0.62),
story: new THREE.Vector3(-3.05, 0, 1.65)
};
const electricStories = {
tv: [
"01 / Plugged in",
"Maya settles onto the sofa for the match. The television throws blue light across the room, and dinner can wait five more minutes."
],
laptop: [
"01 / Plugged in",
"Maya opens her laptop to finish a game online. Every score updates instantly, and the soft click of the keyboard fills the room."
],
lamp: [
"01 / Plugged in",
"Maya pulls a book into the reading lamp’s warm circle. One small bulb is enough light for the whole chapter."
]
};
const analogStories = {
board: [
"02 / Switch",
"Maya pulls a wooden board game from the shelf. There is no loading screen and no signal—only a good game and one more round."
],
puzzle: [
"02 / Switch",
"The laptop stays closed. A paper puzzle and a pencil turn a blank rectangle into a tiny map that no server can interrupt."
],
story: [
"02 / Switch",
"Maya trades the electric lamp for a candle and a paper story. The room becomes softer, but it does not become empty."
]
};
function setNarrative(chapterText, text) {
ui.chapter.textContent = chapterText;
ui.storyText.textContent = text;
}
function renderChoices() {
if (phase === "electric") {
ui.choiceTitle.textContent = "Choose an electric activity";
ui.choices.innerHTML = `
<button class="choice ${selectedElectric === "tv" ? "active" : ""}" data-action="tv">
<span class="key">1</span><span><strong>TV match</strong><small>Electric</small></span>
</button>
<button class="choice ${selectedElectric === "laptop" ? "active" : ""}" data-action="laptop">
<span class="key">2</span><span><strong>Online game</strong><small>Electric</small></span>
</button>
<button class="choice ${selectedElectric === "lamp" ? "active" : ""}" data-action="lamp">
<span class="key">3</span><span><strong>Read a book</strong><small>Electric</small></span>
</button>`;
} else {
ui.choiceTitle.textContent = "Switch to what works without the grid";
const labels = {
board: ["Board game", "No power"],
puzzle: ["Paper puzzle", "No power"],
story: ["Storybook", "Candle light"]
};
ui.choices.innerHTML = Object.entries(labels).map(([action, value], index) => {
const done = completed.has(action);
return `
<button class="choice ${done ? "done" : ""}" data-action="${action}" ${phase === "independent" ? "disabled" : ""}>
<span class="key">${done ? "✓" : index + 1}</span>
<span><strong>${value[0]}</strong><small>${done ? "Found" : value[1]}</small></span>
</button>`;
}).join("");
}
}
function renderStatus() {
phaseSteps.forEach((step) => {
const stepPhase = step.dataset.phase;
step.classList.toggle("current", stepPhase === phase);
step.classList.toggle("complete",
(phase === "electric" && stepPhase === "electric") ||
(phase === "outage" && stepPhase === "electric") ||
(phase === "independent" && stepPhase !== "independent")
);
});
if (phase === "electric") {
statusText.textContent = "Connected";
statusSub.textContent = "Grid power is stable";
progress.textContent = "0 / 3 non-grid options ready";
powerBtn.textContent = "Pull the plug";
powerBtn.disabled = false;
} else if (phase === "outage") {
statusText.textContent = "Interrupted";
statusSub.textContent = "The whole house is offline";
progress.textContent = `${completed.size} / 3 alternatives used`;
powerBtn.textContent = "Grid offline";
powerBtn.disabled = true;
} else {
statusText.textContent = "Independent";
statusSub.textContent = "The evening continues";
progress.textContent = "3 / 3 alternatives found";
powerBtn.textContent = "Replay evening";
powerBtn.disabled = false;
}
}
function renderUI() {
renderChoices();
renderStatus();
}
function chooseElectric(action) {
if (phase !== "electric") return;
selectedElectric = action;
characterTarget.copy(electricDestinations[action]);
characterTargetYaw = action === "tv" ? Math.PI : action === "laptop" ? 0.35 : 2.2;
const story = electricStories[action];
setNarrative(story[0], story[1]);
renderUI();
}
function triggerOutage() {
if (phase !== "electric") return;
phase = "outage";
powerTarget = 0;
analogActive = null;
characterTarget.set(0.15, 0, 1.35);
characterTargetYaw = 2.5;
document.body.classList.add("is-dark");
setNarrative(
"02 / The interruption",
"Click. The television dies, the router light vanishes, and the ceiling lamp goes cold. Maya takes a breath. “Okay—not everything needs a plug.”"
);
renderUI();
}
function chooseAlternative(action) {
if (phase !== "outage") return;
completed.add(action);
analogLit.add(action);
analogActive = action;
characterTarget.copy(analogDestinations[action]);
characterTargetYaw =
action === "board" ? 2.7 :
action === "puzzle" ? 2.7 :
1.6;
const story = analogStories[action];
setNarrative(story[0], story[1]);
renderUI();
if (completed.size === 3) {
phase = "independent";
setNarrative(
"03 / Together",
"The TV is off. The laptop is closed. But the board game, the puzzle, and the story are all still here—and Maya keeps the evening going without the grid."
);
renderUI();
spawnConfetti();
ui.finalCard.classList.add("show");
ui.finalCard.setAttribute("aria-hidden", "false");
}
}
function resetStory() {
phase = "electric";
selectedElectric = null;
analogActive = null;
powerTarget = 1;
completed.clear();
analogLit.clear();
characterTarget.set(0.2, 0, 1.55);
characterTargetYaw = 2.65;
document.body.classList.remove("is-dark");
ui.finalCard.classList.remove("show");
ui.finalCard.setAttribute("aria-hidden", "true");
clearConfetti();
setNarrative(
"01 / Plugged in",
"It is 7:12 PM. Maya is winding down in her little voxel home. The TV, laptop, and reading lamp are all ready—but the evening still has one more surprise in store."
);
renderUI();
}
function handleAction(action) {
if (phase === "electric") {
chooseElectric(action);
} else if (phase === "outage") {
chooseAlternative(action);
}
}
ui.choices.addEventListener("click", (event) => {
const button = event.target.closest("button[data-action]");
if (!button) return;
handleAction(button.dataset.action);
});
$("#hotspotLayer").addEventListener("click", (event) => {
const button = event.target.closest("button[data-action]");
if (!button) return;
handleAction(button.dataset.action);
});
powerBtn.addEventListener("click", () => {
if (phase === "electric") triggerOutage();
else if (phase === "independent") resetStory();
});
replayBtn.addEventListener("click", resetStory);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
const key = event.key.toLowerCase();
if (key === "p") {
if (phase === "electric") triggerOutage();
else if (phase === "independent") resetStory();
}
if (["1", "2", "3"].includes(key)) {
const index = Number(key) - 1;
if (phase === "electric") {
handleAction(["tv", "laptop", "lamp"][index]);
} else {
handleAction(["board", "puzzle", "story"][index]);
}
}
});
// -------------------------------------------------------------------------
// Voxel confetti
// -------------------------------------------------------------------------
const confettiGroup = new THREE.Group();
scene.add(confettiGroup);
const confetti = [];
function spawnConfetti() {
clearConfetti();
const colors = ["#ed754d", "#75c7c8", "#f1ca67", "#8ec38b", "#fff0cf"];
for (let i = 0; i < 42; i++) {
const mesh = box(
confettiGroup,
[0.1, 0.1, 0.1],
[character.position.x, 2.8, character.position.z],
makeMaterial(colors[i % colors.length])
);
confetti.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 3.2,
2.4 + Math.random() * 2.6,
(Math.random() - 0.5) * 3.2
),
spin: new THREE.Vector3(
Math.random() * 5,
Math.random() * 5,
Math.random() * 5
)
});
}
}
function clearConfetti() {
while (confettiGroup.children.length) {
const child = confettiGroup.children.pop();
child.geometry.dispose();
child.material.dispose();
}
confetti.length = 0;
}
function updateConfetti(dt) {
confetti.forEach((piece) => {
piece.velocity.y -= 3.5 * dt;
piece.mesh.position.addScaledVector(piece.velocity, dt);
piece.mesh.rotation.x += piece.spin.x * dt;
piece.mesh.rotation.y += piece.spin.y * dt;
piece.mesh.rotation.z += piece.spin.z * dt;
if (piece.mesh.position.y < 0.08) {
piece.mesh.visible = false;
}
});
}
// -------------------------------------------------------------------------
// Hotspot projection and direct object picking
// -------------------------------------------------------------------------
const anchors = {
tv: new THREE.Vector3(-2.65, 2.55, -3.25),
laptop: new THREE.Vector3(3.35, 1.78, 1.95),
lamp: new THREE.Vector3(-4.78, 2.7, 2.65),
board: new THREE.Vector3(-2.5, 1.35, 0.48),
puzzle: new THREE.Vector3(2.18, 1.32, 0.35),
story: new THREE.Vector3(-3.72, 1.35, 2.45)
};
const projected = new THREE.Vector3();
function updateHotspots() {
const visibleActions = phase === "electric"
? ["tv", "laptop", "lamp"]
: ["board", "puzzle", "story"];
hotspotButtons.forEach((button) => {
const action = button.dataset.action;
const shouldShow = visibleActions.includes(action);
if (!shouldShow) {
button.style.display = "none";
return;
}
projected.copy(anchors[action]).project(camera);
if (projected.z < -1 || projected.z > 1) {
button.style.display = "none";
return;
}
button.style.display = "flex";
button.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
button.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
button.classList.toggle("done", completed.has(action));
});
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickAction(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(actionRoots, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.action) {
object = object.parent;
}
if (object && object.userData.action) {
const action = object.userData.action;
const allowed = phase === "electric"
? ["tv", "laptop", "lamp"].includes(action)
: phase === "outage" && ["board", "puzzle", "story"].includes(action);
if (allowed) return action;
}
}
return null;
}
// Direct camera controls, implemented without addons.
let pointerDown = false;
let pointerMoved = 0;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = 0;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (pointerDown) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
pointerMoved += Math.abs(deltaX) + Math.abs(deltaY);
controls.targetAzimuth -= deltaX * 0.006;
controls.targetElevation = THREE.MathUtils.clamp(
controls.targetElevation + deltaY * 0.004,
0.35,
1.15
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else {
const action = pickAction(event.clientX, event.clientY);
canvas.style.cursor = action ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
if (pointerDown && pointerMoved < 7) {
const action = pickAction(event.clientX, event.clientY);
if (action) handleAction(action);
}
pointerDown = false;
canvas.style.cursor = "grab";
canvas.releasePointerCapture(event.pointerId);
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
controls.targetZoom = THREE.MathUtils.clamp(
controls.targetZoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.55
);
}, { passive: false });
canvas.addEventListener("dblclick", () => {
controls.targetAzimuth = 0.68;
controls.targetElevation = 0.68;
controls.targetZoom = 1;
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function updateCharacter(dt, time) {
const dx = characterTarget.x - character.position.x;
const dz = characterTarget.z - character.position.z;
const distance = Math.hypot(dx, dz);
characterMoving = distance > 0.045;
if (characterMoving) {
const step = Math.min(distance, dt * 2.15);
character.position.x += (dx / distance) * step;
character.position.z += (dz / distance) * step;
characterTargetYaw = Math.atan2(dx, dz);
}
character.rotation.y = dampAngle(
character.rotation.y,
characterTargetYaw,
9,
dt
);
character.position.y = 0.05 + (
characterMoving
? Math.abs(Math.sin(time * 11)) * 0.035
: Math.sin(time * 1.6) * 0.007
);
const stride = characterMoving ? Math.sin(time * 11) * 0.58 : 0;
leftLeg.rotation.x = stride;
rightLeg.rotation.x = -stride;
leftArm.rotation.x = -stride * 0.75;
rightArm.rotation.x = stride * 0.75;
}
function updatePower(dt, time) {
powerLevel = THREE.MathUtils.lerp(
powerLevel,
powerTarget,
1 - Math.exp(-3.4 * dt)
);
hemisphere.intensity = 0.72 + powerLevel * 0.88;
sun.intensity = 0.35 + powerLevel * 2.15;
ceilingLight.intensity = 28 * powerLevel;
kitchenLight.intensity = 12 * powerLevel;
electricLampLight.intensity = 8 * powerLevel;
lampBulbMaterial.emissiveIntensity = 2.2 * powerLevel;
scene.background.lerpColors(nightColor, dayColor, powerLevel);
scene.fog.color.copy(scene.background);
const daySky = new THREE.Color("#8bc8dc");
const nightSky = new THREE.Color("#152b40");
skyMaterial.color.lerpColors(nightSky, daySky, powerLevel);
skyMaterial.emissiveIntensity = 0.18 + powerLevel * 0.3;
const tvOff = new THREE.Color("#182428");
const tvOn = new THREE.Color("#65cbd2");
tvScreenMaterial.color.lerpColors(tvOff, tvOn, powerLevel);
tvScreenMaterial.emissiveIntensity = powerLevel * 1.35;
const laptopOff = new THREE.Color("#19272c");
const laptopOn = new THREE.Color("#75d0d3");
laptopScreenMaterial.color.lerpColors(laptopOff, laptopOn, powerLevel);
laptopScreenMaterial.emissiveIntensity = powerLevel * 1.15;
tvPixels.forEach(({ mesh }) => {
mesh.visible = powerLevel > 0.3;
});
laptopPixels.forEach(({ mesh }) => {
mesh.visible = powerLevel > 0.3;
});
nightDecor.forEach((object, index) => {
object.visible = powerLevel < 0.45;
if (object.userData.starPhase !== undefined) {
const scale = 0.85 + Math.sin(time * 2 + object.userData.starPhase) * 0.15;
object.scale.setScalar(scale);
}
});
}
function updateProps(time) {
tvPixels.forEach(({ mesh, x, phase }) => {
mesh.position.x = x + Math.sin(time * 2.2 + phase) * 0.08;
mesh.position.y += Math.sin(time * 3 + phase) * 0.0008;
});
laptopPixels.forEach(({ mesh, x, phase }) => {
mesh.position.x = x + Math.sin(time * 3 + phase) * 0.035;
});
boardPieces.forEach((piece, index) => {
const bounce = analogActive === "board"
? Math.sin(time * 4 + index) * 0.045
: 0;
piece.position.y = piece.userData.baseY + bounce;
piece.rotation.y = analogActive === "board"
? Math.sin(time * 2 + index) * 0.08
: 0;
});
puzzleTiles.forEach((tile) => {
const active = analogActive === "puzzle";
tile.position.x = tile.userData.baseX + (
active ? Math.sin(time * 2.4 + tile.userData.phase) * 0.035 : 0
);
tile.position.z = tile.userData.baseZ + (
active ? Math.cos(time * 2.1 + tile.userData.phase) * 0.025 : 0
);
});
storyBook.position.y = storyBook.userData.baseY + (
analogActive === "story" ? Math.sin(time * 3) * 0.015 : 0
);
storyBook.rotation.y = analogActive === "story"
? Math.sin(time * 2) * 0.08
: 0;
bookPages.rotation.y = analogActive === "story"
? Math.sin(time * 2 + 0.7) * 0.11
: 0;
candles.forEach(({ flame, light, phase }) => {
const flicker = 0.9 + Math.sin(time * 9 + phase) * 0.08
+ Math.sin(time * 14.3 + phase * 2) * 0.04;
flame.scale.y = flicker;
flame.position.x = Math.sin(time * 6 + phase) * 0.012;
const lit = analogLit.has(
candles.indexOf(candles.find((item) => item === flame)) >= 0
? [...analogLit][0]
: ""
);
// Each candle is associated with its activity by its parent group.
const parentIsLit =
(flame.parent.parent === boardRoot && analogLit.has("board")) ||
(flame.parent.parent === puzzleRoot && analogLit.has("puzzle")) ||
(flame.parent.parent === storyRoot && analogLit.has("story"));
light.intensity = parentIsLit ? 13 * flicker : 0;
});
}
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateCamera(dt);
updatePower(dt, time);
updateCharacter(dt, time);
updateProps(time);
updateConfetti(dt);
updateHotspots();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
// Start the story.
setNarrative(
"01 / Plugged in",
"It is 7:12 PM. Maya is winding down in her little voxel home. The TV, laptop, and reading lamp are all ready—but the evening still has one more surprise in store."
);
renderUI();
animate();
</script>
</body>
</html>
```