Prompt specification
Build or customize the Three.js 3D scene "Order of transformations comparison" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Order of transformations comparison
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/order-of-transformations-comparison-x1746
Scene Description Prompt:
Compare stylized copies transformed by rotation then translation versus translation then rotation in one fixed coordinate frame. One HTML file using Three.js from cdnjs.
Working Three.js Starter (order-of-transformations-comparison-x1746):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Transform Order — Three.js</title>
<style>
:root {
color-scheme: dark;
--bg: #07101f;
--panel: rgba(10, 20, 36, 0.88);
--border: rgba(160, 195, 230, 0.16);
--text: #edf5ff;
--muted: #91a4bd;
--cyan: #31dfcc;
--pink: #ff7894;
--yellow: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
cursor: grab;
touch-action: none;
}
#viewport canvas.dragging {
cursor: grabbing;
}
.title {
position: fixed;
top: 24px;
left: 28px;
z-index: 5;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 5px;
font-size: clamp(24px, 3vw, 42px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 0;
max-width: 470px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
right: 20px;
width: 340px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 19px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 24px 80px rgba(0, 0, 0, 0.38),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 17px;
}
.panel-header h2 {
margin: 0;
font-size: 14px;
letter-spacing: 0.04em;
}
.frame-badge {
padding: 5px 8px;
border: 1px solid rgba(49, 223, 204, 0.25);
border-radius: 999px;
background: rgba(49, 223, 204, 0.08);
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.control {
margin-bottom: 17px;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
color: #c9d7e8;
font-size: 12px;
font-weight: 650;
}
output {
min-width: 54px;
color: var(--text);
text-align: right;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
}
select,
button {
width: 100%;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: inherit;
}
select {
padding: 9px 10px;
outline: none;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 8px 0 3px;
border-radius: 99px;
outline: none;
background: rgba(255, 255, 255, 0.12);
accent-color: var(--cyan);
cursor: pointer;
}
.xyz {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 9px;
}
.xyz .control {
margin: 0;
}
.xyz .control-label {
margin-bottom: 7px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.x-label {
color: #ff6d82;
}
.y-label {
color: #5be3a7;
}
.z-label {
color: #68aaff;
}
.formula-stack {
display: grid;
gap: 8px;
margin-top: 18px;
}
.formula {
display: grid;
grid-template-columns: 4px 1fr;
overflow: hidden;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(0, 0, 0, 0.13);
}
.formula::before {
content: "";
background: var(--formula-color);
}
.formula-content {
padding: 10px 11px;
}
.formula-title {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 5px;
color: var(--formula-color);
font-size: 11px;
font-weight: 800;
}
.formula-math {
color: #bdcadd;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.55;
}
.difference {
margin-top: 9px;
padding: 10px 11px;
border: 1px solid rgba(255, 209, 102, 0.2);
border-radius: 11px;
background: rgba(255, 209, 102, 0.055);
color: #c8d3e2;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.55;
}
.difference strong {
color: var(--yellow);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 16px;
}
button {
padding: 9px 10px;
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(160, 195, 230, 0.34);
background: rgba(255, 255, 255, 0.09);
}
button:active {
transform: translateY(1px);
}
#sweep.active {
border-color: rgba(49, 223, 204, 0.42);
background: rgba(49, 223, 204, 0.13);
color: var(--cyan);
}
.camera-help {
margin: 14px 2px 0;
color: #71849c;
font-size: 10px;
line-height: 1.55;
}
.legend {
position: fixed;
z-index: 5;
bottom: 24px;
left: 28px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: 520px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(8, 16, 29, 0.72);
color: #a9b9cd;
font-size: 10px;
backdrop-filter: blur(10px);
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--dot-color);
box-shadow: 0 0 12px var(--dot-color);
}
.scene-label {
position: fixed;
z-index: 4;
top: 0;
left: 0;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 8px;
background: rgba(5, 12, 24, 0.8);
color: var(--label-color, #ffffff);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
@media (max-width: 780px) {
.title {
top: 16px;
left: 16px;
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 47vh;
padding: 14px;
border-radius: 14px;
}
.panel-header {
margin-bottom: 11px;
}
.formula-stack,
.difference,
.camera-help {
display: none;
}
.control {
margin-bottom: 12px;
}
.legend {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="title">
<div class="eyebrow">Fixed coordinate frame</div>
<h1>Order of transforms</h1>
<p class="subtitle">
Compare rotating a stylized copy and then translating it with translating
it first and then rotating it. All operations occur in the same world frame.
</p>
</header>
<aside class="panel">
<div class="panel-header">
<h2>TRANSFORMATION CONTROLS</h2>
<span class="frame-badge">World frame</span>
</div>
<div class="control">
<label class="control-label" for="axis">
Rotation axis
<output id="axisOut">Y</output>
</label>
<select id="axis">
<option value="x">X axis</option>
<option value="y" selected>Y axis</option>
<option value="z">Z axis</option>
</select>
</div>
<div class="control">
<label class="control-label" for="angle">
Rotation angle
<output id="angleOut">55°</output>
</label>
<input id="angle" type="range" min="-180" max="180" step="1" value="55">
</div>
<div class="xyz">
<div class="control">
<label class="control-label" for="tx">
<span class="x-label">X</span>
<output id="txOut">2.40</output>
</label>
<input id="tx" type="range" min="-3" max="3" step="0.05" value="2.4">
</div>
<div class="control">
<label class="control-label" for="ty">
<span class="y-label">Y</span>
<output id="tyOut">0.50</output>
</label>
<input id="ty" type="range" min="-2" max="2.5" step="0.05" value="0.5">
</div>
<div class="control">
<label class="control-label" for="tz">
<span class="z-label">Z</span>
<output id="tzOut">-0.90</output>
</label>
<input id="tz" type="range" min="-3" max="3" step="0.05" value="-0.9">
</div>
</div>
<div class="formula-stack">
<div
class="formula"
style="--formula-color: var(--cyan)"
>
<div></div>
<div class="formula-content">
<div class="formula-title">
<span>ROTATE → TRANSLATE</span>
<span>T · R</span>
</div>
<div class="formula-math">
p′ = R p + t<br>
final center = t
</div>
</div>
</div>
<div
class="formula"
style="--formula-color: var(--pink)"
>
<div></div>
<div class="formula-content">
<div class="formula-title">
<span>TRANSLATE → ROTATE</span>
<span>R · T</span>
</div>
<div class="formula-math">
p′ = R(p + t)<br>
final center = R t
</div>
</div>
</div>
</div>
<div class="difference">
<strong>Difference:</strong> t − R t<br>
The order matters whenever R t ≠ t.
</div>
<div class="actions">
<button id="reset" type="button">Reset transforms</button>
<button id="sweep" type="button">▶ Sweep angle</button>
</div>
<p class="camera-help">
Drag: orbit · Shift/right drag: pan · Wheel/pinch: zoom ·
Double-click: reset camera
</p>
</aside>
<div class="legend">
<div class="legend-item">
<span class="dot" style="--dot-color: #31dfcc"></span>
Rotate → translate
</div>
<div class="legend-item">
<span class="dot" style="--dot-color: #ff7894"></span>
Translate → rotate
</div>
<div class="legend-item">
<span class="dot" style="--dot-color: #ffd166"></span>
Translation vector t
</div>
<div class="legend-item">
<span class="dot" style="--dot-color: #93a7bd"></span>
Original source
</div>
</div>
<div id="rtLabel" class="scene-label" style="--label-color: #31dfcc">
ROTATE → TRANSLATE
</div>
<div id="trLabel" class="scene-label" style="--label-color: #ff7894">
TRANSLATE → ROTATE
</div>
<div id="sourceLabel" class="scene-label" style="--label-color: #93a7bd">
SOURCE
</div>
<div id="vectorLabel" class="scene-label" style="--label-color: #ffd166">
t
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const viewport = $("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101f);
scene.fog = new THREE.Fog(0x07101f, 13, 29);
const camera = new THREE.PerspectiveCamera(
45,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xbadfff,
0x11182a,
2.2
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(5, 9, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x31dfcc, 16, 12, 2);
cyanLight.position.set(-4, 2, 3);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff7894, 16, 12, 2);
pinkLight.position.set(4, 1, -4);
scene.add(pinkLight);
// A single fixed world frame.
const fixedFrame = new THREE.Group();
scene.add(fixedFrame);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x0a1424,
roughness: 0.92,
metalness: 0.08,
transparent: true,
opacity: 0.88
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.2;
floor.receiveShadow = true;
fixedFrame.add(floor);
const grid = new THREE.GridHelper(
12,
24,
0x38516c,
0x1b2b40
);
grid.position.y = -2.19;
grid.material.transparent = true;
grid.material.opacity = 0.5;
fixedFrame.add(grid);
const origin = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 20, 20),
new THREE.MeshBasicMaterial({ color: 0xdceaff })
);
fixedFrame.add(origin);
function makeTextSprite(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 256, 128);
context.fillStyle = "rgba(5, 12, 24, 0.72)";
context.fillRect(18, 18, 220, 92);
context.fillStyle = color;
context.font = "800 70px ui-monospace, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 128, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
})
);
sprite.scale.set(0.48, 0.24, 1);
sprite.renderOrder = 20;
return sprite;
}
const xAxis = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(),
3.2,
0xff647c,
0.19,
0.09
);
const yAxis = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
3.2,
0x55e3a7,
0.19,
0.09
);
const zAxis = new THREE.ArrowHelper(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(),
3.2,
0x63a8ff,
0.19,
0.09
);
fixedFrame.add(xAxis, yAxis, zAxis);
const xLabel = makeTextSprite("X", "#ff647c");
xLabel.position.set(3.45, 0, 0);
const yLabel = makeTextSprite("Y", "#55e3a7");
yLabel.position.set(0, 3.42, 0);
const zLabel = makeTextSprite("Z", "#63a8ff");
zLabel.position.set(0, 0, 3.45);
fixedFrame.add(xLabel, yLabel, zLabel);
// The stylized source is assembled from Three.js core primitives.
function makeGlyph(color, ghost = false) {
const group = new THREE.Group();
const baseColor = new THREE.Color(color);
const accentColor = baseColor.clone().lerp(
new THREE.Color(0xffffff),
0.48
);
const mainMaterial = new THREE.MeshStandardMaterial({
color: baseColor,
roughness: 0.3,
metalness: 0.42,
emissive: baseColor,
emissiveIntensity: ghost ? 0.025 : 0.12,
transparent: ghost,
opacity: ghost ? 0.15 : 1,
depthWrite: !ghost
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: accentColor,
roughness: 0.24,
metalness: 0.5,
emissive: accentColor,
emissiveIntensity: ghost ? 0.02 : 0.08,
transparent: ghost,
opacity: ghost ? 0.12 : 1,
depthWrite: !ghost
});
function addMesh(geometry, material, position, rotation = null) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
if (rotation) mesh.rotation.set(...rotation);
mesh.castShadow = !ghost;
mesh.receiveShadow = !ghost;
group.add(mesh);
return mesh;
}
// Cylindrical hull aligned along +Z.
addMesh(
new THREE.CylinderGeometry(0.26, 0.26, 1.25, 28),
mainMaterial,
new THREE.Vector3(0, 0, 0),
[Math.PI / 2, 0, 0]
);
// Rounded tail.
addMesh(
new THREE.SphereGeometry(0.265, 24, 16),
mainMaterial,
new THREE.Vector3(0, 0, -0.67)
);
// Pointed nose.
addMesh(
new THREE.ConeGeometry(0.26, 0.58, 28),
mainMaterial,
new THREE.Vector3(0, 0, 0.92),
[Math.PI / 2, 0, 0]
);
// Bright equatorial ring.
addMesh(
new THREE.TorusGeometry(0.285, 0.045, 12, 32),
accentMaterial,
new THREE.Vector3(0, 0, 0.04)
);
// Side fins.
addMesh(
new THREE.BoxGeometry(0.38, 0.055, 0.38),
accentMaterial,
new THREE.Vector3(0.36, 0, -0.38),
[0, -0.22, 0]
);
addMesh(
new THREE.BoxGeometry(0.38, 0.055, 0.38),
accentMaterial,
new THREE.Vector3(-0.36, 0, -0.38),
[0, 0.22, 0]
);
// Upper fin makes the silhouette easy to recognize.
addMesh(
new THREE.BoxGeometry(0.055, 0.34, 0.42),
accentMaterial,
new THREE.Vector3(0, 0.27, -0.38)
);
// Small side marker.
addMesh(
new THREE.SphereGeometry(0.075, 16, 12),
accentMaterial,
new THREE.Vector3(0.235, 0.12, 0.22)
);
group.scale.setScalar(0.82);
return group;
}
const source = makeGlyph(0x93a7bd, true);
fixedFrame.add(source);
const RT_COLOR = 0x31dfcc;
const TR_COLOR = 0xff7894;
/*
Rotation → translation means:
p' = R p + t = T R p
In a scene graph, the outer parent is applied last.
Therefore the translation parent contains the rotation child.
*/
const rtTranslationOuter = new THREE.Group();
const rtRotationInner = new THREE.Group();
fixedFrame.add(rtTranslationOuter);
rtTranslationOuter.add(rtRotationInner);
rtRotationInner.add(makeGlyph(RT_COLOR));
/*
Translation → rotation means:
p' = R(p + t) = R T p
The rotation parent contains the translation child.
*/
const trRotationOuter = new THREE.Group();
const trTranslationInner = new THREE.Group();
fixedFrame.add(trRotationOuter);
trRotationOuter.add(trTranslationInner);
trTranslationInner.add(makeGlyph(TR_COLOR));
// Translation vector t.
const translationArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(),
1,
0xffd166,
0.17,
0.085
);
translationArrow.line.material.transparent = true;
translationArrow.line.material.opacity = 0.72;
translationArrow.cone.material.transparent = true;
translationArrow.cone.material.opacity = 0.9;
fixedFrame.add(translationArrow);
const translationTip = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 16, 12),
new THREE.MeshBasicMaterial({ color: 0xffd166 })
);
fixedFrame.add(translationTip);
function makeDynamicLine(color, opacity, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]);
const material = dashed
? new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.14,
gapSize: 0.09
})
: new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const line = new THREE.Line(geometry, material);
scene.add(line);
return line;
}
const rotatedVectorLine = makeDynamicLine(TR_COLOR, 0.5, true);
const differenceLine = makeDynamicLine(0xe6efff, 0.45, true);
const state = {
axis: "y",
angle: 55,
tx: 2.4,
ty: 0.5,
tz: -0.9
};
const axisVectors = {
x: new THREE.Vector3(1, 0, 0),
y: new THREE.Vector3(0, 1, 0),
z: new THREE.Vector3(0, 0, 1)
};
const translation = new THREE.Vector3();
const rtCenter = new THREE.Vector3();
const trCenter = new THREE.Vector3();
const rotationQuaternion = new THREE.Quaternion();
function updateLine(line, from, to) {
const positions = line.geometry.attributes.position;
positions.setXYZ(0, from.x, from.y, from.z);
positions.setXYZ(1, to.x, to.y, to.z);
positions.needsUpdate = true;
line.geometry.computeBoundingSphere();
line.computeLineDistances();
}
function updateTransforms() {
translation.set(state.tx, state.ty, state.tz);
rotationQuaternion.setFromAxisAngle(
axisVectors[state.axis],
THREE.MathUtils.degToRad(state.angle)
);
// Rotation → translation: T R.
rtTranslationOuter.position.copy(translation);
rtRotationInner.quaternion.copy(rotationQuaternion);
// Translation → rotation: R T.
trRotationOuter.quaternion.copy(rotationQuaternion);
trTranslationInner.position.copy(translation);
fixedFrame.updateMatrixWorld(true);
rtRotationInner.getWorldPosition(rtCenter);
trTranslationInner.getWorldPosition(trCenter);
const length = translation.length();
if (length > 0.001) {
translationArrow.visible = true;
translationArrow.setDirection(translation.clone().normalize());
translationArrow.setLength(length, 0.17, 0.085);
} else {
translationArrow.visible = false;
}
translationTip.position.copy(translation);
updateLine(rotatedVectorLine, new THREE.Vector3(), trCenter);
const differenceLength = rtCenter.distanceTo(trCenter);
differenceLine.visible = differenceLength > 0.005;
updateLine(differenceLine, rtCenter, trCenter);
}
function updateOutputs() {
$("axisOut").textContent = state.axis.toUpperCase();
$("angleOut").textContent = `${Math.round(state.angle)}°`;
$("txOut").textContent = state.tx.toFixed(2);
$("tyOut").textContent = state.ty.toFixed(2);
$("tzOut").textContent = state.tz.toFixed(2);
}
function readControls() {
state.axis = $("axis").value;
state.angle = Number($("angle").value);
state.tx = Number($("tx").value);
state.ty = Number($("ty").value);
state.tz = Number($("tz").value);
updateOutputs();
updateTransforms();
}
$("axis").addEventListener("change", readControls);
for (const id of ["angle", "tx", "ty", "tz"]) {
$(id).addEventListener("input", () => {
sweeping = false;
$("sweep").classList.remove("active");
$("sweep").textContent = "▶ Sweep angle";
readControls();
});
}
$("reset").addEventListener("click", () => {
sweeping = false;
$("sweep").classList.remove("active");
$("sweep").textContent = "▶ Sweep angle";
state.axis = "y";
state.angle = 55;
state.tx = 2.4;
state.ty = 0.5;
state.tz = -0.9;
$("axis").value = state.axis;
$("angle").value = state.angle;
$("tx").value = state.tx;
$("ty").value = state.ty;
$("tz").value = state.tz;
updateOutputs();
updateTransforms();
});
let sweeping = false;
$("sweep").addEventListener("click", () => {
sweeping = !sweeping;
$("sweep").classList.toggle("active", sweeping);
$("sweep").textContent = sweeping ? "Ⅱ Pause sweep" : "▶ Sweep angle";
});
// Camera controls are implemented directly, without Three.js addons.
const cameraState = {
radius: 10,
theta: 0.72,
phi: 1.08,
target: new THREE.Vector3(0, 0, 0)
};
function updateCamera() {
camera.position.setFromSphericalCoords(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.add(cameraState.target);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.radius = 10;
cameraState.theta = 0.72;
cameraState.phi = 1.08;
cameraState.target.set(0, 0, 0);
updateCamera();
}
updateCamera();
const pointers = new Map();
let interactionMode = "orbit";
let previousX = 0;
let previousY = 0;
let pinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
pinchDistance = pointerDistance();
} else {
interactionMode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
previousX = event.clientX;
previousY = event.clientY;
}
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const nextDistance = pointerDistance();
if (pinchDistance > 0 && nextDistance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (pinchDistance / nextDistance),
4,
24
);
updateCamera();
}
pinchDistance = nextDistance;
return;
}
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
if (interactionMode === "orbit") {
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.006,
0.12,
Math.PI - 0.12
);
} else {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const panScale = cameraState.radius * 0.0014;
cameraState.target.addScaledVector(
right,
-deltaX * panScale
);
cameraState.target.addScaledVector(
up,
deltaY * panScale
);
}
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
pinchDistance = 0;
}
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
previousX = remaining.x;
previousY = remaining.y;
}
if (pointers.size === 0) {
renderer.domElement.classList.remove("dragging");
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
4,
24
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// Screen-space labels.
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
const labelOffset = new THREE.Vector3(0, 0.95, 0);
placeLabel(
$("rtLabel"),
rtCenter.clone().add(labelOffset)
);
placeLabel(
$("trLabel"),
trCenter.clone().add(labelOffset)
);
placeLabel(
$("sourceLabel"),
new THREE.Vector3(0, 0.95, 0)
);
placeLabel(
$("vectorLabel"),
translation.clone().multiplyScalar(0.48).add(
new THREE.Vector3(0, 0.22, 0)
)
);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateOutputs();
updateTransforms();
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (sweeping) {
state.angle += delta * 24;
if (state.angle > 180) state.angle -= 360;
if (state.angle < -180) state.angle += 360;
$("angle").value = Math.round(state.angle);
$("angleOut").textContent = `${Math.round(state.angle)}°`;
updateTransforms();
}
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```