Prompt specification
Build or customize the Three.js 3D scene "Series and parallel resistor blocks" 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: Series and parallel resistor blocks
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/series-and-parallel-resistor-blocks-x1665
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring resistors connected in series and parallel. Build two clearly separated circuit boards containing the same two resistors in different connections; label nodes explicitly so visual placement cannot obscure electrical topology. Low-poly.
Working Three.js Starter (series-and-parallel-resistor-blocks-x1665):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Low-Poly Circuit Topology</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: rgba(10, 24, 39, 0.84);
--border: rgba(148, 190, 210, 0.2);
--text: #e8f3f7;
--muted: #9db3c1;
--cyan: #62e6d2;
--red: #ff7474;
--amber: #ffd166;
--copper: #e4a64d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#app {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
}
.intro {
top: 22px;
left: 24px;
width: min(390px, calc(100vw - 48px));
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 360px;
font-size: clamp(24px, 3vw, 38px);
line-height: 1.02;
letter-spacing: -0.04em;
}
.intro p {
max-width: 350px;
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
top: 22px;
right: 24px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
border: 1px solid var(--border);
border-radius: 9px;
padding: 9px 12px;
background: rgba(12, 29, 45, 0.88);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(10px);
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
button:hover {
border-color: rgba(98, 230, 210, 0.65);
background: rgba(20, 47, 61, 0.94);
transform: translateY(-1px);
}
button[aria-pressed="false"] {
color: var(--muted);
}
.topology {
right: 24px;
bottom: 22px;
left: 24px;
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
max-width: 980px;
margin-left: auto;
}
.topology-card {
min-width: 0;
border: 1px solid var(--border);
border-radius: 12px;
padding: 12px 14px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(12px);
}
.topology-card h2 {
margin: 0 0 8px;
font-size: 10px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.topology-card.series h2 {
color: var(--amber);
}
.topology-card.parallel h2 {
color: var(--cyan);
}
.diagram {
margin: 0;
overflow-x: auto;
color: #dce8ed;
font: 12px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
white-space: pre;
}
.diagram .in {
color: var(--red);
}
.diagram .out {
color: var(--cyan);
}
.diagram .junction {
color: var(--amber);
}
.diagram .r {
color: #f0c78e;
}
#labels-layer {
position: fixed;
inset: 0;
z-index: 5;
overflow: hidden;
pointer-events: none;
transition: opacity 180ms ease;
}
#labels-layer.hidden {
opacity: 0;
}
.world-label {
position: absolute;
white-space: nowrap;
pointer-events: none;
will-change: left, top;
}
.node-label {
transform: translate(-50%, calc(-100% - 6px));
border: 1px solid rgba(255, 209, 102, 0.55);
border-radius: 999px;
padding: 4px 8px;
background: rgba(8, 18, 28, 0.9);
color: var(--amber);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.04em;
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
}
.node-label.terminal-in {
border-color: rgba(255, 116, 116, 0.65);
color: var(--red);
}
.node-label.terminal-out {
border-color: rgba(98, 230, 210, 0.65);
color: var(--cyan);
}
.component-label {
transform: translate(-50%, calc(-100% - 3px));
border: 1px solid rgba(228, 166, 77, 0.4);
border-radius: 7px;
padding: 4px 8px;
background: rgba(8, 18, 28, 0.82);
color: #f2d19f;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.025em;
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.24);
}
.board-label {
transform: translate(-50%, -100%);
color: #d8f8f1;
text-align: center;
text-shadow: 0 2px 10px rgba(0, 0, 0, 0.65);
}
.board-label small {
display: block;
margin-bottom: 4px;
color: #70cfc2;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.22em;
}
.board-label strong {
font-size: 19px;
font-weight: 850;
letter-spacing: 0.08em;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: var(--bg);
color: var(--cyan);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 300ms ease, visibility 300ms ease;
}
#loading.is-hidden {
visibility: hidden;
opacity: 0;
}
@media (max-width: 720px) {
.intro {
top: 14px;
left: 14px;
}
.intro p {
display: none;
}
.controls {
top: 14px;
right: 14px;
}
button {
padding: 8px 9px;
font-size: 10px;
}
.topology {
right: 10px;
bottom: 10px;
left: 10px;
grid-template-columns: 1fr;
gap: 6px;
}
.topology-card {
padding: 8px 10px;
}
.topology-card h2 {
margin-bottom: 4px;
}
.diagram {
font-size: 10px;
}
.board-label strong {
font-size: 15px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="hud intro">
<div class="eyebrow">Three.js / Low-Poly Circuit Lab</div>
<h1>Same resistors.<br />Different topology.</h1>
<p>
Drag to orbit, scroll to zoom, and shift/right-drag to pan.
Keyboard: R resets, T selects top view, L toggles labels.
</p>
</header>
<nav class="hud controls" aria-label="Scene controls">
<button id="reset-view" type="button">Reset</button>
<button id="top-view" type="button">Top</button>
<button id="toggle-labels" type="button" aria-pressed="true">Labels</button>
</nav>
<section class="hud topology" aria-label="Explicit circuit topology">
<article class="topology-card series">
<h2>Board 01 / Series</h2>
<pre class="diagram"><span class="in">A (+)</span> ── <span class="r">R1 100 Ω</span> ── <span class="junction">B</span> ── <span class="r">R2 220 Ω</span> ── <span class="out">C (−)</span></pre>
</article>
<article class="topology-card parallel">
<h2>Board 02 / Parallel</h2>
<pre class="diagram"><span class="in">P (+)</span> ──┬── <span class="r">R1 100 Ω</span> ──┬── <span class="out">Q (−)</span>
└── <span class="r">R2 220 Ω</span> ──┘</pre>
</article>
</section>
<div id="labels-layer" aria-hidden="true"></div>
<div id="loading">Loading circuit scene</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const labelsLayer = document.getElementById("labels-layer");
const loading = document.getElementById("loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 24, 48);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Two low-poly circuit boards showing the same two resistors in series and parallel"
);
app.appendChild(renderer.domElement);
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc6eaff, 0x071018, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff1d5, 3.2);
keyLight.position.set(-7, 15, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x69d8ff, 1.4);
rimLight.position.set(10, 8, -9);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb44d, 18, 20, 2);
warmLight.position.set(0, 5, 4);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Shared materials and geometry helpers
// -------------------------------------------------------------------------
const NET_Y = 0.25;
const makeMaterial = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
flatShading: true,
...options
});
const materials = {
board: makeMaterial(0x174d45, { roughness: 0.9 }),
boardEdge: makeMaterial(0x0b292b, { roughness: 0.86 }),
trace: makeMaterial(0x24675d, { roughness: 0.9 }),
accent: makeMaterial(0x38b8a8, {
roughness: 0.55,
metalness: 0.25,
emissive: 0x0b3732,
emissiveIntensity: 0.35
}),
screw: makeMaterial(0x6d7c80, {
roughness: 0.38,
metalness: 0.78
}),
hole: makeMaterial(0x061519, { roughness: 1 }),
wire: makeMaterial(0xe4a64d, {
roughness: 0.43,
metalness: 0.55,
emissive: 0x3b2106,
emissiveIntensity: 0.12
}),
resistor: makeMaterial(0xd4ae75, { roughness: 0.82 }),
resistorEnd: makeMaterial(0xa87842, { roughness: 0.8 }),
lead: makeMaterial(0xc3ced0, {
roughness: 0.28,
metalness: 0.88
}),
brown: makeMaterial(0x70401f),
black: makeMaterial(0x15191c),
red: makeMaterial(0xca3c32),
gold: makeMaterial(0xd6a83d, {
roughness: 0.32,
metalness: 0.6
}),
input: makeMaterial(0xff6868, {
roughness: 0.45,
emissive: 0x4d0909,
emissiveIntensity: 0.22
}),
output: makeMaterial(0x59e0ce, {
roughness: 0.45,
emissive: 0x073f39,
emissiveIntensity: 0.25
}),
junction: makeMaterial(0xffcf5c, {
roughness: 0.45,
emissive: 0x493000,
emissiveIntensity: 0.2
}),
ground: makeMaterial(0x08131f, { roughness: 1 })
};
function enableShadows(object) {
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
return object;
}
function addWire(group, from, to, radius = 0.055) {
const a = Array.isArray(from)
? new THREE.Vector3(...from)
: from.clone();
const b = Array.isArray(to)
? new THREE.Vector3(...to)
: to.clone();
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
if (length < 0.0001) return null;
const wire = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 8),
materials.wire
);
wire.position.copy(a).add(b).multiplyScalar(0.5);
wire.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
wire.castShadow = true;
wire.receiveShadow = true;
group.add(wire);
return wire;
}
function addElbow(group, x, y, z) {
const elbow = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 0),
materials.wire
);
elbow.position.set(x, y, z);
elbow.castShadow = true;
group.add(elbow);
}
// -------------------------------------------------------------------------
// Board construction
// -------------------------------------------------------------------------
function addTrace(group, x, z, width, depth) {
const trace = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.018, depth),
materials.trace
);
trace.position.set(x, 0.012, z);
trace.receiveShadow = true;
group.add(trace);
}
function createBoard(xPosition, accentColor) {
const group = new THREE.Group();
group.position.x = xPosition;
scene.add(group);
const edge = new THREE.Mesh(
new THREE.BoxGeometry(8.48, 0.12, 5.48),
materials.boardEdge
);
edge.position.y = -0.28;
edge.receiveShadow = true;
group.add(edge);
const board = new THREE.Mesh(
new THREE.BoxGeometry(8.38, 0.3, 5.38),
materials.board
);
board.position.y = -0.15;
board.castShadow = true;
board.receiveShadow = true;
group.add(board);
const accentMaterial = makeMaterial(accentColor, {
roughness: 0.48,
metalness: 0.3,
emissive: accentColor,
emissiveIntensity: 0.08
});
const backStripe = new THREE.Mesh(
new THREE.BoxGeometry(7.7, 0.026, 0.075),
accentMaterial
);
backStripe.position.set(0, 0.018, -2.35);
group.add(backStripe);
// Subtle PCB traces placed away from the active network.
addTrace(group, -3.35, -1.9, 1.15, 0.05);
addTrace(group, -2.75, -1.66, 0.05, 0.48);
addTrace(group, 3.25, 1.92, 1.25, 0.05);
addTrace(group, 2.63, 1.68, 0.05, 0.48);
addTrace(group, -3.25, 1.95, 0.8, 0.045);
addTrace(group, 3.25, -1.95, 0.8, 0.045);
const screwPositions = [
[-3.72, -2.18],
[3.72, -2.18],
[-3.72, 2.18],
[3.72, 2.18]
];
for (const [x, z] of screwPositions) {
const hole = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.025, 8),
materials.hole
);
hole.position.set(x, 0.018, z);
group.add(hole);
const screw = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.115, 0.065, 8),
materials.screw
);
screw.position.set(x, 0.05, z);
screw.castShadow = true;
group.add(screw);
const slot = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.012, 0.028),
materials.hole
);
slot.position.set(x, 0.087, z);
group.add(slot);
}
return group;
}
function addTerminal(group, x, z, material, type) {
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.26, 0.26, 0.09, 8),
material
);
base.position.set(x, 0.045, z);
base.castShadow = true;
group.add(base);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.3, 8),
materials.lead
);
post.position.set(x, 0.18, z);
post.castShadow = true;
group.add(post);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.035, 6, 12),
material
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, NET_Y, z);
ring.castShadow = true;
group.add(ring);
const cap = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.1, 0),
material
);
cap.position.set(x, NET_Y + 0.09, z);
cap.castShadow = true;
group.add(cap);
}
function addJunction(group, x, z) {
const junction = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.135, 0),
materials.junction
);
junction.position.set(x, NET_Y, z);
junction.castShadow = true;
group.add(junction);
}
// -------------------------------------------------------------------------
// Axial low-poly resistor
// -------------------------------------------------------------------------
function createResistor(group, x, z, bandMaterials) {
const root = new THREE.Group();
root.position.set(x, NET_Y, z);
group.add(root);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.26, 0.26, 1.35, 8),
materials.resistor
);
body.rotation.z = Math.PI / 2;
body.castShadow = true;
body.receiveShadow = true;
root.add(body);
const leftEnd = new THREE.Mesh(
new THREE.CylinderGeometry(0.22, 0.27, 0.2, 8),
materials.resistorEnd
);
leftEnd.rotation.z = Math.PI / 2;
leftEnd.position.x = -0.73;
leftEnd.castShadow = true;
root.add(leftEnd);
const rightEnd = new THREE.Mesh(
new THREE.CylinderGeometry(0.27, 0.22, 0.2, 8),
materials.resistorEnd
);
rightEnd.rotation.z = Math.PI / 2;
rightEnd.position.x = 0.73;
rightEnd.castShadow = true;
root.add(rightEnd);
const leadGeometry = new THREE.CylinderGeometry(
0.043,
0.043,
1,
6
);
const leftLead = new THREE.Mesh(
leadGeometry,
materials.lead
);
leftLead.rotation.z = Math.PI / 2;
leftLead.position.x = -1.08;
leftLead.castShadow = true;
root.add(leftLead);
const rightLead = new THREE.Mesh(
leadGeometry,
materials.lead
);
rightLead.rotation.z = Math.PI / 2;
rightLead.position.x = 1.08;
rightLead.castShadow = true;
root.add(rightLead);
const bandOffsets = [-0.42, -0.25, -0.08, 0.13];
bandOffsets.forEach((offset, index) => {
const band = new THREE.Mesh(
new THREE.CylinderGeometry(0.269, 0.269, 0.09, 8),
bandMaterials[index]
);
band.rotation.z = Math.PI / 2;
band.position.x = offset;
band.castShadow = true;
root.add(band);
});
return {
root,
left: new THREE.Vector3(x - 1.58, NET_Y, z),
right: new THREE.Vector3(x + 1.58, NET_Y, z)
};
}
// -------------------------------------------------------------------------
// World-space HTML labels
// -------------------------------------------------------------------------
const worldLabels = [];
function addWorldLabel(parent, anchor, html, className) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.innerHTML = html;
labelsLayer.appendChild(element);
worldLabels.push({
element,
parent,
anchor: anchor.clone()
});
}
// -------------------------------------------------------------------------
// Board 01: series network
// -------------------------------------------------------------------------
const seriesBoard = createBoard(-4.9, 0xffc85c);
const seriesA = new THREE.Vector3(-3.55, NET_Y, 0);
const seriesB = new THREE.Vector3(0, NET_Y, 0);
const seriesC = new THREE.Vector3(3.55, NET_Y, 0);
const seriesR1 = createResistor(seriesBoard, -1.75, 0, [
materials.brown,
materials.black,
materials.brown,
materials.gold
]);
const seriesR2 = createResistor(seriesBoard, 1.75, 0, [
materials.red,
materials.red,
materials.brown,
materials.gold
]);
addWire(seriesBoard, seriesA, seriesR1.left);
addWire(seriesBoard, seriesR1.right, seriesB);
addWire(seriesBoard, seriesB, seriesR2.left);
addWire(seriesBoard, seriesR2.right, seriesC);
addTerminal(seriesBoard, seriesA.x, seriesA.z, materials.input, "input");
addJunction(seriesBoard, seriesB.x, seriesB.z);
addTerminal(seriesBoard, seriesC.x, seriesC.z, materials.output, "output");
addWorldLabel(
seriesBoard,
new THREE.Vector3(-3.55, 0.68, 0),
"A (+)",
"node-label terminal-in"
);
addWorldLabel(
seriesBoard,
new THREE.Vector3(0, 0.68, 0),
"B · JUNCTION",
"node-label"
);
addWorldLabel(
seriesBoard,
new THREE.Vector3(3.55, 0.68, 0),
"C (−)",
"node-label terminal-out"
);
addWorldLabel(
seriesBoard,
new THREE.Vector3(-1.75, 0.85, 0),
"R1 · 100 Ω",
"component-label"
);
addWorldLabel(
seriesBoard,
new THREE.Vector3(1.75, 0.85, 0),
"R2 · 220 Ω",
"component-label"
);
addWorldLabel(
seriesBoard,
new THREE.Vector3(0, 0.08, -2.3),
"<small>BOARD 01</small><strong>SERIES NETWORK</strong>",
"board-label"
);
// -------------------------------------------------------------------------
// Board 02: parallel network
// -------------------------------------------------------------------------
const parallelBoard = createBoard(4.9, 0x62e6d2);
const parallelP = new THREE.Vector3(-3.45, NET_Y, 0);
const parallelQ = new THREE.Vector3(3.15, NET_Y, 0);
const parallelR1 = createResistor(parallelBoard, -0.2, -1.35, [
materials.brown,
materials.black,
materials.brown,
materials.gold
]);
const parallelR2 = createResistor(parallelBoard, -0.2, 1.35, [
materials.red,
materials.red,
materials.brown,
materials.gold
]);
for (const branch of [parallelR1, parallelR2]) {
const z = branch.left.z;
addWire(
parallelBoard,
parallelP,
new THREE.Vector3(parallelP.x, NET_Y, z)
);
addWire(
parallelBoard,
new THREE.Vector3(parallelP.x, NET_Y, z),
branch.left
);
addWire(
parallelBoard,
branch.right,
new THREE.Vector3(parallelQ.x, NET_Y, z)
);
addWire(
parallelBoard,
new THREE.Vector3(parallelQ.x, NET_Y, z),
parallelQ
);
addElbow(parallelBoard, parallelP.x, NET_Y, z);
addElbow(parallelBoard, parallelQ.x, NET_Y, z);
}
addTerminal(
parallelBoard,
parallelP.x,
parallelP.z,
materials.input,
"input"
);
addTerminal(
parallelBoard,
parallelQ.x,
parallelQ.z,
materials.output,
"output"
);
addWorldLabel(
parallelBoard,
new THREE.Vector3(-3.45, 0.68, 0),
"P (+)",
"node-label terminal-in"
);
addWorldLabel(
parallelBoard,
new THREE.Vector3(3.15, 0.68, 0),
"Q (−)",
"node-label terminal-out"
);
addWorldLabel(
parallelBoard,
new THREE.Vector3(-0.2, 0.85, -1.35),
"R1 · 100 Ω",
"component-label"
);
addWorldLabel(
parallelBoard,
new THREE.Vector3(-0.2, 0.85, 1.35),
"R2 · 220 Ω",
"component-label"
);
addWorldLabel(
parallelBoard,
new THREE.Vector3(0, 0.08, -2.3),
"<small>BOARD 02</small><strong>PARALLEL NETWORK</strong>",
"board-label"
);
// -------------------------------------------------------------------------
// Ground and grid
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.39;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
40,
40,
0x245064,
0x112b38
);
grid.position.y = -0.375;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// -------------------------------------------------------------------------
// Direct camera controls, implemented without addons
// -------------------------------------------------------------------------
const view = {
target: new THREE.Vector3(0, 0, 0),
radius: 22,
phi: 0.91,
theta: 0
};
const initialView = { ...view };
function updateCamera() {
const offset = new THREE.Vector3().setFromSphericalCoords(
view.radius,
view.phi,
view.theta
);
camera.position.copy(view.target).add(offset);
camera.lookAt(view.target);
camera.updateMatrixWorld();
}
function resetView() {
view.target.set(0, 0, 0);
view.radius = initialView.radius;
view.phi = initialView.phi;
view.theta = initialView.theta;
updateCamera();
}
function topView() {
view.target.set(0, 0, 0);
view.radius = 21;
view.phi = 0.035;
view.theta = 0;
updateCamera();
}
const pointers = new Map();
let activeDrag = null;
let lastPinchDistance = 0;
let lastPinchCenter = null;
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
activeDrag = {
mode:
event.button === 2 || event.shiftKey ? "pan" : "orbit",
x: event.clientX,
y: event.clientY
};
} else {
activeDrag = null;
updatePinchState();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size >= 2) {
updatePinchState();
return;
}
if (!activeDrag) return;
const dx = event.clientX - activeDrag.x;
const dy = event.clientY - activeDrag.y;
activeDrag.x = event.clientX;
activeDrag.y = event.clientY;
if (activeDrag.mode === "orbit") {
view.theta -= dx * 0.006;
view.phi -= dy * 0.005;
view.phi = THREE.MathUtils.clamp(view.phi, 0.035, 1.35);
} else {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const scale = view.radius * 0.00135;
view.target.addScaledVector(right, -dx * scale);
view.target.addScaledVector(up, dy * scale);
view.target.x = THREE.MathUtils.clamp(view.target.x, -5, 5);
view.target.y = THREE.MathUtils.clamp(view.target.y, -0.2, 0.8);
view.target.z = THREE.MathUtils.clamp(view.target.z, -3, 3);
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
activeDrag = null;
lastPinchDistance = 0;
lastPinchCenter = null;
} else {
updatePinchState();
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
function updatePinchState() {
const points = [...pointers.values()];
if (points.length < 2) return;
const [a, b] = points;
const distance = Math.hypot(b.x - a.x, b.y - a.y);
const center = {
x: (a.x + b.x) * 0.5,
y: (a.y + b.y) * 0.5
};
if (lastPinchDistance > 0) {
view.radius = THREE.MathUtils.clamp(
view.radius * (lastPinchDistance / distance),
10,
36
);
}
if (lastPinchCenter) {
view.theta -= (center.x - lastPinchCenter.x) * 0.004;
view.phi -= (center.y - lastPinchCenter.y) * 0.003;
view.phi = THREE.MathUtils.clamp(view.phi, 0.035, 1.35);
}
lastPinchDistance = distance;
lastPinchCenter = center;
updateCamera();
}
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
10,
36
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset-view").addEventListener(
"click",
resetView
);
document.getElementById("top-view").addEventListener(
"click",
topView
);
const labelButton = document.getElementById("toggle-labels");
let labelsEnabled = true;
labelButton.addEventListener("click", () => {
labelsEnabled = !labelsEnabled;
labelsLayer.classList.toggle("hidden", !labelsEnabled);
labelButton.setAttribute("aria-pressed", String(labelsEnabled));
});
window.addEventListener("keydown", (event) => {
if (event.metaKey || event.ctrlKey || event.altKey) return;
switch (event.key.toLowerCase()) {
case "r":
resetView();
break;
case "t":
topView();
break;
case "l":
labelButton.click();
break;
}
});
// -------------------------------------------------------------------------
// Label projection and rendering
// -------------------------------------------------------------------------
function updateWorldLabels() {
const width = app.clientWidth;
const height = app.clientHeight;
for (const label of worldLabels) {
const projected = label.anchor.clone();
label.parent.localToWorld(projected);
projected.project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
projected.x >= -1.2 &&
projected.x <= 1.2 &&
projected.y >= -1.2 &&
projected.y <= 1.2;
label.element.style.opacity = visible ? "1" : "0";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
updateCamera();
let firstFrame = true;
renderer.setAnimationLoop(() => {
scene.updateMatrixWorld(true);
updateWorldLabels();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("is-hidden");
}
});
</script>
</body>
</html>
```