Prompt specification
Build or customize the Three.js 3D scene "Transform-order playground" 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: Transform-order playground
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/transform-order-playground-x1748
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing geometric transformations and their order. Let users reorder a small sequence of transforms on a marked object; display intermediate states and explain noncommutativity without relying on identical symmetric shapes. Low-poly.
Working Three.js Starter (transform-order-playground-x1748):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transform Order Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #081116;
--panel: #101b22;
--panel-2: #14232b;
--line: #29404b;
--text: #edf5f7;
--muted: #91a8b3;
--cyan: #53d7c2;
--gold: #f4c95d;
--coral: #ff7b78;
--purple: #c889ff;
--radius: 18px;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
color: var(--text);
background:
radial-gradient(circle at 10% 0%, rgba(83, 215, 194, 0.10), transparent 30rem),
radial-gradient(circle at 90% 15%, rgba(200, 137, 255, 0.09), transparent 28rem),
var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: var(--text);
}
.app {
width: min(1500px, calc(100% - 32px));
margin: 0 auto;
padding: 24px 0 42px;
}
.topbar {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
margin-bottom: 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 0.75rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 2px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(83, 215, 194, 0.6);
}
h1 {
margin: 0;
font-size: clamp(1.8rem, 4vw, 3.15rem);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 720px;
margin: 10px 0 0;
color: var(--muted);
line-height: 1.55;
}
.top-actions {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
padding-top: 4px;
}
.button {
border: 1px solid var(--line);
border-radius: 10px;
padding: 9px 13px;
background: #14232b;
cursor: pointer;
transition: border-color 150ms, background 150ms, transform 150ms;
}
.button:hover {
border-color: #4e7784;
background: #19303a;
transform: translateY(-1px);
}
.button.primary {
border-color: rgba(83, 215, 194, 0.55);
background: rgba(83, 215, 194, 0.12);
color: #bff8ed;
}
.layout {
display: grid;
grid-template-columns: 390px minmax(0, 1fr);
gap: 16px;
align-items: start;
}
.panel {
overflow: hidden;
border: 1px solid var(--line);
border-radius: var(--radius);
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.025), transparent 38%),
var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
}
.panel-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
padding: 18px 20px;
border-bottom: 1px solid var(--line);
}
.panel-header h2 {
margin: 0;
font-size: 1rem;
letter-spacing: -0.01em;
}
.panel-header p {
margin: 5px 0 0;
color: var(--muted);
font-size: 0.82rem;
line-height: 1.45;
}
.step-badge {
flex: 0 0 auto;
border: 1px solid rgba(83, 215, 194, 0.35);
border-radius: 999px;
padding: 5px 8px;
color: var(--cyan);
background: rgba(83, 215, 194, 0.08);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.1em;
}
.sequence-panel {
padding-bottom: 18px;
}
.sequence-intro {
padding: 18px 20px 0;
color: var(--muted);
font-size: 0.86rem;
line-height: 1.55;
}
.sequence-intro strong {
color: var(--text);
}
.sequence {
display: grid;
gap: 10px;
padding: 16px 16px 10px;
}
.transform-card {
position: relative;
display: grid;
grid-template-columns: 26px 42px minmax(0, 1fr) auto;
align-items: center;
gap: 10px;
min-height: 76px;
padding: 11px;
border: 1px solid #29404b;
border-left: 3px solid var(--op-color);
border-radius: 13px;
background:
linear-gradient(90deg, color-mix(in srgb, var(--op-color) 9%, transparent), transparent 58%),
#132129;
cursor: grab;
transition: border-color 150ms, opacity 150ms, transform 150ms;
}
.transform-card:hover {
border-color: #4b6b77;
transform: translateX(2px);
}
.transform-card.dragging {
opacity: 0.42;
}
.transform-card.drag-over {
border-color: var(--cyan);
box-shadow: 0 0 0 2px rgba(83, 215, 194, 0.14);
}
.drag-handle {
color: #617a85;
font-size: 1.25rem;
letter-spacing: -0.2em;
user-select: none;
}
.op-icon {
display: grid;
place-items: center;
width: 42px;
height: 42px;
border: 1px solid color-mix(in srgb, var(--op-color) 45%, transparent);
border-radius: 11px;
color: var(--op-color);
background: color-mix(in srgb, var(--op-color) 10%, transparent);
font-size: 1.35rem;
font-weight: 800;
}
.op-copy {
min-width: 0;
}
.op-copy span {
display: block;
margin-bottom: 2px;
color: #6f8994;
font-size: 0.64rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.op-copy strong {
display: block;
font-size: 0.96rem;
}
.op-copy small {
display: block;
margin-top: 3px;
overflow: hidden;
color: var(--muted);
font-size: 0.74rem;
text-overflow: ellipsis;
white-space: nowrap;
}
.order-actions {
display: grid;
gap: 3px;
}
.move-button {
width: 27px;
height: 25px;
padding: 0;
border: 1px solid #31505c;
border-radius: 7px;
background: #172a33;
cursor: pointer;
}
.move-button:hover:not(:disabled) {
border-color: var(--cyan);
color: var(--cyan);
}
.move-button:disabled {
opacity: 0.25;
cursor: default;
}
.sequence-actions {
display: flex;
gap: 8px;
padding: 4px 16px 0;
}
.sequence-actions .button {
flex: 1;
font-size: 0.78rem;
}
.note {
margin: 14px 16px 0;
padding: 11px 12px;
border: 1px solid #263d47;
border-radius: 10px;
color: #a8bdc6;
background: rgba(0, 0, 0, 0.12);
font-size: 0.76rem;
line-height: 1.5;
}
.note strong {
color: var(--text);
}
.viewer-panel {
min-width: 0;
}
.viewer-header {
align-items: center;
}
.toggle {
display: flex;
align-items: center;
gap: 9px;
color: #bdd0d7;
font-size: 0.78rem;
cursor: pointer;
white-space: nowrap;
}
.toggle input {
width: 16px;
height: 16px;
accent-color: var(--purple);
}
.main-viewport {
position: relative;
height: 510px;
overflow: hidden;
background: #0b151b;
}
.three-canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.viewport-overlay {
position: absolute;
z-index: 2;
pointer-events: none;
}
.viewport-label {
top: 14px;
left: 14px;
border: 1px solid #2c4550;
border-radius: 9px;
padding: 7px 9px;
color: #a8c0c9;
background: rgba(8, 17, 22, 0.75);
backdrop-filter: blur(8px);
font-size: 0.68rem;
font-weight: 800;
letter-spacing: 0.12em;
}
.axis-legend {
top: 15px;
right: 15px;
display: flex;
gap: 6px;
}
.axis-chip {
display: grid;
place-items: center;
width: 25px;
height: 25px;
border-radius: 7px;
color: white;
font-size: 0.7rem;
font-weight: 900;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
.axis-x {
background: #e85e68;
}
.axis-y {
background: #65bd77;
}
.axis-z {
background: #628ee8;
}
.view-legend {
left: 14px;
bottom: 14px;
display: flex;
flex-wrap: wrap;
gap: 12px;
padding: 8px 10px;
border: 1px solid #263e49;
border-radius: 9px;
background: rgba(8, 17, 22, 0.78);
backdrop-filter: blur(8px);
font-size: 0.7rem;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--cyan);
}
.legend-dot.ghost {
background: var(--purple);
}
.readout {
right: 14px;
bottom: 14px;
min-width: 160px;
padding: 8px 10px;
border: 1px solid #263e49;
border-radius: 9px;
background: rgba(8, 17, 22, 0.78);
backdrop-filter: blur(8px);
text-align: right;
}
.readout span {
display: block;
color: var(--muted);
font-size: 0.65rem;
text-transform: uppercase;
letter-spacing: 0.1em;
}
.readout b {
display: block;
margin-top: 2px;
color: #d9f7f1;
font-size: 0.76rem;
}
.action-ribbon {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
gap: 9px;
min-height: 64px;
padding: 12px 18px;
border-top: 1px solid var(--line);
background: #0d181e;
}
.ribbon-arrow {
color: #617984;
font-size: 1.1rem;
}
.ribbon-chip {
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid color-mix(in srgb, var(--op-color) 38%, transparent);
border-radius: 8px;
color: var(--op-color);
background: color-mix(in srgb, var(--op-color) 8%, transparent);
font-size: 0.75rem;
font-weight: 700;
}
.ribbon-chip i {
font-style: normal;
font-size: 1rem;
}
.stages-panel {
margin-top: 16px;
}
.stage-grid {
display: grid;
grid-template-columns: repeat(4, minmax(210px, 1fr));
gap: 12px;
padding: 14px;
overflow-x: auto;
}
.stage-card {
min-width: 210px;
overflow: hidden;
border: 1px solid #29404b;
border-radius: 13px;
background: #0d181e;
}
.stage-head {
display: flex;
align-items: center;
gap: 10px;
padding: 11px 12px;
border-bottom: 1px solid #233944;
}
.stage-number {
display: grid;
place-items: center;
width: 29px;
height: 29px;
border-radius: 8px;
color: var(--cyan);
background: rgba(83, 215, 194, 0.10);
font-size: 0.7rem;
font-weight: 900;
}
.stage-head strong {
display: block;
font-size: 0.8rem;
}
.stage-head small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 0.66rem;
}
.stage-viewport {
height: 205px;
background: #0a141a;
}
.stage-foot {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
min-height: 46px;
padding: 9px 11px;
border-top: 1px solid #233944;
}
.stage-foot code {
color: #c9dce3;
font-size: 0.68rem;
}
.stage-foot span {
color: #748e99;
font-size: 0.64rem;
white-space: nowrap;
}
.explanation {
margin-top: 16px;
padding: 20px;
}
.explanation-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.explanation h2 {
margin: 0;
font-size: 1.05rem;
}
.math-badge {
flex: 0 0 auto;
padding: 7px 10px;
border: 1px solid rgba(200, 137, 255, 0.35);
border-radius: 9px;
color: #e2c6ff;
background: rgba(200, 137, 255, 0.08);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.82rem;
}
.explanation-copy {
max-width: 950px;
margin: 11px 0 16px;
color: #afc2ca;
font-size: 0.87rem;
line-height: 1.65;
}
.explanation-copy strong {
color: var(--text);
}
.comparison-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
}
.comparison-card {
min-width: 0;
padding: 13px;
border: 1px solid #2a414b;
border-radius: 11px;
background: #0d181e;
}
.comparison-card.reverse {
border-color: rgba(200, 137, 255, 0.36);
background: rgba(200, 137, 255, 0.035);
}
.comparison-label {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: var(--muted);
font-size: 0.7rem;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.comparison-label::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
}
.comparison-card.reverse .comparison-label::before {
background: var(--purple);
}
.comparison-card code {
display: block;
overflow-wrap: anywhere;
color: #edf8fb;
font-size: 0.9rem;
}
.comparison-card small {
display: block;
margin-top: 8px;
color: #809aa5;
font-size: 0.7rem;
line-height: 1.5;
}
.difference-note {
margin-top: 10px;
padding: 10px 12px;
border-left: 3px solid var(--gold);
border-radius: 0 8px 8px 0;
color: #c6d4d9;
background: rgba(244, 201, 93, 0.06);
font-size: 0.78rem;
line-height: 1.5;
}
.footer-note {
margin-top: 12px;
color: #617b86;
font-size: 0.7rem;
text-align: center;
}
@media (max-width: 980px) {
.layout {
grid-template-columns: 1fr;
}
.sequence {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.transform-card {
grid-template-columns: 38px minmax(0, 1fr) auto;
}
.drag-handle {
display: none;
}
}
@media (max-width: 680px) {
.app {
width: min(100% - 18px, 1500px);
padding-top: 16px;
}
.topbar,
.explanation-header {
flex-direction: column;
}
.top-actions {
justify-content: flex-start;
}
.sequence {
grid-template-columns: 1fr;
}
.transform-card {
grid-template-columns: 26px 42px minmax(0, 1fr) auto;
}
.drag-handle {
display: block;
}
.main-viewport {
height: 420px;
}
.comparison-grid {
grid-template-columns: 1fr;
}
.readout {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div>
<div class="eyebrow">Transform Order Lab</div>
<h1>Same moves. Different result.</h1>
<p class="subtitle">
Reorder a short sequence of geometric transformations and inspect every intermediate state.
The asymmetric, low-poly survey craft makes orientation and noncommutativity visible.
</p>
</div>
<div class="top-actions">
<button class="button" id="resetSteps">Reset steps</button>
<button class="button" id="resetCamera">Reset camera</button>
</div>
</header>
<main class="layout">
<section class="panel sequence-panel">
<div class="panel-header">
<div>
<h2>1. Build the sequence</h2>
<p>Drag cards or use the arrow buttons.</p>
</div>
<span class="step-badge">3 STEPS</span>
</div>
<div class="sequence-intro">
The <strong>first card acts first</strong>. Every operation uses fixed world axes,
so a later turn does not rotate an earlier translation or stretch.
</div>
<div class="sequence" id="sequence" aria-label="Transformation sequence"></div>
<div class="sequence-actions">
<button class="button" id="reverseOrder">Reverse cards</button>
<button class="button primary" id="applyReversed">Use reversed order</button>
</div>
<div class="note">
<strong>Marker:</strong> the teal nose shows the front, the coral “A” patch marks the top,
and the wings are deliberately unequal. Symmetry cannot hide the difference between orders.
</div>
</section>
<section class="panel viewer-panel">
<div class="panel-header viewer-header">
<div>
<h2>2. Inspect the world</h2>
<p>Drag to orbit. Scroll to zoom. All views use the same fixed world axes.</p>
</div>
<label class="toggle">
<input type="checkbox" id="compareToggle" checked>
Overlay reversed order
</label>
</div>
<div class="main-viewport" id="mainViewport">
<div class="viewport-overlay viewport-label">FINAL STATE · WORLD VIEW</div>
<div class="viewport-overlay axis-legend">
<span class="axis-chip axis-x">X</span>
<span class="axis-chip axis-y">Y</span>
<span class="axis-chip axis-z">Z</span>
</div>
<div class="viewport-overlay view-legend">
<span class="legend-item">
<span class="legend-dot"></span>
Current order
</span>
<span class="legend-item">
<span class="legend-dot ghost"></span>
Reversed order
</span>
</div>
<div class="viewport-overlay readout">
<span>Current final position</span>
<b id="mainReadout">—</b>
</div>
</div>
<div class="action-ribbon" id="actionRibbon" aria-label="Current operation order"></div>
</section>
</main>
<section class="panel stages-panel">
<div class="panel-header">
<div>
<h2>3. Intermediate states</h2>
<p>Each small viewport applies one more card to the previous state.</p>
</div>
<span class="step-badge">CURRENT ORDER</span>
</div>
<div class="stage-grid" id="stageGrid"></div>
</section>
<section class="panel explanation">
<div class="explanation-header">
<div>
<h2>Why order changes the answer</h2>
</div>
<div class="math-badge">A then B = BA · B then A = AB</div>
</div>
<p class="explanation-copy">
<strong>Most transformations do not commute.</strong>
For example, <strong>Turn → Stretch</strong> first turns the long body onto world X and then
stretches it lengthwise. <strong>Stretch → Turn</strong> enlarges the world-X dimension before
rotating it, affecting a different part of the craft. The marker and unequal wings reveal
the difference that a symmetric object could conceal.
</p>
<div class="comparison-grid">
<div class="comparison-card">
<div class="comparison-label">Current order</div>
<code id="currentChain">—</code>
<small id="currentStats">—</small>
</div>
<div class="comparison-card reverse">
<div class="comparison-label">Reversed order</div>
<code id="reverseChain">—</code>
<small id="reverseStats">—</small>
</div>
</div>
<div class="difference-note" id="differenceNote">—</div>
</section>
<div class="footer-note">
Matrix chains are written right-to-left because the rightmost transformation acts first.
All transformations use fixed world-space axes.
</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 $ = (selector) => document.querySelector(selector);
const operations = {
turn: {
label: "Turn",
symbol: "↻",
color: "#f4c95d",
detail: "+90° around world Y",
code: "Rᵧ(90°)",
matrix: new THREE.Matrix4().makeRotationY(Math.PI / 2)
},
stretch: {
label: "Stretch",
symbol: "↔",
color: "#ff7b78",
detail: "×1.45 along world X",
code: "Sₓ(1.45)",
matrix: new THREE.Matrix4().makeScale(1.45, 1, 1)
},
slide: {
label: "Slide",
symbol: "→",
color: "#53d7c2",
detail: "+2 along world X",
code: "Tₓ(+2)",
matrix: new THREE.Matrix4().makeTranslation(2, 0, 0)
}
};
const initialSequence = ["turn", "stretch", "slide"];
let sequence = [...initialSequence];
let draggedKey = null;
function createMarkerTexture() {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 128, 128);
ctx.fillStyle = "#e85f61";
ctx.fillRect(7, 7, 114, 114);
ctx.strokeStyle = "#ffd5d1";
ctx.lineWidth = 5;
ctx.strokeRect(13, 13, 102, 102);
ctx.fillStyle = "#ffffff";
ctx.font = "900 76px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("A", 64, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const markerTexture = createMarkerTexture();
function makeCraft() {
const group = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xdce7eb,
roughness: 0.72,
metalness: 0.04,
flatShading: true
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x31434c,
roughness: 0.78,
metalness: 0.08,
flatShading: true
});
const tealMaterial = new THREE.MeshStandardMaterial({
color: 0x35cbb8,
roughness: 0.58,
metalness: 0.06,
flatShading: true
});
const coralMaterial = new THREE.MeshStandardMaterial({
color: 0xe85f61,
roughness: 0.72,
flatShading: true
});
const markerMaterial = new THREE.MeshBasicMaterial({
map: markerTexture,
transparent: true,
side: THREE.DoubleSide,
depthWrite: false
});
function addMesh(geometry, material, position, rotation = null) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
if (rotation) {
mesh.rotation.set(rotation.x, rotation.y, rotation.z);
}
mesh.castShadow = material !== markerMaterial;
mesh.receiveShadow = true;
group.add(mesh);
return mesh;
}
// Main low-poly fuselage, aligned along +Z.
addMesh(
new THREE.CylinderGeometry(0.38, 0.30, 1.35, 4, 1, false),
bodyMaterial,
new THREE.Vector3(0, 0, 0),
new THREE.Euler(Math.PI / 2, 0, 0)
);
// Teal nose identifies the front.
addMesh(
new THREE.ConeGeometry(0.39, 0.68, 4, 1, false),
tealMaterial,
new THREE.Vector3(0, 0, 1.00),
new THREE.Euler(Math.PI / 2, 0, 0)
);
// Teal band gives another rotational cue.
addMesh(
new THREE.CylinderGeometry(0.405, 0.405, 0.12, 4, 1, false),
tealMaterial,
new THREE.Vector3(0, 0, 0.47),
new THREE.Euler(Math.PI / 2, 0, 0)
);
// Tapered-looking tail made from a second low-poly cone.
addMesh(
new THREE.ConeGeometry(0.31, 0.44, 4, 1, false),
darkMaterial,
new THREE.Vector3(0, 0, -0.88),
new THREE.Euler(-Math.PI / 2, 0, 0)
);
// Deliberately unequal swept wings.
addMesh(
new THREE.BoxGeometry(1.28, 0.10, 0.43),
bodyMaterial,
new THREE.Vector3(-0.80, 0, -0.23),
new THREE.Euler(0, -0.23, 0)
);
addMesh(
new THREE.BoxGeometry(0.78, 0.10, 0.36),
darkMaterial,
new THREE.Vector3(0.56, 0, -0.24),
new THREE.Euler(0, 0.29, 0)
);
// Coral dorsal fin.
addMesh(
new THREE.BoxGeometry(0.10, 0.62, 0.50),
coralMaterial,
new THREE.Vector3(0, 0.42, -0.46)
);
// The "A" marker lies on the top surface.
const marker = addMesh(
new THREE.PlaneGeometry(0.46, 0.31),
markerMaterial,
new THREE.Vector3(0, 0.392, 0.04),
new THREE.Euler(-Math.PI / 2, 0, 0)
);
marker.renderOrder = 3;
return group;
}
function makeGhostCraft() {
const ghost = makeCraft();
ghost.traverse((object) => {
if (object.isMesh) {
object.material = new THREE.MeshBasicMaterial({
color: 0xc889ff,
transparent: true,
opacity: 0.22,
depthWrite: false,
side: THREE.DoubleSide
});
object.castShadow = false;
object.receiveShadow = false;
object.renderOrder = 2;
}
});
return ghost;
}
function createViewport(container, options = {}) {
const base = options.base ?? 4.7;
const pixelRatio = options.pixelRatio ?? 1.5;
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, pixelRatio));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.className = "three-canvas";
renderer.domElement.setAttribute("role", "img");
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D view of the marked low-poly craft"
);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b151b);
const camera = new THREE.OrthographicCamera(-5, 5, 4, -4, 0.1, 100);
camera.position.set(7.4, 6.1, 9.0);
camera.lookAt(0, 0, 0);
const hemisphere = new THREE.HemisphereLight(0xbceeff, 0x26302d, 1.55);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d6, 3.1);
keyLight.position.set(4, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(512, 512);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x75d9ff, 1.05);
rimLight.position.set(-5, 3, -4);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(12, 12),
new THREE.MeshStandardMaterial({
color: 0x0e1a21,
roughness: 0.95,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.55;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(12, 12, 0x49636e, 0x263b45);
grid.position.y = -0.535;
const gridMaterials = Array.isArray(grid.material)
? grid.material
: [grid.material];
gridMaterials.forEach((material) => {
material.transparent = true;
material.opacity = 0.48;
material.depthWrite = false;
});
scene.add(grid);
const axes = new THREE.AxesHelper(3.1);
axes.position.y = -0.51;
const axesMaterials = Array.isArray(axes.material)
? axes.material
: [axes.material];
axesMaterials.forEach((material) => {
material.transparent = true;
material.opacity = 0.7;
});
scene.add(axes);
function resize() {
const width = Math.max(1, container.clientWidth);
const height = Math.max(1, container.clientHeight);
const aspect = width / height;
renderer.setSize(width, height, false);
camera.left = -base * aspect;
camera.right = base * aspect;
camera.top = base;
camera.bottom = -base;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(container);
resize();
return {
container,
renderer,
scene,
camera,
resize
};
}
function setObjectMatrix(object, matrix) {
const position = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
matrix.decompose(position, quaternion, scale);
object.position.copy(position);
object.quaternion.copy(quaternion);
object.scale.copy(scale);
}
function computeSequence(keys) {
let net = new THREE.Matrix4();
const stages = [net.clone()];
for (const key of keys) {
// A new operation acts on the current result: Mnew = A * Mcurrent.
net = operations[key].matrix.clone().multiply(net);
stages.push(net.clone());
}
return {
final: net,
stages
};
}
function chainCode(keys) {
if (keys.length === 0) return "I";
return [...keys]
.reverse()
.map((key) => operations[key].code)
.join(" · ");
}
function signed(value, digits = 2) {
const clean = Math.abs(value) < 0.0005 ? 0 : value;
return `${clean > 0 ? "+" : ""}${clean.toFixed(digits)}`;
}
function matrixStats(matrix) {
const position = new THREE.Vector3().setFromMatrixPosition(matrix);
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
matrix.decompose(position, quaternion, scale);
const euler = new THREE.Euler().setFromQuaternion(quaternion, "YXZ");
const yaw = Math.round(THREE.MathUtils.radToDeg(euler.y));
return { position, quaternion, scale, yaw };
}
function positionText(position) {
return `(${signed(position.x)}, ${signed(position.y)}, ${signed(position.z)})`;
}
// Main world view.
const mainView = createViewport($("#mainViewport"), {
base: 4.8,
pixelRatio: 1.75
});
const currentCraft = makeCraft();
mainView.scene.add(currentCraft);
const reversedCraft = makeGhostCraft();
mainView.scene.add(reversedCraft);
function createTrajectory(color) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -0.49, 0),
new THREE.Vector3(0, -0.49, 0)
]);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.85
});
const line = new THREE.Line(geometry, material);
mainView.scene.add(line);
return line;
}
const currentTrajectory = createTrajectory(0x53d7c2);
const reversedTrajectory = createTrajectory(0xc889ff);
// Direct, dependency-free camera controls.
const cameraState = {
theta: 0.72,
phi: 1.03,
zoom: 1,
dragging: false,
previousX: 0,
previousY: 0
};
function updateMainCamera() {
const radius = 12;
const sinPhi = Math.sin(cameraState.phi);
mainView.camera.position.set(
radius * sinPhi * Math.sin(cameraState.theta),
radius * Math.cos(cameraState.phi),
radius * sinPhi * Math.cos(cameraState.theta)
);
mainView.camera.zoom = cameraState.zoom;
mainView.camera.updateProjectionMatrix();
mainView.camera.lookAt(0, 0, 0);
mainView.renderer.render(mainView.scene, mainView.camera);
}
const mainCanvas = mainView.renderer.domElement;
mainCanvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
cameraState.dragging = true;
cameraState.previousX = event.clientX;
cameraState.previousY = event.clientY;
mainCanvas.setPointerCapture(event.pointerId);
});
mainCanvas.addEventListener("pointermove", (event) => {
if (!cameraState.dragging) return;
const deltaX = event.clientX - cameraState.previousX;
const deltaY = event.clientY - cameraState.previousY;
cameraState.previousX = event.clientX;
cameraState.previousY = event.clientY;
cameraState.theta -= deltaX * 0.007;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + deltaY * 0.006,
0.48,
1.42
);
updateMainCamera();
});
function stopCameraDrag(event) {
cameraState.dragging = false;
if (mainCanvas.hasPointerCapture(event.pointerId)) {
mainCanvas.releasePointerCapture(event.pointerId);
}
}
mainCanvas.addEventListener("pointerup", stopCameraDrag);
mainCanvas.addEventListener("pointercancel", stopCameraDrag);
mainCanvas.addEventListener("contextmenu", (event) => event.preventDefault());
mainCanvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.70,
1.65
);
updateMainCamera();
},
{ passive: false }
);
updateMainCamera();
// Intermediate-state viewports.
const stageViews = [];
const stageGrid = $("#stageGrid");
for (let index = 0; index <= 3; index += 1) {
const card = document.createElement("article");
card.className = "stage-card";
card.innerHTML = `
<div class="stage-head">
<span class="stage-number">${String(index).padStart(2, "0")}</span>
<div>
<strong></strong>
<small></small>
</div>
</div>
<div class="stage-viewport"></div>
<div class="stage-foot">
<code></code>
<span></span>
</div>
`;
stageGrid.appendChild(card);
const view = createViewport(card.querySelector(".stage-viewport"), {
base: 4.35,
pixelRatio: 1.25
});
const craft = makeCraft();
view.scene.add(craft);
stageViews.push({
card,
view,
craft,
title: card.querySelector(".stage-head strong"),
subtitle: card.querySelector(".stage-head small"),
matrixCode: card.querySelector(".stage-foot code"),
position: card.querySelector(".stage-foot span")
});
}
function renderSequence() {
const sequenceElement = $("#sequence");
sequenceElement.innerHTML = sequence
.map((key, index) => {
const operation = operations[key];
return `
<div
class="transform-card"
draggable="true"
data-key="${key}"
style="--op-color:${operation.color}"
>
<span class="drag-handle" aria-hidden="true">⋮⋮</span>
<span class="op-icon">${operation.symbol}</span>
<div class="op-copy">
<span>Step ${String(index + 1).padStart(2, "0")}</span>
<strong>${operation.label}</strong>
<small>${operation.detail}</small>
</div>
<div class="order-actions">
<button
class="move-button"
data-direction="-1"
aria-label="Move ${operation.label} earlier"
${index === 0 ? "disabled" : ""}
>↑</button>
<button
class="move-button"
data-direction="1"
aria-label="Move ${operation.label} later"
${index === sequence.length - 1 ? "disabled" : ""}
>↓</button>
</div>
</div>
`;
})
.join("");
}
function updateRibbon() {
$("#actionRibbon").innerHTML = sequence
.map((key, index) => {
const operation = operations[key];
const arrow = index < sequence.length - 1
? '<span class="ribbon-arrow">→</span>'
: "";
return `
<span class="ribbon-chip" style="--op-color:${operation.color}">
<i>${operation.symbol}</i>
${operation.label}
</span>
${arrow}
`;
})
.join("");
}
function updateStages(stages) {
stageViews.forEach((stage, index) => {
const matrix = stages[index];
const stats = matrixStats(matrix);
const prefix = sequence.slice(0, index);
setObjectMatrix(stage.craft, matrix);
if (index === 0) {
stage.title.textContent = "Original";
stage.subtitle.textContent = "Identity transform";
} else {
const operation = operations[sequence[index - 1]];
stage.title.textContent = `After ${operation.label}`;
stage.subtitle.textContent = `${operation.detail}`;
}
stage.matrixCode.textContent = chainCode(prefix);
stage.position.textContent = positionText(stats.position);
stage.view.renderer.render(stage.view.scene, stage.view.camera);
});
}
function updateExplanation(currentFinal, reversedFinal) {
const current = matrixStats(currentFinal);
const reversed = matrixStats(reversedFinal);
$("#currentChain").textContent = chainCode(sequence);
$("#reverseChain").textContent = chainCode([...sequence].reverse());
$("#currentStats").textContent =
`${positionText(current.position)} · yaw ${current.yaw}° · ` +
`x-scale ${current.scale.x.toFixed(2)}`;
$("#reverseStats").textContent =
`${positionText(reversed.position)} · yaw ${reversed.yaw}° · ` +
`x-scale ${reversed.scale.x.toFixed(2)}`;
const delta = reversed.position.clone().sub(current.position);
$("#differenceNote").textContent =
`The reversed result differs by Δx ${signed(delta.x)}, ` +
`Δy ${signed(delta.y)}, Δz ${signed(delta.z)} from the current result. ` +
`Its orientation and world-axis scale also depend on the order in which the operators act.`;
$("#mainReadout").textContent =
`x ${signed(current.position.x)} · y ${signed(current.position.y)} · ` +
`z ${signed(current.position.z)}`;
}
function updateAll() {
renderSequence();
updateRibbon();
const current = computeSequence(sequence);
const reversed = computeSequence([...sequence].reverse());
setObjectMatrix(currentCraft, current.final);
setObjectMatrix(reversedCraft, reversed.final);
const compareEnabled = $("#compareToggle").checked;
reversedCraft.visible = compareEnabled;
reversedTrajectory.visible = compareEnabled;
const currentPosition = new THREE.Vector3()
.setFromMatrixPosition(current.final);
const reversedPosition = new THREE.Vector3()
.setFromMatrixPosition(reversed.final);
currentTrajectory.geometry.setFromPoints([
new THREE.Vector3(0, -0.49, 0),
new THREE.Vector3(currentPosition.x, -0.49, currentPosition.z)
]);
reversedTrajectory.geometry.setFromPoints([
new THREE.Vector3(0, -0.49, 0),
new THREE.Vector3(reversedPosition.x, -0.49, reversedPosition.z)
]);
updateStages(current.stages);
updateExplanation(current.final, reversed.final);
mainView.renderer.render(mainView.scene, mainView.camera);
}
// Reordering via the arrow buttons.
$("#sequence").addEventListener("click", (event) => {
const button = event.target.closest("button[data-direction]");
if (!button) return;
const card = button.closest(".transform-card");
const from = sequence.indexOf(card.dataset.key);
const direction = Number(button.dataset.direction);
const to = from + direction;
if (to < 0 || to >= sequence.length) return;
const [item] = sequence.splice(from, 1);
sequence.splice(to, 0, item);
updateAll();
});
// Drag-and-drop reordering.
$("#sequence").addEventListener("dragstart", (event) => {
const card = event.target.closest(".transform-card");
if (!card) return;
draggedKey = card.dataset.key;
card.classList.add("dragging");
event.dataTransfer.effectAllowed = "move";
event.dataTransfer.setData("text/plain", draggedKey);
});
$("#sequence").addEventListener("dragover", (event) => {
event.preventDefault();
event.dataTransfer.dropEffect = "move";
document.querySelectorAll(".transform-card").forEach((card) => {
card.classList.remove("drag-over");
});
const target = event.target.closest(".transform-card");
if (target && target.dataset.key !== draggedKey) {
target.classList.add("drag-over");
}
});
$("#sequence").addEventListener("drop", (event) => {
event.preventDefault();
const target = event.target.closest(".transform-card");
if (!target || !draggedKey) return;
const from = sequence.indexOf(draggedKey);
const to = sequence.indexOf(target.dataset.key);
if (from !== -1 && to !== -1 && from !== to) {
const [item] = sequence.splice(from, 1);
sequence.splice(to, 0, item);
}
draggedKey = null;
updateAll();
});
$("#sequence").addEventListener("dragend", () => {
draggedKey = null;
document.querySelectorAll(".transform-card").forEach((card) => {
card.classList.remove("dragging", "drag-over");
});
});
$("#reverseOrder").addEventListener("click", () => {
sequence.reverse();
updateAll();
});
$("#applyReversed").addEventListener("click", () => {
sequence = [...sequence].reverse();
updateAll();
});
$("#resetSteps").addEventListener("click", () => {
sequence = [...initialSequence];
updateAll();
});
$("#compareToggle").addEventListener("change", updateAll);
$("#resetCamera").addEventListener("click", () => {
cameraState.theta = 0.72;
cameraState.phi = 1.03;
cameraState.zoom = 1;
updateMainCamera();
});
updateAll();
</script>
</body>
</html>
```