Prompt specification
Build or customize the Three.js 3D scene "Coordinate-frame transformation explorer" 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: Coordinate-frame transformation explorer
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/coordinate-frame-transformation-explorer-x1692
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: coordinate-frame transformation explorer. Allow translating and rotating a local frame around fixed points; compute local and world coordinates consistently and visualize the transform without moving the underlying reference objects. Return one self-contained HTML file.
Working Three.js Starter (coordinate-frame-transformation-explorer-x1692):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Frame Lab — Coordinate Transformation Explorer</title>
<style>
:root {
color-scheme: dark;
--bg: #07101c;
--panel: rgba(11, 22, 37, 0.88);
--panel-solid: #0b1625;
--panel-soft: rgba(255, 255, 255, 0.035);
--line: rgba(163, 190, 220, 0.14);
--line-bright: rgba(163, 190, 220, 0.26);
--text: #edf5ff;
--muted: #8fa4bb;
--dim: #60758d;
--x: #ff647c;
--y: #55e3a5;
--z: #62aaff;
--gold: #f6c85f;
--cyan: #74e6ef;
--shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
--radius: 16px;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button {
color: inherit;
}
#app,
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#scene {
display: block;
outline: none;
touch-action: none;
}
.topbar {
position: fixed;
z-index: 20;
top: 0;
left: 0;
right: 0;
height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 22px;
background: linear-gradient(180deg, rgba(5, 12, 22, 0.96), rgba(5, 12, 22, 0.72));
border-bottom: 1px solid var(--line);
backdrop-filter: blur(16px);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
flex: 0 0 auto;
border: 1px solid rgba(116, 230, 239, 0.3);
border-radius: 11px;
background:
linear-gradient(90deg, transparent 48%, rgba(255, 100, 124, 0.8) 49%, rgba(255, 100, 124, 0.8) 54%, transparent 55%),
linear-gradient(0deg, transparent 48%, rgba(85, 227, 165, 0.8) 49%, rgba(85, 227, 165, 0.8) 54%, transparent 55%),
rgba(116, 230, 239, 0.05);
box-shadow: inset 0 0 18px rgba(116, 230, 239, 0.06);
}
.brand-mark::after {
content: "";
position: absolute;
left: 18px;
top: 17px;
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px rgba(116, 230, 239, 0.85);
}
.brand-copy {
min-width: 0;
}
.eyebrow {
margin: 0 0 3px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: 16px;
font-weight: 700;
letter-spacing: 0.02em;
white-space: nowrap;
}
.brand p {
margin: 2px 0 0;
color: var(--muted);
font-size: 11px;
white-space: nowrap;
}
.top-actions {
display: flex;
align-items: center;
gap: 8px;
}
.status-pill {
display: flex;
align-items: center;
gap: 8px;
height: 34px;
padding: 0 12px;
color: #b7c9db;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.025);
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--y);
box-shadow: 0 0 10px rgba(85, 227, 165, 0.8);
}
.icon-button,
.top-button {
height: 34px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
color: #b9c9da;
cursor: pointer;
transition: 160ms ease;
}
.icon-button {
min-width: 34px;
padding: 0 10px;
font-size: 11px;
font-weight: 700;
}
.top-button {
padding: 0 12px;
font-size: 11px;
font-weight: 700;
}
.icon-button:hover,
.top-button:hover,
.icon-button.active {
color: #fff;
border-color: rgba(116, 230, 239, 0.36);
background: rgba(116, 230, 239, 0.09);
}
.panel {
position: fixed;
z-index: 10;
top: 90px;
bottom: 20px;
width: 344px;
overflow: auto;
border: 1px solid var(--line);
border-radius: var(--radius);
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: rgba(143, 164, 187, 0.28) transparent;
}
.controls-panel {
left: 20px;
}
.inspector-panel {
right: 20px;
width: 336px;
bottom: auto;
max-height: calc(100vh - 110px);
}
.panel-head {
padding: 19px 19px 17px;
border-bottom: 1px solid var(--line);
}
.panel-head-row {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.panel-head h2 {
margin: 0;
font-size: 18px;
font-weight: 730;
letter-spacing: -0.02em;
}
.panel-head p {
margin: 7px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.index-badge {
display: grid;
place-items: center;
min-width: 34px;
height: 25px;
padding: 0 7px;
border: 1px solid rgba(116, 230, 239, 0.2);
border-radius: 7px;
color: var(--cyan);
background: rgba(116, 230, 239, 0.06);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.panel-section {
padding: 17px 18px;
border-bottom: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.section-title h3 {
margin: 0;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-note {
color: var(--dim);
font: 600 9px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.05em;
}
.control-row {
display: grid;
grid-template-columns: 24px minmax(80px, 1fr) 70px;
align-items: center;
gap: 9px;
min-height: 36px;
}
.control-row + .control-row {
margin-top: 6px;
}
.axis-label {
display: flex;
align-items: center;
justify-content: center;
height: 24px;
border: 1px solid color-mix(in srgb, var(--axis) 30%, transparent);
border-radius: 6px;
color: var(--axis);
background: color-mix(in srgb, var(--axis) 8%, transparent);
font: 800 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
appearance: none;
background: linear-gradient(90deg, rgba(116, 230, 239, 0.55), rgba(255, 255, 255, 0.12));
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 13px;
height: 13px;
border: 2px solid #dffaff;
border-radius: 50%;
appearance: none;
background: #163245;
box-shadow: 0 0 0 3px rgba(116, 230, 239, 0.11);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #dffaff;
border-radius: 50%;
background: #163245;
box-shadow: 0 0 0 3px rgba(116, 230, 239, 0.11);
}
.number-shell {
position: relative;
}
.number-shell input,
.coordinate-input {
width: 100%;
height: 29px;
padding: 0 25px 0 8px;
border: 1px solid var(--line);
border-radius: 7px;
outline: none;
color: #e7f1fc;
background: rgba(3, 10, 18, 0.46);
font: 600 11px ui-monospace, SFMono-Regular, Menlo, monospace;
transition: 150ms ease;
}
.number-shell input:focus,
.coordinate-input:focus,
select:focus {
border-color: rgba(116, 230, 239, 0.55);
box-shadow: 0 0 0 3px rgba(116, 230, 239, 0.07);
}
.number-shell span {
position: absolute;
top: 8px;
right: 7px;
color: var(--dim);
pointer-events: none;
font: 700 8px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.helper {
margin: 11px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.formula-mini {
margin-top: 12px;
padding: 9px 10px;
border-left: 2px solid rgba(116, 230, 239, 0.45);
color: #c5d6e7;
background: rgba(116, 230, 239, 0.035);
font: 500 10px ui-monospace, SFMono-Regular, Menlo, monospace;
line-height: 1.55;
}
.order-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
}
.order-row label {
color: var(--muted);
font-size: 10px;
}
select {
height: 30px;
padding: 0 28px 0 10px;
border: 1px solid var(--line);
border-radius: 7px;
outline: none;
color: #dfeaf6;
background: #0b1725;
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.preset-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.preset-button,
.wide-button {
min-height: 34px;
border: 1px solid var(--line);
border-radius: 8px;
color: #b8c8d8;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
font-size: 10px;
font-weight: 700;
transition: 150ms ease;
}
.preset-button:hover,
.wide-button:hover {
color: #fff;
border-color: rgba(116, 230, 239, 0.3);
background: rgba(116, 230, 239, 0.07);
}
.wide-button {
width: 100%;
margin-top: 9px;
}
.wide-button.primary {
color: var(--cyan);
border-color: rgba(116, 230, 239, 0.24);
background: rgba(116, 230, 239, 0.07);
}
.wide-button.active {
color: #06121a;
border-color: var(--cyan);
background: var(--cyan);
}
.wide-button:disabled {
opacity: 0.35;
cursor: not-allowed;
}
.inspector-tag {
display: inline-flex;
align-items: center;
height: 23px;
margin-bottom: 12px;
padding: 0 8px;
border: 1px solid rgba(116, 230, 239, 0.19);
border-radius: 999px;
color: var(--cyan);
background: rgba(116, 230, 239, 0.055);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.coordinate-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.coordinate-title span {
color: #b7c8da;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.coordinate-title small {
color: var(--dim);
font: 600 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.coordinate-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.coordinate-cell {
position: relative;
}
.coordinate-cell span {
position: absolute;
top: 7px;
left: 8px;
z-index: 1;
color: var(--axis);
font: 800 9px ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
}
.coordinate-cell input {
padding-left: 22px;
}
.world-output {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.world-value {
min-width: 0;
padding: 8px 7px;
border: 1px solid var(--line);
border-radius: 7px;
background: rgba(3, 10, 18, 0.34);
}
.world-value span {
display: block;
margin-bottom: 4px;
color: var(--axis);
font: 800 8px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.world-value output {
display: block;
overflow: hidden;
color: #dce8f5;
font: 600 10px ui-monospace, SFMono-Regular, Menlo, monospace;
text-overflow: ellipsis;
white-space: nowrap;
}
.origin-readout {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.022);
}
.origin-readout span {
color: var(--muted);
font-size: 9px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.origin-readout output {
color: #dce8f5;
font: 600 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation {
padding: 11px;
border: 1px solid rgba(116, 230, 239, 0.13);
border-radius: 9px;
background: rgba(116, 230, 239, 0.035);
color: #d6e6f4;
font: 500 10px/1.75 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation .inverse {
display: block;
margin-top: 3px;
color: #879db3;
}
.matrix-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 4px;
margin-top: 10px;
}
.matrix-grid span {
overflow: hidden;
padding: 7px 3px;
border: 1px solid rgba(163, 190, 220, 0.08);
border-radius: 5px;
color: #aebfd0;
background: rgba(3, 10, 18, 0.36);
text-align: center;
font: 500 9px ui-monospace, SFMono-Regular, Menlo, monospace;
text-overflow: ellipsis;
}
.matrix-note {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: var(--dim);
font: 600 8px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.fixed-note {
display: flex;
gap: 9px;
margin: 14px 0 0;
padding: 10px;
border: 1px solid rgba(85, 227, 165, 0.15);
border-radius: 8px;
color: #9eb3c7;
background: rgba(85, 227, 165, 0.035);
font-size: 9px;
line-height: 1.5;
}
.lock-icon {
flex: 0 0 auto;
color: var(--y);
font-size: 12px;
}
.bottom-hint {
position: fixed;
z-index: 5;
left: 382px;
bottom: 22px;
display: flex;
align-items: center;
gap: 14px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
color: #8196ab;
background: rgba(7, 16, 28, 0.66);
backdrop-filter: blur(10px);
font-size: 9px;
pointer-events: none;
}
.hint-item {
display: flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
kbd {
min-width: 23px;
padding: 3px 5px;
border: 1px solid var(--line-bright);
border-bottom-width: 2px;
border-radius: 4px;
color: #b6c7d8;
background: rgba(255, 255, 255, 0.04);
text-align: center;
font: 700 8px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.legend {
position: fixed;
z-index: 5;
right: 374px;
bottom: 22px;
display: flex;
align-items: center;
gap: 13px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
color: #8196ab;
background: rgba(7, 16, 28, 0.66);
backdrop-filter: blur(10px);
font-size: 9px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--color);
box-shadow: 0 0 8px color-mix(in srgb, var(--color) 65%, transparent);
}
#toggleControls {
display: none;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
transition: opacity 350ms ease, visibility 350ms ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
.loader {
display: flex;
align-items: center;
gap: 12px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.loader-ring {
width: 24px;
height: 24px;
border: 2px solid rgba(116, 230, 239, 0.16);
border-top-color: var(--cyan);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (max-width: 1120px) {
.inspector-panel {
width: 310px;
}
.bottom-hint {
left: 382px;
}
.legend {
display: none;
}
}
@media (max-width: 820px) {
.topbar {
height: 64px;
padding: 0 12px;
}
.brand p,
.status-pill,
#labelsBtn {
display: none;
}
.brand h1 {
font-size: 14px;
}
#toggleControls {
display: inline-block;
}
.panel {
top: 76px;
bottom: 14px;
width: min(350px, calc(100vw - 24px));
transition: transform 220ms ease, opacity 220ms ease;
}
.controls-panel {
left: 12px;
transform: translateX(calc(-100% - 24px));
opacity: 0;
pointer-events: none;
}
body.show-controls .controls-panel {
transform: translateX(0);
opacity: 1;
pointer-events: auto;
}
.inspector-panel {
right: 12px;
width: min(310px, calc(100vw - 24px));
max-height: 46vh;
}
.bottom-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<canvas id="scene" aria-label="Interactive 3D coordinate transformation scene"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<p class="eyebrow">Spatial Systems / 01</p>
<h1>Frame Lab</h1>
<p>Coordinate-frame transformation explorer</p>
</div>
</div>
<div class="top-actions">
<div class="status-pill">
<span class="status-dot"></span>
<span>World locked</span>
</div>
<button class="icon-button" id="toggleControls" title="Show controls">Controls</button>
<button class="icon-button active" id="gridBtn" title="Toggle grid">Grid</button>
<button class="icon-button" id="labelsBtn" title="Toggle labels">Labels</button>
<button class="top-button" id="cameraBtn">Reset view</button>
</div>
</header>
<aside class="panel controls-panel" id="controlsPanel">
<div class="panel-head">
<div class="panel-head-row">
<div>
<p class="eyebrow">Transform</p>
<h2>Local frame</h2>
</div>
<span class="index-badge">L</span>
</div>
<p>Move the local coordinate system around a fixed world-space pivot. Reference objects remain unchanged.</p>
</div>
<section class="panel-section">
<div class="section-title">
<h3>Translation</h3>
<span class="section-note">REFERENCE SPACE</span>
</div>
<div id="translationControls"></div>
<p class="helper">The unrotated local origin is translated before the pivot rotation is applied.</p>
<div class="formula-mini">origin = p + R · (t − p)</div>
</section>
<section class="panel-section">
<div class="section-title">
<h3>Fixed pivot</h3>
<span class="section-note">WORLD SPACE</span>
</div>
<div id="pivotControls"></div>
<p class="helper">This point stays fixed in the scene while the local frame is transformed around it.</p>
</section>
<section class="panel-section">
<div class="section-title">
<h3>Orientation</h3>
<span class="section-note">EULER</span>
</div>
<div id="rotationControls"></div>
<div class="order-row">
<label for="rotationOrder">Rotation order</label>
<select id="rotationOrder">
<option value="XYZ">XYZ</option>
<option value="ZYX">ZYX</option>
<option value="YXZ">YXZ</option>
<option value="YZX">YZX</option>
<option value="ZXY">ZXY</option>
</select>
</div>
</section>
<section class="panel-section">
<div class="section-title">
<h3>Scenarios</h3>
<span class="section-note">PRESETS</span>
</div>
<div class="preset-grid">
<button class="preset-button" data-preset="identity">Identity</button>
<button class="preset-button" data-preset="orbit">Orbit</button>
<button class="preset-button" data-preset="pose">Spatial</button>
</div>
<button class="wide-button primary" id="animateBtn">▶ Animate Y rotation</button>
<button class="wide-button" id="resetTransform">Reset transform</button>
</section>
<section class="panel-section">
<div class="section-title">
<h3>Transformation</h3>
<span class="section-note">LOCAL → WORLD</span>
</div>
<div class="equation">
w = p + R · ((l + t) − p)
<span class="inverse">l = R⁻¹ · (w − p) + p − t</span>
</div>
</section>
</aside>
<aside class="panel inspector-panel">
<div class="panel-head">
<div class="panel-head-row">
<div>
<p class="eyebrow">Inspector</p>
<h2>Coordinate readout</h2>
</div>
<span class="index-badge">XY</span>
</div>
</div>
<section class="panel-section">
<div class="inspector-tag" id="selectionTag">Custom probe</div>
<div class="coordinate-title">
<span>Local coordinates</span>
<small>EDITABLE</small>
</div>
<div class="coordinate-grid">
<label class="coordinate-cell" style="--axis: var(--x)">
<span>X</span>
<input class="coordinate-input" id="localX" type="number" step="0.1" value="1.5" aria-label="Local X coordinate">
</label>
<label class="coordinate-cell" style="--axis: var(--y)">
<span>Y</span>
<input class="coordinate-input" id="localY" type="number" step="0.1" value="0.9" aria-label="Local Y coordinate">
</label>
<label class="coordinate-cell" style="--axis: var(--z)">
<span>Z</span>
<input class="coordinate-input" id="localZ" type="number" step="0.1" value="0.4" aria-label="Local Z coordinate">
</label>
</div>
<button class="wide-button" id="pinPivotBtn" disabled>Select a reference to pin as pivot</button>
<div class="coordinate-title" style="margin-top: 16px">
<span>World coordinates</span>
<small>COMPUTED</small>
</div>
<div class="world-output">
<div class="world-value" style="--axis: var(--x)">
<span>X</span>
<output id="worldX">0.000</output>
</div>
<div class="world-value" style="--axis: var(--y)">
<span>Y</span>
<output id="worldY">0.000</output>
</div>
<div class="world-value" style="--axis: var(--z)">
<span>Z</span>
<output id="worldZ">0.000</output>
</div>
</div>
<div class="origin-readout">
<span>Local origin in world</span>
<output id="originReadout">0, 0, 0</output>
</div>
</section>
<section class="panel-section">
<div class="section-title">
<h3>Affine matrix</h3>
<span class="section-note">4 × 4</span>
</div>
<div class="equation">M = T(p) · R · T(t − p)</div>
<div class="matrix-grid" id="matrixGrid"></div>
<div class="matrix-note">
<span>Rigid / det 1</span>
<span id="matrixStatus">LIVE</span>
</div>
</section>
<section class="panel-section">
<div class="fixed-note">
<span class="lock-icon">◆</span>
<span>Fixed reference geometry is rendered in world space only. It never inherits the local frame transform.</span>
</div>
</section>
</aside>
<div class="bottom-hint">
<div class="hint-item"><kbd>Drag</kbd> orbit</div>
<div class="hint-item"><kbd>Right</kbd> pan</div>
<div class="hint-item"><kbd>Wheel</kbd> zoom</div>
<div class="hint-item"><kbd>Click</kbd> inspect reference</div>
</div>
<div class="legend">
<div class="legend-item"><span class="legend-dot" style="--color: var(--x)"></span>X</div>
<div class="legend-item"><span class="legend-dot" style="--color: var(--y)"></span>Y</div>
<div class="legend-item"><span class="legend-dot" style="--color: var(--z)"></span>Z</div>
<div class="legend-item"><span class="legend-dot" style="--color: var(--gold)"></span>Pivot</div>
<div class="legend-item"><span class="legend-dot" style="--color: var(--cyan)"></span>Probe</div>
</div>
<div id="loading">
<div class="loader"><span class="loader-ring"></span>Building frame space</div>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101c);
scene.fog = new THREE.Fog(0x07101c, 18, 42);
const camera = new THREE.PerspectiveCamera(43, window.innerWidth / window.innerHeight, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.9, 0);
const spherical = new THREE.Spherical(14.5, 1.06, 0.72);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
const hemi = new THREE.HemisphereLight(0xb8dcff, 0x182031, 1.6);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xd8eeff, 2.4);
keyLight.position.set(6, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1536, 1536);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x5dd9ff, 18, 18, 2);
rimLight.position.set(-5, 4, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffc46b, 12, 15, 2);
warmLight.position.set(5, 3, 4);
scene.add(warmLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1522,
roughness: 0.95,
metalness: 0.04
});
const ground = new THREE.Mesh(new THREE.PlaneGeometry(60, 60), groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(44, 44, 0x34516d, 0x193047);
grid.position.y = 0.005;
grid.material.transparent = true;
grid.material.opacity = 0.48;
grid.material.depthWrite = false;
scene.add(grid);
const AXIS_COLORS = {
x: 0xff647c,
y: 0x55e3a5,
z: 0x62aaff
};
function createLabel(text, color = "#dceaf7", width = 1.25, height = 0.31) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 320;
labelCanvas.height = 80;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 320, 80);
context.fillStyle = "rgba(7, 16, 28, 0.82)";
context.beginPath();
context.roundRect(5, 8, 310, 64, 16);
context.fill();
context.strokeStyle = color;
context.globalAlpha = 0.32;
context.lineWidth = 2;
context.stroke();
context.globalAlpha = 1;
context.fillStyle = color;
context.font = "700 30px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 160, 41);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
sprite.userData.isLabel = true;
return sprite;
}
function createArrow(color, length = 3.1, radius = 0.022) {
const group = new THREE.Group();
const shaftLength = length - 0.32;
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.15,
roughness: 0.42,
metalness: 0.12
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, shaftLength, 6),
material
);
shaft.position.y = shaftLength / 2;
const tip = new THREE.Mesh(
new THREE.ConeGeometry(radius * 4.2, 0.32, 6),
material
);
tip.position.y = length - 0.16;
group.add(shaft, tip);
return group;
}
function addAxis(group, axis, color, length, labelText, labelOffset) {
const arrow = createArrow(color, length);
if (axis === "x") arrow.rotation.z = -Math.PI / 2;
if (axis === "z") arrow.rotation.x = Math.PI / 2;
group.add(arrow);
const label = createLabel(labelText, `#${new THREE.Color(color).getHexString()}`, 0.95, 0.24);
label.position.copy(labelOffset);
group.add(label);
return label;
}
const worldAxes = new THREE.Group();
scene.add(worldAxes);
const worldAxisLength = 5.3;
const worldAxisMaterial = new THREE.MeshBasicMaterial({
color: 0x38506a,
transparent: true,
opacity: 0.55
});
function createMutedWorldArrow(axis) {
const group = new THREE.Group();
const shaftLength = worldAxisLength - 0.28;
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.014, 0.014, shaftLength, 5),
worldAxisMaterial
);
shaft.position.y = shaftLength / 2;
const tip = new THREE.Mesh(
new THREE.ConeGeometry(0.065, 0.28, 5),
worldAxisMaterial
);
tip.position.y = worldAxisLength - 0.14;
group.add(shaft, tip);
if (axis === "x") group.rotation.z = -Math.PI / 2;
if (axis === "z") group.rotation.x = Math.PI / 2;
worldAxes.add(group);
const label = createLabel(`W·${axis.toUpperCase()}`, "#7e93a9", 0.92, 0.23);
if (axis === "x") label.position.set(worldAxisLength + 0.25, 0, 0);
if (axis === "y") label.position.set(0, worldAxisLength + 0.25, 0);
if (axis === "z") label.position.set(0, 0, worldAxisLength + 0.25);
worldAxes.add(label);
}
createMutedWorldArrow("x");
createMutedWorldArrow("y");
createMutedWorldArrow("z");
const localFrame = new THREE.Group();
scene.add(localFrame);
addAxis(localFrame, "x", AXIS_COLORS.x, 3.15, "L·X", new THREE.Vector3(3.35, 0, 0));
addAxis(localFrame, "y", AXIS_COLORS.y, 3.15, "L·Y", new THREE.Vector3(0, 3.35, 0));
addAxis(localFrame, "z", AXIS_COLORS.z, 3.15, "L·Z", new THREE.Vector3(0, 0, 3.35));
const localOrigin = new THREE.Mesh(
new THREE.OctahedronGeometry(0.13, 0),
new THREE.MeshStandardMaterial({
color: 0xe9f8ff,
emissive: 0x74e6ef,
emissiveIntensity: 0.25,
roughness: 0.35
})
);
localFrame.add(localOrigin);
const pivotGroup = new THREE.Group();
scene.add(pivotGroup);
const pivotMaterial = new THREE.MeshStandardMaterial({
color: 0xf6c85f,
emissive: 0xf6c85f,
emissiveIntensity: 0.45,
roughness: 0.3,
metalness: 0.25
});
const pivotMesh = new THREE.Mesh(new THREE.OctahedronGeometry(0.18, 0), pivotMaterial);
pivotMesh.castShadow = true;
pivotGroup.add(pivotMesh);
const pivotRingMaterial = new THREE.MeshBasicMaterial({
color: 0xf6c85f,
transparent: true,
opacity: 0.22,
depthWrite: false
});
const pivotRingA = new THREE.Mesh(new THREE.TorusGeometry(1.05, 0.012, 3, 64), pivotRingMaterial);
pivotRingA.rotation.x = Math.PI / 2;
const pivotRingB = new THREE.Mesh(new THREE.TorusGeometry(1.05, 0.012, 3, 64), pivotRingMaterial);
pivotRingB.rotation.y = Math.PI / 2;
pivotGroup.add(pivotRingA, pivotRingB);
const pivotLabel = createLabel("PIVOT / P", "#f6c85f", 1.25, 0.29);
pivotLabel.position.set(0, 0.52, 0);
pivotGroup.add(pivotLabel);
const relationGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]);
const relationLine = new THREE.Line(
relationGeometry,
new THREE.LineDashedMaterial({
color: 0xf6c85f,
dashSize: 0.14,
gapSize: 0.09,
transparent: true,
opacity: 0.72
})
);
scene.add(relationLine);
const probe = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105, 1),
new THREE.MeshStandardMaterial({
color: 0x74e6ef,
emissive: 0x74e6ef,
emissiveIntensity: 0.8,
roughness: 0.25
})
);
probe.castShadow = true;
scene.add(probe);
const probeLabel = createLabel("PROBE / P", "#74e6ef", 1.18, 0.28);
scene.add(probeLabel);
const decompositionLines = {};
for (const axis of ["x", "y", "z"]) {
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]),
new THREE.LineBasicMaterial({
color: AXIS_COLORS[axis],
transparent: true,
opacity: 0.78
})
);
decompositionLines[axis] = line;
scene.add(line);
}
const referenceMeshes = [];
const referenceMaterials = [];
function addReference({ name, geometry, position, color, rotation = [0, 0, 0], labelOffset = 1.05 }) {
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.02,
roughness: 0.68,
metalness: 0.08,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
mesh.rotation.set(...rotation);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.referenceName = name;
mesh.userData.center = position.clone();
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 25),
new THREE.LineBasicMaterial({
color: 0xd8edff,
transparent: true,
opacity: 0.16
})
);
mesh.add(edges);
scene.add(mesh);
referenceMeshes.push(mesh);
referenceMaterials.push(material);
const marker = new THREE.Mesh(
new THREE.RingGeometry(0.23, 0.32, 24),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.3,
side: THREE.DoubleSide,
depthWrite: false
})
);
marker.rotation.x = -Math.PI / 2;
marker.position.set(position.x, 0.014, position.z);
scene.add(marker);
const stemGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(position.x, 0.02, position.z),
new THREE.Vector3(position.x, position.y, position.z)
]);
const stem = new THREE.Line(
stemGeometry,
new THREE.LineDashedMaterial({
color,
dashSize: 0.08,
gapSize: 0.07,
transparent: true,
opacity: 0.26
})
);
stem.computeLineDistances();
scene.add(stem);
const label = createLabel(name, `#${new THREE.Color(color).getHexString()}`, 0.9, 0.24);
label.position.set(position.x, position.y + labelOffset, position.z);
scene.add(label);
return mesh;
}
addReference({
name: "ANCHOR A",
geometry: new THREE.BoxGeometry(1.1, 1.1, 1.1),
position: new THREE.Vector3(-3.1, 0.55, -1.7),
color: 0xffa85c
});
addReference({
name: "ANCHOR B",
geometry: new THREE.ConeGeometry(0.78, 1.55, 4),
position: new THREE.Vector3(2.65, 0.78, -2.35),
color: 0x45d7c4,
rotation: [0, Math.PI / 4, 0],
labelOffset: 1.1
});
addReference({
name: "ANCHOR C",
geometry: new THREE.IcosahedronGeometry(0.75, 0),
position: new THREE.Vector3(3.15, 1.12, 2.45),
color: 0xa98cff,
labelOffset: 1.0
});
addReference({
name: "ANCHOR D",
geometry: new THREE.CylinderGeometry(0.48, 0.64, 1.3, 6),
position: new THREE.Vector3(-2.75, 0.65, 2.7),
color: 0x5a9df5,
labelOffset: 1.05
});
const state = {
tx: 0, ty: 0, tz: 0,
px: 0, py: 0, pz: 0,
rx: 0, ry: 0, rz: 0,
order: "XYZ"
};
const localProbe = new THREE.Vector3(1.5, 0.9, 0.4);
const rotationEuler = new THREE.Euler();
const rotationMatrix = new THREE.Matrix4();
const transformMatrix = new THREE.Matrix4();
const inverseMatrix = new THREE.Matrix4();
const axisColors = {
x: "#ff647c",
y: "#55e3a5",
z: "#62aaff"
};
function renderControlRows(containerId, keys, options) {
const container = document.querySelector(`#${containerId}`);
container.innerHTML = keys.map((key, index) => {
const axis = ["x", "y", "z"][index];
const value = state[key];
return `
<div class="control-row">
<div class="axis-label" style="--axis:${axisColors[axis]}">${axis.toUpperCase()}</div>
<input type="range" data-key="${key}" min="${options.min}" max="${options.max}" step="${options.step}" value="${value}" aria-label="${axis.toUpperCase()} ${options.name}">
<div class="number-shell">
<input type="number" data-key="${key}" min="${options.min}" max="${options.max}" step="${options.step}" value="${value}" aria-label="${axis.toUpperCase()} ${options.name}">
<span>${options.unit}</span>
</div>
</div>
`;
}).join("");
container.querySelectorAll("input[data-key]").forEach(input => {
input.addEventListener("input", () => {
const value = Number.parseFloat(input.value);
if (!Number.isFinite(value)) return;
state[input.dataset.key] = value;
syncControlInputs(input.dataset.key);
updateTransform();
});
});
}
renderControlRows("translationControls", ["tx", "ty", "tz"], {
min: -6, max: 6, step: 0.05, unit: "m", name: "translation"
});
renderControlRows("pivotControls", ["px", "py", "pz"], {
min: -6, max: 6, step: 0.05, unit: "m", name: "pivot"
});
renderControlRows("rotationControls", ["rx", "ry", "rz"], {
min: -180, max: 180, step: 1, unit: "°", name: "rotation"
});
function syncControlInputs(key) {
document.querySelectorAll(`input[data-key="${key}"]`).forEach(input => {
if (document.activeElement !== input || input.type === "range") {
input.value = Number(state[key].toFixed(3));
}
});
}
function syncAllControls() {
Object.keys(state).forEach(key => {
if (key !== "order") syncControlInputs(key);
});
document.querySelector("#rotationOrder").value = state.order;
}
function formatNumber(value) {
const rounded = Math.abs(value) < 0.0005 ? 0 : value;
return rounded.toFixed(3);
}
function setLinePoints(line, pointA, pointB) {
const attribute = line.geometry.getAttribute("position");
attribute.setXYZ(0, pointA.x, pointA.y, pointA.z);
attribute.setXYZ(1, pointB.x, pointB.y, pointB.z);
attribute.needsUpdate = true;
line.geometry.computeBoundingSphere();
}
const worldOrigin = new THREE.Vector3();
const probeWorld = new THREE.Vector3();
const tempVector = new THREE.Vector3();
const tempVectorB = new THREE.Vector3();
function updateTransform() {
const translation = new THREE.Vector3(state.tx, state.ty, state.tz);
const pivot = new THREE.Vector3(state.px, state.py, state.pz);
rotationEuler.set(
THREE.MathUtils.degToRad(state.rx),
THREE.MathUtils.degToRad(state.ry),
THREE.MathUtils.degToRad(state.rz),
state.order
);
rotationMatrix.makeRotationFromEuler(rotationEuler);
const pivotTranslation = new THREE.Matrix4().makeTranslation(pivot.x, pivot.y, pivot.z);
const preRotationTranslation = new THREE.Matrix4().makeTranslation(
translation.x - pivot.x,
translation.y - pivot.y,
translation.z - pivot.z
);
transformMatrix
.copy(pivotTranslation)
.multiply(rotationMatrix)
.multiply(preRotationTranslation);
inverseMatrix.copy(transformMatrix).invert();
worldOrigin.set(0, 0, 0).applyMatrix4(transformMatrix);
probeWorld.copy(localProbe).applyMatrix4(transformMatrix);
localFrame.position.copy(worldOrigin);
localFrame.quaternion.setFromRotationMatrix(rotationMatrix);
pivotGroup.position.copy(pivot);
setLinePoints(relationLine, pivot, worldOrigin);
relationLine.computeLineDistances();
probe.position.copy(probeWorld);
probeLabel.position.copy(probeWorld).add(new THREE.Vector3(0, 0.34, 0));
const xEnd = tempVector.set(localProbe.x, 0, 0).applyQuaternion(localFrame.quaternion).add(worldOrigin);
const yEnd = tempVectorB.set(0, localProbe.y, 0).applyQuaternion(localFrame.quaternion).add(worldOrigin);
const zEnd = new THREE.Vector3(0, 0, localProbe.z).applyQuaternion(localFrame.quaternion).add(worldOrigin);
setLinePoints(decompositionLines.x, worldOrigin, xEnd);
setLinePoints(decompositionLines.y, worldOrigin, yEnd);
setLinePoints(decompositionLines.z, worldOrigin, zEnd);
document.querySelector("#worldX").textContent = formatNumber(probeWorld.x);
document.querySelector("#worldY").textContent = formatNumber(probeWorld.y);
document.querySelector("#worldZ").textContent = formatNumber(probeWorld.z);
document.querySelector("#originReadout").textContent =
`${formatNumber(worldOrigin.x)}, ${formatNumber(worldOrigin.y)}, ${formatNumber(worldOrigin.z)}`;
const elements = transformMatrix.elements;
const matrixCells = [];
for (let row = 0; row < 4; row++) {
for (let column = 0; column < 4; column++) {
matrixCells.push(elements[column * 4 + row]);
}
}
const matrixGrid = document.querySelector("#matrixGrid");
matrixGrid.innerHTML = matrixCells.map(value =>
`<span>${Math.abs(value) < 0.0005 ? "0" : value.toFixed(2)}</span>`
).join("");
}
function syncLocalInputs() {
document.querySelector("#localX").value = Number(localProbe.x.toFixed(3));
document.querySelector("#localY").value = Number(localProbe.y.toFixed(3));
document.querySelector("#localZ").value = Number(localProbe.z.toFixed(3));
}
["localX", "localY", "localZ"].forEach((id, index) => {
document.querySelector(`#${id}`).addEventListener("input", event => {
const value = Number.parseFloat(event.target.value);
if (!Number.isFinite(value)) return;
localProbe.setComponent(index, value);
updateTransform();
});
document.querySelector(`#${id}`).addEventListener("change", () => {
syncLocalInputs();
});
});
document.querySelector("#rotationOrder").addEventListener("change", event => {
state.order = event.target.value;
updateTransform();
});
function applyPreset(name) {
Object.assign(state, {
tx: 0, ty: 0, tz: 0,
px: 0, py: 0, pz: 0,
rx: 0, ry: 0, rz: 0,
order: "XYZ"
});
if (name === "orbit") {
state.tx = 3.15;
state.ry = 58;
} else if (name === "pose") {
state.tx = 1.45;
state.ty = 0.35;
state.tz = -0.9;
state.px = 0.35;
state.py = 0.15;
state.pz = 0.2;
state.rx = 18;
state.ry = -32;
state.rz = 12;
}
syncAllControls();
updateTransform();
}
document.querySelectorAll("[data-preset]").forEach(button => {
button.addEventListener("click", () => applyPreset(button.dataset.preset));
});
document.querySelector("#resetTransform").addEventListener("click", () => applyPreset("identity"));
let animating = false;
const animateButton = document.querySelector("#animateBtn");
animateButton.addEventListener("click", () => {
animating = !animating;
animateButton.classList.toggle("active", animating);
animateButton.textContent = animating ? "Ⅱ Pause animation" : "▶ Animate Y rotation";
});
let labelsVisible = true;
document.querySelector("#labelsBtn").addEventListener("click", event => {
labelsVisible = !labelsVisible;
event.currentTarget.classList.toggle("active", labelsVisible);
scene.traverse(object => {
if (object.userData.isLabel) object.visible = labelsVisible;
});
});
document.querySelector("#gridBtn").addEventListener("click", event => {
grid.visible = !grid.visible;
event.currentTarget.classList.toggle("active", grid.visible);
});
document.querySelector("#toggleControls").addEventListener("click", () => {
document.body.classList.toggle("show-controls");
});
function resetCamera() {
cameraTarget.set(0, 0.9, 0);
spherical.set(14.5, 1.06, 0.72);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
document.querySelector("#cameraBtn").addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let selectedReference = null;
function clearSelection() {
if (!selectedReference) return;
selectedReference.material.emissiveIntensity = 0.02;
selectedReference = null;
document.querySelector("#selectionTag").textContent = "Custom probe";
document.querySelector("#pinPivotBtn").disabled = true;
document.querySelector("#pinPivotBtn").textContent = "Select a reference to pin as pivot";
}
function selectReference(mesh, hitPoint) {
clearSelection();
selectedReference = mesh;
mesh.material.emissiveIntensity = 0.24;
document.querySelector("#selectionTag").textContent = mesh.userData.referenceName;
document.querySelector("#pinPivotBtn").disabled = false;
document.querySelector("#pinPivotBtn").textContent = `Pin ${mesh.userData.referenceName.replace("ANCHOR ", "")} as pivot`;
const localHit = hitPoint.clone().applyMatrix4(inverseMatrix);
localProbe.copy(localHit);
syncLocalInputs();
updateTransform();
}
document.querySelector("#pinPivotBtn").addEventListener("click", () => {
if (!selectedReference) return;
const center = selectedReference.userData.center;
state.px = Number(center.x.toFixed(3));
state.py = Number(center.y.toFixed(3));
state.pz = Number(center.z.toFixed(3));
syncAllControls();
updateTransform();
});
function pickReference(event) {
const bounds = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y = -((event.clientY - bounds.top) / bounds.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(referenceMeshes, false);
if (intersections.length) {
selectReference(intersections[0].object, intersections[0].point);
} else {
clearSelection();
}
}
const pointerState = {
active: false,
button: 0,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false
};
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
pointerState.active = true;
pointerState.button = event.button;
pointerState.startX = pointerState.lastX = event.clientX;
pointerState.startY = pointerState.lastY = event.clientY;
pointerState.moved = false;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!pointerState.active) return;
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
if (Math.hypot(event.clientX - pointerState.startX, event.clientY - pointerState.startY) > 4) {
pointerState.moved = true;
}
if (pointerState.button === 2 || event.shiftKey) {
const distance = camera.position.distanceTo(cameraTarget);
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(camera.quaternion);
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(camera.quaternion);
const scale = distance * 0.0015;
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(up, dy * scale);
} else {
spherical.theta -= dx * 0.006;
spherical.phi -= dy * 0.006;
spherical.phi = THREE.MathUtils.clamp(spherical.phi, 0.16, Math.PI - 0.16);
}
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
});
function endPointer(event) {
if (!pointerState.active) return;
if (!pointerState.moved && pointerState.button === 0) pickReference(event);
pointerState.active = false;
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
spherical.radius = THREE.MathUtils.clamp(spherical.radius, 5.5, 30);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}, { passive: false });
window.addEventListener("keydown", event => {
if (event.target.matches("input, select")) return;
const step = event.shiftKey ? 0.25 : 0.05;
if (event.key === "ArrowLeft") state.tx -= step;
else if (event.key === "ArrowRight") state.tx += step;
else if (event.key === "ArrowUp") state.ty += step;
else if (event.key === "ArrowDown") state.ty -= step;
else if (event.key === "[") state.ry -= 5;
else if (event.key === "]") state.ry += 5;
else return;
event.preventDefault();
syncAllControls();
updateTransform();
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
syncAllControls();
updateTransform();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (animating) {
state.ry = (state.ry + delta * 18 + 180) % 360 - 180;
syncControlInputs("ry");
updateTransform();
}
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => document.querySelector("#loading").classList.add("hidden"));
</script>
</body>
</html>
```