Prompt specification
Build or customize the Three.js 3D scene "Desk cable guide cross-section" 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: Desk cable guide cross-section
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/desk-cable-guide-cross-section-x0173
Scene Description Prompt:
I'd like to see this as a stylized Three.js scene: desk cable guide cross-section. Model a cable tray, clip, and grommet with three contrasting cable paths; each path should remain continuous and separately visible through the shared routing system. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (desk-cable-guide-cross-section-x0173):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Desk Cable Guide — Stylized Cross-Section</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf8ff;
--muted: #8da8b8;
--panel: rgba(7, 18, 31, 0.78);
--line: rgba(154, 216, 235, 0.18);
--cyan: #39e4d0;
--amber: #ffad42;
--violet: #b681ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 68% 28%, rgba(39, 93, 118, 0.22), transparent 38%),
radial-gradient(circle at 18% 76%, rgba(31, 54, 82, 0.28), transparent 42%),
linear-gradient(145deg, #091522 0%, #06101b 55%, #030912 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.title-block {
position: absolute;
top: clamp(20px, 4vw, 48px);
left: clamp(20px, 4vw, 56px);
max-width: min(420px, calc(100vw - 40px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 1px;
content: "";
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
h1 {
margin: 0;
color: var(--ink);
font-size: clamp(25px, 3.2vw, 44px);
font-weight: 650;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 360px;
margin: 14px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
letter-spacing: 0.025em;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 20px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
min-width: 108px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(8, 20, 33, 0.58);
backdrop-filter: blur(12px);
color: #b9cbd6;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.swatch {
width: 15px;
height: 4px;
border-radius: 4px;
background: var(--color);
box-shadow: 0 0 9px color-mix(in srgb, var(--color) 70%, transparent);
}
.tools {
position: absolute;
top: clamp(20px, 4vw, 42px);
right: clamp(20px, 4vw, 48px);
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
padding: 10px 13px;
border: 1px solid rgba(156, 213, 234, 0.19);
border-radius: 8px;
background: rgba(9, 23, 38, 0.74);
color: #bdd0dc;
font: inherit;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
color 160ms ease,
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(57, 228, 208, 0.48);
background: rgba(13, 37, 52, 0.88);
color: #ecffff;
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.pin {
position: absolute;
display: flex;
align-items: center;
gap: 8px;
padding: 7px 10px;
border: 1px solid rgba(158, 216, 235, 0.2);
border-radius: 6px;
background: rgba(6, 17, 29, 0.82);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
color: #d5e8f1;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.11em;
text-transform: uppercase;
white-space: nowrap;
opacity: 0;
transform: translate(-50%, -100%);
transition: opacity 180ms ease;
backdrop-filter: blur(10px);
}
.pin::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 20px;
content: "";
background: linear-gradient(var(--cyan), transparent);
opacity: 0.65;
}
.pin b {
color: var(--cyan);
font-size: 9px;
}
.hint {
position: absolute;
bottom: 24px;
left: 50%;
padding: 8px 13px;
border: 1px solid rgba(157, 210, 228, 0.13);
border-radius: 999px;
background: rgba(5, 15, 25, 0.6);
color: #7892a2;
font-size: 9px;
font-weight: 650;
letter-spacing: 0.12em;
text-transform: uppercase;
transform: translateX(-50%);
backdrop-filter: blur(10px);
white-space: nowrap;
}
.section-tag {
position: absolute;
right: clamp(20px, 4vw, 48px);
bottom: 25px;
color: rgba(146, 180, 196, 0.6);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.18em;
text-transform: uppercase;
}
@media (max-width: 720px) {
.subtitle {
display: none;
}
.legend-item {
min-width: auto;
}
.tools {
top: auto;
right: 18px;
bottom: 66px;
}
.pin {
display: none;
}
.hint {
bottom: 18px;
max-width: calc(100vw - 32px);
overflow: hidden;
text-overflow: ellipsis;
}
.section-tag {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D cutaway of a desk cable guide with three continuous cable paths"></canvas>
<div class="ui">
<section class="title-block">
<div class="eyebrow">Routing study / Section A—A</div>
<h1>Desk Cable Guide</h1>
<p class="subtitle">
A cutaway routing assembly with an open cable tray, shared snap clip,
and three-port desk grommet. Each cable remains individually visible.
</p>
<div class="legend" aria-label="Cable path legend">
<div class="legend-item"><span class="swatch" style="--color: var(--cyan)"></span>A / Data</div>
<div class="legend-item"><span class="swatch" style="--color: var(--amber)"></span>B / Power</div>
<div class="legend-item"><span class="swatch" style="--color: var(--violet)"></span>C / Aux</div>
</div>
</section>
<div class="tools">
<button id="flowButton" type="button" aria-pressed="true">Flow: On</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div id="trayPin" class="pin"><b>01</b><span>Cable tray</span></div>
<div id="clipPin" class="pin"><b>02</b><span>Snap clip</span></div>
<div id="grommetPin" class="pin"><b>03</b><span>3-port grommet</span></div>
<div class="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<div class="section-tag">Three continuous paths / One shared route</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const flowButton = document.getElementById("flowButton");
const resetButton = document.getElementById("resetButton");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x06101b, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07111d, 22, 43);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const homeView = {
theta: 0.94,
phi: 1.09,
radius: 17.4,
target: new THREE.Vector3(0.15, 0.75, 0)
};
const desired = {
theta: homeView.theta,
phi: homeView.phi,
radius: homeView.radius,
target: homeView.target.clone()
};
const current = {
theta: homeView.theta,
phi: homeView.phi,
radius: homeView.radius,
target: homeView.target.clone()
};
const hemi = new THREE.HemisphereLight(0xccefff, 0x102031, 1.65);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(4, 10, 8);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -11;
key.shadow.camera.right = 11;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -9;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
key.shadow.bias = -0.00035;
key.shadow.normalBias = 0.025;
scene.add(key);
const fill = new THREE.PointLight(0x4bcfff, 28, 26, 2);
fill.position.set(-6, 4.5, -5);
scene.add(fill);
const rim = new THREE.PointLight(0xb176ff, 24, 24, 2);
rim.position.set(7, 5, -3);
scene.add(rim);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x081421,
roughness: 0.94,
metalness: 0.08
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.16;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(42, 42, 0x335469, 0x173041);
grid.position.y = -0.145;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const mat = {
tray: new THREE.MeshPhysicalMaterial({
color: 0x263d4b,
metalness: 0.52,
roughness: 0.32,
clearcoat: 0.35,
clearcoatRoughness: 0.4
}),
trayLight: new THREE.MeshStandardMaterial({
color: 0x3e5967,
metalness: 0.46,
roughness: 0.38
}),
trayDark: new THREE.MeshStandardMaterial({
color: 0x132733,
metalness: 0.32,
roughness: 0.5
}),
rubber: new THREE.MeshPhysicalMaterial({
color: 0x101c24,
metalness: 0.04,
roughness: 0.72
}),
clip: new THREE.MeshPhysicalMaterial({
color: 0xb9d9df,
metalness: 0.56,
roughness: 0.26,
clearcoat: 0.25
}),
accent: new THREE.MeshStandardMaterial({
color: 0x36d9ca,
emissive: 0x0a5a54,
emissiveIntensity: 0.7,
metalness: 0.35,
roughness: 0.3
})
};
function addEdges(mesh, color = 0x8fd9e8, opacity = 0.25) {
const edges = new THREE.EdgesGeometry(mesh.geometry, 24);
const line = new THREE.LineSegments(
edges,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(line);
return line;
}
function box(width, height, depth, material, x, y, z, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Cable tray: long base, tall rear wall, and intentionally low front wall
// to expose the routing cross-section.
const trayBase = box(12.4, 0.24, 3.3, mat.tray, 0, 0.03, 0);
addEdges(trayBase, 0x84b8c7, 0.3);
const trayFloor = box(11.95, 0.055, 2.88, mat.trayLight, 0, 0.175, 0);
addEdges(trayFloor, 0x6d9eae, 0.18);
const rearWall = box(12.4, 1.78, 0.18, mat.tray, 0, 1.02, -1.56);
addEdges(rearWall, 0x84b8c7, 0.28);
const frontWall = box(12.4, 0.34, 0.18, mat.tray, 0, 0.32, 1.56);
addEdges(frontWall, 0x84b8c7, 0.3);
// A narrow illuminated cut edge emphasizes the sectioned front wall.
const cutEdge = box(
12.25,
0.035,
0.205,
new THREE.MeshBasicMaterial({ color: 0x43d7cd }),
0,
0.505,
1.56,
false
);
box(12.25, 0.045, 0.055, mat.accent, 0, 1.82, -1.45, false);
// End caps and longitudinal supports.
box(0.18, 1.78, 3.05, mat.trayDark, -6.11, 1.02, 0);
box(0.18, 1.78, 3.05, mat.trayDark, 6.11, 1.02, 0);
for (const x of [-4.7, -1.3, 2.1, 5.25]) {
box(0.12, 1.48, 0.12, mat.trayLight, x, 0.94, -1.43);
}
// Three subtle support channels keep each route visually separate.
for (const z of [-0.84, 0, 0.84]) {
box(11.85, 0.11, 0.22, mat.trayDark, 0, 0.255, z, false);
}
// Small hardware details.
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xa8c2c9,
metalness: 0.8,
roughness: 0.25
});
for (const x of [-5.45, -3.65, -1.85, 0.0, 1.85, 3.65, 5.45]) {
const bolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.055, 0.04, 16),
boltMaterial
);
bolt.rotation.x = Math.PI / 2;
bolt.position.set(x, 0.52, 1.665);
scene.add(bolt);
}
// Shared snap clip: a base rail and three open saddle arches.
const clipX = -1.45;
const clipRail = box(0.34, 0.14, 2.78, mat.clip, clipX, 0.23, 0);
addEdges(clipRail, 0xe2ffff, 0.32);
const clipDetails = [
{ z: -0.84, y: 0.41 },
{ z: 0.00, y: 0.49 },
{ z: 0.84, y: 0.45 }
];
for (const detail of clipDetails) {
const radius = 0.29;
const points = [
new THREE.Vector3(clipX, 0.24, detail.z + radius),
new THREE.Vector3(clipX, detail.y - 0.07, detail.z + radius)
];
for (let i = 0; i <= 28; i++) {
const angle = (i / 28) * Math.PI;
points.push(
new THREE.Vector3(
clipX,
detail.y + Math.sin(angle) * radius,
detail.z + Math.cos(angle) * radius
)
);
}
points.push(
new THREE.Vector3(clipX, detail.y - 0.07, detail.z - radius),
new THREE.Vector3(clipX, 0.24, detail.z - radius)
);
const curve = new THREE.CatmullRomCurve3(points, false, "centripetal");
const arch = new THREE.Mesh(
new THREE.TubeGeometry(curve, 88, 0.055, 8, false),
mat.clip
);
arch.castShadow = true;
scene.add(arch);
}
for (const z of [-1.27, 1.27]) {
const screw = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.08, 24),
boltMaterial
);
screw.rotation.z = Math.PI / 2;
screw.position.set(clipX - 0.19, 0.25, z);
scene.add(screw);
const screwSlot = new THREE.Mesh(
new THREE.BoxGeometry(0.012, 0.018, 0.13),
mat.trayDark
);
screwSlot.position.set(clipX - 0.236, 0.25, z);
scene.add(screwSlot);
}
// Rounded rectangular grommet with three real openings.
function roundedRectShape(width, height, radius) {
const shape = new THREE.Shape();
shape.moveTo(-width / 2 + radius, -height / 2);
shape.lineTo(width / 2 - radius, -height / 2);
shape.quadraticCurveTo(width / 2, -height / 2, width / 2, -height / 2 + radius);
shape.lineTo(width / 2, height / 2 - radius);
shape.quadraticCurveTo(width / 2, height / 2, width / 2 - radius, height / 2);
shape.lineTo(-width / 2 + radius, height / 2);
shape.quadraticCurveTo(-width / 2, height / 2, -width / 2, height / 2 - radius);
shape.lineTo(-width / 2, -height / 2 + radius);
shape.quadraticCurveTo(-width / 2, -height / 2, -width / 2 + radius, -height / 2);
return shape;
}
const grommetCenterY = 1.12;
const grommetCableY = 0.41;
const grommetShape = roundedRectShape(3.3, 2.05, 0.22);
for (const z of [-0.84, 0, 0.84]) {
const hole = new THREE.Path();
hole.absellipse(
-z,
grommetCableY - grommetCenterY,
0.39,
0.33,
0,
Math.PI * 2,
true
);
grommetShape.holes.push(hole);
}
const grommetGeometry = new THREE.ExtrudeGeometry(grommetShape, {
depth: 0.38,
steps: 1,
curveSegments: 32,
bevelEnabled: true,
bevelThickness: 0.055,
bevelSize: 0.035,
bevelSegments: 3
});
grommetGeometry.rotateY(Math.PI / 2);
const grommet = new THREE.Mesh(grommetGeometry, mat.rubber);
grommet.position.set(4.45, grommetCenterY, 0);
grommet.castShadow = true;
grommet.receiveShadow = true;
scene.add(grommet);
addEdges(grommet, 0x4a7786, 0.36);
const grommetRingMaterial = new THREE.MeshStandardMaterial({
color: 0x315765,
metalness: 0.28,
roughness: 0.4,
emissive: 0x082b31,
emissiveIntensity: 0.45
});
for (const z of [-0.84, 0, 0.84]) {
for (const x of [4.43, 4.85]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.365, 0.047, 10, 48),
grommetRingMaterial
);
ring.rotation.y = Math.PI / 2;
ring.scale.y = 0.86;
ring.position.set(x, grommetCableY, z);
scene.add(ring);
}
}
// Continuous cable paths. Exact points around the clip and grommet keep
// every route aligned while allowing distinct vertical movement.
const cableDefinitions = [
{
name: "A / Data",
color: 0x39e4d0,
points: [
[-6.80, 0.46, -1.04],
[-5.70, 0.42, -0.98],
[-4.40, 0.51, -0.90],
[-3.00, 0.45, -0.86],
[-1.80, 0.41, -0.84],
[-1.45, 0.41, -0.84],
[-0.50, 0.47, -0.82],
[0.80, 0.54, -0.88],
[2.20, 0.49, -0.82],
[3.40, 0.43, -0.84],
[4.00, 0.41, -0.84],
[4.55, 0.41, -0.84],
[5.15, 0.47, -0.82],
[6.80, 0.42, -0.86]
]
},
{
name: "B / Power",
color: 0xffad42,
points: [
[-6.80, 0.60, 0.03],
[-5.70, 0.55, 0.02],
[-4.40, 0.61, 0.00],
[-3.00, 0.55, 0.00],
[-1.80, 0.49, 0.00],
[-1.45, 0.49, 0.00],
[-0.50, 0.55, -0.02],
[0.80, 0.60, 0.03],
[2.20, 0.55, 0.01],
[3.40, 0.46, 0.00],
[4.00, 0.41, 0.00],
[4.55, 0.41, 0.00],
[5.15, 0.48, 0.00],
[6.80, 0.44, -0.02]
]
},
{
name: "C / Aux",
color: 0xb681ff,
points: [
[-6.80, 0.52, 1.04],
[-5.70, 0.48, 0.98],
[-4.40, 0.55, 0.90],
[-3.00, 0.50, 0.86],
[-1.80, 0.45, 0.84],
[-1.45, 0.45, 0.84],
[-0.50, 0.51, 0.86],
[0.80, 0.57, 0.92],
[2.20, 0.52, 0.84],
[3.40, 0.44, 0.84],
[4.00, 0.41, 0.84],
[4.55, 0.41, 0.84],
[5.15, 0.47, 0.82],
[6.80, 0.43, 0.88]
]
}
];
const cableCurves = [];
const flowGroup = new THREE.Group();
scene.add(flowGroup);
for (const definition of cableDefinitions) {
const points = definition.points.map(
([x, y, z]) => new THREE.Vector3(x, y, z)
);
const curve = new THREE.CatmullRomCurve3(points, false, "centripetal");
curve.arcLengthDivisions = 500;
cableCurves.push(curve);
const cableMaterial = new THREE.MeshPhysicalMaterial({
color: definition.color,
emissive: definition.color,
emissiveIntensity: 0.22,
metalness: 0.06,
roughness: 0.3,
clearcoat: 0.75,
clearcoatRoughness: 0.28
});
const cable = new THREE.Mesh(
new THREE.TubeGeometry(curve, 260, 0.125, 12, false),
cableMaterial
);
cable.castShadow = true;
cable.receiveShadow = true;
scene.add(cable);
// Dark connector sleeves at both ends visually continue the routes
// beyond the guide assembly.
const sleeveMaterial = new THREE.MeshStandardMaterial({
color: 0x1a2a34,
metalness: 0.62,
roughness: 0.34
});
for (const x of [-6.58, 6.58]) {
const sleeve = new THREE.Mesh(
new THREE.CylinderGeometry(0.225, 0.225, 0.56, 28),
sleeveMaterial
);
sleeve.rotation.z = Math.PI / 2;
sleeve.position.set(x, 0.48, definition.points[x < 0 ? 0 : definition.points.length - 1][2]);
sleeve.castShadow = true;
scene.add(sleeve);
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.226, 0.034, 8, 32),
new THREE.MeshBasicMaterial({ color: definition.color })
);
band.rotation.y = Math.PI / 2;
band.position.set(
x < 0 ? x + 0.29 : x - 0.29,
0.48,
definition.points[x < 0 ? 0 : definition.points.length - 1][2]
);
scene.add(band);
}
const pulseColor = new THREE.Color(definition.color).lerp(
new THREE.Color(0xffffff),
0.58
);
for (let i = 0; i < 3; i++) {
const pulse = new THREE.Mesh(
new THREE.SphereGeometry(0.145, 14, 10),
new THREE.MeshBasicMaterial({ color: pulseColor })
);
pulse.userData.offset = i / 3;
flowGroup.add(pulse);
}
}
// A tiny section marker on the floor.
const sectionGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-6.5, -0.125, 1.95),
new THREE.Vector3(6.5, -0.125, 1.95)
]);
const sectionLine = new THREE.Line(
sectionGeometry,
new THREE.LineDashedMaterial({
color: 0x39e4d0,
transparent: true,
opacity: 0.34,
dashSize: 0.18,
gapSize: 0.12
})
);
sectionLine.computeLineDistances();
scene.add(sectionLine);
const pins = [
{
element: document.getElementById("trayPin"),
position: new THREE.Vector3(-4.4, 0.5, 1.55)
},
{
element: document.getElementById("clipPin"),
position: new THREE.Vector3(-1.45, 1.08, 0.15)
},
{
element: document.getElementById("grommetPin"),
position: new THREE.Vector3(4.58, 2.25, 0)
}
];
function updatePins() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const pin of pins) {
const projected = pin.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
pin.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
pin.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
pin.element.style.opacity = visible ? "1" : "0";
}
}
function updateCamera() {
current.theta = THREE.MathUtils.lerp(current.theta, desired.theta, 0.1);
current.phi = THREE.MathUtils.lerp(current.phi, desired.phi, 0.1);
current.radius = THREE.MathUtils.lerp(current.radius, desired.radius, 0.1);
current.target.lerp(desired.target, 0.1);
const sinPhi = Math.sin(current.phi);
camera.position.set(
current.target.x + current.radius * sinPhi * Math.sin(current.theta),
current.target.y + current.radius * Math.cos(current.phi),
current.target.z + current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(current.target);
camera.updateMatrixWorld();
}
// Camera controls are implemented directly without Three.js addons.
const pointers = new Map();
let previousPinchDistance = 0;
let panning = false;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
canvas.classList.add("dragging");
panning = event.shiftKey || event.button === 2;
if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0) {
desired.radius = THREE.MathUtils.clamp(
desired.radius * (previousPinchDistance / distance),
9.5,
28
);
}
previousPinchDistance = distance;
return;
}
if (panning || event.shiftKey || event.buttons === 2) {
const scale = desired.radius * 0.00135;
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
desired.target
.addScaledVector(right, -dx * scale)
.addScaledVector(up, dy * scale);
desired.target.x = THREE.MathUtils.clamp(desired.target.x, -4, 5.5);
desired.target.y = THREE.MathUtils.clamp(desired.target.y, 0.1, 2.8);
desired.target.z = THREE.MathUtils.clamp(desired.target.z, -2, 2);
} else {
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.006,
0.42,
1.46
);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
if (pointers.size === 0) {
canvas.classList.remove("dragging");
} else if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
pointers.set([...pointers.keys()][0], remaining);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
9.5,
28
);
},
{ passive: false }
);
function resetView() {
desired.theta = homeView.theta;
desired.phi = homeView.phi;
desired.radius = homeView.radius;
desired.target.copy(homeView.target);
}
resetButton.addEventListener("click", resetView);
let flowEnabled = true;
flowButton.addEventListener("click", () => {
flowEnabled = !flowEnabled;
flowGroup.visible = flowEnabled;
flowButton.textContent = `Flow: ${flowEnabled ? "On" : "Off"}`;
flowButton.setAttribute("aria-pressed", String(flowEnabled));
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetView();
if (event.code === "Space") {
event.preventDefault();
flowButton.click();
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
updateCamera();
if (flowEnabled) {
for (const curve of cableCurves) {
const pulses = flowGroup.children.filter(
(child) => child.userData.offset !== undefined
);
// Each curve owns three consecutive pulses in the flow group.
const curveIndex = cableCurves.indexOf(curve);
for (let i = 0; i < 3; i++) {
const pulse = pulses[curveIndex * 3 + i];
if (!pulse) continue;
const t = (elapsed * 0.075 + pulse.userData.offset + curveIndex * 0.11) % 1;
pulse.position.copy(curve.getPointAt(t));
}
}
}
updatePins();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```