Prompt specification
Build or customize the Three.js 3D scene "Transformation gizmos around an asymmetric object" 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: Transformation gizmos around an asymmetric object
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/transformation-gizmos-around-an-asymmetric-object-x1745
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: transformation gizmos around an asymmetric object. Build a deliberately asymmetric toy shape with local axes and separate translation, rotation, and scale handles; preserve visible landmarks so every transformation can be distinguished. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (transformation-gizmos-around-an-asymmetric-object-x1745):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>KITE-07 — Local Transform Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #080d18;
--panel: rgba(14, 22, 38, 0.82);
--panel-border: rgba(163, 191, 226, 0.16);
--text: #edf4ff;
--muted: #8d9db5;
--x: #ff6178;
--y: #73e6a0;
--z: #5ba7ff;
--amber: #ffc85a;
}
* {
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 {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 46%, transparent 30%, rgba(3, 7, 15, 0.15) 66%, rgba(3, 7, 15, 0.72) 115%),
linear-gradient(180deg, rgba(5, 9, 18, 0.18), transparent 20%, transparent 78%, rgba(5, 9, 18, 0.48));
}
.topbar {
position: fixed;
top: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 14px;
pointer-events: none;
}
.mark {
position: relative;
width: 45px;
height: 45px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 14px;
background: rgba(12, 19, 33, 0.72);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
.mark::before,
.mark::after {
content: "";
position: absolute;
left: 11px;
bottom: 11px;
height: 22px;
width: 4px;
border-radius: 4px;
transform-origin: bottom;
}
.mark::before {
background: var(--y);
transform: rotate(-24deg);
}
.mark::after {
background: var(--z);
transform: rotate(34deg) translate(7px, 2px);
}
.eyebrow {
margin: 0 0 4px;
color: #8ea0bb;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.4vw, 31px);
line-height: 1;
letter-spacing: -0.045em;
font-weight: 720;
}
h1 span {
color: #8190a7;
font-weight: 450;
}
.subtitle {
margin-top: 8px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.01em;
}
.panel {
position: fixed;
top: 22px;
right: 24px;
width: 286px;
padding: 17px;
border: 1px solid var(--panel-border);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 24px 80px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(20px) saturate(125%);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.04em;
}
.local-badge {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 8px;
border: 1px solid rgba(115, 230, 160, 0.2);
border-radius: 999px;
color: var(--y);
background: rgba(115, 230, 160, 0.07);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.local-badge i {
width: 5px;
height: 5px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.mode-list {
display: grid;
gap: 8px;
}
.mode {
width: 100%;
display: grid;
grid-template-columns: 34px 1fr auto;
align-items: center;
gap: 10px;
padding: 10px;
color: #aab7ca;
border: 1px solid transparent;
border-radius: 13px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
text-align: left;
transition: 160ms ease;
}
.mode:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.055);
border-color: rgba(255, 255, 255, 0.08);
}
.mode.active {
color: var(--text);
border-color: rgba(116, 159, 213, 0.28);
background: linear-gradient(100deg, rgba(91, 167, 255, 0.14), rgba(255, 255, 255, 0.035));
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.025);
}
.mode-icon {
width: 34px;
height: 34px;
display: grid;
place-items: center;
border-radius: 10px;
color: #dce9ff;
background: rgba(255, 255, 255, 0.055);
font-size: 20px;
font-weight: 400;
}
.mode.active .mode-icon {
color: var(--z);
background: rgba(91, 167, 255, 0.12);
}
.mode-copy {
display: flex;
flex-direction: column;
gap: 2px;
}
.mode-copy b {
font-size: 12px;
font-weight: 680;
}
.mode-copy small {
color: #718198;
font-size: 10px;
}
kbd {
min-width: 23px;
padding: 4px 6px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-bottom-color: rgba(255, 255, 255, 0.2);
border-radius: 6px;
color: #8ea0ba;
background: rgba(0, 0, 0, 0.18);
font-family: inherit;
font-size: 9px;
font-weight: 700;
text-align: center;
}
.panel-divider {
height: 1px;
margin: 15px 0;
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.11), transparent);
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
color: #9eacc0;
font-size: 11px;
}
.toggle {
position: relative;
width: 35px;
height: 20px;
appearance: none;
border: 0;
border-radius: 999px;
background: #283348;
cursor: pointer;
transition: 160ms ease;
}
.toggle::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 14px;
height: 14px;
border-radius: 50%;
background: #dce7f8;
transition: 160ms ease;
}
.toggle:checked {
background: #3d8bf1;
}
.toggle:checked::after {
transform: translateX(15px);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
.small-button {
padding: 9px 10px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 10px;
color: #aebbd0;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
font-size: 10px;
font-weight: 650;
transition: 150ms ease;
}
.small-button:hover {
color: white;
border-color: rgba(255, 255, 255, 0.18);
background: rgba(255, 255, 255, 0.065);
}
.stats {
position: fixed;
left: 24px;
bottom: 24px;
width: 286px;
padding: 15px 17px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: rgba(12, 19, 33, 0.76);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
pointer-events: none;
}
.object-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.object-name {
display: flex;
align-items: center;
gap: 9px;
font-size: 12px;
font-weight: 720;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 200, 90, 0.7);
}
.object-id {
color: #62738d;
font-size: 9px;
letter-spacing: 0.12em;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.stat {
min-width: 0;
padding: 8px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.stat-label {
margin-bottom: 4px;
color: #687991;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
}
.stat-value {
overflow: hidden;
color: #dce7f6;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.axis-x {
color: var(--x);
}
.axis-y {
color: var(--y);
}
.axis-z {
color: var(--z);
}
.shape-key {
position: fixed;
left: 50%;
bottom: 25px;
display: flex;
gap: 18px;
padding: 10px 15px;
transform: translateX(-50%);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
color: #75859c;
background: rgba(10, 16, 28, 0.66);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.09em;
backdrop-filter: blur(15px);
pointer-events: none;
}
.shape-key span {
display: flex;
align-items: center;
gap: 7px;
}
.shape-symbol {
display: inline-block;
width: 15px;
height: 15px;
color: #c9d9ef;
font-size: 15px;
line-height: 14px;
text-align: center;
}
.pointer-readout {
position: fixed;
right: 24px;
bottom: 24px;
max-width: 286px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
color: #8091a9;
background: rgba(10, 16, 28, 0.7);
font-size: 10px;
backdrop-filter: blur(15px);
pointer-events: none;
}
@media (max-width: 760px) {
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: 225px;
padding: 12px;
}
.topbar {
top: 14px;
left: 14px;
}
.subtitle,
.stats,
.shape-key {
display: none;
}
.pointer-readout {
right: auto;
left: 14px;
bottom: 16px;
max-width: calc(100vw - 270px);
}
.mode-copy small,
.panel-divider,
.toggle-row {
display: none;
}
.mode-list {
gap: 5px;
}
.mode {
grid-template-columns: 28px 1fr auto;
padding: 7px;
}
.mode-icon {
width: 28px;
height: 28px;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive Three.js transform gizmo scene"></main>
<div class="vignette"></div>
<header class="topbar">
<div class="mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Local Transform Lab</p>
<h1>KITE–07 <span>/ asymmetric toy</span></h1>
<div class="subtitle">Cone-tipped arrows move · rings rotate · cube-tipped axes scale</div>
</div>
</header>
<aside class="panel">
<div class="panel-heading">
<strong>TRANSFORM HANDLES</strong>
<span class="local-badge"><i></i>LOCAL XYZ</span>
</div>
<div class="mode-list">
<button class="mode active" data-mode="translate" aria-pressed="true">
<span class="mode-icon">↗</span>
<span class="mode-copy"><b>Translate</b><small>Constrained axis arrows</small></span>
<kbd>W</kbd>
</button>
<button class="mode" data-mode="rotate" aria-pressed="false">
<span class="mode-icon">↻</span>
<span class="mode-copy"><b>Rotate</b><small>Local axis rings</small></span>
<kbd>E</kbd>
</button>
<button class="mode" data-mode="scale" aria-pressed="false">
<span class="mode-icon">◇</span>
<span class="mode-copy"><b>Scale</b><small>Cube endpoint handles</small></span>
<kbd>R</kbd>
</button>
</div>
<div class="panel-divider"></div>
<label class="toggle-row">
Show all guide families
<input id="allGuides" class="toggle" type="checkbox" checked />
</label>
<div class="button-row">
<button id="resetButton" class="small-button">Reset object</button>
<button id="focusButton" class="small-button">Reframe view</button>
</div>
</aside>
<section class="stats" aria-label="Object transform values">
<div class="object-row">
<div class="object-name"><i class="status-dot"></i>KITE–07</div>
<div class="object-id">LOCAL SPACE</div>
</div>
<div class="stats-grid">
<div class="stat">
<div class="stat-label axis-x">POSITION X</div>
<div id="posX" class="stat-value">0.00</div>
</div>
<div class="stat">
<div class="stat-label axis-y">POSITION Y</div>
<div id="posY" class="stat-value">0.00</div>
</div>
<div class="stat">
<div class="stat-label axis-z">POSITION Z</div>
<div id="posZ" class="stat-value">0.00</div>
</div>
<div class="stat">
<div class="stat-label axis-x">ROT X</div>
<div id="rotX" class="stat-value">0.0°</div>
</div>
<div class="stat">
<div class="stat-label axis-y">ROT Y</div>
<div id="rotY" class="stat-value">0.0°</div>
</div>
<div class="stat">
<div class="stat-label axis-z">ROT Z</div>
<div id="rotZ" class="stat-value">0.0°</div>
</div>
<div class="stat">
<div class="stat-label axis-x">SCALE X</div>
<div id="scaleX" class="stat-value">1.00</div>
</div>
<div class="stat">
<div class="stat-label axis-y">SCALE Y</div>
<div id="scaleY" class="stat-value">1.00</div>
</div>
<div class="stat">
<div class="stat-label axis-z">SCALE Z</div>
<div id="scaleZ" class="stat-value">1.00</div>
</div>
</div>
</section>
<div class="shape-key" aria-hidden="true">
<span><i class="shape-symbol">→</i>TRANSLATE</span>
<span><i class="shape-symbol">◯</i>ROTATE</span>
<span><i class="shape-symbol">■</i>SCALE</span>
</div>
<div id="pointerReadout" class="pointer-readout">Drag empty space to orbit · wheel to zoom</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const pointerReadout = document.getElementById("pointerReadout");
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute("aria-label", "Interactive asymmetric toy and local transform gizmos");
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080d18);
scene.fog = new THREE.Fog(0x080d18, 9, 23);
const camera = new THREE.PerspectiveCamera(
50,
window.innerWidth / window.innerHeight,
0.1,
100
);
const view = {
theta: 0.66,
phi: 1.18,
radius: 7.1,
target: new THREE.Vector3(0, 1.38, 0)
};
function updateCamera() {
camera.position.setFromSpherical(
new THREE.Spherical(view.radius, view.phi, view.theta)
).add(view.target);
camera.lookAt(view.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc9e8ff, 0x182036, 2.35);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d7, 4.3);
keyLight.position.set(4.5, 7.5, 5.2);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5c9fff, 2.3);
rimLight.position.set(-4, 3.2, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8b52, 13, 8, 2);
warmLight.position.set(-2.6, 2.8, 3.5);
scene.add(warmLight);
// Low-poly display plinth.
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x111a2b,
roughness: 0.88,
metalness: 0.12
});
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(2.85, 3.02, 0.22, 48),
platformMaterial
);
platform.position.y = -0.12;
platform.receiveShadow = true;
scene.add(platform);
const grid = new THREE.GridHelper(8, 20, 0x536b8d, 0x263650);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.19;
scene.add(grid);
for (const radius of [2.15, 2.63]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.009, 4, 96),
new THREE.MeshBasicMaterial({
color: radius === 2.63 ? 0x52719b : 0x304866,
transparent: true,
opacity: radius === 2.63 ? 0.28 : 0.2
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.015;
scene.add(ring);
}
// Asymmetric toy object. Its landmarks remain attached under all transforms.
const toy = new THREE.Group();
const initialPosition = new THREE.Vector3(0, 1.52, 0);
const initialQuaternion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(-0.035, 0.28, 0)
);
const initialScale = new THREE.Vector3(1, 1, 1);
toy.position.copy(initialPosition);
toy.quaternion.copy(initialQuaternion);
toy.scale.copy(initialScale);
scene.add(toy);
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.04,
flatShading: true,
...options
});
const hullMaterial = standard(0xe8dfc9);
const noseMaterial = standard(0xf1b84c, { roughness: 0.62 });
const tealMaterial = standard(0x34c3ad, { roughness: 0.58 });
const coralMaterial = standard(0xf26778, { roughness: 0.64 });
const blueMaterial = standard(0x416ed8, { roughness: 0.52 });
const darkMaterial = standard(0x17243a, { roughness: 0.48, metalness: 0.18 });
const eyeMaterial = standard(0x102033, { roughness: 0.2, metalness: 0.35 });
const flameMaterial = standard(0xff873c, {
emissive: 0xff4c13,
emissiveIntensity: 0.7,
roughness: 0.48
});
function addToyMesh(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 = true;
mesh.receiveShadow = true;
toy.add(mesh);
return mesh;
}
addToyMesh(
new THREE.CylinderGeometry(0.48, 0.6, 1.12, 6, 1, false),
hullMaterial,
new THREE.Vector3(0, 0, 0),
new THREE.Euler(0, Math.PI / 6, 0)
);
addToyMesh(
new THREE.ConeGeometry(0.5, 0.7, 4, 1, false),
noseMaterial,
new THREE.Vector3(0, 0.86, 0),
new THREE.Euler(0, Math.PI / 4, 0)
);
addToyMesh(
new THREE.CylinderGeometry(0.41, 0.49, 0.3, 6),
blueMaterial,
new THREE.Vector3(0, -0.68, 0),
new THREE.Euler(0, Math.PI / 6, 0)
);
addToyMesh(
new THREE.CylinderGeometry(0.28, 0.34, 0.22, 8),
darkMaterial,
new THREE.Vector3(0, -0.91, 0)
);
addToyMesh(
new THREE.ConeGeometry(0.22, 0.42, 5),
flameMaterial,
new THREE.Vector3(0, -1.2, 0),
new THREE.Euler(0, 0, Math.PI)
);
// A triangular teal fin on the left: the strongest asymmetry landmark.
const finGeometry = new THREE.BufferGeometry();
finGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
-0.40, 0.32, 0.03,
-0.98, -0.31, 0.03,
-0.42, -0.43, 0.03,
-0.40, 0.32, 0.18,
-0.98, -0.31, 0.18,
-0.42, -0.43, 0.18
], 3)
);
finGeometry.setIndex([
0, 1, 2, 3, 5, 4,
0, 3, 4, 0, 4, 1,
1, 4, 5, 1, 5, 2,
2, 5, 3, 2, 3, 0
]);
const flatFinGeometry = finGeometry.toNonIndexed();
flatFinGeometry.computeVertexNormals();
addToyMesh(flatFinGeometry, tealMaterial, new THREE.Vector3(0, 0, 0));
addToyMesh(
new THREE.BoxGeometry(0.27, 0.48, 0.19),
coralMaterial,
new THREE.Vector3(0.57, 0.16, 0.01),
new THREE.Euler(0.05, 0, -0.2)
);
addToyMesh(
new THREE.BoxGeometry(0.15, 0.46, 0.065),
tealMaterial,
new THREE.Vector3(-0.14, -0.17, 0.49)
);
const eye = addToyMesh(
new THREE.SphereGeometry(0.145, 12, 8),
eyeMaterial,
new THREE.Vector3(0.1, 0.27, 0.505)
);
eye.scale.z = 0.42;
addToyMesh(
new THREE.BoxGeometry(0.045, 0.055, 0.025),
standard(0xffffff, { roughness: 0.25 }),
new THREE.Vector3(0.055, 0.31, 0.57)
);
addToyMesh(
new THREE.CylinderGeometry(0.1, 0.1, 0.045, 10),
coralMaterial,
new THREE.Vector3(0.34, 0.01, 0.43),
new THREE.Euler(Math.PI / 2, 0, 0)
);
addToyMesh(
new THREE.CylinderGeometry(0.024, 0.024, 0.36, 6),
darkMaterial,
new THREE.Vector3(-0.31, 0.98, -0.18)
);
addToyMesh(
new THREE.OctahedronGeometry(0.085, 0),
coralMaterial,
new THREE.Vector3(-0.31, 1.18, -0.18)
);
// Distant low-poly crystals add depth without competing with the toy.
const decorations = [];
const decorationGeometry = new THREE.TetrahedronGeometry(0.075, 0);
const decorationMaterial = new THREE.MeshStandardMaterial({
color: 0x29415f,
emissive: 0x102239,
emissiveIntensity: 0.8,
roughness: 0.8,
flatShading: true
});
for (let i = 0; i < 18; i++) {
const angle = (i / 18) * Math.PI * 2 + Math.sin(i * 3.1) * 0.25;
const radius = 4.2 + (i % 4) * 0.55;
const mesh = new THREE.Mesh(decorationGeometry, decorationMaterial);
mesh.position.set(
Math.cos(angle) * radius,
0.45 + ((i * 17) % 11) * 0.27,
Math.sin(angle) * radius
);
mesh.rotation.set(i * 0.4, i * 0.71, i * 0.2);
mesh.userData.baseY = mesh.position.y;
mesh.userData.phase = i * 0.61;
scene.add(mesh);
decorations.push(mesh);
}
// Gizmo construction.
const AXIS_COLORS = {
x: 0xff6178,
y: 0x73e6a0,
z: 0x5ba7ff
};
const AXIS_VECTORS = {
x: new THREE.Vector3(1, 0, 0),
y: new THREE.Vector3(0, 1, 0),
z: new THREE.Vector3(0, 0, 1)
};
function gizmoMaterial(color, opacity = 1) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthTest: false,
depthWrite: false,
toneMapped: false
});
material.userData.baseOpacity = opacity;
return material;
}
function markHandle(mesh, action, axis = null, sign = 0) {
mesh.userData.action = action;
mesh.userData.axis = axis;
mesh.userData.sign = sign;
mesh.renderOrder = 20;
return mesh;
}
function makeLabel(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.beginPath();
context.arc(64, 64, 43, 0, Math.PI * 2);
context.fillStyle = "rgba(8, 14, 26, 0.92)";
context.fill();
context.lineWidth = 5;
context.strokeStyle = `#${new THREE.Color(color).getHexString()}`;
context.stroke();
context.fillStyle = `#${new THREE.Color(color).getHexString()}`;
context.font = "700 48px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 64, 67);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
material.userData.baseOpacity = 1;
const sprite = new THREE.Sprite(material);
sprite.scale.set(0.23, 0.23, 1);
sprite.renderOrder = 30;
return sprite;
}
const gizmoRoot = new THREE.Group();
const translateGizmo = new THREE.Group();
const rotateGizmo = new THREE.Group();
const scaleGizmo = new THREE.Group();
gizmoRoot.add(translateGizmo, rotateGizmo, scaleGizmo);
scene.add(gizmoRoot);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const Z_AXIS = new THREE.Vector3(0, 0, 1);
function orientAlongAxis(mesh, axis) {
mesh.quaternion.setFromUnitVectors(Y_AXIS, axis);
}
// Translation: narrow shafts with cone tips.
for (const axisName of ["x", "y", "z"]) {
const axis = AXIS_VECTORS[axisName];
const color = AXIS_COLORS[axisName];
const shaft = markHandle(
new THREE.Mesh(
new THREE.CylinderGeometry(0.026, 0.026, 0.79, 8),
gizmoMaterial(color, 0.95)
),
"translate",
axisName
);
shaft.position.copy(axis).multiplyScalar(0.67);
orientAlongAxis(shaft, axis);
translateGizmo.add(shaft);
const cone = markHandle(
new THREE.Mesh(
new THREE.ConeGeometry(0.095, 0.25, 12),
gizmoMaterial(color, 1)
),
"translate",
axisName
);
cone.position.copy(axis).multiplyScalar(1.19);
orientAlongAxis(cone, axis);
translateGizmo.add(cone);
const label = makeLabel(axisName.toUpperCase(), color);
label.position.copy(axis).multiplyScalar(1.47);
translateGizmo.add(label);
}
const origin = new THREE.Mesh(
new THREE.OctahedronGeometry(0.105, 0),
gizmoMaterial(0xf2f7ff, 1)
);
origin.renderOrder = 25;
translateGizmo.add(origin);
// Rotation: full axis rings, each with a small diamond knob.
const rotationRadii = { x: 1.43, y: 1.28, z: 1.12 };
for (const axisName of ["x", "y", "z"]) {
const axis = AXIS_VECTORS[axisName];
const color = AXIS_COLORS[axisName];
const radius = rotationRadii[axisName];
const ring = markHandle(
new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.027, 6, 64),
gizmoMaterial(color, 0.9)
),
"rotate",
axisName
);
ring.quaternion.setFromUnitVectors(Z_AXIS, axis);
rotateGizmo.add(ring);
let tangent = new THREE.Vector3(0, 1, 0).cross(axis);
if (tangent.lengthSq() < 0.001) tangent.set(1, 0, 0).cross(axis);
tangent.normalize();
const knob = markHandle(
new THREE.Mesh(
new THREE.OctahedronGeometry(0.075, 0),
gizmoMaterial(color, 1)
),
"rotate",
axisName
);
knob.position.copy(tangent).multiplyScalar(radius);
rotateGizmo.add(knob);
const label = makeLabel(axisName.toUpperCase(), color);
label.position.copy(axis).multiplyScalar(radius + 0.19);
rotateGizmo.add(label);
}
// Scale: square cube handles at both ends, plus an amber uniform cube.
const scaleHandles = {};
for (const axisName of ["x", "y", "z"]) {
const axis = AXIS_VECTORS[axisName];
const color = AXIS_COLORS[axisName];
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, 1, 8),
gizmoMaterial(color, 0.6)
);
orientAlongAxis(stem, axis);
stem.raycast = () => {};
stem.renderOrder = 18;
scaleGizmo.add(stem);
const plus = markHandle(
new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.18, 0.18),
gizmoMaterial(color, 1)
),
"scale",
axisName,
1
);
scaleGizmo.add(plus);
const minus = markHandle(
new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.16, 0.16),
gizmoMaterial(color, 0.48)
),
"scale",
axisName,
-1
);
scaleGizmo.add(minus);
const label = makeLabel(axisName.toUpperCase(), color);
scaleGizmo.add(label);
scaleHandles[axisName] = { stem, plus, minus, label };
}
const uniformHandle = markHandle(
new THREE.Mesh(
new THREE.BoxGeometry(0.24, 0.24, 0.24),
gizmoMaterial(0xffc85a, 1)
),
"scale",
"uniform",
0
);
scaleGizmo.add(uniformHandle);
const state = {
mode: "translate",
showAll: true,
drag: null,
orbit: null,
hover: null
};
function emphasizeGroup(group, factor) {
group.traverse((child) => {
if (!child.material) return;
const materials = Array.isArray(child.material) ? child.material : [child.material];
for (const material of materials) {
const base = material.userData.baseOpacity ?? 1;
material.opacity = base * factor;
material.transparent = true;
}
});
}
function refreshGizmoVisibility() {
const groups = {
translate: translateGizmo,
rotate: rotateGizmo,
scale: scaleGizmo
};
for (const [mode, group] of Object.entries(groups)) {
group.visible = state.showAll || state.mode === mode;
const factor = mode === state.mode ? 1 : state.showAll ? 0.28 : 0;
emphasizeGroup(group, factor);
}
}
function setMode(mode) {
state.mode = mode;
document.querySelectorAll(".mode").forEach((button) => {
const active = button.dataset.mode === mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
refreshGizmoVisibility();
}
document.querySelectorAll(".mode").forEach((button) => {
button.addEventListener("click", () => setMode(button.dataset.mode));
});
document.getElementById("allGuides").addEventListener("change", (event) => {
state.showAll = event.target.checked;
refreshGizmoVisibility();
});
document.getElementById("resetButton").addEventListener("click", () => {
toy.position.copy(initialPosition);
toy.quaternion.copy(initialQuaternion);
toy.scale.copy(initialScale);
});
document.getElementById("focusButton").addEventListener("click", () => {
view.theta = 0.66;
view.phi = 1.18;
view.radius = 7.1;
view.target.set(0, 1.38, 0);
updateCamera();
});
function updateGizmoTransform() {
gizmoRoot.position.copy(toy.position);
gizmoRoot.quaternion.copy(toy.quaternion);
const scaleLength = 1.55;
for (const axisName of ["x", "y", "z"]) {
const axis = AXIS_VECTORS[axisName];
const component = Math.max(0.15, toy.scale[axisName]);
const length = scaleLength * component;
const handles = scaleHandles[axisName];
handles.plus.position.copy(axis).multiplyScalar(length);
handles.minus.position.copy(axis).multiplyScalar(-length);
handles.label.position.copy(axis).multiplyScalar(length + 0.19);
const inner = length * 0.84;
handles.stem.scale.set(1, inner * 2, 1);
handles.stem.position.copy(axis).multiplyScalar(0);
orientAlongAxis(handles.stem, axis);
}
}
setMode("translate");
// Pointer and custom camera controls.
const canvas = renderer.domElement;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerPixels = new THREE.Vector2();
let canvasRect = canvas.getBoundingClientRect();
function updatePointer(event) {
canvasRect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - canvasRect.left) / canvasRect.width) * 2 - 1;
pointer.y = -((event.clientY - canvasRect.top) / canvasRect.height) * 2 + 1;
pointerPixels.set(event.clientX - canvasRect.left, event.clientY - canvasRect.top);
}
function setRayFromPointer() {
raycaster.setFromCamera(pointer, camera);
}
function activeGizmoGroups() {
if (state.showAll) return [translateGizmo, rotateGizmo, scaleGizmo];
return [{
translate: translateGizmo,
rotate: rotateGizmo,
scale: scaleGizmo
}[state.mode]];
}
function findGizmoHit(event) {
updatePointer(event);
setRayFromPointer();
const hits = raycaster.intersectObjects(activeGizmoGroups(), true);
for (const hit of hits) {
if (hit.object.userData.action) return hit;
}
return null;
}
function projectToPixels(worldPoint) {
const projected = worldPoint.clone().project(camera);
return new THREE.Vector2(
(projected.x * 0.5 + 0.5) * canvasRect.width,
(-projected.y * 0.5 + 0.5) * canvasRect.height
);
}
function makeMotionPlane(axisWorld, originPoint) {
const normal = camera.position.clone().sub(originPoint);
normal.addScaledVector(axisWorld, -normal.dot(axisWorld));
if (normal.lengthSq() < 0.0001) {
normal.set(0, 1, 0).cross(axisWorld);
if (normal.lengthSq() < 0.0001) normal.set(1, 0, 0).cross(axisWorld);
}
normal.normalize();
return new THREE.Plane().setFromNormalAndCoplanarPoint(normal, originPoint);
}
function beginTransform(event, hit) {
const data = hit.object.userData;
setMode(data.action);
const drag = {
action: data.action,
axisName: data.axis,
sign: data.sign,
pointerId: event.pointerId,
startPosition: toy.position.clone(),
startQuaternion: toy.quaternion.clone(),
startScale: toy.scale.clone(),
startPointerPixels: pointerPixels.clone()
};
if (data.axis && data.axis !== "uniform") {
const axisLocal = AXIS_VECTORS[data.axis].clone();
drag.axisWorld = axisLocal.applyQuaternion(toy.quaternion).normalize();
drag.motionPlane = makeMotionPlane(drag.axisWorld, toy.position);
setRayFromPointer();
const intersection = new THREE.Vector3();
drag.startPlanePoint = raycaster.ray.intersectPlane(drag.motionPlane, intersection);
drag.screenOrigin = projectToPixels(toy.position);
drag.screenAxis = projectToPixels(
toy.position.clone().add(drag.axisWorld)
).sub(drag.screenOrigin);
}
if (data.action === "rotate") {
drag.rotationPlane = new THREE.Plane().setFromNormalAndCoplanarPoint(
drag.axisWorld,
toy.position
);
setRayFromPointer();
const intersection = new THREE.Vector3();
if (raycaster.ray.intersectPlane(drag.rotationPlane, intersection)) {
drag.startVector = intersection.sub(toy.position);
drag.startVector.addScaledVector(drag.axisWorld, -drag.startVector.dot(drag.axisWorld));
} else {
drag.startVector = null;
}
drag.screenCenter = projectToPixels(toy.position);
drag.startScreenAngle = Math.atan2(
drag.startPointerPixels.y - drag.screenCenter.y,
drag.startPointerPixels.x - drag.screenCenter.x
);
}
state.drag = drag;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
pointerReadout.textContent = data.action === "rotate"
? "Drag the selected ring around its local axis"
: data.action === "scale"
? "Drag a cube handle along its local axis"
: "Drag the selected arrow along its local axis";
}
function axisDelta(event, drag) {
updatePointer(event);
setRayFromPointer();
const intersection = new THREE.Vector3();
if (drag.startPlanePoint && raycaster.ray.intersectPlane(drag.motionPlane, intersection)) {
return intersection.sub(drag.startPlanePoint).dot(drag.axisWorld);
}
const screenDelta = pointerPixels.clone().sub(drag.startPointerPixels);
const axisLengthSq = drag.screenAxis.lengthSq();
if (axisLengthSq < 0.0001) return 0;
return screenDelta.dot(drag.screenAxis) / axisLengthSq;
}
function updateTransform(event, drag) {
if (drag.action === "translate") {
const delta = axisDelta(event, drag);
toy.position.copy(drag.startPosition).addScaledVector(drag.axisWorld, delta);
return;
}
if (drag.action === "scale") {
if (drag.axisName === "uniform") {
updatePointer(event);
const factor = THREE.MathUtils.clamp(
1 + (drag.startPointerPixels.y - pointerPixels.y) / 160,
0.15,
4
);
toy.scale.copy(drag.startScale).multiplyScalar(factor);
toy.scale.clampScalar(0.15, 5);
} else {
const axisIndex = { x: 0, y: 1, z: 2 }[drag.axisName];
const startLength = Math.max(0.15, drag.startScale.getComponent(axisIndex)) * 1.55;
const delta = axisDelta(event, drag);
const factor = THREE.MathUtils.clamp(
1 + drag.sign * delta / startLength,
0.15,
4
);
toy.scale.copy(drag.startScale);
toy.scale.setComponent(axisIndex, THREE.MathUtils.clamp(
drag.startScale.getComponent(axisIndex) * factor,
0.15,
5
));
}
return;
}
if (drag.action === "rotate") {
updatePointer(event);
setRayFromPointer();
const intersection = new THREE.Vector3();
let angle = 0;
if (
drag.startVector &&
raycaster.ray.intersectPlane(drag.rotationPlane, intersection)
) {
const currentVector = intersection.sub(toy.position);
currentVector.addScaledVector(
drag.axisWorld,
-currentVector.dot(drag.axisWorld)
);
const cross = drag.startVector.clone().cross(currentVector);
const dot = drag.startVector.dot(currentVector);
angle = Math.atan2(cross.dot(drag.axisWorld), dot);
} else {
const currentAngle = Math.atan2(
pointerPixels.y - drag.screenCenter.y,
pointerPixels.x - drag.screenCenter.x
);
const forward = camera.getWorldDirection(new THREE.Vector3());
const screenSign = drag.axisWorld.dot(forward) > 0 ? -1 : 1;
angle = (currentAngle - drag.startScreenAngle) * screenSign;
}
const rotation = new THREE.Quaternion().setFromAxisAngle(drag.axisWorld, angle);
toy.quaternion.copy(rotation).multiply(drag.startQuaternion).normalize();
}
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 1 && event.button !== 2) return;
event.preventDefault();
const hit = findGizmoHit(event);
if (hit && event.button === 0) {
beginTransform(event, hit);
return;
}
state.orbit = {
pointerId: event.pointerId,
mode: event.button === 2 || event.button === 1 || event.shiftKey ? "pan" : "orbit",
x: event.clientX,
y: event.clientY
};
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (state.drag && state.drag.pointerId === event.pointerId) {
event.preventDefault();
updateTransform(event, state.drag);
return;
}
if (state.orbit && state.orbit.pointerId === event.pointerId) {
event.preventDefault();
const dx = event.clientX - state.orbit.x;
const dy = event.clientY - state.orbit.y;
state.orbit.x = event.clientX;
state.orbit.y = event.clientY;
if (state.orbit.mode === "orbit") {
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(view.phi - dy * 0.005, 0.25, Math.PI - 0.25);
} else {
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(camera.quaternion);
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(camera.quaternion);
view.target
.addScaledVector(right, -dx * view.radius * 0.00125)
.addScaledVector(up, dy * view.radius * 0.00125);
}
updateCamera();
return;
}
const hit = findGizmoHit(event);
if (hit) {
const data = hit.object.userData;
canvas.style.cursor = "pointer";
if (data.action === "translate") {
pointerReadout.textContent = `Drag ${data.axis.toUpperCase()} translation arrow`;
} else if (data.action === "rotate") {
pointerReadout.textContent = `Drag ${data.axis.toUpperCase()} rotation ring`;
} else if (data.axis === "uniform") {
pointerReadout.textContent = "Drag amber center cube for uniform scale";
} else {
pointerReadout.textContent = `Drag ${data.axis.toUpperCase()} cube handle to scale`;
}
} else {
canvas.style.cursor = "grab";
pointerReadout.textContent = "Drag empty space to orbit · shift-drag to pan · wheel to zoom";
}
});
function endPointerInteraction(event) {
if (state.drag && state.drag.pointerId === event.pointerId) {
state.drag = null;
}
if (state.orbit && state.orbit.pointerId === event.pointerId) {
state.orbit = null;
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
canvas.style.cursor = "grab";
}
canvas.addEventListener("pointerup", endPointerInteraction);
canvas.addEventListener("pointercancel", endPointerInteraction);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
3.7,
13
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
view.theta = 0.66;
view.phi = 1.18;
view.radius = 7.1;
view.target.set(0, 1.38, 0);
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
const key = event.key.toLowerCase();
if (key === "w") setMode("translate");
if (key === "e") setMode("rotate");
if (key === "r") setMode("scale");
if (key === "f") {
view.theta = 0.66;
view.phi = 1.18;
view.radius = 7.1;
view.target.set(0, 1.38, 0);
updateCamera();
}
if (key === "0") {
toy.position.copy(initialPosition);
toy.quaternion.copy(initialQuaternion);
toy.scale.copy(initialScale);
}
if (key === "escape" && state.drag) {
const drag = state.drag;
toy.position.copy(drag.startPosition);
toy.quaternion.copy(drag.startQuaternion);
toy.scale.copy(drag.startScale);
state.drag = null;
}
});
// Transform readouts.
const readout = {
posX: document.getElementById("posX"),
posY: document.getElementById("posY"),
posZ: document.getElementById("posZ"),
rotX: document.getElementById("rotX"),
rotY: document.getElementById("rotY"),
rotZ: document.getElementById("rotZ"),
scaleX: document.getElementById("scaleX"),
scaleY: document.getElementById("scaleY"),
scaleZ: document.getElementById("scaleZ")
};
const degrees = THREE.MathUtils.radToDeg;
const fixed = (value, digits = 2) => {
const rounded = Number(value).toFixed(digits);
return Number(rounded) === 0 ? (0).toFixed(digits) : rounded;
};
function updateReadouts() {
const euler = new THREE.Euler().setFromQuaternion(toy.quaternion, "YXZ");
readout.posX.textContent = fixed(toy.position.x);
readout.posY.textContent = fixed(toy.position.y);
readout.posZ.textContent = fixed(toy.position.z);
readout.rotX.textContent = `${fixed(degrees(euler.x), 1)}°`;
readout.rotY.textContent = `${fixed(degrees(euler.y), 1)}°`;
readout.rotZ.textContent = `${fixed(degrees(euler.z), 1)}°`;
readout.scaleX.textContent = fixed(toy.scale.x);
readout.scaleY.textContent = fixed(toy.scale.y);
readout.scaleZ.textContent = fixed(toy.scale.z);
}
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
canvasRect = canvas.getBoundingClientRect();
}
window.addEventListener("resize", onResize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
for (const decoration of decorations) {
decoration.rotation.y += 0.0015;
decoration.position.y =
decoration.userData.baseY + Math.sin(time * 0.55 + decoration.userData.phase) * 0.045;
}
updateGizmoTransform();
updateReadouts();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```