Prompt specification
Build or customize the Three.js 3D scene "Community evening setup" 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: Community evening setup
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/community-evening-setup-x0028
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing an extension cable reel. Create a courtyard event setup with booths and low-voltage decorative lights; let users route protected cable paths while a diagram traces each connected lighting branch. Low-poly.
Working Three.js Starter (community-evening-setup-x0028):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cable & Courtyard — Event Lighting Planner</title>
<style>
:root {
--ink: #17252a;
--muted: #66777c;
--paper: rgba(250, 248, 239, 0.94);
--paper-solid: #faf8ef;
--line: rgba(23, 37, 42, 0.13);
--accent: #e7a72f;
--teal: #23a69a;
--coral: #e76f51;
--shadow: 0 18px 50px rgba(25, 45, 51, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #9fcbd7;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
cursor: crosshair;
touch-action: none;
}
.brand {
position: fixed;
top: 20px;
left: 20px;
z-index: 5;
padding: 13px 16px 12px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 15px;
background: rgba(250, 248, 239, 0.9);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
pointer-events: none;
}
.brand strong {
display: block;
font-size: 17px;
letter-spacing: -0.03em;
}
.brand span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
right: 20px;
width: 340px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.64);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(15px);
scrollbar-width: thin;
}
.panel h1 {
margin: 0;
font-size: 20px;
letter-spacing: -0.04em;
}
.eyebrow {
margin-bottom: 5px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.intro {
margin: 7px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 15px 0 7px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-title span:last-child {
color: var(--muted);
font-size: 10px;
font-weight: 600;
letter-spacing: 0;
text-transform: none;
}
#branchList {
display: grid;
gap: 7px;
}
.branch-row {
display: grid;
grid-template-columns: 32px 1fr auto;
align-items: center;
gap: 9px;
width: 100%;
padding: 9px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.58);
color: var(--ink);
text-align: left;
cursor: pointer;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease;
}
.branch-row:hover {
transform: translateY(-1px);
border-color: rgba(23, 37, 42, 0.25);
}
.branch-row.selected {
border-color: var(--branch-color);
background: #fff;
box-shadow: 0 5px 15px rgba(23, 37, 42, 0.07);
}
.branch-number {
display: grid;
place-items: center;
width: 29px;
height: 29px;
border-radius: 9px;
background: var(--branch-color);
color: #fff;
font-size: 12px;
font-weight: 800;
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
}
.branch-name {
display: block;
font-size: 12px;
font-weight: 750;
}
.branch-meta {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.status {
padding: 4px 6px;
border-radius: 999px;
background: rgba(23, 37, 42, 0.07);
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.status.connected {
background: rgba(35, 166, 154, 0.13);
color: #16776e;
}
.selected-card {
margin-top: 12px;
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.58);
}
#selectedName {
margin: 0 0 3px;
font-size: 13px;
font-weight: 800;
}
#selectedDescription {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#diagram {
display: block;
width: 100%;
height: auto;
overflow: visible;
border: 1px solid var(--line);
border-radius: 12px;
background: #f2efe5;
}
.diagram-caption {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 9px;
}
.legend-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 4px;
border-radius: 50%;
background: var(--teal);
}
.route-tools {
display: grid;
grid-template-columns: 1fr 1fr 1.35fr;
gap: 7px;
margin-top: 13px;
}
.tool-button {
min-height: 36px;
padding: 7px 6px;
border: 1px solid var(--line);
border-radius: 9px;
background: #fff;
color: var(--ink);
font-size: 10px;
font-weight: 800;
cursor: pointer;
transition:
transform 120ms ease,
opacity 120ms ease;
}
.tool-button:hover:not(:disabled) {
transform: translateY(-1px);
}
.tool-button:disabled {
opacity: 0.38;
cursor: default;
}
.tool-button.primary {
border-color: var(--ink);
background: var(--ink);
color: #fff;
}
.tip {
margin: 12px 0 0;
padding: 10px 11px;
border-left: 3px solid var(--accent);
border-radius: 3px 8px 8px 3px;
background: rgba(231, 167, 47, 0.1);
color: #5e542f;
font-size: 10px;
line-height: 1.45;
}
.tip strong {
color: var(--ink);
}
.bottom-hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
max-width: calc(100vw - 40px);
padding: 9px 14px;
border: 1px solid rgba(255, 255, 255, 0.5);
border-radius: 999px;
background: rgba(23, 37, 42, 0.82);
color: #fff;
box-shadow: 0 8px 24px rgba(23, 37, 42, 0.18);
font-size: 10px;
text-align: center;
white-space: nowrap;
pointer-events: none;
backdrop-filter: blur(8px);
}
.bottom-hint b {
color: #ffd36f;
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
top: 22px;
transform: translate(-50%, -20px);
max-width: min(420px, calc(100vw - 30px));
padding: 10px 15px;
border-radius: 10px;
background: #17252a;
color: #fff;
box-shadow: var(--shadow);
font-size: 11px;
opacity: 0;
pointer-events: none;
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #9fcbd7;
color: var(--ink);
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 250ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.diagram-route {
fill: none;
stroke-linecap: round;
stroke-linejoin: round;
}
.diagram-route.connected {
stroke-dasharray: 6 4;
animation: diagramFlow 0.9s linear infinite;
}
@keyframes diagramFlow {
to {
stroke-dashoffset: -10;
}
}
@media (max-width: 760px) {
.brand {
top: 10px;
left: 10px;
padding: 10px 12px;
}
.brand strong {
font-size: 14px;
}
.panel {
top: auto;
right: 10px;
bottom: 56px;
width: min(340px, calc(100vw - 20px));
max-height: 43vh;
padding: 14px;
}
.panel h1 {
font-size: 17px;
}
.intro,
#diagram,
.diagram-caption {
display: none;
}
.bottom-hint {
bottom: 10px;
overflow: hidden;
text-overflow: ellipsis;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="brand">
<strong>Cable & Courtyard</strong>
<span>Low-voltage event planner</span>
</div>
<aside class="panel" aria-label="Lighting route planner">
<div class="eyebrow">Protected power routing</div>
<h1>Courtyard lighting</h1>
<p class="intro">
Select a branch, lay protected cable across the courtyard, and connect it
to the numbered lighting target.
</p>
<div class="section-title">
<span>Lighting branches</span>
<span>1 demo connected</span>
</div>
<div id="branchList"></div>
<div class="selected-card">
<p id="selectedName"></p>
<p id="selectedDescription"></p>
</div>
<div class="section-title">
<span>Live wiring diagram</span>
<span>Top view</span>
</div>
<svg id="diagram" viewBox="0 0 300 220" role="img" aria-label="Live courtyard cable diagram">
<rect x="14" y="10" width="272" height="200" rx="8" fill="#e9e4d7" />
<path d="M14 62H286M14 158H286M82 10V210M216 10V210"
stroke="#c9c1b1" stroke-width="1" stroke-dasharray="3 4" />
<rect x="31" y="28" width="54" height="38" rx="3" fill="#d7b7a7" />
<rect x="214" y="27" width="48" height="37" rx="3" fill="#a8c7bc" />
<rect x="32" y="158" width="52" height="34" rx="3" fill="#d7c493" />
<path d="M21 197H279" stroke="#a8c0ae" stroke-width="5" opacity=".5" />
<g id="diagramRoutes"></g>
<g id="diagramNodes"></g>
</svg>
<div class="diagram-caption">
<span><i class="legend-dot"></i> Connected and energized</span>
<span>Reel at center</span>
</div>
<div class="route-tools">
<button class="tool-button" id="undoButton" type="button">Undo</button>
<button class="tool-button" id="clearButton" type="button">Clear</button>
<button class="tool-button primary" id="connectButton" type="button">Connect target</button>
</div>
<p class="tip">
<strong>Click the courtyard</strong> to add cable waypoints. Click a numbered
target or use <strong>Connect target</strong> to finish. Black-and-yellow
blocks represent protective cable ramps.
</p>
</aside>
<div class="bottom-hint">
<b>Click</b> ground to route · <b>Drag</b> to orbit · <b>Scroll</b> to zoom ·
<b>Click</b> a numbered target to connect
</div>
<div id="toast" role="status"></div>
<div id="loading">Building courtyard…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9fcbd7);
scene.fog = new THREE.Fog(0x9fcbd7, 35, 68);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
viewport.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraState = {
target: new THREE.Vector3(0, 0.7, 0),
yaw: -0.58,
pitch: 0.66,
distance: 31
};
function updateCamera() {
const { target, yaw, pitch, distance } = cameraState;
const horizontal = Math.cos(pitch) * distance;
camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * distance,
target.z + Math.cos(yaw) * horizontal
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xdff5ff, 0x8c7752, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d0, 3.2);
sun.position.set(-15, 24, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -22;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x9ad9ff, 0.7);
fillLight.position.set(16, 12, -18);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true,
...options
});
const materials = {
ground: material(0xc9b995),
courtyard: material(0xd8ccb0),
grout: new THREE.LineBasicMaterial({
color: 0x9f947c,
transparent: true,
opacity: 0.42
}),
wall: material(0xd7c4a4),
wallCap: material(0xb59b7b),
wood: material(0x7a5237),
woodLight: material(0xa57445),
darkWood: material(0x46352c),
counter: material(0xe4d4af),
metal: material(0x46545a, { metalness: 0.5, roughness: 0.48 }),
blackMetal: material(0x242e31, { metalness: 0.45 }),
rampYellow: material(0xe7ad32),
rampDark: material(0x30383a),
cable: material(0x171d1f, { roughness: 0.65 }),
leaf: material(0x557d53),
leafLight: material(0x769963),
pot: material(0xb46f4d),
cream: material(0xf3e8ce),
red: material(0xc85d48),
blue: material(0x4c8190),
green: material(0x5f9b83)
};
function addBox(
width,
height,
depth,
mat,
x = 0,
y = 0,
z = 0,
parent = scene
) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(width, height, depth), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
radiusTop,
radiusBottom,
height,
segments,
mat,
x = 0,
y = 0,
z = 0,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, color = "#17252a", background = "#faf4df") {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 80;
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = color;
context.font = "800 30px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text.toUpperCase(), 160, 42);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({ map: texture, depthWrite: false })
);
sprite.scale.set(3.5, 0.875, 1);
return sprite;
}
function clearGroup(group) {
while (group.children.length) {
const child = group.children.pop();
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
}
}
// -------------------------------------------------------------------------
// Courtyard environment
// -------------------------------------------------------------------------
function buildCourtyard() {
addBox(40, 0.5, 30, materials.ground, 0, -0.34, 0);
addBox(35, 0.24, 25, materials.courtyard, 0, -0.08, 0);
const gridPositions = [];
for (let x = -16; x <= 16; x += 2) {
gridPositions.push(x, 0.055, -12, x, 0.055, 12);
}
for (let z = -12; z <= 12; z += 2) {
gridPositions.push(-17, 0.055, z, 17, 0.055, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
scene.add(new THREE.LineSegments(gridGeometry, materials.grout));
// Back and side walls.
addBox(35, 2.7, 0.55, materials.wall, 0, 1.3, -12.5);
addBox(35, 0.18, 0.75, materials.wallCap, 0, 2.72, -12.5);
addBox(0.55, 2.25, 25, materials.wall, -17.3, 1.08, 0);
addBox(0.75, 0.18, 25, materials.wallCap, -17.3, 2.27, 0);
addBox(0.55, 1.45, 25, materials.wall, 17.3, 0.7, 0);
// A shallow planted border at the back.
addBox(29, 0.42, 1.2, materials.pot, 0, 0.18, -11.5);
for (let x = -13; x <= 13; x += 3.1) {
const shrub = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.62 + (x % 2) * 0.08, 0),
x % 3 < 1 ? materials.leaf : materials.leafLight
);
shrub.position.set(x, 0.82, -11.5);
shrub.scale.y = 0.8;
shrub.castShadow = true;
scene.add(shrub);
}
// Benches along the back wall.
for (const x of [-5.4, 5.4]) {
addBox(4.4, 0.22, 0.75, materials.woodLight, x, 0.72, -10.9);
addBox(4.4, 0.75, 0.16, materials.wood, x, 1.12, -11.2);
addBox(0.18, 0.65, 0.6, materials.darkWood, x - 1.7, 0.33, -10.9);
addBox(0.18, 0.65, 0.6, materials.darkWood, x + 1.7, 0.33, -10.9);
}
}
function createBooth(x, z, width, depth, color, title, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
addBox(width, 2.5, 0.18, materials.wood, 0, 1.25, -depth / 2, group);
addBox(width + 0.45, 0.16, depth + 0.45, color, 0, 3.05, 0, group);
// Alternating canopy fabric strips.
const stripCount = Math.max(4, Math.floor(width / 1.05));
const stripWidth = (width + 0.45) / stripCount;
for (let i = 0; i < stripCount; i++) {
if (i % 2 === 0) continue;
addBox(
stripWidth * 0.98,
0.035,
depth + 0.47,
materials.cream,
-width / 2 - 0.225 + stripWidth * (i + 0.5),
3.14,
0,
group
);
}
// Front valance.
addBox(width + 0.45, 0.38, 0.10, color, 0, 2.86, depth / 2 + 0.25, group);
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
addCylinder(
0.065,
0.065,
3,
6,
materials.darkWood,
sx * width * 0.47,
1.5,
sz * depth * 0.47,
group
);
}
}
addBox(width * 0.8, 1.05, 0.9, materials.counter, 0, 0.53, depth * 0.18, group);
addBox(width * 0.88, 0.12, 1.05, materials.woodLight, 0, 1.11, depth * 0.18, group);
// Small products on the counter.
for (let i = 0; i < 4; i++) {
const product = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.16, 0),
i % 2 ? materials.red : materials.blue
);
product.position.set(
-width * 0.27 + i * (width * 0.18),
1.3,
depth * 0.18
);
product.castShadow = true;
group.add(product);
}
const label = makeLabel(title);
label.position.set(0, 3.72, 0.1);
group.add(label);
}
function createTable(x, z, color = materials.blue) {
addCylinder(0.9, 0.9, 0.13, 8, color, x, 0.92, z);
addCylinder(0.08, 0.1, 0.85, 6, materials.darkWood, x, 0.43, z);
addCylinder(0.4, 0.45, 0.1, 8, materials.darkWood, x, 0.05, z);
for (let i = 0; i < 3; i++) {
const angle = (i / 3) * Math.PI * 2;
const seat = addBox(0.52, 0.12, 0.52, materials.woodLight);
seat.position.set(
x + Math.cos(angle) * 1.25,
0.5,
z + Math.sin(angle) * 1.25
);
addBox(0.12, 0.45, 0.12, materials.darkWood, seat.position.x, 0.25, seat.position.z);
}
}
function createPlanter(x, z, scale = 1) {
addBox(1.45 * scale, 0.7 * scale, 1.45 * scale, materials.pot, x, 0.35 * scale, z);
addCylinder(
0.14 * scale,
0.18 * scale,
1.6 * scale,
6,
materials.wood,
x,
1.25 * scale,
z
);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.95 * scale, 0),
materials.leaf
);
crown.position.set(x, 2.35 * scale, z);
crown.scale.set(1, 1.12, 0.92);
crown.castShadow = true;
scene.add(crown);
}
buildCourtyard();
createBooth(-11.1, -6.6, 6.1, 3.8, materials.red, "Flower Cart", 0);
createBooth(8.8, -6.8, 5.8, 3.7, materials.blue, "Coffee Bar", 0);
createBooth(-10.8, 6.7, 5.7, 3.5, materials.green, "Local Goods", Math.PI);
createTable(7.7, 5.7, materials.blue);
createTable(-3.8, -1.4, materials.green);
createPlanter(15.1, -9.6, 0.85);
createPlanter(-15.4, 9.7, 0.95);
createPlanter(14.9, 9.8, 0.8);
// -------------------------------------------------------------------------
// Cable reel and distribution point
// -------------------------------------------------------------------------
const hub = { x: 0, z: 6.2 };
function createCableReel() {
const group = new THREE.Group();
group.position.set(hub.x, 0, hub.z + 0.35);
scene.add(group);
// Axle and cheeks.
const axle = addCylinder(0.11, 0.11, 1.55, 8, materials.metal, 0, 1.05, 0, group);
axle.rotation.z = Math.PI / 2;
const cheekMaterial = material(0xdc743f, { roughness: 0.62 });
for (const side of [-1, 1]) {
const cheek = addCylinder(0.94, 0.94, 0.16, 12, cheekMaterial, side * 0.55, 1.05, 0, group);
cheek.rotation.z = Math.PI / 2;
const hubCap = addCylinder(0.28, 0.28, 0.2, 10, materials.darkWood, side * 0.68, 1.05, 0, group);
hubCap.rotation.z = Math.PI / 2;
}
// Wound black cable.
for (let i = 0; i < 7; i++) {
const coil = new THREE.Mesh(
new THREE.TorusGeometry(0.42 + i * 0.055, 0.065, 6, 18),
materials.cable
);
coil.rotation.y = Math.PI / 2;
coil.position.x = -0.34 + i * 0.1;
coil.position.y = 1.05;
coil.castShadow = true;
group.add(coil);
}
// Frame.
for (const side of [-1, 1]) {
const leg = addBox(0.12, 1.55, 0.12, materials.darkWood, side * 0.82, 0.73, 0, group);
leg.rotation.z = side * -0.16;
const foot = addBox(0.55, 0.12, 1.25, materials.darkWood, side * 0.93, 0.07, 0, group);
foot.rotation.y = side * 0.12;
}
// Crank.
addCylinder(0.055, 0.055, 0.48, 8, materials.metal, 0.88, 1.05, 0, group)
.rotation.z = Math.PI / 2;
addBox(0.09, 0.43, 0.09, materials.metal, 1.05, 1.25, 0, group);
addCylinder(0.09, 0.09, 0.32, 8, materials.woodLight, 1.05, 1.43, 0, group);
// Distribution pad and plug.
addCylinder(0.46, 0.52, 0.14, 10, materials.blackMetal, 0, 0.08, -0.8, group);
addBox(0.34, 0.18, 0.45, materials.rampYellow, 0, 0.2, -0.85, group);
const label = makeLabel("Power Reel", "#fff8e9", "#bd5b35");
label.position.set(0, 2.4, 0);
group.add(label);
}
createCableReel();
// -------------------------------------------------------------------------
// Lighting branches
// -------------------------------------------------------------------------
const branches = [
{
id: "market",
number: 1,
name: "Market String",
color: "#f2b53f",
colorHex: 0xf2b53f,
target: { x: 10.5, z: -8.3 },
description: "Warm festoon lights above the flower cart and coffee bar.",
route: []
},
{
id: "lanterns",
number: 2,
name: "Lantern Walk",
color: "#23a69a",
colorHex: 0x23a69a,
target: { x: -14.1, z: 7.1 },
description: "Low-voltage lanterns along the west edge of the event space.",
route: []
},
{
id: "pergola",
number: 3,
name: "Pergola Glow",
color: "#e76f51",
colorHex: 0xe76f51,
target: { x: 13.7, z: 8.1 },
description: "Decorative strings around the east-side seating and pergola.",
route: []
}
];
const branchPickMeshes = [];
const endpointGroups = [];
branches.forEach((branch) => {
branch.route = [{ ...hub }];
branch.connected = false;
branch.routeGroup = new THREE.Group();
scene.add(branch.routeGroup);
branch.pulseMeshes = [];
branch.bulbMaterial = material(0xffedbd, {
emissive: branch.colorHex,
emissiveIntensity: 0.15,
roughness: 0.45
});
branch.endpointMaterial = material(branch.colorHex, {
emissive: branch.colorHex,
emissiveIntensity: 0.75,
roughness: 0.4
});
branch.accentMaterial = material(branch.colorHex, {
emissive: branch.colorHex,
emissiveIntensity: 0.9,
roughness: 0.45
});
});
// The first branch is supplied as a completed demonstration.
branches[0].route = [
{ ...hub },
{ x: 3.2, z: 5.9 },
{ x: 4.1, z: 0.2 },
{ x: 4.2, z: -4.8 },
{ x: 7.1, z: -7.3 },
{ ...branches[0].target }
];
branches[0].connected = true;
function addString(branch, start, end, bulbCount, addPoles = true) {
const a = new THREE.Vector3(start.x, start.y, start.z);
const b = new THREE.Vector3(end.x, end.y, end.z);
const points = [];
for (let i = 0; i <= 24; i++) {
const t = i / 24;
const point = a.clone().lerp(b, t);
point.y -= Math.sin(t * Math.PI) * 0.65;
points.push(point);
}
const curve = new THREE.CatmullRomCurve3(points);
const cable = new THREE.Mesh(
new THREE.TubeGeometry(curve, 32, 0.028, 5, false),
materials.cable
);
cable.castShadow = true;
scene.add(cable);
for (let i = 0; i < bulbCount; i++) {
const t = (i + 1) / (bulbCount + 1);
const point = curve.getPointAt(t);
const socket = addCylinder(
0.06,
0.075,
0.13,
6,
materials.darkWood,
point.x,
point.y - 0.09,
point.z
);
socket.userData.branchId = branch.id;
branchPickMeshes.push(socket);
const bulb = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.14, 1),
branch.bulbMaterial
);
bulb.position.set(point.x, point.y - 0.22, point.z);
bulb.castShadow = true;
bulb.userData.branchId = branch.id;
scene.add(bulb);
branchPickMeshes.push(bulb);
}
if (addPoles) {
for (const point of [a, b]) {
addCylinder(0.055, 0.075, point.y, 7, materials.darkWood, point.x, point.y / 2, point.z);
addCylinder(0.18, 0.22, 0.12, 8, materials.metal, point.x, 0.06, point.z);
}
}
const light = new THREE.PointLight(branch.colorHex, 0, 9, 2);
light.position.copy(a.clone().lerp(b, 0.5));
light.position.y -= 0.25;
scene.add(light);
branch.pointLights = branch.pointLights || [];
branch.pointLights.push(light);
}
function addLantern(branch, x, z) {
addCylinder(0.07, 0.1, 2.15, 7, materials.darkWood, x, 1.08, z);
addCylinder(0.2, 0.25, 0.12, 8, materials.metal, x, 0.06, z);
const cap = new THREE.Mesh(
new THREE.ConeGeometry(0.38, 0.26, 4),
materials.blackMetal
);
cap.position.set(x, 2.28, z);
cap.rotation.y = Math.PI / 4;
cap.castShadow = true;
scene.add(cap);
const glow = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 0.48, 0.38),
branch.bulbMaterial
);
glow.position.set(x, 1.96, z);
glow.castShadow = true;
glow.userData.branchId = branch.id;
scene.add(glow);
branchPickMeshes.push(glow);
addBox(0.46, 0.07, 0.46, materials.blackMetal, x, 1.69, z);
}
function addEndpoint(branch) {
const group = new THREE.Group();
group.position.set(branch.target.x, 0, branch.target.z);
group.userData.branchId = branch.id;
addCylinder(0.42, 0.48, 0.12, 10, materials.blackMetal, 0, 0.07, 0, group);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.35, 0.075, 6, 18),
branch.endpointMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.2;
group.add(ring);
const orb = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.17, 1),
branch.endpointMaterial
);
orb.position.y = 0.48;
group.add(orb);
const label = makeLabel(String(branch.number), "#ffffff", branch.color);
label.position.set(0, 1.15, 0);
group.add(label);
branch.endpointRing = ring;
branch.endpointOrb = orb;
endpointGroups.push(group);
scene.add(group);
}
// Branch 1: festoon over the market stalls.
addString(
branches[0],
{ x: -11.4, y: 4.35, z: -8.5 },
{ x: 10.5, y: 4.35, z: -8.3 },
13,
true
);
// Branch 2: individual lanterns along the west path.
for (const z of [-2.2, 0.2, 2.6, 5.0, 7.1]) {
addLantern(branches[1], -14.1, z);
}
// Branch 3: pergola strings and seating lights.
const pergola = new THREE.Group();
scene.add(pergola);
for (const x of [9.2, 14.0]) {
for (const z of [-0.8, 8.0]) {
addCylinder(0.09, 0.12, 3.7, 7, materials.wood, x, 1.85, z, pergola);
}
}
addBox(5.3, 0.18, 0.18, materials.woodLight, 11.6, 3.75, -0.8, pergola);
addBox(5.3, 0.18, 0.18, materials.woodLight, 11.6, 3.75, 8.0, pergola);
addBox(0.18, 0.18, 9.0, materials.woodLight, 9.2, 3.75, 3.6, pergola);
addBox(0.18, 0.18, 9.0, materials.woodLight, 14.0, 3.75, 3.6, pergola);
addString(
branches[2],
{ x: 9.2, y: 3.65, z: -0.8 },
{ x: 14.0, y: 3.65, z: -0.8 },
5,
false
);
addString(
branches[2],
{ x: 14.0, y: 3.65, z: -0.8 },
{ x: 14.0, y: 3.65, z: 8.0 },
8,
false
);
addString(
branches[2],
{ x: 9.2, y: 3.65, z: 8.0 },
{ x: 14.0, y: 3.65, z: 8.0 },
5,
false
);
branches.forEach(addEndpoint);
// -------------------------------------------------------------------------
// Protected route geometry
// -------------------------------------------------------------------------
const routeCableMaterial = materials.cable;
const routeRampYellow = materials.rampYellow;
const routeRampDark = materials.rampDark;
function makeSegmentCylinder(a, b, radius, mat, y, parent) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (length < 0.001) return null;
const geometry = new THREE.CylinderGeometry(radius, radius, length, 6);
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(
(a.x + b.x) / 2,
y,
(a.z + b.z) / 2
);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(dx, 0, dz).normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function rebuildRoute(branch) {
clearGroup(branch.routeGroup);
for (const pulse of branch.pulseMeshes) {
scene.remove(pulse);
pulse.geometry.dispose();
}
branch.pulseMeshes = [];
for (let i = 0; i < branch.route.length - 1; i++) {
const a = branch.route[i];
const b = branch.route[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (length < 0.001) continue;
const angle = -Math.atan2(dz, dx);
const sections = Math.max(1, Math.ceil(length / 0.88));
// Alternating black and yellow protective ramp sections.
for (let section = 0; section < sections; section++) {
const t0 = section / sections;
const t1 = (section + 1) / sections;
const p0 = {
x: a.x + dx * t0,
z: a.z + dz * t0
};
const p1 = {
x: a.x + dx * t1,
z: a.z + dz * t1
};
const segmentLength = Math.hypot(p1.x - p0.x, p1.z - p0.z);
const ramp = new THREE.Mesh(
new THREE.BoxGeometry(segmentLength * 0.94, 0.12, 0.56),
section % 2 === 0 ? routeRampYellow : routeRampDark
);
ramp.position.set(
(p0.x + p1.x) / 2,
0.065,
(p0.z + p1.z) / 2
);
ramp.rotation.y = angle;
ramp.castShadow = true;
ramp.receiveShadow = true;
branch.routeGroup.add(ramp);
}
// The actual cable sits on top of the protective ramp.
makeSegmentCylinder(a, b, 0.055, routeCableMaterial, 0.16, branch.routeGroup);
// A subtle colored identification line above the cable.
makeSegmentCylinder(a, b, 0.018, branch.accentMaterial, 0.225, branch.routeGroup);
}
// Visible waypoint pins.
branch.route.forEach((point, index) => {
if (index === 0 || index === branch.route.length - 1) return;
const pin = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.12, 0.2, 6),
branch.accentMaterial
);
pin.position.set(point.x, 0.15, point.z);
pin.castShadow = true;
branch.routeGroup.add(pin);
});
if (branch.connected) {
for (let i = 0; i < 3; i++) {
const pulse = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 1),
branch.accentMaterial
);
pulse.visible = false;
scene.add(pulse);
branch.pulseMeshes.push(pulse);
}
}
}
function routeLength(branch) {
let length = 0;
for (let i = 0; i < branch.route.length - 1; i++) {
length += Math.hypot(
branch.route[i + 1].x - branch.route[i].x,
branch.route[i + 1].z - branch.route[i].z
);
}
return length;
}
function sampleRoute(branch, distance) {
const points = branch.route;
if (points.length === 0) return new THREE.Vector3(hub.x, 0.3, hub.z);
if (points.length === 1) return new THREE.Vector3(points[0].x, 0.3, points[0].z);
let remaining = Math.max(0, distance);
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const length = Math.hypot(b.x - a.x, b.z - a.z);
if (remaining <= length || i === points.length - 2) {
const t = length > 0 ? Math.min(1, remaining / length) : 0;
return new THREE.Vector3(
a.x + (b.x - a.x) * t,
0.3,
a.z + (b.z - a.z) * t
);
}
remaining -= length;
}
return new THREE.Vector3(points.at(-1).x, 0.3, points.at(-1).z);
}
// -------------------------------------------------------------------------
// UI and live diagram
// -------------------------------------------------------------------------
const branchList = document.getElementById("branchList");
const selectedName = document.getElementById("selectedName");
const selectedDescription = document.getElementById("selectedDescription");
const undoButton = document.getElementById("undoButton");
const clearButton = document.getElementById("clearButton");
const connectButton = document.getElementById("connectButton");
const diagramRoutes = document.getElementById("diagramRoutes");
const diagramNodes = document.getElementById("diagramNodes");
const toastElement = document.getElementById("toast");
let selectedBranchId = "market";
let toastTimer = 0;
function showToast(message) {
toastElement.textContent = message;
toastElement.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toastElement.classList.remove("visible");
}, 2400);
}
function getSelectedBranch() {
return branches.find((branch) => branch.id === selectedBranchId);
}
function mapPoint(point) {
return {
x: 14 + ((point.x + 18) / 36) * 272,
y: 10 + ((point.z + 14) / 28) * 200
};
}
function updateDiagram() {
diagramRoutes.innerHTML = branches
.map((branch) => {
if (branch.route.length < 2) return "";
const d = branch.route
.map((point, index) => {
const mapped = mapPoint(point);
return `${index === 0 ? "M" : "L"}${mapped.x.toFixed(1)},${mapped.y.toFixed(1)}`;
})
.join(" ");
return `
<path
class="diagram-route ${branch.connected ? "connected" : ""}"
d="${d}"
stroke="${branch.color}"
stroke-width="${branch.connected ? 3.2 : 2}"
opacity="${branch.connected ? 1 : 0.5}"
${branch.connected ? "" : 'stroke-dasharray="3 4"'}
/>
`;
})
.join("");
const hubPoint = mapPoint(hub);
diagramNodes.innerHTML = `
<g>
<circle cx="${hubPoint.x}" cy="${hubPoint.y}" r="8" fill="#27363a" />
<circle cx="${hubPoint.x}" cy="${hubPoint.y}" r="3" fill="#ffd36f" />
<path d="M${hubPoint.x - 4},${hubPoint.y + 7}L${hubPoint.x},${hubPoint.y + 11}L${hubPoint.x + 4},${hubPoint.y + 7}"
fill="#27363a" />
<text x="${hubPoint.x}" y="${hubPoint.y + 20}" text-anchor="middle"
font-family="system-ui" font-size="8" font-weight="700" fill="#526268">REEL</text>
</g>
${branches
.map((branch) => {
const point = mapPoint(branch.target);
return `
<g>
<circle cx="${point.x}" cy="${point.y}" r="9"
fill="${branch.connected ? branch.color : "#faf8ef"}"
stroke="${branch.color}" stroke-width="2" />
<text x="${point.x}" y="${point.y + 3.2}" text-anchor="middle"
font-family="system-ui" font-size="9" font-weight="800"
fill="${branch.connected ? "#ffffff" : branch.color}">${branch.number}</text>
</g>
`;
})
.join("")}
`;
}
function updateLights() {
branches.forEach((branch) => {
branch.bulbMaterial.emissiveIntensity = branch.connected ? 2.4 : 0.13;
for (const light of branch.pointLights || []) {
light.intensity = branch.connected ? 14 : 0;
}
branch.endpointMaterial.emissiveIntensity = branch.connected ? 1.5 : 0.48;
});
}
function updateUI() {
const selected = getSelectedBranch();
branchList.innerHTML = branches
.map(
(branch) => `
<button
type="button"
class="branch-row ${branch.id === selectedBranchId ? "selected" : ""}"
data-branch-id="${branch.id}"
style="--branch-color:${branch.color}"
>
<span class="branch-number">${branch.number}</span>
<span>
<span class="branch-name">${branch.name}</span>
<span class="branch-meta">${
branch.route.length - 1
} route points · ${routeLength(branch).toFixed(1)} m</span>
</span>
<span class="status ${branch.connected ? "connected" : ""}">
${branch.connected ? "Live" : "Open"}
</span>
</button>
`
)
.join("");
selectedName.textContent = `${selected.number}. ${selected.name}`;
selectedDescription.textContent = selected.description;
undoButton.disabled = selected.route.length <= 1;
clearButton.disabled = selected.route.length <= 1;
connectButton.disabled = selected.connected;
connectButton.textContent = selected.connected ? "Connected" : "Connect target";
}
function refreshBranch(branch) {
rebuildRoute(branch);
updateLights();
updateDiagram();
updateUI();
}
function selectBranch(id) {
selectedBranchId = id;
updateUI();
}
function connectSelected() {
const branch = getSelectedBranch();
if (branch.connected) return;
const last = branch.route.at(-1);
const alreadyAtTarget =
Math.hypot(last.x - branch.target.x, last.z - branch.target.z) < 0.2;
if (!alreadyAtTarget) {
branch.route.push({ ...branch.target });
}
branch.connected = true;
refreshBranch(branch);
showToast(`${branch.name} is connected and energized.`);
}
function undoRoute() {
const branch = getSelectedBranch();
if (branch.route.length <= 1) return;
branch.route.pop();
branch.connected = false;
refreshBranch(branch);
showToast("Removed the last route point.");
}
function clearRoute() {
const branch = getSelectedBranch();
branch.route = [{ ...hub }];
branch.connected = false;
refreshBranch(branch);
showToast("Route cleared back to the power reel.");
}
branchList.addEventListener("click", (event) => {
const button = event.target.closest("[data-branch-id]");
if (!button) return;
selectBranch(button.dataset.branchId);
});
undoButton.addEventListener("click", undoRoute);
clearButton.addEventListener("click", clearRoute);
connectButton.addEventListener("click", connectSelected);
// -------------------------------------------------------------------------
// Direct camera controls and route interaction
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const groundPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const groundHit = new THREE.Vector3();
let pointerDown = false;
let pointerMoved = false;
let pointerStartX = 0;
let pointerStartY = 0;
let previousX = 0;
let previousY = 0;
let activePointerId = null;
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findBranchId(object) {
let current = object;
while (current) {
if (current.userData && current.userData.branchId) {
return current.userData.branchId;
}
current = current.parent;
}
return null;
}
function pickEndpoint(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(endpointGroups, true);
if (!hits.length) return null;
const branchId = findBranchId(hits[0].object);
return branchId || null;
}
function pickBranchMesh(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(branchPickMeshes, false);
if (!hits.length) return null;
return findBranchId(hits[0].object);
}
function addGroundWaypoint(event) {
const branch = getSelectedBranch();
if (branch.connected) {
showToast("This branch is already connected. Clear it to route a new path.");
return;
}
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
if (!raycaster.ray.intersectPlane(groundPlane, groundHit)) return;
const point = { x: groundHit.x, z: groundHit.z };
const last = branch.route.at(-1);
if (Math.hypot(point.x - last.x, point.z - last.z) < 0.2) return;
const distanceToTarget = Math.hypot(
point.x - branch.target.x,
point.z - branch.target.z
);
if (distanceToTarget < 1.15) {
branch.route.push({ ...branch.target });
branch.connected = true;
refreshBranch(branch);
showToast(`${branch.name} is connected and energized.`);
} else {
branch.route.push(point);
refreshBranch(branch);
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (activePointerId !== null) return;
activePointerId = event.pointerId;
pointerDown = true;
pointerMoved = false;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown || event.pointerId !== activePointerId) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
const totalMovement = Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
);
if (totalMovement > 5) pointerMoved = true;
if (pointerMoved) {
if (event.shiftKey) {
const panScale = cameraState.distance * 0.0015;
const right = new THREE.Vector3(
Math.cos(cameraState.yaw),
0,
-Math.sin(cameraState.yaw)
);
const forward = new THREE.Vector3(
Math.sin(cameraState.yaw),
0,
Math.cos(cameraState.yaw)
);
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, -dy * panScale);
} else {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.25,
1.25
);
}
updateCamera();
}
previousX = event.clientX;
previousY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (event.pointerId !== activePointerId) return;
const wasClick = pointerDown && !pointerMoved && event.button === 0;
pointerDown = false;
activePointerId = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (!wasClick) return;
const endpointId = pickEndpoint(event);
if (endpointId) {
if (endpointId !== selectedBranchId) {
selectBranch(endpointId);
showToast(`${getSelectedBranch().name} selected.`);
} else {
connectSelected();
}
return;
}
const branchId = pickBranchMesh(event);
if (branchId) {
selectBranch(branchId);
showToast(`${getSelectedBranch().name} selected.`);
return;
}
addGroundWaypoint(event);
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
pointerMoved = false;
activePointerId = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.018,
12,
52
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
branches.forEach((branch) => {
if (branch.endpointRing) {
const selected = branch.id === selectedBranchId;
const pulse = 1 + Math.sin(elapsed * 3 + branch.number) * 0.08;
branch.endpointRing.scale.setScalar(selected ? pulse * 1.18 : pulse);
branch.endpointOrb.rotation.y = elapsed * 0.8;
branch.endpointOrb.position.y = 0.48 + Math.sin(elapsed * 2.4 + branch.number) * 0.05;
}
if (branch.connected) {
const length = routeLength(branch);
branch.pulseMeshes.forEach((pulse, index) => {
const distance = (elapsed * 2.6 + (index * length) / 3) % length;
pulse.position.copy(sampleRoute(branch, distance));
pulse.visible = true;
});
}
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
branches.forEach(rebuildRoute);
updateLights();
updateDiagram();
updateUI();
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
showToast("Demo route connected. Select a branch and lay your own cable path.");
});
</script>
</body>
</html>
```