Prompt specification
Build or customize the Three.js 3D scene "Two-hinge axis alignment" 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: Two-hinge axis alignment
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-hinge-axis-alignment-x0273
Scene Description Prompt:
Compare aligned and misaligned door-hinge axes using stylized hinges and visible guides. Use Three.js from cdnjs in one HTML file.
Working Three.js Starter (two-hinge-axis-alignment-x0273):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hinge Axis Alignment Comparison</title>
<style>
:root {
--ink: #253142;
--muted: #6e7b8c;
--green: #0b9f75;
--green-soft: #e4f6ef;
--red: #df5c63;
--red-soft: #fdebed;
--panel: rgba(255, 255, 255, 0.9);
--dock-height: 112px;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eef2f5;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage {
position: absolute;
inset: 0 0 var(--dock-height) 0;
overflow: hidden;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas.dragging {
cursor: grabbing;
}
.topbar {
position: absolute;
z-index: 5;
top: 20px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
border-radius: 12px;
background: #26374b;
box-shadow: 0 7px 18px rgba(30, 46, 65, 0.18);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
border-radius: 999px;
}
.brand-mark::before {
width: 4px;
height: 23px;
left: 17px;
top: 7px;
background: #55d5ad;
}
.brand-mark::after {
width: 17px;
height: 4px;
left: 5px;
top: 17px;
background: #f0b15d;
transform: rotate(-32deg);
transform-origin: center;
}
h1 {
margin: 0;
font-size: clamp(19px, 2vw, 27px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 6px;
color: var(--muted);
font-size: 12px;
line-height: 1.4;
}
.top-actions {
pointer-events: auto;
}
.ghost-button {
border: 1px solid rgba(65, 79, 96, 0.17);
border-radius: 11px;
padding: 9px 13px;
background: rgba(255, 255, 255, 0.82);
color: #445266;
box-shadow: 0 5px 16px rgba(40, 55, 72, 0.08);
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease;
}
.ghost-button:hover {
transform: translateY(-1px);
background: #fff;
}
.panel-label {
position: absolute;
z-index: 4;
width: 260px;
padding: 13px 15px 14px;
border: 1px solid rgba(62, 77, 95, 0.1);
border-radius: 16px;
background: rgba(255, 255, 255, 0.79);
box-shadow: 0 12px 35px rgba(43, 57, 73, 0.08);
backdrop-filter: blur(12px);
pointer-events: none;
transform: translateX(-50%);
}
.eyebrow {
display: block;
margin-bottom: 5px;
color: #8a95a3;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.panel-label strong {
display: block;
font-size: 16px;
letter-spacing: -0.02em;
}
.panel-label p {
margin: 7px 0 11px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.badge {
display: inline-flex;
align-items: center;
gap: 6px;
border-radius: 999px;
padding: 5px 8px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge.green {
color: #087657;
background: var(--green-soft);
}
.badge.red {
color: #b13f47;
background: var(--red-soft);
}
.badge::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
}
.dock {
position: fixed;
z-index: 20;
left: 50%;
bottom: 14px;
width: min(900px, calc(100% - 24px));
min-height: 84px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 23px;
padding: 14px 17px;
border: 1px solid rgba(61, 76, 94, 0.12);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 16px 48px rgba(39, 53, 70, 0.15);
backdrop-filter: blur(18px);
}
.control-block {
flex: 1 1 320px;
min-width: 230px;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
}
.control-head label {
font-size: 12px;
font-weight: 750;
}
output {
min-width: 62px;
padding: 4px 8px;
border-radius: 7px;
background: #f2f4f7;
color: #465367;
font-size: 11px;
font-weight: 800;
text-align: center;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0 7px;
border-radius: 999px;
appearance: none;
background: linear-gradient(
90deg,
var(--green) 0%,
var(--green) 60%,
#e0e5eb 60%,
#e0e5eb 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #fff;
border-radius: 50%;
appearance: none;
background: var(--red);
box-shadow: 0 2px 7px rgba(38, 51, 68, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--red);
box-shadow: 0 2px 7px rgba(38, 51, 68, 0.25);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #929ca9;
font-size: 9px;
}
.legend {
display: grid;
gap: 8px;
min-width: 155px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
color: #5d6979;
font-size: 10px;
font-weight: 650;
}
.legend-line {
position: relative;
width: 23px;
height: 3px;
border-radius: 99px;
}
.legend-line::after {
content: "";
position: absolute;
top: -2px;
right: -2px;
border-width: 3px 0 3px 4px;
border-style: solid;
}
.legend-line.green {
background: var(--green);
}
.legend-line.green::after {
border-color: transparent transparent transparent var(--green);
}
.legend-line.red {
background: var(--red);
}
.legend-line.red::after {
border-color: transparent transparent transparent var(--red);
}
.dock-actions {
display: flex;
align-items: center;
gap: 10px;
}
.check {
display: flex;
align-items: center;
gap: 7px;
color: #586576;
font-size: 11px;
font-weight: 700;
white-space: nowrap;
cursor: pointer;
}
.check input {
width: 16px;
height: 16px;
accent-color: var(--green);
}
.interaction-hint {
position: absolute;
z-index: 4;
right: 22px;
bottom: 15px;
padding: 7px 10px;
border-radius: 9px;
background: rgba(255, 255, 255, 0.72);
color: #7b8796;
font-size: 9px;
pointer-events: none;
}
@media (max-width: 720px) {
:root {
--dock-height: 150px;
}
.topbar {
top: 14px;
left: 15px;
right: 15px;
}
.brand-mark {
width: 34px;
height: 34px;
}
.brand-mark::before {
left: 15px;
top: 5px;
}
.brand-mark::after {
left: 4px;
top: 15px;
}
.subtitle {
display: none;
}
.panel-label {
width: 220px;
padding: 10px 12px;
transform: none;
}
.panel-label p {
display: none;
}
.panel-label strong {
font-size: 14px;
}
.dock {
bottom: 9px;
width: calc(100% - 18px);
flex-wrap: wrap;
gap: 11px 18px;
padding: 12px 14px;
}
.control-block {
flex-basis: 100%;
min-width: 0;
}
.legend {
display: flex;
gap: 15px;
}
.dock-actions {
margin-left: auto;
}
.interaction-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="stage">
<canvas id="scene" aria-label="Interactive 3D comparison of aligned and misaligned door hinge axes"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>Hinge Axis Alignment</h1>
<div class="subtitle">
A rigid door needs every hinge pin to share one continuous vertical pivot.
</div>
</div>
</div>
<div class="top-actions">
<button class="ghost-button" id="resetButton" type="button">Reset view</button>
</div>
</header>
<section class="panel-label" id="alignedLabel">
<span class="eyebrow">Reference</span>
<strong>Aligned hinge axes</strong>
<p>All three pin centers lie on one vertical line.</p>
<span class="badge green">One shared pivot</span>
</section>
<section class="panel-label" id="misalignedLabel">
<span class="eyebrow">Comparison</span>
<strong>Misaligned hinge axes</strong>
<p>Separated pin centers prevent a clean, common swing axis.</p>
<span class="badge red" id="misalignmentStatus">60 mm spread</span>
</section>
<div class="interaction-hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</main>
<section class="dock" aria-label="Comparison controls">
<div class="control-block">
<div class="control-head">
<label for="spreadSlider">Hinge-axis separation</label>
<output id="spreadValue" for="spreadSlider">60 mm</output>
</div>
<input
id="spreadSlider"
type="range"
min="0"
max="100"
value="60"
step="1"
>
<div class="range-labels">
<span>Coincident</span>
<span>Wide offset</span>
</div>
</div>
<div class="legend" aria-label="Guide colors">
<div class="legend-item">
<span class="legend-line green"></span>
Shared hinge axis
</div>
<div class="legend-item">
<span class="legend-line red"></span>
Offset hinge axes
</div>
</div>
<div class="dock-actions">
<label class="check">
<input id="guidesToggle" type="checkbox" checked>
Guides
</label>
<button class="ghost-button" id="dockResetButton" type="button">Reset</button>
</div>
</section>
<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.querySelector("#stage");
const canvas = document.querySelector("#scene");
const alignedLabel = document.querySelector("#alignedLabel");
const misalignedLabel = document.querySelector("#misalignedLabel");
const spreadSlider = document.querySelector("#spreadSlider");
const spreadValue = document.querySelector("#spreadValue");
const misalignmentStatus = document.querySelector("#misalignmentStatus");
const guidesToggle = document.querySelector("#guidesToggle");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.autoClear = true;
const cameraA = new THREE.PerspectiveCamera(44, 1, 0.1, 100);
const cameraB = new THREE.PerspectiveCamera(44, 1, 0.1, 100);
const orbit = {
radius: 12.1,
phi: 1.245,
theta: 0.075,
target: new THREE.Vector3(0, 2.05, 0)
};
const defaultOrbit = {
radius: 12.1,
phi: 1.245,
theta: 0.075
};
const X_AXIS = new THREE.Vector3(1, 0, 0);
const HINGE_HEIGHTS = [0.72, 2.05, 3.38];
const NOMINAL_AXIS_X = -1.38;
const COLORS = {
ink: 0x293648,
frame: 0x3d4b5e,
frameDark: 0x303d4e,
door: 0x5f8794,
doorInset: 0x7399a4,
hinge: 0xe5a14c,
hingeDark: 0xb9782e,
green: 0x0baf80,
red: 0xe45d64,
base: 0xe2e7ec,
floor: 0xf1f4f6
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, accent) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const context = labelCanvas.getContext("2d");
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + width, y, x + width, y + height, radius);
ctx.arcTo(x + width, y + height, x, y + height, radius);
ctx.arcTo(x, y + height, x, y, radius);
ctx.arcTo(x, y, x + width, y, radius);
ctx.closePath();
}
context.clearRect(0, 0, 512, 128);
roundedRect(context, 9, 14, 494, 100, 38);
context.fillStyle = "rgba(255,255,255,0.94)";
context.fill();
context.lineWidth = 4;
context.strokeStyle = accent;
context.stroke();
context.fillStyle = "#2b3749";
context.font = "800 40px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 65);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sprite.scale.set(1.72, 0.43, 1);
sprite.renderOrder = 30;
return sprite;
}
function createGuide(scene, root, x, color) {
const lineMaterial = new THREE.LineDashedMaterial({
color,
dashSize: 0.13,
gapSize: 0.075,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, 0.04, 0.035),
new THREE.Vector3(x, 4.62, 0.035)
]),
lineMaterial
);
line.computeLineDistances();
line.renderOrder = 20;
root.add(line);
const arrowMaterial = new THREE.MeshBasicMaterial({
color,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.07, 0.19, 14),
arrowMaterial
);
arrow.position.set(x, 4.68, 0.035);
arrow.renderOrder = 22;
root.add(arrow);
const baseRing = new THREE.Mesh(
new THREE.TorusGeometry(0.145, 0.017, 8, 36),
arrowMaterial
);
baseRing.rotation.x = Math.PI / 2;
baseRing.position.set(x, 0.018, 0.035);
baseRing.renderOrder = 22;
root.add(baseRing);
const floorLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, 0.012, 0.12),
new THREE.Vector3(x, 0.012, 1.64)
]),
new THREE.LineDashedMaterial({
color,
dashSize: 0.1,
gapSize: 0.065,
transparent: true,
opacity: 0.55,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
floorLine.computeLineDistances();
floorLine.renderOrder = 19;
root.add(floorLine);
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 14, 14),
arrowMaterial
);
marker.position.set(x, 0.3, 0.035);
marker.renderOrder = 25;
root.add(marker);
return {
line,
arrow,
baseRing,
floorLine,
marker
};
}
function updateGuideX(guide, x) {
guide.line.geometry.setFromPoints([
new THREE.Vector3(x, 0.04, 0.035),
new THREE.Vector3(x, 4.62, 0.035)
]);
guide.line.computeLineDistances();
guide.floorLine.geometry.setFromPoints([
new THREE.Vector3(x, 0.012, 0.12),
new THREE.Vector3(x, 0.012, 1.64)
]);
guide.floorLine.computeLineDistances();
guide.arrow.position.x = x;
guide.baseRing.position.x = x;
guide.marker.position.x = x;
}
function createHinge(scene, x, y, materials) {
const group = new THREE.Group();
group.position.set(x, y, 0);
const barrel = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.55, 22),
materials.hinge
);
barrel.castShadow = true;
barrel.receiveShadow = true;
group.add(barrel);
for (const localY of [-0.18, 0.18]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.107, 0.017, 8, 28),
materials.hingeDark
);
ring.rotation.x = Math.PI / 2;
ring.position.y = localY;
ring.castShadow = true;
group.add(ring);
}
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.108, 18, 12),
materials.hinge
);
cap.position.y = 0.275;
cap.scale.y = 0.45;
cap.castShadow = true;
group.add(cap);
scene.add(group);
return group;
}
function positionConnector(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = Math.max(direction.length(), 0.001);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.scale.set(length, 0.105, 0.065);
mesh.quaternion.setFromUnitVectors(
X_AXIS,
direction.normalize()
);
}
function createDoorModel(scene) {
const materials = {
frame: new THREE.MeshStandardMaterial({
color: COLORS.frame,
roughness: 0.68,
metalness: 0.08
}),
frameDark: new THREE.MeshStandardMaterial({
color: COLORS.frameDark,
roughness: 0.75,
metalness: 0.06
}),
door: new THREE.MeshStandardMaterial({
color: COLORS.door,
roughness: 0.5,
metalness: 0.08
}),
doorInset: new THREE.MeshStandardMaterial({
color: COLORS.doorInset,
roughness: 0.62,
metalness: 0.05
}),
hinge: new THREE.MeshStandardMaterial({
color: COLORS.hinge,
roughness: 0.31,
metalness: 0.58
}),
hingeDark: new THREE.MeshStandardMaterial({
color: COLORS.hingeDark,
roughness: 0.38,
metalness: 0.5
}),
handle: new THREE.MeshStandardMaterial({
color: 0xd8dde3,
roughness: 0.25,
metalness: 0.78
})
};
addBox(
scene,
[5.55, 0.17, 3.85],
[0, -0.075, 0],
new THREE.MeshStandardMaterial({
color: COLORS.base,
roughness: 0.85,
metalness: 0.02
})
);
const plinthOutline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(5.55, 0.17, 3.85)),
new THREE.LineBasicMaterial({
color: 0xcbd3dc,
transparent: true,
opacity: 0.75
})
);
plinthOutline.position.set(0, -0.075, 0);
scene.add(plinthOutline);
addBox(
scene,
[0.24, 4.24, 0.32],
[-1.73, 2.12, -0.08],
materials.frame
);
addBox(
scene,
[0.24, 4.24, 0.32],
[1.51, 2.12, -0.08],
materials.frame
);
addBox(
scene,
[3.48, 0.24, 0.32],
[-0.11, 4.18, -0.08],
materials.frameDark
);
addBox(
scene,
[3.18, 0.075, 0.5],
[-0.08, 0.055, 0],
materials.frameDark
);
const doorPivot = new THREE.Group();
doorPivot.position.set(NOMINAL_AXIS_X, 0, 0);
doorPivot.rotation.y = -0.48;
scene.add(doorPivot);
addBox(
doorPivot,
[2.45, 3.86, 0.16],
[1.225, 2.08, 0],
materials.door
);
addBox(
doorPivot,
[2.13, 3.48, 0.035],
[1.225, 2.08, 0.095],
materials.doorInset
);
const handle = new THREE.Mesh(
new THREE.CylinderGeometry(0.042, 0.042, 0.34, 16),
materials.handle
);
handle.position.set(2.15, 1.82, 0.19);
handle.castShadow = true;
doorPivot.add(handle);
for (const handleY of [1.69, 1.95]) {
const mount = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 14, 10),
materials.handle
);
mount.position.set(2.15, handleY, 0.15);
mount.scale.z = 0.55;
doorPivot.add(mount);
}
for (const y of HINGE_HEIGHTS) {
addBox(
doorPivot,
[0.43, 0.13, 0.055],
[0.22, y, 0.125],
materials.hinge
);
addBox(
scene,
[0.36, 0.14, 0.055],
[-1.67, y, 0.115],
materials.hinge
);
}
return {
materials,
doorPivot
};
}
function createScene(kind) {
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.floor);
scene.fog = new THREE.Fog(COLORS.floor, 19, 34);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(34, 34),
new THREE.MeshStandardMaterial({
color: COLORS.floor,
roughness: 0.95,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.19;
floor.receiveShadow = true;
scene.add(floor);
const hemisphere = new THREE.HemisphereLight(
0xffffff,
0x8793a2,
2.25
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff6e8, 3.1);
sun.position.set(-5.5, 10, 7.5);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -4;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc7dcff, 1.25);
fill.position.set(6, 5, -5);
scene.add(fill);
const { materials, doorPivot } = createDoorModel(scene);
const guideRoot = new THREE.Group();
scene.add(guideRoot);
const hinges = [];
const straps = [];
const guides = [];
for (let i = 0; i < HINGE_HEIGHTS.length; i++) {
hinges.push(
createHinge(
scene,
NOMINAL_AXIS_X,
HINGE_HEIGHTS[i],
materials
)
);
straps.push({
frame: addBox(
scene,
[1, 1, 1],
[0, 0, 0],
materials.hinge
),
door: addBox(
scene,
[1, 1, 1],
[0, 0, 0],
materials.hinge
)
});
}
const data = {
scene,
kind,
doorPivot,
hinges,
straps,
guides,
guideRoot
};
if (kind === "aligned") {
guides.push(
createGuide(scene, guideRoot, NOMINAL_AXIS_X, COLORS.green)
);
const sweepPoints = [];
for (let i = 0; i <= 28; i++) {
const angle = (i / 28) * 0.92;
sweepPoints.push(
new THREE.Vector3(
NOMINAL_AXIS_X + Math.cos(angle) * 2.35,
0.012,
Math.sin(angle) * 2.35
)
);
}
const sweep = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(sweepPoints),
new THREE.LineDashedMaterial({
color: COLORS.green,
dashSize: 0.12,
gapSize: 0.075,
transparent: true,
opacity: 0.5,
depthTest: false,
depthWrite: false,
toneMapped: false
})
);
sweep.computeLineDistances();
sweep.renderOrder = 18;
guideRoot.add(sweep);
const label = makeLabel("ONE SHARED AXIS", "#0b9f75");
label.position.set(NOMINAL_AXIS_X, 4.98, 0.05);
guideRoot.add(label);
updateAlignedScene(data);
} else {
for (let i = 0; i < 3; i++) {
guides.push(
createGuide(scene, guideRoot, NOMINAL_AXIS_X, COLORS.red)
);
}
const redMaterial = new THREE.MeshBasicMaterial({
color: COLORS.red,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const spreadBar = new THREE.Mesh(
new THREE.BoxGeometry(1, 0.025, 0.025),
redMaterial
);
spreadBar.position.set(NOMINAL_AXIS_X, 4.52, 0.035);
spreadBar.renderOrder = 22;
guideRoot.add(spreadBar);
const spreadTicks = [-1, 1].map(() => {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 0.2, 0.025),
redMaterial
);
tick.position.y = 4.52;
tick.renderOrder = 22;
guideRoot.add(tick);
return tick;
});
data.spreadBar = spreadBar;
data.spreadTicks = spreadTicks;
const label = makeLabel("THREE OFFSET AXES", "#df5c63");
label.position.set(NOMINAL_AXIS_X, 4.98, 0.05);
guideRoot.add(label);
updateMisalignedScene(data, 60);
}
return data;
}
function updateConnections(data, offsets) {
data.doorPivot.updateMatrixWorld(true);
for (let i = 0; i < data.hinges.length; i++) {
const x = NOMINAL_AXIS_X + offsets[i];
const y = HINGE_HEIGHTS[i];
data.hinges[i].position.x = x;
const barrelPoint = new THREE.Vector3(x, y, 0.025);
const framePoint = new THREE.Vector3(-1.59, y, 0.06);
const doorPoint = data.doorPivot.localToWorld(
new THREE.Vector3(0.38, y, 0.14)
);
positionConnector(data.straps[i].frame, barrelPoint, framePoint);
positionConnector(data.straps[i].door, barrelPoint, doorPoint);
}
}
function updateAlignedScene(data) {
const offsets = [0, 0, 0];
updateConnections(data, offsets);
updateGuideX(data.guides[0], NOMINAL_AXIS_X);
}
function updateMisalignedScene(data, millimeters) {
const spread = millimeters / 100;
const offsets = [-spread / 2, 0, spread / 2];
updateConnections(data, offsets);
for (let i = 0; i < data.guides.length; i++) {
updateGuideX(data.guides[i], NOMINAL_AXIS_X + offsets[i]);
}
data.spreadBar.scale.x = Math.max(spread, 0.001);
data.spreadBar.visible = spread > 0.005;
data.spreadTicks[0].position.x = NOMINAL_AXIS_X - spread / 2;
data.spreadTicks[1].position.x = NOMINAL_AXIS_X + spread / 2;
data.spreadTicks.forEach((tick) => {
tick.visible = spread > 0.005;
});
}
const alignedData = createScene("aligned");
const misalignedData = createScene("misaligned");
function applyCamera() {
const sinPhi = Math.sin(orbit.phi);
const position = new THREE.Vector3(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
cameraA.position.copy(position);
cameraB.position.copy(position);
cameraA.lookAt(orbit.target);
cameraB.lookAt(orbit.target);
}
function resetCamera() {
orbit.radius = defaultOrbit.radius;
orbit.phi = defaultOrbit.phi;
orbit.theta = defaultOrbit.theta;
applyCamera();
}
function updateSpread() {
const millimeters = Number(spreadSlider.value);
spreadValue.textContent = `${millimeters} mm`;
misalignmentStatus.textContent =
millimeters === 0 ? "Axes coincident" : `${millimeters} mm spread`;
const progress = (millimeters / Number(spreadSlider.max)) * 100;
spreadSlider.style.background =
`linear-gradient(90deg, var(--green) 0%, var(--green) 60%, ` +
`#e0e5eb 60%, #e0e5eb 100%)`;
updateMisalignedScene(misalignedData, millimeters);
}
spreadSlider.addEventListener("input", updateSpread);
guidesToggle.addEventListener("change", () => {
alignedData.guideRoot.visible = guidesToggle.checked;
misalignedData.guideRoot.visible = guidesToggle.checked;
});
document.querySelector("#resetButton").addEventListener("click", resetCamera);
document.querySelector("#dockResetButton").addEventListener("click", resetCamera);
let dragging = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
orbit.theta -= dx * 0.0055;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.004,
0.72,
1.52
);
applyCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("pointerleave", (event) => {
if (dragging && event.buttons === 0) endDrag(event);
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8.4,
18
);
applyCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (["INPUT", "BUTTON"].includes(document.activeElement?.tagName)) return;
let changed = true;
switch (event.key) {
case "ArrowLeft":
orbit.theta += 0.08;
break;
case "ArrowRight":
orbit.theta -= 0.08;
break;
case "ArrowUp":
orbit.phi = Math.max(0.72, orbit.phi - 0.06);
break;
case "ArrowDown":
orbit.phi = Math.min(1.52, orbit.phi + 0.06);
break;
case "r":
case "R":
resetCamera();
break;
default:
changed = false;
}
if (changed) {
event.preventDefault();
applyCamera();
}
});
let width = 1;
let height = 1;
let stacked = false;
function positionLabels() {
if (stacked) {
alignedLabel.style.left = "16px";
alignedLabel.style.top = "65px";
misalignedLabel.style.left = "16px";
misalignedLabel.style.top = `${Math.round(height * 0.54 + 16)}px`;
} else {
alignedLabel.style.left = "25%";
alignedLabel.style.top = "78px";
misalignedLabel.style.left = "75%";
misalignedLabel.style.top = "78px";
}
}
function resize() {
width = Math.max(stage.clientWidth, 1);
height = Math.max(stage.clientHeight, 1);
stacked = width < 720;
renderer.setSize(width, height, false);
positionLabels();
}
window.addEventListener("resize", resize);
if ("ResizeObserver" in window) {
new ResizeObserver(resize).observe(stage);
}
function renderViewport(scene, camera, x, y, viewportWidth, viewportHeight) {
camera.aspect = viewportWidth / viewportHeight;
camera.updateProjectionMatrix();
renderer.setViewport(x, y, viewportWidth, viewportHeight);
renderer.setScissor(x, y, viewportWidth, viewportHeight);
renderer.render(scene, camera);
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
alignedData.guides.forEach((guide, index) => {
const phase = (elapsed * 0.29 + index * 0.21) % 1;
guide.marker.position.y = 0.25 + phase * 4.12;
const pulse = 1 + Math.sin(elapsed * 4 + index) * 0.12;
guide.marker.scale.setScalar(pulse);
});
misalignedData.guides.forEach((guide, index) => {
const phase = (elapsed * 0.29 + index * 0.21) % 1;
guide.marker.position.y = 0.25 + phase * 4.12;
const pulse = 1 + Math.sin(elapsed * 4 + index) * 0.12;
guide.marker.scale.setScalar(pulse);
});
renderer.setScissorTest(true);
if (stacked) {
const topHeight = Math.floor(height * 0.54);
renderViewport(
alignedData.scene,
cameraA,
0,
height - topHeight,
width,
topHeight
);
renderViewport(
misalignedData.scene,
cameraB,
0,
0,
width,
height - topHeight
);
} else {
const leftWidth = Math.floor(width / 2);
renderViewport(
alignedData.scene,
cameraA,
0,
0,
leftWidth,
height
);
renderViewport(
misalignedData.scene,
cameraB,
leftWidth,
0,
width - leftWidth,
height
);
}
}
updateSpread();
applyCamera();
resize();
animate();
</script>
</body>
</html>
```