Prompt specification
Build or customize the Three.js 3D scene "Small-space social layout puzzle" 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: Small-space social layout puzzle
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/small-space-social-layout-puzzle-x0204
Scene Description Prompt:
Create an apartment room with movable sofa modules and visitor markers; users satisfy seating, conversation, and passage goals using the same limited set of pieces. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (small-space-social-layout-puzzle-x0204):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Flexible Living — Apartment Planner</title>
<style>
:root {
--ink: #25312f;
--muted: #66716e;
--paper: rgba(255, 253, 248, 0.91);
--line: rgba(37, 49, 47, 0.12);
--accent: #e97852;
--green: #3f9b78;
--gold: #e8b54d;
--shadow: 0 18px 45px rgba(47, 53, 47, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e5dc;
color: var(--ink);
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;
touch-action: none;
outline: none;
}
.glass {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.76);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(1.08);
-webkit-backdrop-filter: blur(18px) saturate(1.08);
}
#intro {
position: fixed;
top: 22px;
left: 22px;
width: min(365px, calc(100vw - 44px));
padding: 19px 21px 18px;
border-radius: 20px;
z-index: 5;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
#intro h1 {
margin: 0;
font-size: clamp(23px, 2.4vw, 34px);
line-height: 1.02;
letter-spacing: -0.045em;
}
#intro p {
margin: 11px 0 0;
max-width: 31em;
color: var(--muted);
font-size: 12.5px;
line-height: 1.55;
}
#goals {
position: fixed;
top: 22px;
right: 22px;
width: 322px;
padding: 16px;
border-radius: 20px;
z-index: 5;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
padding: 2px 2px 12px;
}
.panel-heading span {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
}
#goalCount {
padding: 5px 8px;
border-radius: 999px;
background: #ece9e1;
color: var(--ink);
font-size: 11px;
font-weight: 800;
}
.goal {
position: relative;
display: grid;
grid-template-columns: 28px 1fr;
gap: 10px;
margin-top: 8px;
padding: 12px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.48);
transition: border-color 180ms ease, background 180ms ease;
}
.goal.passed {
border-color: rgba(63, 155, 120, 0.42);
background: rgba(228, 246, 237, 0.78);
}
.goal-check {
position: relative;
width: 24px;
height: 24px;
border: 1.5px solid #b8bdb8;
border-radius: 50%;
color: transparent;
font-size: 13px;
font-weight: 900;
text-align: center;
line-height: 21px;
}
.goal.passed .goal-check {
border-color: var(--green);
background: var(--green);
color: white;
}
.goal h3 {
margin: 1px 0 3px;
font-size: 13px;
letter-spacing: -0.01em;
}
.goal p {
min-height: 27px;
margin: 0;
color: var(--muted);
font-size: 10.5px;
line-height: 1.35;
}
.bar {
height: 3px;
margin-top: 9px;
overflow: hidden;
border-radius: 99px;
background: #dedfd9;
}
.bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--accent);
transition: width 180ms ease, background 180ms ease;
}
.goal.passed .bar i { background: var(--green); }
.panel-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.panel-actions button,
#hintBtn {
min-height: 37px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.62);
color: var(--ink);
cursor: pointer;
font-size: 10.5px;
font-weight: 750;
transition: transform 140ms ease, background 140ms ease;
}
.panel-actions button:hover,
#hintBtn:hover {
transform: translateY(-1px);
background: white;
}
#hintBtn {
width: 100%;
margin-top: 8px;
}
#hintBtn.active {
border-color: rgba(233, 120, 82, 0.42);
background: #fff0e9;
color: #b65334;
}
#inventory {
position: fixed;
left: 50%;
bottom: 22px;
z-index: 6;
display: flex;
align-items: center;
gap: 10px;
padding: 10px;
border-radius: 18px;
transform: translateX(-50%);
}
.inventory-item {
display: flex;
align-items: center;
gap: 9px;
min-width: 144px;
padding: 8px 11px;
border: 0;
border-radius: 12px;
background: transparent;
color: var(--ink);
cursor: pointer;
text-align: left;
}
.inventory-item:hover { background: rgba(37, 49, 47, 0.055); }
.inventory-icon {
position: relative;
width: 29px;
height: 29px;
flex: 0 0 auto;
border-radius: 9px;
background: #d7e4df;
}
.inventory-icon.sofa::after {
content: "";
position: absolute;
left: 5px;
right: 5px;
bottom: 6px;
height: 12px;
border-radius: 3px 3px 2px 2px;
background: #438878;
box-shadow: 0 -4px 0 -1px #438878;
}
.inventory-icon.visitor::after {
content: "";
position: absolute;
left: 10px;
top: 5px;
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 10px 0 4px var(--accent);
}
.inventory-copy strong {
display: block;
font-size: 11px;
}
.inventory-copy small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9.5px;
}
#divider {
width: 1px;
height: 31px;
background: var(--line);
}
#instructions {
max-width: 230px;
padding: 0 8px 0 2px;
color: var(--muted);
font-size: 9.5px;
line-height: 1.4;
}
kbd {
display: inline-block;
min-width: 18px;
padding: 1px 4px;
border: 1px solid #cfd2cc;
border-bottom-width: 2px;
border-radius: 4px;
background: white;
color: var(--ink);
font: 700 9px inherit;
text-align: center;
}
#selection {
position: fixed;
left: 22px;
bottom: 22px;
z-index: 5;
min-width: 190px;
max-width: 250px;
padding: 12px 14px;
border-radius: 15px;
pointer-events: none;
}
#selection small {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
}
#selectionName {
font-size: 12px;
font-weight: 800;
}
#selectionHint {
margin-top: 4px;
color: var(--muted);
font-size: 9.5px;
line-height: 1.35;
}
#success {
position: fixed;
top: 50%;
left: 50%;
z-index: 20;
width: min(390px, calc(100vw - 36px));
padding: 25px;
border-radius: 23px;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -44%) scale(0.96);
transition: opacity 220ms ease, transform 220ms ease;
}
#success.show {
opacity: 1;
pointer-events: auto;
transform: translate(-50%, -50%) scale(1);
}
#success .success-icon {
display: grid;
width: 48px;
height: 48px;
margin: 0 auto 13px;
place-items: center;
border-radius: 50%;
background: var(--green);
color: white;
font-size: 24px;
}
#success h2 {
margin: 0;
font-size: 27px;
letter-spacing: -0.04em;
}
#success p {
margin: 9px 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
#success button {
width: 100%;
padding: 11px;
border: 0;
border-radius: 12px;
background: var(--ink);
color: white;
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
#loading {
position: fixed;
inset: 0;
z-index: 50;
display: grid;
place-items: center;
background: #e9e5dc;
color: var(--muted);
font-size: 12px;
font-weight: 750;
letter-spacing: 0.08em;
}
@media (max-width: 850px) {
#intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
#intro h1 { font-size: 23px; }
#intro p { display: none; }
#goals {
top: 103px;
right: 12px;
width: 268px;
padding: 11px;
}
.goal { padding: 9px; }
.goal p { font-size: 9.5px; }
#inventory {
bottom: 10px;
width: calc(100vw - 20px);
gap: 3px;
}
.inventory-item { min-width: 0; flex: 1; padding: 7px; }
.inventory-copy small, #instructions, #divider { display: none; }
#selection { display: none; }
}
@media (max-width: 520px) {
#goals {
top: auto;
right: 10px;
bottom: 78px;
width: 235px;
}
.panel-actions { grid-template-columns: 1fr; }
#hintBtn { display: none; }
.goal p { min-height: 0; }
.goal { margin-top: 5px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D apartment planning scene"></canvas>
<section id="intro" class="glass">
<div class="eyebrow">Apartment / Live planner</div>
<h1>One room.<br />Three ways to live.</h1>
<p>
Reuse the same three sofa modules and four visitor markers to create
comfortable seating, a connected conversation area, and a clear route.
</p>
</section>
<aside id="goals" class="glass">
<div class="panel-heading">
<span>LIVE OBJECTIVES</span>
<strong id="goalCount">0 / 3</strong>
</div>
<article class="goal" id="goalSeating">
<div class="goal-check">✓</div>
<div>
<h3>Make everyone comfortable</h3>
<p id="seatingText">0 / 4 visitors are seated</p>
<div class="bar"><i id="seatingBar"></i></div>
</div>
</article>
<article class="goal" id="goalConversation">
<div class="goal-check">✓</div>
<div>
<h3>Bring people together</h3>
<p id="conversationText">Arrange the room around the rug</p>
<div class="bar"><i id="conversationBar"></i></div>
</div>
</article>
<article class="goal" id="goalPassage">
<div class="goal-check">✓</div>
<div>
<h3>Keep the route open</h3>
<p id="passageText">The marked route must stay clear</p>
<div class="bar"><i id="passageBar"></i></div>
</div>
</article>
<div class="panel-actions">
<button id="rotateBtn" type="button">Rotate selected · R</button>
<button id="resetBtn" type="button">Reset layout</button>
</div>
<button id="hintBtn" type="button">Show a suggested layout</button>
</aside>
<section id="selection" class="glass">
<small>SELECTED PIECE</small>
<div id="selectionName">Nothing selected</div>
<div id="selectionHint">Drag any sofa or visitor marker.</div>
</section>
<section id="inventory" class="glass">
<button class="inventory-item" id="selectSofa" type="button">
<span class="inventory-icon sofa"></span>
<span class="inventory-copy">
<strong>3 sofa modules</strong>
<small>2 seat pockets each</small>
</span>
</button>
<button class="inventory-item" id="selectVisitor" type="button">
<span class="inventory-icon visitor"></span>
<span class="inventory-copy">
<strong>4 visitor markers</strong>
<small>Drop onto seat pockets</small>
</span>
</button>
<div id="divider"></div>
<div id="instructions">
<kbd>R</kbd> rotate · arrows nudge<br />
Drag empty space to orbit · wheel to zoom
</div>
</section>
<section id="success" class="glass">
<div class="success-icon">✓</div>
<h2>The room is ready.</h2>
<p>
Seating, conversation, and circulation all work with the same limited
collection of pieces.
</p>
<button id="keepBtn" type="button">Keep arranging</button>
</section>
<div id="loading">BUILDING THE ROOM…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e9e5dc");
scene.fog = new THREE.Fog("#e9e5dc", 23, 40);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
target: new THREE.Vector3(0, 0.35, -0.15),
radius: window.innerWidth < 700 ? 20.5 : 17.2,
theta: 0.66,
phi: 0.84
};
function updateCamera() {
const s = cameraState;
camera.position.set(
s.target.x + s.radius * Math.sin(s.phi) * Math.sin(s.theta),
s.target.y + s.radius * Math.cos(s.phi),
s.target.z + s.radius * Math.sin(s.phi) * Math.cos(s.theta)
);
camera.lookAt(s.target);
camera.updateMatrixWorld();
}
updateCamera();
const hemi = new THREE.HemisphereLight("#fff9e9", "#7e8a83", 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight("#fff0d2", 3.4);
sun.position.set(6, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b8d9df", 1.15);
fill.position.set(-6, 5, -4);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0
})
);
}
return materialCache.get(color);
}
function box(parent, w, h, d, color, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
typeof color === "string" ? material(color) : color
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(parent, top, bottom, height, color, x = 0, y = 0, z = 0, sides = 10) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(top, bottom, height, sides),
typeof color === "string" ? material(color) : color
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, {
width = 512,
height = 128,
background = "rgba(37,49,47,0.88)",
foreground = "#fffdf7",
fontSize = 43,
depthTest = false
} = {}) {
const c = document.createElement("canvas");
c.width = width;
c.height = height;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = background;
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = foreground;
ctx.font = `800 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, width / 2, height / 2 + 2);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest,
depthWrite: false
})
);
return sprite;
}
const room = new THREE.Group();
scene.add(room);
const CONVERSATION_CENTER = new THREE.Vector3(0, 0, -0.3);
const pickables = [];
const sofas = [];
const visitors = [];
const particles = [];
// -------------------------------------------------------------------------
// Apartment shell and furnishings
// -------------------------------------------------------------------------
function createRoom() {
// Floor foundation and individual low-poly planks.
box(room, 10, 0.2, 8, "#b78f65", 0, -0.12, 0);
const plankColors = ["#d8b98f", "#e0c49d", "#d2b187", "#e5cba7"];
for (let i = 0; i < 20; i++) {
const plank = box(
room,
0.475,
0.035,
7.98,
plankColors[i % plankColors.length],
-4.75 + i * 0.5,
-0.005,
0
);
plank.castShadow = false;
}
// Back wall with a window opening.
box(room, 7.85, 3.2, 0.16, "#f4efe5", -1.075, 1.6, -4.05);
box(room, 0.55, 3.2, 0.16, "#f4efe5", 4.725, 1.6, -4.05);
box(room, 1.6, 1.02, 0.16, "#f4efe5", 3.65, 0.51, -4.05);
box(room, 1.6, 0.48, 0.16, "#f4efe5", 3.65, 2.96, -4.05);
// Left wall with an open entry.
box(room, 0.16, 3.2, 5.75, "#eee8dc", -5.05, 1.6, -1.125);
box(room, 0.16, 3.2, 0.75, "#eee8dc", -5.05, 1.6, 3.625);
box(room, 0.16, 0.8, 1.5, "#eee8dc", -5.05, 2.8, 2.5);
// Door opening and frame.
box(room, 0.06, 2.38, 1.42, "#45524f", -4.96, 1.19, 2.5);
box(room, 0.24, 2.48, 0.1, "#c9a97e", -4.88, 1.24, 1.75);
box(room, 0.24, 2.48, 0.1, "#c9a97e", -4.88, 1.24, 3.25);
box(room, 0.24, 0.1, 1.6, "#c9a97e", -4.88, 2.46, 2.5);
box(room, 0.4, 0.04, 1.45, "#c7a77b", -4.86, 0.025, 2.5);
// Window.
box(room, 1.52, 1.53, 0.045, "#9dcbd1", 3.65, 1.83, -4.0);
box(room, 0.08, 1.62, 0.11, "#d6c6a9", 2.91, 1.83, -3.94);
box(room, 0.08, 1.62, 0.11, "#d6c6a9", 4.39, 1.83, -3.94);
box(room, 1.56, 0.08, 0.11, "#d6c6a9", 3.65, 1.09, -3.94);
box(room, 1.56, 0.08, 0.11, "#d6c6a9", 3.65, 2.57, -3.94);
box(room, 0.06, 1.5, 0.09, "#d6c6a9", 3.65, 1.83, -3.92);
box(room, 1.72, 0.09, 0.3, "#d3b78d", 3.65, 1.02, -3.86);
// Skirting.
box(room, 7.8, 0.13, 0.07, "#d8d1c3", -1.1, 0.07, -3.93);
box(room, 0.07, 0.13, 5.7, "#d8d1c3", -4.93, 0.07, -1.15);
// Conversation rug.
box(room, 3.55, 0.035, 2.72, "#c4ae8b", 0, 0.025, -0.3);
box(room, 3.31, 0.04, 2.48, "#dfcfb4", 0, 0.04, -0.3);
// Coffee table.
const table = new THREE.Group();
table.position.set(0, 0, -0.3);
room.add(table);
box(table, 1.26, 0.12, 0.74, "#765744", 0, 0.48, 0);
cylinder(table, 0.07, 0.09, 0.4, "#765744", -0.46, 0.22, -0.23, 8);
cylinder(table, 0.07, 0.09, 0.4, "#765744", 0.46, 0.22, -0.23, 8);
cylinder(table, 0.07, 0.09, 0.4, "#765744", -0.46, 0.22, 0.23, 8);
cylinder(table, 0.07, 0.09, 0.4, "#765744", 0.46, 0.22, 0.23, 8);
box(table, 0.32, 0.055, 0.25, "#d97155", -0.22, 0.57, 0.02);
box(table, 0.28, 0.045, 0.21, "#f0c968", -0.19, 0.62, 0.01);
// Social-zone outline.
const zone = new THREE.Mesh(
new THREE.RingGeometry(2.31, 2.345, 64),
new THREE.MeshBasicMaterial({
color: "#ffffff",
transparent: true,
opacity: 0.68,
depthWrite: false,
side: THREE.DoubleSide
})
);
zone.rotation.x = -Math.PI / 2;
zone.position.set(CONVERSATION_CENTER.x, 0.07, CONVERSATION_CENTER.z);
room.add(zone);
// Sideboard.
box(room, 1.8, 0.64, 0.46, "#8e6c55", -2.75, 0.42, -3.55);
box(room, 0.05, 0.46, 0.03, "#c7a77f", -3.12, 0.43, -3.305);
box(room, 0.05, 0.46, 0.03, "#c7a77f", -2.38, 0.43, -3.305);
cylinder(room, 0.035, 0.035, 0.18, "#584a3e", -3.35, 0.09, -3.35, 6);
cylinder(room, 0.035, 0.035, 0.18, "#584a3e", -2.15, 0.09, -3.35, 6);
// A few books and a vase.
box(room, 0.38, 0.08, 0.25, "#d96f52", -3.15, 0.79, -3.54);
box(room, 0.34, 0.07, 0.23, "#e7bd61", -3.12, 0.865, -3.54);
cylinder(room, 0.11, 0.08, 0.3, "#729a8f", -2.42, 0.89, -3.55, 8);
// Low-poly plant.
const plant = new THREE.Group();
plant.position.set(-4.15, 0, -3.12);
room.add(plant);
cylinder(plant, 0.24, 0.18, 0.36, "#bc7456", 0, 0.18, 0, 8);
const leafColors = ["#4e8063", "#5f9470", "#789c68"];
for (let i = 0; i < 7; i++) {
const leaf = new THREE.Mesh(
new THREE.ConeGeometry(0.19, 0.82, 5),
material(leafColors[i % leafColors.length])
);
const angle = (i / 7) * Math.PI * 2;
leaf.position.set(
Math.cos(angle) * 0.18,
0.66 + (i % 2) * 0.08,
Math.sin(angle) * 0.18
);
leaf.rotation.z = Math.cos(angle) * 0.36;
leaf.rotation.x = Math.sin(angle) * 0.36;
leaf.castShadow = true;
plant.add(leaf);
}
// Floor lamp.
const lamp = new THREE.Group();
lamp.position.set(4.35, 0, -3.1);
room.add(lamp);
cylinder(lamp, 0.22, 0.25, 0.08, "#4b5552", 0, 0.04, 0, 10);
cylinder(lamp, 0.025, 0.025, 1.8, "#4b5552", 0, 0.94, 0, 8);
const shade = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.32, 0.38, 8, 1, true),
material("#e2c79f")
);
shade.position.y = 1.88;
shade.castShadow = true;
lamp.add(shade);
// Wall art.
box(room, 1.35, 1.05, 0.08, "#af8767", -0.95, 1.83, -3.91);
box(room, 1.18, 0.88, 0.04, "#f6e6ca", -0.95, 1.83, -3.85);
box(room, 0.42, 0.42, 0.025, "#d97455", -1.14, 1.9, -3.82);
box(room, 0.31, 0.55, 0.025, "#6d9b86", -0.71, 1.77, -3.82);
}
createRoom();
// -------------------------------------------------------------------------
// Marked circulation route
// -------------------------------------------------------------------------
const corridorSegments = [
{ x: -0.675, z: 2.75, hx: 4.325, hz: 0.7 },
{ x: 3.65, z: -0.2, hx: 0.7, hz: 3.65 }
];
function addRouteSegment(segment) {
const route = new THREE.Mesh(
new THREE.PlaneGeometry(segment.hx * 2, segment.hz * 2),
new THREE.MeshBasicMaterial({
color: "#e4ae3f",
transparent: true,
opacity: 0.18,
depthWrite: false,
side: THREE.DoubleSide
})
);
route.rotation.x = -Math.PI / 2;
route.position.set(segment.x, 0.075, segment.z);
room.add(route);
const points = [
new THREE.Vector3(segment.x - segment.hx, 0.082, segment.z - segment.hz),
new THREE.Vector3(segment.x + segment.hx, 0.082, segment.z - segment.hz),
new THREE.Vector3(segment.x + segment.hx, 0.082, segment.z + segment.hz),
new THREE.Vector3(segment.x - segment.hx, 0.082, segment.z + segment.hz)
];
const outline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: "#c9932c",
transparent: true,
opacity: 0.8
})
);
room.add(outline);
}
corridorSegments.forEach(addRouteSegment);
function addFloorArrow(x, z, yaw) {
const shape = new THREE.Shape();
shape.moveTo(0, 0.28);
shape.lineTo(-0.16, -0.18);
shape.lineTo(0.16, -0.18);
shape.closePath();
const geometry = new THREE.ShapeGeometry(shape);
geometry.rotateX(-Math.PI / 2);
const arrow = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
color: "#bc8525",
transparent: true,
opacity: 0.72,
depthWrite: false,
side: THREE.DoubleSide
})
);
arrow.position.set(x, 0.09, z);
arrow.rotation.y = yaw;
room.add(arrow);
}
[-3.3, -1.1, 1.1].forEach(x => addFloorArrow(x, 2.75, -Math.PI / 2));
[1.5, -0.25, -2].forEach(z => addFloorArrow(3.65, z, 0));
const entryLabel = makeTextSprite("ENTRY", {
width: 300,
height: 96,
fontSize: 47,
background: "rgba(46,61,57,0.92)"
});
entryLabel.position.set(-4.25, 0.75, 2.5);
entryLabel.scale.set(1.18, 0.38, 1);
scene.add(entryLabel);
const balconyLabel = makeTextSprite("BALCONY", {
width: 360,
height: 96,
fontSize: 44,
background: "rgba(46,61,57,0.92)"
});
balconyLabel.position.set(3.65, 0.72, -3.72);
balconyLabel.scale.set(1.38, 0.37, 1);
scene.add(balconyLabel);
// -------------------------------------------------------------------------
// Movable sofa modules
// -------------------------------------------------------------------------
function createSofa(index, color, position, yaw) {
const root = new THREE.Group();
root.position.set(position[0], 0, position[1]);
root.rotation.y = yaw;
scene.add(root);
// Feet.
[-0.72, 0.72].forEach(x => {
[-0.31, 0.31].forEach(z => {
cylinder(root, 0.055, 0.075, 0.18, "#4e4d47", x, 0.09, z, 6);
});
});
box(root, 1.8, 0.3, 0.92, color, 0, 0.28, 0);
box(root, 1.8, 0.61, 0.17, color, 0, 0.66, -0.37);
box(root, 0.16, 0.51, 0.89, color, -0.82, 0.5, 0.01);
box(root, 0.16, 0.51, 0.89, color, 0.82, 0.5, 0.01);
const cushionColor = new THREE.Color(color).offsetHSL(0, -0.05, 0.07);
box(root, 0.77, 0.16, 0.64, cushionColor, -0.395, 0.48, 0.035);
box(root, 0.77, 0.16, 0.64, cushionColor, 0.395, 0.48, 0.035);
// Small orientation notch on the front.
box(root, 0.24, 0.035, 0.035, "#f4e9d2", 0, 0.3, 0.475);
const sofa = {
id: `S${index + 1}`,
type: "sofa",
index,
root,
color,
seats: [],
home: { position: position.slice(), yaw }
};
[-0.39, 0.39].forEach(localX => {
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.18, 0.235, 18),
new THREE.MeshBasicMaterial({
color: "#f1bd52",
transparent: true,
opacity: 0.95,
depthWrite: false,
side: THREE.DoubleSide
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(localX, 0.575, 0.08);
root.add(ring);
sofa.seats.push({
local: new THREE.Vector3(localX, 0, 0.08),
occupant: null,
ring
});
});
root.traverse(object => {
if (object.isMesh) {
object.userData.owner = sofa;
pickables.push(object);
}
});
sofas.push(sofa);
return sofa;
}
createSofa(0, "#438879", [-2.6, -0.4], Math.PI / 2);
createSofa(1, "#cc765d", [-0.6, -2.4], 0);
createSofa(2, "#c89d4f", [3.25, 1.6], 0);
// -------------------------------------------------------------------------
// Movable visitor markers
// -------------------------------------------------------------------------
function createVisitor(index, color, position) {
const root = new THREE.Group();
root.position.set(position[0], 0, position[1]);
scene.add(root);
const baseMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
transparent: true,
opacity: 1
});
const base = cylinder(root, 0.23, 0.28, 0.08, baseMaterial, 0, 0.055, 0, 16);
const torso = cylinder(root, 0.15, 0.19, 0.42, color, 0, 0.45, 0, 8);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.165, 1),
material("#e7b895")
);
head.position.y = 0.83;
head.castShadow = true;
root.add(head);
const legs = [];
[-0.08, 0.08].forEach(x => {
const leg = cylinder(root, 0.05, 0.055, 0.28, "#4d5654", x, 0.19, 0, 6);
legs.push(leg);
});
const arms = [];
[-0.21, 0.21].forEach(x => {
const arm = cylinder(root, 0.045, 0.055, 0.33, color, x, 0.47, 0, 6);
arm.rotation.z = x > 0 ? -0.12 : 0.12;
arms.push(arm);
});
const number = makeTextSprite(String(index + 1), {
width: 128,
height: 128,
background: "rgba(255,255,255,0.95)",
foreground: "#293633",
fontSize: 64
});
number.position.set(0, 1.27, 0);
number.scale.set(0.39, 0.39, 1);
root.add(number);
const visitor = {
id: `V${index + 1}`,
type: "visitor",
index,
root,
color,
assigned: null,
visual: { base, torso, head, legs, arms, number },
home: { position: position.slice(), yaw: 0 }
};
root.traverse(object => {
if (object.isMesh) {
object.userData.owner = visitor;
pickables.push(object);
}
});
visitors.push(visitor);
return visitor;
}
createVisitor(0, "#e66f50", [-3.8, 1.2]);
createVisitor(1, "#5b8fb3", [0.6, 1.4]);
createVisitor(2, "#8e78b5", [4.1, -2.0]);
createVisitor(3, "#d4a541", [-3.2, -2.8]);
// -------------------------------------------------------------------------
// Seat assignment and spatial helpers
// -------------------------------------------------------------------------
function seatWorldPosition(sofa, seatIndex) {
const local = sofa.seats[seatIndex].local;
const yaw = sofa.root.rotation.y;
const c = Math.cos(yaw);
const s = Math.sin(yaw);
return new THREE.Vector3(
sofa.root.position.x + c * local.x + s * local.z,
0,
sofa.root.position.z - s * local.x + c * local.z
);
}
function refreshOccupants() {
for (const sofa of sofas) {
for (const seat of sofa.seats) {
seat.occupant = null;
seat.ring.material.color.set("#f1bd52");
}
}
for (const visitor of visitors) {
if (!visitor.assigned) continue;
const { sofa, index } = visitor.assigned;
const seat = sofa.seats[index];
if (seat.occupant) {
visitor.assigned = null;
setVisitorSeated(visitor, false);
} else {
seat.occupant = visitor;
seat.ring.material.color.set("#54b98c");
}
}
}
function setVisitorSeated(visitor, seated) {
const { torso, head, legs, arms, base } = visitor.visual;
torso.position.y = seated ? 0.61 : 0.45;
head.position.y = seated ? 0.98 : 0.83;
legs.forEach((leg, i) => {
const x = i === 0 ? -0.08 : 0.08;
if (seated) {
leg.position.set(x, 0.43, 0.15);
leg.rotation.x = Math.PI / 2;
} else {
leg.position.set(x, 0.19, 0);
leg.rotation.x = 0;
}
});
arms.forEach((arm, i) => {
arm.position.y = seated ? 0.61 : 0.47;
arm.rotation.x = seated ? -0.3 : 0;
arm.rotation.z = i === 0 ? 0.12 : -0.12;
});
base.material.opacity = seated ? 0.62 : 1;
}
function releaseVisitor(visitor) {
visitor.assigned = null;
setVisitorSeated(visitor, false);
refreshOccupants();
}
function trySeatVisitor(visitor, worldPosition) {
releaseVisitor(visitor);
let best = null;
let bestDistance = Infinity;
for (const sofa of sofas) {
for (let i = 0; i < sofa.seats.length; i++) {
const seat = sofa.seats[i];
if (seat.occupant) continue;
const position = seatWorldPosition(sofa, i);
const distance = Math.hypot(
worldPosition.x - position.x,
worldPosition.z - position.z
);
if (distance < 0.62 && distance < bestDistance) {
best = { sofa, index: i, position };
bestDistance = distance;
}
}
}
if (best) {
visitor.root.position.set(best.position.x, 0, best.position.z);
visitor.root.rotation.y = best.sofa.root.rotation.y;
visitor.assigned = { sofa: best.sofa, index: best.index };
setVisitorSeated(visitor, true);
refreshOccupants();
} else {
visitor.root.position.set(worldPosition.x, 0, worldPosition.z);
const dx = CONVERSATION_CENTER.x - visitor.root.position.x;
const dz = CONVERSATION_CENTER.z - visitor.root.position.z;
visitor.root.rotation.y = Math.atan2(dx, dz);
setVisitorSeated(visitor, false);
}
}
function syncSofaRiders(sofa) {
for (const visitor of visitors) {
if (!visitor.assigned || visitor.assigned.sofa !== sofa) continue;
const position = seatWorldPosition(sofa, visitor.assigned.index);
visitor.root.position.set(position.x, 0, position.z);
visitor.root.rotation.y = sofa.root.rotation.y;
}
}
function clampSofa(sofa) {
sofa.root.position.x = THREE.MathUtils.clamp(
sofa.root.position.x, -4.15, 4.15
);
sofa.root.position.z = THREE.MathUtils.clamp(
sofa.root.position.z, -3.25, 3.45
);
}
function clampVisitor(visitor) {
visitor.root.position.x = THREE.MathUtils.clamp(
visitor.root.position.x, -4.65, 4.65
);
visitor.root.position.z = THREE.MathUtils.clamp(
visitor.root.position.z, -3.6, 3.65
);
}
function distanceXZ(a, b) {
return Math.hypot(a.x - b.x, a.z - b.z);
}
function forwardVector(root) {
return new THREE.Vector3(
Math.sin(root.rotation.y),
0,
Math.cos(root.rotation.y)
);
}
function facesCenter(root, position) {
const toward = new THREE.Vector3(
CONVERSATION_CENTER.x - position.x,
0,
CONVERSATION_CENTER.z - position.z
).normalize();
return forwardVector(root).dot(toward) > 0.12;
}
// -------------------------------------------------------------------------
// Collision / circulation evaluation
// -------------------------------------------------------------------------
function rotatedRectangle(cx, cz, hx, hz, yaw) {
const c = Math.cos(yaw);
const s = Math.sin(yaw);
const corners = [
[-hx, -hz],
[hx, -hz],
[hx, hz],
[-hx, hz]
];
return corners.map(([x, z]) => ({
x: cx + c * x + s * z,
z: cz - s * x + c * z
}));
}
function polygonsOverlap(a, b) {
for (const polygon of [a, b]) {
for (let i = 0; i < polygon.length; i++) {
const p1 = polygon[i];
const p2 = polygon[(i + 1) % polygon.length];
const axisX = -(p2.z - p1.z);
const axisZ = p2.x - p1.x;
const length = Math.hypot(axisX, axisZ);
if (length < 0.00001) continue;
const nx = axisX / length;
const nz = axisZ / length;
const project = poly => poly.map(point => point.x * nx + point.z * nz);
const aProjection = project(a);
const bProjection = project(b);
const aMin = Math.min(...aProjection);
const aMax = Math.max(...aProjection);
const bMin = Math.min(...bProjection);
const bMax = Math.max(...bProjection);
if (aMax <= bMin || bMax <= aMin) return false;
}
}
return true;
}
function sofasBlockingRoute() {
const blocked = [];
for (const sofa of sofas) {
const footprint = rotatedRectangle(
sofa.root.position.x,
sofa.root.position.z,
0.91,
0.48,
sofa.root.rotation.y
);
const intersects = corridorSegments.some(segment => {
const corridor = rotatedRectangle(
segment.x, segment.z, segment.hx, segment.hz, 0
);
return polygonsOverlap(footprint, corridor);
});
if (intersects) blocked.push(sofa);
}
return blocked;
}
// -------------------------------------------------------------------------
// Goal UI
// -------------------------------------------------------------------------
const seatingGoal = document.querySelector("#goalSeating");
const conversationGoal = document.querySelector("#goalConversation");
const passageGoal = document.querySelector("#goalPassage");
const successPanel = document.querySelector("#success");
let wasComplete = false;
function setGoal(element, passed, detail, progress) {
element.classList.toggle("passed", passed);
element.querySelector("p").textContent = detail;
element.querySelector(".bar i").style.width =
`${THREE.MathUtils.clamp(progress, 0, 1) * 100}%`;
element.setAttribute("aria-label", `${element.querySelector("h3").textContent}: ${detail}`);
}
function updateEvaluation() {
const seated = visitors.filter(v => v.assigned).length;
const visitorsInZone = visitors.filter(v =>
distanceXZ(v.root.position, CONVERSATION_CENTER) <= 2.35
);
const sofasInZone = sofas.filter(s =>
distanceXZ(s.root.position, CONVERSATION_CENTER) <= 3.4
);
const visitorFacing = visitorsInZone.filter(v =>
facesCenter(v.root, v.root.position)
).length;
const sofaFacing = sofasInZone.filter(s =>
facesCenter(s.root, s.root.position)
).length;
const blocked = sofasBlockingRoute();
const seatingPassed = seated === visitors.length;
const conversationPassed =
visitorsInZone.length === visitors.length &&
sofasInZone.length === sofas.length &&
visitorFacing === visitors.length &&
sofaFacing === sofas.length;
const passagePassed = blocked.length === 0;
setGoal(
seatingGoal,
seatingPassed,
`${seated} / ${visitors.length} visitors are seated`,
seated / visitors.length
);
const conversationProgress =
(visitorsInZone.length + sofasInZone.length + visitorFacing + sofaFacing) /
(visitors.length * 2 + sofas.length * 2);
setGoal(
conversationGoal,
conversationPassed,
`${visitorsInZone.length}/4 visitors · ${sofasInZone.length}/3 modules in the talk zone`,
conversationProgress
);
setGoal(
passageGoal,
passagePassed,
passagePassed
? "The 1.4 m entry-to-balcony route is clear"
: `${blocked.length} sofa module${blocked.length === 1 ? "" : "s"} block the marked route`,
(sofas.length - blocked.length) / sofas.length
);
const completed = [seatingPassed, conversationPassed, passagePassed];
const count = completed.filter(Boolean).length;
document.querySelector("#goalCount").textContent = `${count} / 3`;
if (completed.every(Boolean) && !wasComplete) {
celebrate();
}
wasComplete = completed.every(Boolean);
successPanel.classList.toggle("show", wasComplete);
}
// -------------------------------------------------------------------------
// Selection, dragging, and direct camera controls
// -------------------------------------------------------------------------
let selected = null;
const selectionRing = new THREE.Mesh(
new THREE.RingGeometry(0.43, 0.51, 32),
new THREE.MeshBasicMaterial({
color: "#e66f50",
transparent: true,
opacity: 0.92,
depthWrite: false,
side: THREE.DoubleSide
})
);
selectionRing.rotation.x = -Math.PI / 2;
selectionRing.visible = false;
scene.add(selectionRing);
function selectObject(object) {
selected = object;
if (!object) {
selectionRing.visible = false;
document.querySelector("#selectionName").textContent = "Nothing selected";
document.querySelector("#selectionHint").textContent = "Drag any sofa or visitor marker.";
return;
}
selectionRing.visible = true;
selectionRing.position.set(
object.root.position.x,
0.095,
object.root.position.z
);
selectionRing.scale.setScalar(object.type === "sofa" ? 1.55 : 1.12);
document.querySelector("#selectionName").textContent =
object.type === "sofa"
? `Sofa module ${object.index + 1}`
: `Visitor marker ${object.index + 1}`;
document.querySelector("#selectionHint").textContent =
object.type === "sofa"
? "Drag to move. Seated visitors travel with the module."
: "Drop near a gold seat pocket to sit.";
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const planePoint = new THREE.Vector3();
let interaction = null;
function setPointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
}
function getOwnerAt(event) {
setPointer(event);
const hits = raycaster.intersectObjects(pickables, false);
return hits.length ? hits[0].object.userData.owner : null;
}
function pointOnFloor(event) {
setPointer(event);
return raycaster.ray.intersectPlane(dragPlane, planePoint)
? planePoint.clone()
: null;
}
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.focus();
if (event.button === 2 || event.button === 1) {
interaction = {
mode: "camera",
pointerId: event.pointerId,
lastX: event.clientX,
lastY: event.clientY
};
canvas.setPointerCapture(event.pointerId);
return;
}
const owner = getOwnerAt(event);
const floorPoint = pointOnFloor(event);
if (owner && floorPoint) {
selectObject(owner);
interaction = {
mode: "drag",
pointerId: event.pointerId,
object: owner,
offsetX: owner.root.position.x - floorPoint.x,
offsetZ: owner.root.position.z - floorPoint.z,
startX: owner.root.position.x,
startZ: owner.root.position.z,
moved: false
};
} else {
interaction = {
mode: "camera",
pointerId: event.pointerId,
lastX: event.clientX,
lastY: event.clientY
};
}
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!interaction) {
const owner = getOwnerAt(event);
canvas.style.cursor = owner ? "grab" : "default";
return;
}
if (interaction.mode === "camera") {
const dx = event.clientX - interaction.lastX;
const dy = event.clientY - interaction.lastY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.46,
1.25
);
interaction.lastX = event.clientX;
interaction.lastY = event.clientY;
updateCamera();
return;
}
const floorPoint = pointOnFloor(event);
if (!floorPoint) return;
const object = interaction.object;
const dx = floorPoint.x + interaction.offsetX;
const dz = floorPoint.z + interaction.offsetZ;
if (Math.hypot(
dx - interaction.startX,
dz - interaction.startZ
) > 0.055) {
interaction.moved = true;
}
if (object.type === "sofa") {
object.root.position.x = Math.round(dx * 4) / 4;
object.root.position.z = Math.round(dz * 4) / 4;
clampSofa(object);
syncSofaRiders(object);
} else {
if (interaction.moved && object.assigned) {
releaseVisitor(object);
}
const proposed = new THREE.Vector3(dx, 0, dz);
clampVisitor({
root: {
position: {
set(x, y, z) {
proposed.x = x;
proposed.z = z;
}
}
}
});
// Apply clamped values directly.
proposed.x = THREE.MathUtils.clamp(proposed.x, -4.65, 4.65);
proposed.z = THREE.MathUtils.clamp(proposed.z, -3.6, 3.65);
trySeatVisitor(object, proposed);
}
selectObject(object);
refreshOccupants();
updateEvaluation();
});
function endInteraction(event) {
if (!interaction) return;
if (interaction.mode === "drag") {
refreshOccupants();
updateEvaluation();
}
if (canvas.hasPointerCapture(interaction.pointerId)) {
canvas.releasePointerCapture(interaction.pointerId);
}
interaction = null;
}
canvas.addEventListener("pointerup", endInteraction);
canvas.addEventListener("pointercancel", endInteraction);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
11,
25
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Keyboard and UI controls
// -------------------------------------------------------------------------
function rotateSelected() {
if (!selected) return;
selected.root.rotation.y += Math.PI / 2;
if (selected.type === "sofa") {
syncSofaRiders(selected);
}
selectObject(selected);
refreshOccupants();
updateEvaluation();
}
function nudgeSelected(dx, dz, step) {
if (!selected) return;
if (selected.type === "sofa") {
selected.root.position.x += dx * step;
selected.root.position.z += dz * step;
clampSofa(selected);
syncSofaRiders(selected);
} else {
releaseVisitor(selected);
const proposed = selected.root.position.clone();
proposed.x += dx * step;
proposed.z += dz * step;
proposed.x = THREE.MathUtils.clamp(proposed.x, -4.65, 4.65);
proposed.z = THREE.MathUtils.clamp(proposed.z, -3.6, 3.65);
trySeatVisitor(selected, proposed);
}
selectObject(selected);
refreshOccupants();
updateEvaluation();
}
window.addEventListener("keydown", event => {
if (!selected) return;
if (event.key.toLowerCase() === "r") {
event.preventDefault();
rotateSelected();
return;
}
const step = event.shiftKey ? 0.5 : 0.25;
if (event.key === "ArrowLeft") {
event.preventDefault();
nudgeSelected(-1, 0, step);
} else if (event.key === "ArrowRight") {
event.preventDefault();
nudgeSelected(1, 0, step);
} else if (event.key === "ArrowUp") {
event.preventDefault();
nudgeSelected(0, -1, step);
} else if (event.key === "ArrowDown") {
event.preventDefault();
nudgeSelected(0, 1, step);
}
});
document.querySelector("#rotateBtn").addEventListener("click", rotateSelected);
document.querySelector("#resetBtn").addEventListener("click", () => {
for (const sofa of sofas) {
sofa.root.position.set(
sofa.home.position[0],
0,
sofa.home.position[1]
);
sofa.root.rotation.y = sofa.home.yaw;
}
for (const visitor of visitors) {
visitor.assigned = null;
visitor.root.position.set(
visitor.home.position[0],
0,
visitor.home.position[1]
);
visitor.root.rotation.y = visitor.home.yaw;
setVisitorSeated(visitor, false);
}
refreshOccupants();
selectObject(null);
updateEvaluation();
});
function selectNext(items) {
const currentIndex = items.indexOf(selected);
const next = items[(currentIndex + 1) % items.length];
selectObject(next);
}
document.querySelector("#selectSofa").addEventListener("click", () => {
selectNext(sofas);
});
document.querySelector("#selectVisitor").addEventListener("click", () => {
selectNext(visitors);
});
document.querySelector("#keepBtn").addEventListener("click", () => {
successPanel.classList.remove("show");
});
// -------------------------------------------------------------------------
// Optional suggested-layout ghost
// -------------------------------------------------------------------------
const hintGroup = new THREE.Group();
hintGroup.visible = false;
scene.add(hintGroup);
const suggestedSofas = [
{ position: [-2.25, -0.35], yaw: Math.PI / 2 },
{ position: [-0.25, -2.15], yaw: 0 },
{ position: [2.25, -0.35], yaw: -Math.PI / 2 }
];
const hintLineMaterial = new THREE.LineBasicMaterial({
color: "#e66f50",
transparent: true,
opacity: 0.72,
depthTest: false
});
const hintSeatMaterial = new THREE.MeshBasicMaterial({
color: "#e66f50",
transparent: true,
opacity: 0.62,
depthTest: false,
depthWrite: false,
side: THREE.DoubleSide
});
suggestedSofas.forEach(spec => {
const edges = new THREE.EdgesGeometry(
new THREE.BoxGeometry(1.8, 0.62, 0.92)
);
const outline = new THREE.LineSegments(edges, hintLineMaterial);
outline.position.set(spec.position[0], 0.34, spec.position[1]);
outline.rotation.y = spec.yaw;
hintGroup.add(outline);
[-0.39, 0.39].forEach(localX => {
const c = Math.cos(spec.yaw);
const s = Math.sin(spec.yaw);
const x = spec.position[0] + c * localX + s * 0.08;
const z = spec.position[1] - s * localX + c * 0.08;
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.18, 0.235, 18),
hintSeatMaterial
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(x, 0.59, z);
hintGroup.add(ring);
});
});
document.querySelector("#hintBtn").addEventListener("click", event => {
hintGroup.visible = !hintGroup.visible;
event.currentTarget.classList.toggle("active", hintGroup.visible);
event.currentTarget.textContent = hintGroup.visible
? "Hide suggested layout"
: "Show a suggested layout";
});
// -------------------------------------------------------------------------
// Lightweight celebration
// -------------------------------------------------------------------------
const confettiGeometry = new THREE.BoxGeometry(0.08, 0.025, 0.16);
const confettiColors = ["#e66f50", "#e6b64e", "#4c9a7c", "#5d8fb2", "#f2d49b"];
function celebrate() {
for (let i = 0; i < 65; i++) {
const mesh = new THREE.Mesh(
confettiGeometry,
new THREE.MeshBasicMaterial({
color: confettiColors[i % confettiColors.length]
})
);
mesh.position.set(
(Math.random() - 0.5) * 6,
2.5 + Math.random() * 3,
(Math.random() - 0.5) * 5
);
mesh.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
scene.add(mesh);
particles.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.2,
1.4 + Math.random() * 2.1,
(Math.random() - 0.5) * 2.2
),
spin: new THREE.Vector3(
Math.random() * 4,
Math.random() * 4,
Math.random() * 4
)
});
}
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.04);
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.velocity.y -= 4.5 * delta;
particle.mesh.position.addScaledVector(particle.velocity, delta);
particle.mesh.rotation.x += particle.spin.x * delta;
particle.mesh.rotation.y += particle.spin.y * delta;
particle.mesh.rotation.z += particle.spin.z * delta;
if (particle.mesh.position.y < 0.05) {
scene.remove(particle.mesh);
particle.mesh.material.dispose();
particles.splice(i, 1);
}
}
renderer.render(scene, camera);
}
refreshOccupants();
updateEvaluation();
renderer.setAnimationLoop(animate);
document.querySelector("#loading")?.remove();
</script>
</body>
</html>
```