Prompt specification
Build or customize the Three.js 3D scene "Latitude bands on a cylinder" 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: Latitude bands on a cylinder
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/latitude-bands-on-a-cylinder-x1318
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts map projections. Wrap a transparent cylinder around a gridded globe and project selected latitude lines. Keep the equatorial region readable while higher latitude spacing visibly expands. A 3D cutaway view.
Working Three.js Starter (latitude-bands-on-a-cylinder-x1318):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meridian — Cylindrical Projection Cutaway</title>
<style>
:root {
color-scheme: dark;
--cyan: #79f7df;
--blue: #72bfff;
--violet: #b899ff;
--orange: #ffb56b;
--panel: rgba(7, 17, 31, 0.72);
--line: rgba(121, 247, 223, 0.2);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #122844 0%, #07111f 42%, #030711 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.lat-label {
position: absolute;
min-width: 42px;
padding: 3px 6px;
border: 1px solid currentColor;
border-radius: 4px;
background: rgba(4, 11, 22, 0.82);
box-shadow: 0 0 16px color-mix(in srgb, currentColor 22%, transparent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
line-height: 1;
text-align: center;
white-space: nowrap;
transform: translate(10px, -50%);
opacity: 0;
transition: opacity 120ms ease;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 16px 60px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-panel {
top: 24px;
left: 24px;
width: min(360px, calc(100vw - 48px));
padding: 18px 20px 17px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 34px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 1;
}
.subtitle {
margin: 10px 0 0;
color: #9db1c8;
font-size: 12px;
line-height: 1.55;
}
#controls {
top: 24px;
right: 24px;
padding: 10px;
}
.button-row {
display: flex;
gap: 6px;
}
button {
border: 1px solid rgba(121, 247, 223, 0.2);
border-radius: 7px;
padding: 8px 10px;
background: rgba(121, 247, 223, 0.05);
color: #d9e7f4;
font: inherit;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
color 150ms ease;
}
button:hover,
button.active {
border-color: rgba(121, 247, 223, 0.6);
background: rgba(121, 247, 223, 0.13);
color: var(--cyan);
}
#legend {
bottom: 24px;
left: 24px;
width: 260px;
padding: 16px 18px;
}
.legend-title {
margin-bottom: 12px;
color: #e9f3fb;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin: 8px 0;
color: #9eb3c8;
font-size: 11px;
}
.swatch {
width: 24px;
height: 2px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 8px currentColor;
}
.swatch.grid {
height: 1px;
background: #5ec8ff;
}
.swatch.ray {
background: repeating-linear-gradient(
90deg,
#ffb56b 0 4px,
transparent 4px 7px
);
}
#projection-panel {
right: 24px;
bottom: 24px;
width: 292px;
padding: 16px 18px;
}
.formula {
margin: 8px 0 12px;
color: #d8e9f7;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 12px;
}
.metric {
padding-top: 11px;
border-top: 1px solid rgba(121, 247, 223, 0.12);
color: #8fa5bb;
font-size: 11px;
line-height: 1.55;
}
.metric strong {
color: var(--orange);
font-weight: 750;
}
#hint {
position: fixed;
z-index: 4;
top: 92px;
right: 24px;
color: rgba(190, 211, 229, 0.55);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 760px) {
#title-panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
}
#controls {
top: auto;
right: 14px;
bottom: 14px;
}
#hint,
#legend,
#projection-panel {
display: none;
}
}
</style>
</head>
<body>
<main id="app" aria-label="Interactive 3D map projection cutaway"></main>
<div id="labels" aria-hidden="true"></div>
<section id="title-panel" class="panel">
<div class="eyebrow">Meridian / Projection Lab</div>
<h1>Cylindrical Cutaway</h1>
<p class="subtitle">
A Mercator cylindrical projection wrapped around a gridded globe.
Selected parallels remain distinct near the equator, then spread toward the poles.
</p>
</section>
<div id="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<nav id="controls" class="panel" aria-label="Scene views">
<div class="button-row">
<button id="reset-view" type="button">Reset</button>
<button id="front-view" type="button">Cutaway</button>
<button id="side-view" type="button">Oblique</button>
<button id="top-view" type="button">Top</button>
<button id="auto-view" type="button">Orbit</button>
</div>
</nav>
<aside id="legend" class="panel">
<div class="legend-title">Visual Key</div>
<div class="legend-row"><span class="swatch grid"></span>Globe graticule</div>
<div class="legend-row"><span class="swatch"></span>Projected parallels</div>
<div class="legend-row"><span class="swatch ray"></span>Projection guides</div>
</aside>
<aside id="projection-panel" class="panel">
<div class="eyebrow">Mercator / Cylindrical</div>
<div class="formula">y ∝ ln(tan(π/4 + φ/2))</div>
<div class="metric">
15° parallels remain readable at the equator. Between 60° and 75°,
projected spacing expands to <strong>≈ 2.7×</strong> the equatorial gap.
</div>
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const labelLayer = document.querySelector("#labels");
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111f, 0.025);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// -------------------------------------------------------------------------
// Geometry constants
// -------------------------------------------------------------------------
const GLOBE_RADIUS = 1.55;
const CYLINDER_RADIUS = 2.62;
const CYLINDER_HEIGHT = 4.1;
const HALF_HEIGHT = CYLINDER_HEIGHT / 2;
// The cylinder and globe are cut over the same 126-degree angular sector.
const CUT_LOW = -0.05;
const CUT_HIGH = 2.15;
const CUT_CENTER = (CUT_LOW + CUT_HIGH) / 2;
const ALPHA_START = CUT_HIGH;
const ALPHA_END = CUT_LOW + Math.PI * 2;
const ALPHA_LENGTH = ALPHA_END - ALPHA_START;
const DEG = Math.PI / 180;
const parallels = [
-75, -60, -45, -30, -15,
0,
15, 30, 45, 60, 75
];
function parallelColor(latitude) {
const colors = {
0: 0x79ffe0,
15: 0x64ddff,
30: 0x7da8ff,
45: 0xb493ff,
60: 0xff8fbd,
75: 0xffb56b
};
return colors[Math.abs(latitude)];
}
function latitudeY(latitude) {
const raw = Math.log(
Math.tan(Math.PI / 4 + latitude * DEG / 2)
);
const max = Math.log(
Math.tan(Math.PI / 4 + 78 * DEG / 2)
);
return (raw / max) * HALF_HEIGHT;
}
function radialPoint(radius, alpha, y) {
return new THREE.Vector3(
radius * Math.sin(alpha),
y,
radius * Math.cos(alpha)
);
}
function globePoint(latitude, alpha, radius = GLOBE_RADIUS) {
return radialPoint(
radius * Math.cos(latitude * DEG),
alpha,
radius * Math.sin(latitude * DEG)
);
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa9eaff, 0x050711, 2.1));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4.5, 6.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x42dfff, 14, 12, 2);
cyanLight.position.set(3.5, 1.3, 4.2);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0xa65cff, 12, 10, 2);
violetLight.position.set(-3, 0.8, -3);
scene.add(violetLight);
const warmLight = new THREE.PointLight(0xff9b55, 7, 8, 2);
warmLight.position.set(0, -1.5, 3.5);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Floor
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.CircleGeometry(4.7, 96),
new THREE.MeshStandardMaterial({
color: 0x071522,
roughness: 0.94,
metalness: 0.08,
transparent: true,
opacity: 0.58
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.2;
floor.receiveShadow = true;
scene.add(floor);
const floorGrid = new THREE.GridHelper(
10,
24,
0x28566d,
0x142b3d
);
floorGrid.position.y = -2.185;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.22;
floorGrid.material.depthWrite = false;
scene.add(floorGrid);
// -------------------------------------------------------------------------
// Cutaway globe shell
// -------------------------------------------------------------------------
const globeGeometry = new THREE.SphereGeometry(
GLOBE_RADIUS,
96,
64,
Math.PI / 2 + ALPHA_START,
ALPHA_LENGTH
);
const globeMaterial = new THREE.MeshPhysicalMaterial({
color: 0x09233a,
emissive: 0x061425,
emissiveIntensity: 0.35,
roughness: 0.62,
metalness: 0.16,
clearcoat: 0.5,
clearcoatRoughness: 0.4,
side: THREE.DoubleSide
});
const globeShell = new THREE.Mesh(globeGeometry, globeMaterial);
globeShell.castShadow = true;
globeShell.receiveShadow = true;
scene.add(globeShell);
// Graticule.
const graticuleMaterial = new THREE.LineBasicMaterial({
color: 0x58c9ff,
transparent: true,
opacity: 0.34,
depthWrite: false
});
for (let latitude = -75; latitude <= 75; latitude += 15) {
const points = [];
for (let i = 0; i <= 160; i++) {
const alpha =
ALPHA_START + (ALPHA_LENGTH * i) / 160;
points.push(globePoint(latitude, alpha));
}
const material = latitude === 0
? new THREE.LineBasicMaterial({
color: 0x79ffe0,
transparent: true,
opacity: 0.78,
depthWrite: false
})
: graticuleMaterial;
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
material
)
);
}
for (let alpha = 0; alpha < Math.PI * 2; alpha += Math.PI / 12) {
if (alpha < ALPHA_START || alpha > ALPHA_END) continue;
const points = [];
for (let latitude = -88; latitude <= 88; latitude += 2) {
points.push(globePoint(latitude, alpha));
}
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
graticuleMaterial
)
);
}
// Bright edges around the globe cut.
const cutEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x9deeff,
transparent: true,
opacity: 0.9
});
for (const alpha of [ALPHA_START, ALPHA_END]) {
const points = [];
for (let latitude = -90; latitude <= 90; latitude += 1.5) {
points.push(globePoint(latitude, alpha, GLOBE_RADIUS + 0.008));
}
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
cutEdgeMaterial
)
);
}
// -------------------------------------------------------------------------
// Inner core, visible through the cutaway
// -------------------------------------------------------------------------
const core = new THREE.Mesh(
new THREE.SphereGeometry(1.13, 64, 48),
new THREE.MeshPhysicalMaterial({
color: 0x211340,
emissive: 0x31105a,
emissiveIntensity: 0.8,
roughness: 0.34,
metalness: 0.25,
clearcoat: 0.8
})
);
core.castShadow = true;
scene.add(core);
const coreWire = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.16, 2),
new THREE.MeshBasicMaterial({
color: 0xc58cff,
wireframe: true,
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
scene.add(coreWire);
const innerCore = new THREE.Mesh(
new THREE.SphereGeometry(0.48, 32, 24),
new THREE.MeshBasicMaterial({
color: 0xc477ff,
transparent: true,
opacity: 0.72
})
);
scene.add(innerCore);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0x75eaff,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const ringA = new THREE.Mesh(
new THREE.TorusGeometry(1.2, 0.012, 6, 96),
ringMaterial
);
ringA.rotation.x = Math.PI / 2;
scene.add(ringA);
const ringB = new THREE.Mesh(
new THREE.TorusGeometry(1.2, 0.012, 6, 96),
ringMaterial
);
ringB.rotation.y = Math.PI / 2;
scene.add(ringB);
const ringC = new THREE.Mesh(
new THREE.TorusGeometry(1.2, 0.012, 6, 96),
ringMaterial
);
ringC.rotation.set(Math.PI / 3, Math.PI / 4, 0);
scene.add(ringC);
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -2.12, 0),
new THREE.Vector3(0, 2.12, 0)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x8edfff,
dashSize: 0.06,
gapSize: 0.045,
transparent: true,
opacity: 0.4
})
);
axis.computeLineDistances();
scene.add(axis);
// -------------------------------------------------------------------------
// Transparent cylindrical projection surface
// -------------------------------------------------------------------------
const cylinderGeometry = new THREE.CylinderGeometry(
CYLINDER_RADIUS,
CYLINDER_RADIUS,
CYLINDER_HEIGHT,
128,
1,
true,
ALPHA_START,
ALPHA_LENGTH
);
const cylinder = new THREE.Mesh(
cylinderGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x54cfff,
transparent: true,
opacity: 0.115,
roughness: 0.2,
metalness: 0.04,
clearcoat: 1,
clearcoatRoughness: 0.2,
side: THREE.DoubleSide,
depthWrite: false
})
);
cylinder.renderOrder = 2;
scene.add(cylinder);
// Fine ribs on the transparent cylinder.
const cylinderGridMaterial = new THREE.LineBasicMaterial({
color: 0x4fc8ff,
transparent: true,
opacity: 0.16,
depthWrite: false
});
for (let alpha = 0; alpha < Math.PI * 2; alpha += Math.PI / 12) {
if (alpha < ALPHA_START || alpha > ALPHA_END) continue;
const points = [
radialPoint(CYLINDER_RADIUS + 0.004, alpha, -HALF_HEIGHT),
radialPoint(CYLINDER_RADIUS + 0.004, alpha, HALF_HEIGHT)
];
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
cylinderGridMaterial
)
);
}
for (const y of [-1.7, -0.85, 0.85, 1.7]) {
const points = [];
for (let i = 0; i <= 140; i++) {
const alpha =
ALPHA_START + (ALPHA_LENGTH * i) / 140;
points.push(
radialPoint(CYLINDER_RADIUS + 0.004, alpha, y)
);
}
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
cylinderGridMaterial
)
);
}
// Top and bottom arcs of the cylinder.
const rimMaterial = new THREE.LineBasicMaterial({
color: 0x8eeaff,
transparent: true,
opacity: 0.72
});
for (const y of [-HALF_HEIGHT, HALF_HEIGHT]) {
const points = [];
for (let i = 0; i <= 160; i++) {
const alpha =
ALPHA_START + (ALPHA_LENGTH * i) / 160;
points.push(
radialPoint(CYLINDER_RADIUS + 0.008, alpha, y)
);
}
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
rimMaterial
)
);
}
// -------------------------------------------------------------------------
// Selected projected latitude lines
// -------------------------------------------------------------------------
const projectedLabels = [];
for (const latitude of parallels) {
const color = parallelColor(latitude);
const y = latitudeY(latitude);
const isEquator = latitude === 0;
// Solid line on the remaining cylinder surface.
const surfacePoints = [];
for (let i = 0; i <= 160; i++) {
const alpha =
ALPHA_START + (ALPHA_LENGTH * i) / 160;
surfacePoints.push(
radialPoint(CYLINDER_RADIUS + 0.025, alpha, y)
);
}
const curve = new THREE.CatmullRomCurve3(surfacePoints);
const projectedLine = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
180,
isEquator ? 0.021 : 0.0105,
6,
false
),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: isEquator ? 1 : 0.86,
depthTest: false,
depthWrite: false
})
);
projectedLine.renderOrder = 12;
scene.add(projectedLine);
// Dashed continuation through the removed sector.
const continuationPoints = [];
for (let i = 0; i <= 100; i++) {
const alpha =
ALPHA_END +
((ALPHA_START + Math.PI * 2 - ALPHA_END) * i) / 100;
continuationPoints.push(
radialPoint(CYLINDER_RADIUS + 0.026, alpha, y)
);
}
const continuation = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(continuationPoints),
new THREE.LineDashedMaterial({
color,
dashSize: isEquator ? 0.11 : 0.075,
gapSize: isEquator ? 0.06 : 0.055,
transparent: true,
opacity: isEquator ? 0.6 : 0.38,
depthTest: false,
depthWrite: false
})
);
continuation.computeLineDistances();
continuation.renderOrder = 11;
scene.add(continuation);
// HTML label anchored near the cutaway ruler.
const label = document.createElement("div");
label.className = "lat-label";
label.style.color = `#${new THREE.Color(color).getHexString()}`;
const hemisphere = latitude === 0
? "EQ"
: latitude > 0
? "N"
: "S";
label.textContent =
`${Math.abs(latitude)}° ${hemisphere}`;
labelLayer.appendChild(label);
projectedLabels.push({
element: label,
anchor: radialPoint(
CYLINDER_RADIUS + 0.12,
CUT_CENTER + 0.42,
y
)
});
}
// -------------------------------------------------------------------------
// Projection guides from globe points to their projected locations
// -------------------------------------------------------------------------
const rayLatitudes = [-60, 0, 60];
const rayAlphas = [CUT_LOW, CUT_CENTER, CUT_HIGH];
for (const latitude of rayLatitudes) {
for (const alpha of rayAlphas) {
const source = globePoint(latitude, alpha);
const target = radialPoint(
CYLINDER_RADIUS + 0.03,
alpha,
latitudeY(latitude)
);
const guide = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
source,
target
]),
new THREE.LineDashedMaterial({
color: 0xffb56b,
dashSize: 0.055,
gapSize: 0.04,
transparent: true,
opacity: 0.6,
depthTest: false,
depthWrite: false
})
);
guide.computeLineDistances();
guide.renderOrder = 10;
scene.add(guide);
const sourceMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 12, 8),
new THREE.MeshBasicMaterial({
color: 0xffc17e,
depthTest: false
})
);
sourceMarker.position.copy(source);
sourceMarker.renderOrder = 13;
scene.add(sourceMarker);
const targetMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.043, 12, 8),
new THREE.MeshBasicMaterial({
color: parallelColor(latitude),
depthTest: false
})
);
targetMarker.position.copy(target);
targetMarker.renderOrder = 13;
scene.add(targetMarker);
}
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit, pan, and zoom
// -------------------------------------------------------------------------
const desired = {
theta: CUT_CENTER,
phi: 1.16,
radius: 7.25,
target: new THREE.Vector3(0, 0, 0)
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius,
target: desired.target.clone()
};
const canvas = renderer.domElement;
const drag = {
active: false,
pointerId: null,
x: 0,
y: 0,
pan: false
};
canvas.addEventListener("pointerdown", (event) => {
drag.active = true;
drag.pointerId = event.pointerId;
drag.x = event.clientX;
drag.y = event.clientY;
drag.pan = event.button === 2 || event.shiftKey;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!drag.active || event.pointerId !== drag.pointerId) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
drag.x = event.clientX;
drag.y = event.clientY;
if (drag.pan || event.shiftKey) {
camera.updateMatrixWorld();
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0);
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 1);
const scale = current.radius * 0.00125;
desired.target
.addScaledVector(right, -dx * scale)
.addScaledVector(up, dy * scale);
desired.target.y = THREE.MathUtils.clamp(
desired.target.y,
-1.2,
1.2
);
} else {
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.005,
0.16,
Math.PI - 0.16
);
}
});
function endDrag(event) {
if (event.pointerId !== drag.pointerId) return;
drag.active = false;
drag.pointerId = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
4.1,
12
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
setView("reset");
});
let autoRotate = false;
function setView(name) {
autoRotate = false;
document.querySelector("#auto-view").classList.remove("active");
if (name === "reset" || name === "cutaway") {
desired.theta = CUT_CENTER;
desired.phi = 1.16;
desired.radius = 7.25;
desired.target.set(0, 0, 0);
} else if (name === "oblique") {
desired.theta = CUT_CENTER + 0.62;
desired.phi = 1.1;
desired.radius = 7.7;
desired.target.set(0, 0, 0);
} else if (name === "top") {
desired.theta = CUT_CENTER;
desired.phi = 0.22;
desired.radius = 7.8;
desired.target.set(0, 0, 0);
}
}
document.querySelector("#reset-view").addEventListener(
"click",
() => setView("reset")
);
document.querySelector("#front-view").addEventListener(
"click",
() => setView("cutaway")
);
document.querySelector("#side-view").addEventListener(
"click",
() => setView("oblique")
);
document.querySelector("#top-view").addEventListener(
"click",
() => setView("top")
);
document.querySelector("#auto-view").addEventListener(
"click",
(event) => {
autoRotate = !autoRotate;
event.currentTarget.classList.toggle("active", autoRotate);
}
);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
const projectedPosition = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of projectedLabels) {
projectedPosition.copy(item.anchor).project(camera);
const onScreen =
projectedPosition.z > -1 &&
projectedPosition.z < 1 &&
projectedPosition.x > -1.08 &&
projectedPosition.x < 1.08 &&
projectedPosition.y > -1.08 &&
projectedPosition.y < 1.08;
item.element.style.opacity = onScreen ? "1" : "0";
if (!onScreen) continue;
const x = (projectedPosition.x * 0.5 + 0.5) * width;
const y = (-projectedPosition.y * 0.5 + 0.5) * height;
item.element.style.left = `${x}px`;
item.element.style.top = `${y}px`;
}
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (autoRotate && !drag.active) {
desired.theta += delta * 0.1;
}
const smoothing = 1 - Math.exp(-delta * 10);
current.theta = THREE.MathUtils.lerp(
current.theta,
desired.theta,
smoothing
);
current.phi = THREE.MathUtils.lerp(
current.phi,
desired.phi,
smoothing
);
current.radius = THREE.MathUtils.lerp(
current.radius,
desired.radius,
smoothing
);
current.target.lerp(desired.target, smoothing);
const horizontalRadius =
current.radius * Math.sin(current.phi);
camera.position.set(
current.target.x +
horizontalRadius * Math.sin(current.theta),
current.target.y +
current.radius * Math.cos(current.phi),
current.target.z +
horizontalRadius * Math.cos(current.theta)
);
camera.lookAt(current.target);
camera.updateMatrixWorld();
coreWire.rotation.y += delta * 0.09;
coreWire.rotation.x += delta * 0.035;
innerCore.rotation.y -= delta * 0.16;
ringA.rotation.z += delta * 0.08;
ringB.rotation.x -= delta * 0.06;
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```