Prompt specification
Build or customize the Three.js 3D scene "Bridge Plank Between Two Supports" 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: Bridge Plank Between Two Supports
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/bridge-plank-between-two-supports-x3441
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: bridge plank between two supports. Model a plank spanning two blocks with visible overlap at both ends. Show an unsupported alternative separately and avoid implying real structural load certification. Return one self-contained HTML file.
Working Three.js Starter (bridge-plank-between-two-supports-x3441):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bridge Plank Concept Comparison</title>
<style>
:root {
--ink: #172833;
--muted: #64737c;
--paper: #f5f3ee;
--panel: #ffffff;
--line: #dce1df;
--teal: #239c91;
--teal-soft: #d9f1ed;
--amber: #d9914e;
--warning: #9a552e;
}
* {
box-sizing: border-box;
}
html {
color-scheme: light;
}
body {
margin: 0;
min-width: 320px;
color: var(--ink);
background:
radial-gradient(circle at 8% 0%, rgba(53, 164, 150, 0.10), transparent 26rem),
radial-gradient(circle at 95% 12%, rgba(217, 145, 78, 0.10), transparent 28rem),
var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
.page {
width: min(1380px, calc(100% - 40px));
margin: 0 auto;
padding: 42px 0 30px;
}
.hero {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 32px;
margin-bottom: 25px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--teal);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 2px;
background: currentColor;
content: "";
}
h1 {
max-width: 800px;
margin: 0;
font-size: clamp(34px, 5vw, 65px);
font-weight: 720;
letter-spacing: -0.055em;
line-height: 0.98;
}
.hero-copy {
max-width: 760px;
margin: 17px 0 0;
color: var(--muted);
font-size: clamp(15px, 1.5vw, 18px);
line-height: 1.55;
}
.hero-actions {
display: flex;
flex-shrink: 0;
flex-direction: column;
align-items: flex-end;
gap: 12px;
}
.concept-badge {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 10px 13px;
border: 1px solid #e8d7c9;
border-radius: 999px;
color: var(--warning);
background: #fffaf5;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.concept-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
content: "";
}
.reset-button {
padding: 10px 14px;
border: 1px solid #cfd7d6;
border-radius: 10px;
color: var(--ink);
background: rgba(255, 255, 255, 0.72);
cursor: pointer;
font-size: 13px;
font-weight: 700;
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
.reset-button:hover {
border-color: var(--teal);
background: white;
transform: translateY(-1px);
}
.reset-button:focus-visible {
outline: 3px solid rgba(35, 156, 145, 0.22);
outline-offset: 2px;
}
.stage {
position: relative;
height: 650px;
overflow: hidden;
border: 1px solid rgba(97, 119, 126, 0.2);
border-radius: 26px;
background: #eee9df;
box-shadow:
0 26px 70px rgba(28, 48, 55, 0.13),
0 3px 10px rgba(28, 48, 55, 0.05);
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene.dragging {
cursor: grabbing;
}
.split-line {
position: absolute;
z-index: 2;
top: 0;
bottom: 0;
left: 66%;
width: 1px;
background: rgba(63, 82, 90, 0.2);
pointer-events: none;
}
.panel-title {
position: absolute;
z-index: 4;
top: 22px;
left: 24px;
width: min(290px, calc(100% - 48px));
padding: 14px 16px;
border: 1px solid rgba(73, 94, 102, 0.13);
border-radius: 14px;
background: rgba(255, 255, 255, 0.76);
box-shadow: 0 8px 25px rgba(32, 50, 58, 0.07);
backdrop-filter: blur(10px);
pointer-events: none;
}
.alt-title {
left: calc(66% + 22px);
}
.panel-number {
display: block;
margin-bottom: 5px;
color: var(--teal);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.alt-title .panel-number {
color: var(--warning);
}
.panel-title h2 {
margin: 0;
font-size: 19px;
letter-spacing: -0.025em;
}
.panel-title p {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.world-label {
position: absolute;
z-index: 5;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
border: 1px solid rgba(35, 156, 145, 0.22);
border-radius: 999px;
color: #176b65;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 4px 12px rgba(23, 40, 51, 0.08);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
}
.world-label::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--teal);
content: "";
}
.world-label.gap-label {
color: #53646d;
border-color: rgba(83, 100, 109, 0.18);
}
.world-label.gap-label::before {
background: #758791;
}
.world-label.unsupported-label {
color: #8b4d2c;
border-color: rgba(154, 85, 46, 0.22);
}
.world-label.unsupported-label::before {
background: var(--amber);
}
.legend {
position: absolute;
z-index: 4;
bottom: 20px;
left: 24px;
display: flex;
flex-wrap: wrap;
gap: 10px 16px;
max-width: calc(66% - 48px);
padding: 10px 12px;
border: 1px solid rgba(73, 94, 102, 0.12);
border-radius: 12px;
color: #52646d;
background: rgba(255, 255, 255, 0.78);
backdrop-filter: blur(10px);
font-size: 11px;
pointer-events: none;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 17px;
height: 10px;
border: 1px solid rgba(35, 156, 145, 0.3);
border-radius: 3px;
background: var(--teal-soft);
}
.gap-mark {
width: 17px;
height: 1px;
background: #758791;
}
.hint {
position: absolute;
z-index: 4;
right: 22px;
bottom: 20px;
padding: 9px 11px;
border-radius: 10px;
color: #63737b;
background: rgba(255, 255, 255, 0.74);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
pointer-events: none;
}
.notes {
display: grid;
grid-template-columns: 1.15fr 0.85fr;
gap: 16px;
margin-top: 18px;
}
.note-card {
min-height: 128px;
padding: 20px 22px;
border: 1px solid rgba(97, 119, 126, 0.17);
border-radius: 17px;
background: rgba(255, 255, 255, 0.72);
}
.note-card.warning {
border-color: #ead5c3;
background: #fffaf4;
}
.note-title {
display: flex;
align-items: center;
gap: 9px;
margin: 0 0 8px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.02em;
}
.note-title::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--amber);
content: "";
}
.note-card p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
footer {
padding: 18px 3px 0;
color: #7c898f;
font-size: 11px;
line-height: 1.5;
text-align: center;
}
@media (max-width: 760px) {
.page {
width: min(100% - 24px, 1380px);
padding-top: 26px;
}
.hero {
align-items: flex-start;
flex-direction: column;
gap: 19px;
}
.hero-actions {
width: 100%;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.stage {
height: 760px;
border-radius: 20px;
}
.split-line {
top: 64%;
right: 0;
bottom: auto;
left: 0;
width: auto;
height: 1px;
}
.panel-title {
top: 14px;
left: 14px;
width: calc(100% - 28px);
padding: 11px 13px;
}
.alt-title {
top: calc(64% + 14px);
left: 14px;
}
.legend {
bottom: calc(36% + 14px);
left: 14px;
max-width: calc(100% - 28px);
}
.hint {
right: 14px;
bottom: 14px;
}
.notes {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<main class="page">
<header class="hero">
<div>
<div class="eyebrow">Arrangement Study / 01</div>
<h1>Bridge plank, two ways.</h1>
<p class="hero-copy">
A stylized comparison of a plank visibly overlapping two support blocks,
with the unsupported alternative presented separately.
</p>
</div>
<div class="hero-actions">
<span class="concept-badge">Concept · not a load design</span>
<button class="reset-button" id="resetView" type="button">Reset view</button>
</div>
</header>
<section class="stage" id="stage" aria-label="Interactive three-dimensional comparison of supported and unsupported planks">
<canvas id="scene" role="img" aria-label="A wooden plank overlapping two support blocks on the left, and an unsupported plank on the right">
This visualization requires a browser with WebGL support.
</canvas>
<div class="split-line" aria-hidden="true"></div>
<div class="panel-title main-title">
<span class="panel-number">01 / Supported arrangement</span>
<h2>Plank over two blocks</h2>
<p>Both ends visibly overlap the top of a support.</p>
</div>
<div class="panel-title alt-title">
<span class="panel-number">02 / Separate alternative</span>
<h2>Unsupported plank</h2>
<p>The same plank is shown alone, with no blocks or supports.</p>
</div>
<div class="world-label" id="leftOverlapLabel">Left overlap</div>
<div class="world-label" id="rightOverlapLabel">Right overlap</div>
<div class="world-label gap-label" id="gapLabel">Clear gap</div>
<div class="world-label unsupported-label" id="unsupportedLabel">Plank only</div>
<div class="legend" aria-hidden="true">
<span class="legend-item"><span class="swatch"></span>Illustrated overlap zone</span>
<span class="legend-item"><span class="gap-mark"></span>Space between blocks</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</section>
<section class="notes" aria-label="Interpretation notes">
<article class="note-card warning">
<h2 class="note-title">Visual concept only</h2>
<p>
This scene communicates arrangement and visible overlap only. It does not
establish bearing capacity, allowable loads, anchorage, material suitability,
connection details, or code compliance.
</p>
</article>
<article class="note-card">
<h2 class="note-title">Real installations need professional review</h2>
<p>
Any real bridge, ramp, or access plank should be designed and verified for
its actual site and loads by a qualified structural professional. No
real-world structural load certification is implied or provided.
</p>
</article>
</section>
<footer>
Stylized procedural geometry · Core Three.js module only · Camera controls implemented directly in this page
</footer>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const canvas = document.getElementById("scene");
const resetButton = document.getElementById("resetView");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.autoClear = true;
const mainScene = new THREE.Scene();
mainScene.background = new THREE.Color(0xf1ede5);
const alternativeScene = new THREE.Scene();
alternativeScene.background = new THREE.Color(0xe8eff1);
const mainCamera = new THREE.OrthographicCamera(-5, 5, 4, -4, 0.1, 100);
const alternativeCamera = new THREE.OrthographicCamera(-5, 5, 4, -4, 0.1, 100);
const initialMain = {
theta: 0.73,
phi: 1.08,
radius: 11,
zoom: 1,
target: new THREE.Vector3(0, 1.02, 0)
};
const initialAlternative = {
theta: 0.54,
phi: 1.14,
radius: 11,
zoom: 1,
target: new THREE.Vector3(0, 1.28, 0)
};
function cloneControlState(state) {
return {
theta: state.theta,
phi: state.phi,
radius: state.radius,
zoom: state.zoom,
target: state.target.clone()
};
}
const mainControl = {
desired: cloneControlState(initialMain),
current: cloneControlState(initialMain)
};
const alternativeControl = {
desired: cloneControlState(initialAlternative),
current: cloneControlState(initialAlternative)
};
const warmWood = new THREE.MeshStandardMaterial({
color: 0xd79855,
roughness: 0.72,
metalness: 0.02
});
const warmWoodSide = new THREE.MeshStandardMaterial({
color: 0xbc733c,
roughness: 0.8,
metalness: 0.01
});
const grainMaterial = new THREE.MeshStandardMaterial({
color: 0x784a2d,
roughness: 0.9,
transparent: true,
opacity: 0.32
});
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x66808b,
roughness: 0.83,
metalness: 0.01
});
const supportInsetMaterial = new THREE.MeshStandardMaterial({
color: 0x536d78,
roughness: 0.9,
metalness: 0.01
});
const supportEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x354e59,
transparent: true,
opacity: 0.19
});
function roundedShape(width, height, radius) {
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
const r = Math.min(radius, width / 2, height / 2);
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
function bevelBoxGeometry(width, height, depth, bevel = 0.05) {
const b = Math.min(bevel, width * 0.12, height * 0.22, depth * 0.12);
const shape = roundedShape(
Math.max(0.01, width - 2 * b),
Math.max(0.01, height - 2 * b),
Math.min(0.1, width * 0.12, height * 0.22)
);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: Math.max(0.01, depth - 2 * b),
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: b,
bevelThickness: b,
curveSegments: 8
});
geometry.center();
geometry.computeVertexNormals();
return geometry;
}
function addShadowedEdges(mesh, opacity = 0.18) {
const material = supportEdgeMaterial.clone();
material.opacity = opacity;
const edges = new THREE.LineSegments(new THREE.EdgesGeometry(mesh.geometry, 28), material);
edges.position.copy(mesh.position);
edges.rotation.copy(mesh.rotation);
mesh.parent.add(edges);
return edges;
}
function createGrain(material = grainMaterial, length = 5.25, topY = 1.892) {
const group = new THREE.Group();
const offsets = [-0.46, -0.17, 0.17, 0.46];
offsets.forEach((offset, row) => {
const points = [];
for (let i = 0; i < 8; i++) {
const x = -length / 2 + (i / 7) * length;
const z = offset + Math.sin(i * 1.72 + row * 0.8) * 0.025;
points.push(new THREE.Vector3(x, topY, z));
}
const curve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.TubeGeometry(curve, 54, 0.008, 5, false);
group.add(new THREE.Mesh(geometry, material));
});
return group;
}
function createWoodPlank() {
const group = new THREE.Group();
const plank = new THREE.Mesh(
bevelBoxGeometry(5.55, 0.28, 1.48, 0.045),
warmWood
);
plank.castShadow = true;
plank.receiveShadow = true;
group.add(plank);
const lowerStrip = new THREE.Mesh(
new THREE.BoxGeometry(5.36, 0.025, 1.40),
warmWoodSide
);
lowerStrip.position.y = -0.145;
group.add(lowerStrip);
group.add(createGrain());
return group;
}
function createSupportBlock(x) {
const group = new THREE.Group();
const body = new THREE.Mesh(
bevelBoxGeometry(1.8, 1.6, 2.15, 0.075),
supportMaterial
);
body.position.y = 0.8;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
addShadowedEdges(body, 0.16);
const inset = new THREE.Mesh(
new THREE.BoxGeometry(0.86, 0.55, 0.035),
supportInsetMaterial
);
inset.position.set(0, 0.77, 1.095);
inset.castShadow = true;
group.add(inset);
const insetLineMaterial = new THREE.LineBasicMaterial({
color: 0xb8c8cc,
transparent: true,
opacity: 0.4
});
const insetGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.31, 0.63, 1.116),
new THREE.Vector3(-0.31, 0.91, 1.116),
new THREE.Vector3(0.31, 0.91, 1.116),
new THREE.Vector3(0.31, 0.63, 1.116)
]);
group.add(new THREE.Line(insetGeometry, insetLineMaterial));
const rivetMaterial = new THREE.MeshStandardMaterial({
color: 0xa9bdc3,
roughness: 0.55,
metalness: 0.08
});
[-0.31, 0.31].forEach((rivetX) => {
const rivet = new THREE.Mesh(
new THREE.CylinderGeometry(0.047, 0.047, 0.024, 18),
rivetMaterial
);
rivet.rotation.x = Math.PI / 2;
rivet.position.set(rivetX, 0.77, 1.125);
group.add(rivet);
});
group.position.x = x;
return group;
}
function rectangleOutline(centerX, centerZ, width, depth, y, color, opacity = 0.8) {
const points = [
new THREE.Vector3(centerX - width / 2, y, centerZ - depth / 2),
new THREE.Vector3(centerX + width / 2, y, centerZ - depth / 2),
new THREE.Vector3(centerX + width / 2, y, centerZ + depth / 2),
new THREE.Vector3(centerX - width / 2, y, centerZ + depth / 2)
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
return new THREE.LineLoop(geometry, material);
}
function addOverlapZone(centerX) {
const material = new THREE.MeshBasicMaterial({
color: 0x35b5a7,
transparent: true,
opacity: 0.2,
depthWrite: false,
side: THREE.DoubleSide
});
const zone = new THREE.Mesh(new THREE.PlaneGeometry(1.56, 1.17), material);
zone.rotation.x = -Math.PI / 2;
zone.position.set(centerX, 1.887, 0);
mainScene.add(zone);
mainScene.add(rectangleOutline(centerX, 0, 1.56, 1.17, 1.892, 0x168c82, 0.82));
}
function addLighting(scene, castShadow = false) {
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x9ba8aa, 2.25);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xfff7e8, 3.25);
key.position.set(-4.5, 8.5, 6.5);
key.castShadow = castShadow;
if (castShadow) {
key.shadow.mapSize.set(1536, 1536);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 30;
key.shadow.bias = -0.00025;
key.shadow.normalBias = 0.025;
}
scene.add(key);
const rim = new THREE.DirectionalLight(0xb9e7e4, 1.15);
rim.position.set(6, 4, -5);
scene.add(rim);
}
// Supported scene
addLighting(mainScene, true);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
new THREE.MeshStandardMaterial({
color: 0xf1ede5,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
mainScene.add(ground);
const groundIsland = new THREE.Mesh(
new THREE.CircleGeometry(5.35, 72),
new THREE.MeshBasicMaterial({
color: 0xe5ddd1,
transparent: true,
opacity: 0.55,
depthWrite: false
})
);
groundIsland.rotation.x = -Math.PI / 2;
groundIsland.position.y = 0.002;
mainScene.add(groundIsland);
mainScene.add(createSupportBlock(-2.05));
mainScene.add(createSupportBlock(2.05));
const supportedPlank = createWoodPlank();
supportedPlank.position.y = 1.74;
mainScene.add(supportedPlank);
addOverlapZone(-1.96);
addOverlapZone(1.96);
const gapLineMaterial = new THREE.LineBasicMaterial({
color: 0x667b85,
transparent: true,
opacity: 0.75
});
const gapGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-1.15, 0.035, 1.37),
new THREE.Vector3(1.15, 0.035, 1.37),
new THREE.Vector3(-1.15, 0.035, 1.29),
new THREE.Vector3(-1.15, 0.035, 1.45),
new THREE.Vector3(1.15, 0.035, 1.29),
new THREE.Vector3(1.15, 0.035, 1.45)
]);
mainScene.add(new THREE.LineSegments(gapGeometry, gapLineMaterial));
// Unsupported alternative, deliberately isolated in its own scene
addLighting(alternativeScene, false);
const unsupportedPlank = createWoodPlank();
unsupportedPlank.position.y = 1.28;
unsupportedPlank.rotation.y = -0.035;
alternativeScene.add(unsupportedPlank);
const alternativeHalo = new THREE.Mesh(
new THREE.CircleGeometry(2.8, 64),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.16,
depthWrite: false
})
);
alternativeHalo.position.set(0, 1.25, -0.9);
alternativeScene.add(alternativeHalo);
const labelAnchors = {
left: {
element: document.getElementById("leftOverlapLabel"),
point: new THREE.Vector3(-1.96, 2.13, 0.62),
camera: mainCamera,
panel: "main"
},
right: {
element: document.getElementById("rightOverlapLabel"),
point: new THREE.Vector3(1.96, 2.13, 0.62),
camera: mainCamera,
panel: "main"
},
gap: {
element: document.getElementById("gapLabel"),
point: new THREE.Vector3(0, 0.10, 1.42),
camera: mainCamera,
panel: "main"
},
unsupported: {
element: document.getElementById("unsupportedLabel"),
point: new THREE.Vector3(0, 1.73, 0.66),
camera: alternativeCamera,
panel: "alternative"
}
};
let stageWidth = 1;
let stageHeight = 1;
let layout = null;
let drag = null;
function computeLayout(width, height) {
const stacked = width < 760;
if (stacked) {
const alternativeHeight = Math.round(height * 0.36);
const mainHeight = height - alternativeHeight;
return {
stacked,
main: {
x: 0,
y: alternativeHeight,
width,
height: mainHeight
},
alternative: {
x: 0,
y: 0,
width,
height: alternativeHeight
}
};
}
const mainWidth = Math.round(width * 0.66);
return {
stacked,
main: {
x: 0,
y: 0,
width: mainWidth,
height
},
alternative: {
x: mainWidth,
y: 0,
width: width - mainWidth,
height
}
};
}
function fitOrthographicCamera(camera, rect, desiredWidth, desiredHeight) {
const aspect = rect.width / rect.height;
const viewHeight = Math.max(desiredHeight, desiredWidth / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
function resize() {
const bounds = stage.getBoundingClientRect();
stageWidth = Math.max(1, bounds.width);
stageHeight = Math.max(1, bounds.height);
renderer.setSize(stageWidth, stageHeight, false);
layout = computeLayout(stageWidth, stageHeight);
fitOrthographicCamera(mainCamera, layout.main, 7.65, 5.55);
fitOrthographicCamera(alternativeCamera, layout.alternative, 7.0, 5.45);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
function panelAt(clientX, clientY) {
const bounds = stage.getBoundingClientRect();
const x = clientX - bounds.left;
const yFromTop = clientY - bounds.top;
const yFromBottom = stageHeight - yFromTop;
const mainRect = layout.main;
const alternativeRect = layout.alternative;
const inMain =
x >= mainRect.x &&
x <= mainRect.x + mainRect.width &&
yFromBottom >= mainRect.y &&
yFromBottom <= mainRect.y + mainRect.height;
if (inMain) return "main";
const inAlternative =
x >= alternativeRect.x &&
x <= alternativeRect.x + alternativeRect.width &&
yFromBottom >= alternativeRect.y &&
yFromBottom <= alternativeRect.y + alternativeRect.height;
return inAlternative ? "alternative" : null;
}
function controlFor(panel) {
return panel === "main" ? mainControl : alternativeControl;
}
function updateDesiredCamera(panel, deltaTheta, deltaPhi) {
const control = controlFor(panel);
control.desired.theta += deltaTheta;
control.desired.phi = THREE.MathUtils.clamp(
control.desired.phi + deltaPhi,
0.48,
1.43
);
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
const panel = panelAt(event.clientX, event.clientY);
if (!panel) return;
drag = {
panel,
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY
};
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
event.preventDefault();
});
canvas.addEventListener("pointermove", (event) => {
if (!drag || event.pointerId !== drag.pointerId) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
updateDesiredCamera(drag.panel, -dx * 0.0075, -dy * 0.006);
drag.x = event.clientX;
drag.y = event.clientY;
});
function endDrag(event) {
if (!drag || event.pointerId !== drag.pointerId) return;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
drag = null;
canvas.classList.remove("dragging");
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
const panel = panelAt(event.clientX, event.clientY);
if (!panel) return;
event.preventDefault();
const control = controlFor(panel);
control.desired.zoom = THREE.MathUtils.clamp(
control.desired.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.65
);
},
{ passive: false }
);
function resetControls() {
mainControl.desired = cloneControlState(initialMain);
alternativeControl.desired = cloneControlState(initialAlternative);
}
canvas.addEventListener("dblclick", resetControls);
resetButton.addEventListener("click", resetControls);
function damp(current, target, smoothing, deltaTime) {
return THREE.MathUtils.lerp(
current,
target,
1 - Math.exp(-smoothing * deltaTime)
);
}
function updateCamera(camera, control, deltaTime) {
const current = control.current;
const desired = control.desired;
current.theta = damp(current.theta, desired.theta, 10, deltaTime);
current.phi = damp(current.phi, desired.phi, 10, deltaTime);
current.zoom = damp(current.zoom, desired.zoom, 10, deltaTime);
const offset = new THREE.Vector3().setFromSphericalCoords(
current.radius,
current.phi,
current.theta
);
camera.position.copy(current.target).add(offset);
camera.lookAt(current.target);
camera.zoom = current.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
const projected = new THREE.Vector3();
function updateWorldLabels() {
Object.values(labelAnchors).forEach((label) => {
const rect = label.panel === "main" ? layout.main : layout.alternative;
projected.copy(label.point).project(label.camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
if (!visible) {
label.element.style.opacity = "0";
return;
}
const x = rect.x + (projected.x * 0.5 + 0.5) * rect.width;
const yFromBottom = rect.y + (projected.y * 0.5 + 0.5) * rect.height;
const yFromTop = stageHeight - yFromBottom;
label.element.style.left = `${x}px`;
label.element.style.top = `${yFromTop}px`;
label.element.style.opacity = "1";
});
}
let previousTime = performance.now();
function animate(now) {
const deltaTime = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
updateCamera(mainCamera, mainControl, deltaTime);
updateCamera(alternativeCamera, alternativeControl, deltaTime);
updateWorldLabels();
renderer.setScissorTest(false);
renderer.setViewport(0, 0, stageWidth, stageHeight);
renderer.clear(true, true, true);
renderer.setScissorTest(true);
renderer.setViewport(
layout.main.x,
layout.main.y,
layout.main.width,
layout.main.height
);
renderer.setScissor(
layout.main.x,
layout.main.y,
layout.main.width,
layout.main.height
);
renderer.render(mainScene, mainCamera);
renderer.setViewport(
layout.alternative.x,
layout.alternative.y,
layout.alternative.width,
layout.alternative.height
);
renderer.setScissor(
layout.alternative.x,
layout.alternative.y,
layout.alternative.width,
layout.alternative.height
);
renderer.render(alternativeScene, alternativeCamera);
renderer.setScissorTest(false);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```