Prompt specification
Build or customize the Three.js 3D scene "Frame folding order comparison" 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: Frame folding order comparison
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/frame-folding-order-comparison-x0586
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a low-poly pair of eyeglasses on a narrow display stand. Keep the front frame and lenses fixed while the two temples rotate inward around visible hinge pins. The hinges have slightly different offsets so the folded arms settle at separate heights instead of intersecting each other. Play the folding sequence one arm at a time, with a pause between steps and a small indicator for the active hinge. Allow the viewer to reverse the order and inspect the resulting clearance.
Working Three.js Starter (frame-folding-order-comparison-x0586):
```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 Eyeglasses — Hinge Fold Study</title>
<style>
:root {
--ink: #23282b;
--muted: #707779;
--paper: rgba(250, 248, 242, 0.9);
--line: rgba(35, 40, 43, 0.14);
--accent: #e86f3d;
--accent-dark: #a83e1c;
--gold: #c5a052;
--green: #3f8972;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e2d8;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
left: 20px;
width: 306px;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
background: var(--paper);
box-shadow:
0 18px 50px rgba(54, 47, 37, 0.14),
inset 0 1px 0 rgba(255, 255, 255, 0.75);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.eyebrow {
margin: 0 0 5px;
color: var(--accent-dark);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 21px;
font-weight: 760;
letter-spacing: -0.035em;
}
.intro {
margin: 7px 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 4px;
padding: 4px;
border-radius: 10px;
background: rgba(35, 40, 43, 0.065);
}
.segmented button {
padding: 7px 8px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition: 160ms ease;
}
.segmented button[aria-pressed="true"] {
background: #fff;
color: var(--ink);
box-shadow: 0 2px 8px rgba(37, 40, 42, 0.09);
}
.sequence {
display: flex;
align-items: center;
gap: 7px;
margin: 12px 0 15px;
}
.chip {
display: flex;
flex: 1;
align-items: center;
gap: 7px;
min-width: 0;
padding: 8px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.45);
transition: 180ms ease;
}
.chip b {
display: grid;
flex: 0 0 auto;
width: 20px;
height: 20px;
place-items: center;
border-radius: 50%;
background: rgba(35, 40, 43, 0.08);
font-size: 9px;
}
.chip > span {
min-width: 0;
font-size: 10px;
font-weight: 750;
white-space: nowrap;
}
.mini-progress {
display: block;
width: 100%;
height: 2px;
margin-top: 4px;
overflow: hidden;
border-radius: 2px;
background: rgba(35, 40, 43, 0.09);
}
.mini-progress i {
display: block;
width: 0;
height: 100%;
background: var(--accent);
transition: width 80ms linear;
}
.chip.active {
border-color: rgba(232, 111, 61, 0.52);
background: rgba(232, 111, 61, 0.1);
}
.chip.active b {
background: var(--accent);
color: white;
}
.chip.done:not(.active) {
border-color: rgba(63, 137, 114, 0.38);
}
.chip.done:not(.active) b {
background: var(--green);
color: white;
}
.sequence-link {
flex: 0 0 auto;
color: #a2a6a5;
font-size: 11px;
}
.button-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
.primary,
.secondary,
.mini-button {
border: 0;
border-radius: 10px;
cursor: pointer;
transition:
transform 130ms ease,
background 160ms ease,
opacity 160ms ease;
}
.primary:active,
.secondary:active,
.mini-button:active {
transform: translateY(1px);
}
.primary {
padding: 10px 12px;
background: var(--ink);
color: white;
font-size: 11px;
font-weight: 760;
}
.primary:hover {
background: #111517;
}
.secondary {
padding: 9px 11px;
background: rgba(35, 40, 43, 0.075);
font-size: 11px;
font-weight: 720;
}
.secondary:hover {
background: rgba(35, 40, 43, 0.12);
}
.manual-title {
margin: 16px 0 7px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.manual-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.mini-button {
padding: 8px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.48);
font-size: 10px;
font-weight: 750;
}
.mini-button:hover,
.mini-button.selected {
border-color: rgba(232, 111, 61, 0.5);
background: rgba(232, 111, 61, 0.09);
}
.clearance-box {
margin-top: 16px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.clearance-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.toggle {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
cursor: pointer;
font-size: 10px;
font-weight: 700;
}
.toggle input {
width: 14px;
height: 14px;
accent-color: var(--accent);
}
.clearance-value {
color: var(--green);
font-size: 12px;
font-weight: 800;
}
.clearance-box .secondary {
width: 100%;
margin-top: 10px;
}
button:disabled {
cursor: not-allowed;
opacity: 0.4;
}
.status {
position: fixed;
z-index: 10;
top: 20px;
right: 20px;
display: flex;
align-items: center;
gap: 10px;
min-width: 185px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 13px;
background: var(--paper);
box-shadow: 0 12px 30px rgba(54, 47, 37, 0.11);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.status-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: #a1a6a5;
box-shadow: 0 0 0 4px rgba(161, 166, 165, 0.12);
}
.status-dot.active {
background: var(--accent);
box-shadow: 0 0 0 4px rgba(232, 111, 61, 0.15);
animation: statusPulse 1s ease-in-out infinite;
}
.status-dot.pause {
background: var(--gold);
box-shadow: 0 0 0 4px rgba(197, 160, 82, 0.15);
}
.status-dot.done {
background: var(--green);
box-shadow: 0 0 0 4px rgba(63, 137, 114, 0.14);
}
@keyframes statusPulse {
50% {
transform: scale(1.22);
opacity: 0.72;
}
}
.status small,
.status strong {
display: block;
}
.status small {
margin-bottom: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.15em;
}
.status strong {
font-size: 11px;
}
.scene-label {
position: fixed;
z-index: 8;
display: none;
padding: 6px 9px;
border: 1px solid rgba(63, 137, 114, 0.3);
border-radius: 8px;
background: rgba(250, 248, 242, 0.92);
color: #2e725f;
font-size: 10px;
font-weight: 800;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
box-shadow: 0 6px 18px rgba(48, 74, 65, 0.12);
}
.gesture-hint {
position: fixed;
z-index: 5;
right: 20px;
bottom: 18px;
padding: 7px 10px;
border-radius: 9px;
background: rgba(250, 248, 242, 0.72);
color: rgba(35, 40, 43, 0.68);
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(8px);
}
@media (max-width: 700px) {
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 48vh;
overflow: auto;
padding: 14px;
}
.status {
top: 12px;
right: 12px;
min-width: 160px;
}
.intro {
margin-bottom: 11px;
}
.gesture-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<aside class="panel">
<p class="eyebrow">Low-poly object study</p>
<h1>Hinge Fold Sequence</h1>
<p class="intro">
Two temples fold inward around visible vertical pins. The right hinge is
offset slightly higher, leaving a measurable gap between the folded arms.
</p>
<div class="section-label">
<span>Fold order</span>
<span>One arm at a time</span>
</div>
<div class="segmented" aria-label="Fold order">
<button id="orderLR" aria-pressed="true">Left → Right</button>
<button id="orderRL" aria-pressed="false">Right → Left</button>
</div>
<div class="sequence">
<div class="chip" id="chipLeft">
<b>01</b>
<span>
Left pin
<small class="mini-progress"><i></i></small>
</span>
</div>
<div class="sequence-link" id="sequenceLink">→</div>
<div class="chip" id="chipRight">
<b>02</b>
<span>
Right pin
<small class="mini-progress"><i></i></small>
</span>
</div>
</div>
<div class="button-row">
<button class="primary" id="playButton">▶ Play sequence</button>
<button class="secondary" id="resetButton">Reset</button>
</div>
<p class="manual-title">Manual hinge control</p>
<div class="manual-row">
<button class="mini-button" id="manualLeft">Fold left pin</button>
<button class="mini-button" id="manualRight">Fold right pin</button>
</div>
<div class="clearance-box">
<div class="clearance-top">
<label class="toggle">
<input id="clearanceToggle" type="checkbox" checked />
Show clearance guide
</label>
<span class="clearance-value">0.08 units</span>
</div>
<button class="secondary" id="inspectButton" disabled>
Inspect folded clearance
</button>
</div>
</aside>
<div class="status">
<span class="status-dot" id="statusDot"></span>
<div>
<small id="statusKicker">HINGE STATUS</small>
<strong id="statusText">Ready to fold</strong>
</div>
</div>
<div class="scene-label" id="clearanceLabel">
0.08 arm-to-arm clearance
</div>
<div class="gesture-hint">Drag to orbit · wheel to zoom · click a hinge pin</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and basic scene
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const app = $("app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e2d8);
scene.fog = new THREE.Fog(0xe8e2d8, 11, 23);
const camera = new THREE.PerspectiveCamera(
36,
innerWidth / innerHeight,
0.05,
50
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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;
app.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff8ed, 0x68747b, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff5df, 3.1);
keyLight.position.set(4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xbdd8e5, 1.35);
rimLight.position.set(-5, 3.5, -4);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0xded7cb,
roughness: 0.94,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x202b31,
roughness: 0.42,
metalness: 0.3,
flatShading: true
});
const templeMaterial = new THREE.MeshStandardMaterial({
color: 0x263238,
roughness: 0.36,
metalness: 0.36,
flatShading: true
});
const goldMaterial = new THREE.MeshStandardMaterial({
color: 0xc5a052,
roughness: 0.28,
metalness: 0.82,
flatShading: true
});
const standMaterial = new THREE.MeshStandardMaterial({
color: 0x4a4038,
roughness: 0.58,
metalness: 0.18,
flatShading: true
});
const standAccentMaterial = new THREE.MeshStandardMaterial({
color: 0x8d7457,
roughness: 0.5,
metalness: 0.35,
flatShading: true
});
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x9ec8d6,
transparent: true,
opacity: 0.2,
roughness: 0.2,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const nosePadMaterial = new THREE.MeshStandardMaterial({
color: 0xd6c6a8,
transparent: true,
opacity: 0.7,
roughness: 0.5
});
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function enableShadows(object) {
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
return object;
}
function cylinderBetween(a, b, radius, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function roundedRectanglePoints(width, height, radius, cornerSegments = 4) {
const points = [];
const corners = [
{
cx: width / 2 - radius,
cy: height / 2 - radius,
start: 0
},
{
cx: -width / 2 + radius,
cy: height / 2 - radius,
start: Math.PI / 2
},
{
cx: -width / 2 + radius,
cy: -height / 2 + radius,
start: Math.PI
},
{
cx: width / 2 - radius,
cy: -height / 2 + radius,
start: (3 * Math.PI) / 2
}
];
for (const corner of corners) {
for (let i = 0; i <= cornerSegments; i++) {
const angle =
corner.start + (i / cornerSegments) * (Math.PI / 2);
points.push(
new THREE.Vector2(
corner.cx + Math.cos(angle) * radius,
corner.cy + Math.sin(angle) * radius
)
);
}
}
return points;
}
function makeLens(centerX, centerY) {
const outline = roundedRectanglePoints(1.72, 1.35, 0.28, 4);
const shape = new THREE.Shape();
shape.moveTo(outline[0].x, outline[0].y);
for (let i = 1; i < outline.length; i++) {
shape.lineTo(outline[i].x, outline[i].y);
}
shape.closePath();
const curve = new THREE.CatmullRomCurve3(
outline.map(
(point) => new THREE.Vector3(point.x, point.y, 0)
),
true,
"centripetal"
);
const rim = new THREE.Mesh(
new THREE.TubeGeometry(curve, 36, 0.07, 5, true),
frameMaterial
);
rim.position.set(centerX, centerY, 0);
rim.castShadow = true;
rim.receiveShadow = true;
scene.add(rim);
const lens = new THREE.Mesh(
new THREE.ShapeGeometry(shape, 1),
glassMaterial
);
lens.position.set(centerX, centerY, 0.012);
lens.receiveShadow = false;
scene.add(lens);
// A restrained polygonal reflection on each lens.
const reflectionGeometry = new THREE.BufferGeometry();
reflectionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
centerX - 0.52,
centerY + 0.48,
0.025,
centerX - 0.15,
centerY + 0.5,
0.025,
centerX - 0.43,
centerY + 0.05,
0.025
],
3
)
);
reflectionGeometry.computeVertexNormals();
const reflection = new THREE.Mesh(
reflectionGeometry,
new THREE.MeshBasicMaterial({
color: 0xeaf8fc,
transparent: true,
opacity: 0.2,
side: THREE.DoubleSide,
depthWrite: false
})
);
scene.add(reflection);
}
// -------------------------------------------------------------------------
// Fixed eyeglass front
// -------------------------------------------------------------------------
makeLens(-0.93, 2.05);
makeLens(0.93, 2.05);
const bridgeCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-0.13, 2.4, 0),
new THREE.Vector3(0, 2.51, 0),
new THREE.Vector3(0.13, 2.4, 0)
],
false,
"centripetal"
);
const bridge = new THREE.Mesh(
new THREE.TubeGeometry(bridgeCurve, 12, 0.055, 5, false),
frameMaterial
);
bridge.castShadow = true;
scene.add(bridge);
for (const side of [-1, 1]) {
const pad = new THREE.Mesh(
new THREE.OctahedronGeometry(0.11, 0),
nosePadMaterial
);
pad.position.set(side * 0.23, 2.02, -0.075);
pad.scale.set(0.65, 1.35, 0.45);
pad.castShadow = true;
scene.add(pad);
}
// -------------------------------------------------------------------------
// Narrow faceted display stand
// -------------------------------------------------------------------------
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.68, 0.78, 0.18, 8),
standMaterial
);
base.position.y = 0.09;
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
const baseInset = new THREE.Mesh(
new THREE.CylinderGeometry(0.52, 0.61, 0.055, 8),
standAccentMaterial
);
baseInset.position.y = 0.2;
baseInset.castShadow = true;
baseInset.receiveShadow = true;
scene.add(baseInset);
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 1.27, 0.2),
standMaterial
);
post.position.set(0, 0.84, -0.035);
post.castShadow = true;
post.receiveShadow = true;
scene.add(post);
const postStripe = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 1.17, 0.015),
standAccentMaterial
);
postStripe.position.set(0, 0.84, 0.073);
scene.add(postStripe);
scene.add(
cylinderBetween(
new THREE.Vector3(0, 1.43, -0.025),
new THREE.Vector3(-0.16, 1.51, -0.01),
0.035,
standMaterial,
5
)
);
scene.add(
cylinderBetween(
new THREE.Vector3(0, 1.43, -0.025),
new THREE.Vector3(0.16, 1.51, -0.01),
0.035,
standMaterial,
5
)
);
// -------------------------------------------------------------------------
// Hinges and rotating temples
// -------------------------------------------------------------------------
const FOLD_ANGLE = THREE.MathUtils.degToRad(84);
const ARM_RADIUS = 0.06;
const LEFT_HINGE_Y = 1.97;
const RIGHT_HINGE_Y = 2.17;
const CLEARANCE =
RIGHT_HINGE_Y - LEFT_HINGE_Y - ARM_RADIUS * 2;
const hitMeshes = [];
function createHinge(side, hingeY) {
const root = new THREE.Group();
root.position.set(
side === "left" ? -1.84 : 1.84,
hingeY,
-0.1
);
scene.add(root);
const pivot = new THREE.Group();
root.add(pivot);
const inward = side === "left" ? 1 : -1;
const templeCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(inward * 0.02, 0, -0.38),
new THREE.Vector3(inward * 0.08, -0.01, -1.15),
new THREE.Vector3(inward * 0.04, -0.07, -2.05),
new THREE.Vector3(0, -0.16, -2.55)
],
false,
"centripetal"
);
const temple = new THREE.Mesh(
new THREE.TubeGeometry(
templeCurve,
18,
ARM_RADIUS,
5,
false
),
templeMaterial
);
temple.castShadow = true;
temple.receiveShadow = true;
pivot.add(temple);
const tip = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.085, 0.27, 5),
frameMaterial
);
tip.position.set(0, -0.16, -2.55);
tip.rotation.x = Math.PI / 2;
tip.castShadow = true;
pivot.add(tip);
const pinMaterial = goldMaterial.clone();
const pin = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.38, 8),
pinMaterial
);
pin.castShadow = true;
root.add(pin);
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.14, 0.19, 8),
frameMaterial
);
collar.position.y = -0.105;
collar.castShadow = true;
root.add(collar);
const activeRing = new THREE.Mesh(
new THREE.TorusGeometry(0.19, 0.022, 5, 18),
new THREE.MeshBasicMaterial({
color: 0xff7542,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
})
);
activeRing.rotation.x = Math.PI / 2;
activeRing.position.y = 0.21;
activeRing.visible = false;
activeRing.renderOrder = 10;
root.add(activeRing);
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
});
const hit = new THREE.Mesh(
new THREE.SphereGeometry(0.26, 8, 6),
hitMaterial
);
hit.userData.hinge = side;
root.add(hit);
hitMeshes.push(hit);
const connectorStart = new THREE.Vector3(
side * 1.77,
2.08,
0
);
const connectorEnd = new THREE.Vector3(
root.position.x,
hingeY,
-0.1
);
scene.add(
cylinderBetween(
connectorStart,
connectorEnd,
0.06,
frameMaterial,
6
)
);
return {
id: side,
label: side === "left" ? "Left" : "Right",
root,
pivot,
pin,
activeRing,
progress: 0,
target: 0
};
}
const arms = {
left: createHinge("left", LEFT_HINGE_Y),
right: createHinge("right", RIGHT_HINGE_Y)
};
// -------------------------------------------------------------------------
// Clearance dimension in the folded position
// -------------------------------------------------------------------------
const clearanceGroup = new THREE.Group();
scene.add(clearanceGroup);
const dimensionZ = -0.31;
const lowerGapY = LEFT_HINGE_Y + ARM_RADIUS;
const upperGapY = RIGHT_HINGE_Y - ARM_RADIUS;
const clearanceAnchor = new THREE.Vector3(0, 2.43, -0.47);
const dimensionMaterial = new THREE.LineBasicMaterial({
color: 0x3f8972,
transparent: true,
opacity: 0.95,
depthTest: false
});
function dimensionLine(points) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, dimensionMaterial);
line.renderOrder = 8;
clearanceGroup.add(line);
return line;
}
dimensionLine([
new THREE.Vector3(0, lowerGapY, dimensionZ),
new THREE.Vector3(0, upperGapY, dimensionZ)
]);
dimensionLine([
new THREE.Vector3(-0.11, lowerGapY, dimensionZ),
new THREE.Vector3(0.11, lowerGapY, dimensionZ)
]);
dimensionLine([
new THREE.Vector3(-0.11, upperGapY, dimensionZ),
new THREE.Vector3(0.11, upperGapY, dimensionZ)
]);
dimensionLine([
new THREE.Vector3(0, upperGapY, dimensionZ),
clearanceAnchor
]);
const clearanceLabel = $("clearanceLabel");
// -------------------------------------------------------------------------
// Sequencing state
// -------------------------------------------------------------------------
let direction = "LR";
let sequenceRunning = false;
let phase = "idle";
let currentHinge = null;
let queuedHinge = null;
let motion = null;
let pauseRemaining = 0;
let pauseNext = null;
function getOrder() {
return direction === "LR" ? ["left", "right"] : ["right", "left"];
}
function bothFolded() {
return arms.left.progress > 0.999 && arms.right.progress > 0.999;
}
function nextUnfolded() {
return getOrder().find((id) => arms[id].progress < 0.999) || null;
}
function beginMotion(id, target) {
const arm = arms[id];
const delta = Math.abs(target - arm.progress);
motion = {
id,
from: arm.progress,
to: target,
elapsed: 0,
duration: Math.max(0.45, delta * 1.28)
};
arm.target = target;
currentHinge = id;
phase = "moving";
}
function beginNextSequenceStep() {
const next = nextUnfolded();
if (!next) {
sequenceRunning = false;
currentHinge = null;
phase = "idle";
return;
}
beginMotion(next, 1);
}
function setAllOpen() {
for (const id of ["left", "right"]) {
arms[id].progress = 0;
arms[id].target = 0;
}
}
function resetSequence() {
sequenceRunning = false;
phase = "idle";
currentHinge = null;
queuedHinge = null;
motion = null;
pauseNext = null;
setAllOpen();
updateUI();
}
function togglePlay() {
if (sequenceRunning) {
sequenceRunning = false;
if (motion) {
phase = "userPaused";
} else {
phase = "idle";
currentHinge = null;
}
updateUI();
return;
}
if (bothFolded()) {
setAllOpen();
}
sequenceRunning = true;
if (motion && currentHinge) {
phase = "moving";
} else {
currentHinge = null;
phase = "pause";
pauseRemaining = 0.22;
pauseNext = "sequence";
}
updateUI();
}
function requestManualHinge(id) {
sequenceRunning = false;
if (motion) {
if (motion.id === id) {
// Reverse the active hinge.
beginMotion(id, arms[id].progress > 0.5 ? 0 : 1);
} else {
// Keep one hinge moving at a time; queue the second.
queuedHinge = id;
phase = "moving";
}
} else {
currentHinge = null;
beginMotion(id, arms[id].progress > 0.5 ? 0 : 1);
}
updateUI();
}
function updateSequence(dt) {
if (phase === "moving" && motion) {
motion.elapsed += dt;
const t = Math.min(motion.elapsed / motion.duration, 1);
const eased =
t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
const arm = arms[motion.id];
arm.progress = THREE.MathUtils.lerp(
motion.from,
motion.to,
eased
);
if (t >= 1) {
arm.progress = motion.to;
motion = null;
if (queuedHinge) {
phase = "pause";
pauseRemaining = 0.25;
pauseNext = "queued";
} else if (sequenceRunning) {
phase = "pause";
pauseRemaining = 0.82;
pauseNext = "sequence";
} else {
phase = "idle";
currentHinge = null;
}
}
}
if (phase === "pause") {
pauseRemaining -= dt;
if (pauseRemaining <= 0) {
if (pauseNext === "queued" && queuedHinge) {
const queued = queuedHinge;
queuedHinge = null;
currentHinge = null;
beginMotion(queued, arms[queued].progress > 0.5 ? 0 : 1);
} else if (pauseNext === "sequence" && sequenceRunning) {
currentHinge = null;
beginNextSequenceStep();
} else {
phase = "idle";
currentHinge = null;
}
}
}
for (const id of ["left", "right"]) {
const arm = arms[id];
const sign = id === "left" ? -1 : 1;
arm.pivot.rotation.y = sign * FOLD_ANGLE * arm.progress;
}
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
function setText(element, text) {
if (element.textContent !== text) {
element.textContent = text;
}
}
function updateUI() {
const order = getOrder();
const next = nextUnfolded();
$("orderLR").setAttribute(
"aria-pressed",
String(direction === "LR")
);
$("orderRL").setAttribute(
"aria-pressed",
String(direction === "RL")
);
$("chipLeft").style.order = direction === "LR" ? "1" : "3";
$("sequenceLink").style.order = "2";
$("chipRight").style.order = direction === "LR" ? "3" : "1";
$("sequenceLink").textContent = direction === "LR" ? "→" : "→";
$("chipLeft").querySelector("b").textContent =
direction === "LR" ? "01" : "02";
$("chipRight").querySelector("b").textContent =
direction === "LR" ? "02" : "01";
for (const id of ["left", "right"]) {
const arm = arms[id];
const chip = id === "left" ? $("chipLeft") : $("chipRight");
const progress = chip.querySelector(".mini-progress i");
progress.style.width = `${arm.progress * 100}%`;
chip.classList.toggle("done", arm.progress > 0.999);
chip.classList.toggle("active", currentHinge === id);
}
$("manualLeft").classList.toggle(
"selected",
currentHinge === "left" || queuedHinge === "left"
);
$("manualRight").classList.toggle(
"selected",
currentHinge === "right" || queuedHinge === "right"
);
setText(
$("manualLeft"),
arms.left.progress > 0.5 ? "Unfold left pin" : "Fold left pin"
);
setText(
$("manualRight"),
arms.right.progress > 0.5 ? "Unfold right pin" : "Fold right pin"
);
const statusDot = $("statusDot");
statusDot.className = "status-dot";
if (phase === "moving" && currentHinge) {
setText($("statusKicker"), "FOLDING");
setText($("statusText"), `${arms[currentHinge].label} hinge active`);
statusDot.classList.add("active");
} else if (phase === "pause" && pauseNext === "sequence") {
setText($("statusKicker"), "PAUSE");
setText(
$("statusText"),
next ? `Next: ${arms[next].label} hinge` : "Sequence complete"
);
statusDot.classList.add("pause");
} else if (phase === "userPaused") {
setText($("statusKicker"), "PAUSED");
setText(
$("statusText"),
currentHinge
? `${arms[currentHinge].label} hinge held`
: "Sequence held"
);
statusDot.classList.add("pause");
} else if (bothFolded()) {
setText($("statusKicker"), "CLEARANCE READY");
setText($("statusText"), "Both temples folded");
statusDot.classList.add("done");
} else if (queuedHinge) {
setText($("statusKicker"), "QUEUED");
setText($("statusText"), `${arms[queuedHinge].label} hinge next`);
statusDot.classList.add("pause");
} else {
setText($("statusKicker"), "HINGE STATUS");
setText($("statusText"), "Ready to fold");
}
let playLabel = "▶ Play sequence";
if (sequenceRunning) {
playLabel = "Ⅱ Pause sequence";
} else if (phase === "userPaused") {
playLabel = "▶ Resume sequence";
} else if (bothFolded()) {
playLabel = "↻ Replay sequence";
}
setText($("playButton"), playLabel);
$("inspectButton").disabled = !bothFolded();
}
$("playButton").addEventListener("click", togglePlay);
$("resetButton").addEventListener("click", resetSequence);
$("manualLeft").addEventListener("click", () =>
requestManualHinge("left")
);
$("manualRight").addEventListener("click", () =>
requestManualHinge("right")
);
$("orderLR").addEventListener("click", () => {
direction = "LR";
updateUI();
});
$("orderRL").addEventListener("click", () => {
direction = "RL";
updateUI();
});
$("clearanceToggle").addEventListener("change", updateUI);
// -------------------------------------------------------------------------
// Custom camera controls — no external controls module
// -------------------------------------------------------------------------
const desiredCamera = {
target: new THREE.Vector3(0, 1.68, -0.48),
theta: 0.62,
phi: 1.27,
radius: 7.5
};
const currentCamera = {
target: desiredCamera.target.clone(),
theta: desiredCamera.theta,
phi: desiredCamera.phi,
radius: desiredCamera.radius
};
function resetCameraView() {
desiredCamera.target.set(0, 1.68, -0.48);
desiredCamera.theta = 0.62;
desiredCamera.phi = 1.27;
desiredCamera.radius = 7.5;
}
function inspectClearance() {
desiredCamera.target.set(0, 2.06, -0.32);
desiredCamera.theta = Math.PI;
desiredCamera.phi = 1.24;
desiredCamera.radius = 3.05;
}
$("inspectButton").addEventListener("click", inspectClearance);
renderer.domElement.addEventListener("dblclick", resetCameraView);
const pointer = {
down: false,
id: null,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false
};
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.down = true;
pointer.id = event.pointerId;
pointer.startX = pointer.lastX = event.clientX;
pointer.startY = pointer.lastY = event.clientY;
pointer.moved = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.down || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
if (
Math.abs(event.clientX - pointer.startX) +
Math.abs(event.clientY - pointer.startY) >
4
) {
pointer.moved = true;
}
desiredCamera.theta -= dx * 0.006;
desiredCamera.phi = THREE.MathUtils.clamp(
desiredCamera.phi - dy * 0.005,
0.32,
1.49
);
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (event.pointerId !== pointer.id) return;
pointer.down = false;
if (!pointer.moved) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNDC.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const intersections = raycaster.intersectObjects(
hitMeshes,
false
);
if (intersections.length) {
const hinge = intersections[0].object.userData.hinge;
requestManualHinge(hinge);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointer.down = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredCamera.radius = THREE.MathUtils.clamp(
desiredCamera.radius + event.deltaY * 0.006,
2.3,
12
);
},
{ passive: false }
);
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 8);
currentCamera.theta = THREE.MathUtils.lerp(
currentCamera.theta,
desiredCamera.theta,
smoothing
);
currentCamera.phi = THREE.MathUtils.lerp(
currentCamera.phi,
desiredCamera.phi,
smoothing
);
currentCamera.radius = THREE.MathUtils.lerp(
currentCamera.radius,
desiredCamera.radius,
smoothing
);
currentCamera.target.lerp(
desiredCamera.target,
smoothing
);
const sinPhi = Math.sin(currentCamera.phi);
camera.position.set(
currentCamera.target.x +
currentCamera.radius * sinPhi * Math.sin(currentCamera.theta),
currentCamera.target.y +
currentCamera.radius * Math.cos(currentCamera.phi),
currentCamera.target.z +
currentCamera.radius * sinPhi * Math.cos(currentCamera.theta)
);
camera.lookAt(currentCamera.target);
}
// -------------------------------------------------------------------------
// Resize, keyboard shortcuts, and render loop
// -------------------------------------------------------------------------
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
addEventListener("resize", onResize);
addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
togglePlay();
} else if (event.key.toLowerCase() === "r") {
resetSequence();
} else if (event.key === "1") {
requestManualHinge("left");
} else if (event.key === "2") {
requestManualHinge("right");
}
});
const clock = new THREE.Clock();
const projectedAnchor = new THREE.Vector3();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateSequence(dt);
updateCamera(dt);
updateUI();
for (const id of ["left", "right"]) {
const arm = arms[id];
const active =
currentHinge === id && phase === "moving";
arm.pin.material.color.setHex(
active ? 0xff7542 : 0xc5a052
);
arm.pin.material.emissive.setHex(
active ? 0x6a1d00 : 0x000000
);
arm.pin.material.emissiveIntensity = active ? 1.15 : 0;
arm.activeRing.visible = active;
if (active) {
const pulse = 1 + Math.sin(elapsed * 7) * 0.11;
arm.activeRing.scale.setScalar(pulse);
}
}
const showClearance =
bothFolded() && $("clearanceToggle").checked;
clearanceGroup.visible = showClearance;
if (showClearance) {
projectedAnchor.copy(clearanceAnchor).project(camera);
const x = (projectedAnchor.x * 0.5 + 0.5) * innerWidth;
const y = (-projectedAnchor.y * 0.5 + 0.5) * innerHeight;
clearanceLabel.style.display = "block";
clearanceLabel.style.left = `${x}px`;
clearanceLabel.style.top = `${y}px`;
} else {
clearanceLabel.style.display = "none";
}
renderer.render(scene, camera);
}
updateUI();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```