Prompt specification
Build or customize the Three.js 3D scene "Hydraulic area-ratio explorer" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Hydraulic area-ratio explorer
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/hydraulic-area-ratio-explorer-x1484
Scene Description Prompt:
Allow choosing piston areas and input displacement; compute output movement and ideal force ratio, explicitly excluding leakage, compressibility, and structural limits from the model. Build this with Three.js from cdnjs as a low-poly scene in one HTML file.
Working Three.js Starter (hydraulic-area-ratio-explorer-x1484):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ideal Hydraulic Actuator</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: #0c1928;
--panel-2: #102238;
--border: #20384e;
--text: #eaf4ff;
--muted: #8fa9bd;
--cyan: #55d9e9;
--orange: #ffb45c;
--yellow: #f5d36b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app {
display: grid;
grid-template-columns: 360px 1fr;
width: 100%;
height: 100%;
}
#panel {
position: relative;
z-index: 2;
overflow-y: auto;
padding: 26px 24px;
background:
linear-gradient(180deg, rgba(15, 31, 48, 0.98), rgba(8, 18, 30, 0.98));
border-right: 1px solid var(--border);
box-shadow: 8px 0 30px rgba(0, 0, 0, 0.22);
}
h1 {
margin: 6px 0 8px;
font-size: 25px;
line-height: 1.1;
letter-spacing: -0.03em;
}
.subtitle {
margin: 0 0 24px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.section-title {
margin: 22px 0 12px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.control {
margin: 0 0 18px;
padding: 14px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(17, 36, 56, 0.72);
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
font-size: 13px;
font-weight: 650;
}
.control-heading span {
color: var(--muted);
font-size: 11px;
font-weight: 500;
}
.number-row {
display: grid;
grid-template-columns: 1fr 88px;
gap: 10px;
align-items: center;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
input[type="number"] {
width: 100%;
padding: 8px 9px;
border: 1px solid #2d4a62;
border-radius: 8px;
outline: none;
background: #081522;
color: var(--text);
font: inherit;
font-size: 13px;
}
input[type="number"]:focus {
border-color: var(--cyan);
box-shadow: 0 0 0 2px rgba(85, 217, 233, 0.14);
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.metric {
min-height: 91px;
padding: 14px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(17, 36, 56, 0.72);
}
.metric.wide {
grid-column: 1 / -1;
}
.metric-label {
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.metric-value {
margin-top: 9px;
color: var(--text);
font-size: 25px;
font-weight: 700;
letter-spacing: -0.04em;
}
.metric-value.cyan {
color: var(--cyan);
}
.metric-value.orange {
color: var(--orange);
}
.metric-note {
margin-top: 5px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.formula {
padding: 13px;
border-left: 3px solid var(--yellow);
border-radius: 0 9px 9px 0;
background: rgba(245, 211, 107, 0.07);
color: #e9edf2;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.8;
}
.assumption {
margin-top: 20px;
padding: 14px;
border: 1px solid rgba(245, 211, 107, 0.28);
border-radius: 12px;
background: rgba(245, 211, 107, 0.055);
color: #c7d3dc;
font-size: 12px;
line-height: 1.55;
}
.assumption strong {
display: block;
margin-bottom: 6px;
color: var(--yellow);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #07111d;
}
#stage canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#stage canvas:active {
cursor: grabbing;
}
.hud {
position: absolute;
z-index: 3;
top: 18px;
left: 18px;
display: flex;
flex-wrap: wrap;
gap: 8px;
pointer-events: none;
}
.pill {
padding: 7px 10px;
border: 1px solid rgba(82, 116, 143, 0.45);
border-radius: 999px;
background: rgba(7, 17, 29, 0.76);
backdrop-filter: blur(8px);
color: #bcd0df;
font-size: 11px;
}
.pill strong {
color: var(--text);
}
.legend {
position: absolute;
z-index: 3;
right: 18px;
bottom: 18px;
padding: 10px 12px;
border: 1px solid rgba(82, 116, 143, 0.45);
border-radius: 10px;
background: rgba(7, 17, 29, 0.76);
backdrop-filter: blur(8px);
color: var(--muted);
font-size: 11px;
line-height: 1.7;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 50%;
}
.swatch.cyan {
background: var(--cyan);
}
.swatch.orange {
background: var(--orange);
}
.swatch.yellow {
background: var(--yellow);
}
.scene-label {
position: absolute;
z-index: 4;
min-width: 150px;
padding: 8px 10px;
border: 1px solid rgba(94, 131, 159, 0.55);
border-radius: 9px;
background: rgba(5, 14, 24, 0.84);
backdrop-filter: blur(8px);
color: #cbd9e5;
font-size: 10px;
line-height: 1.5;
text-align: center;
pointer-events: none;
transform: translate(-50%, -50%);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
}
.scene-label strong {
display: block;
font-size: 12px;
}
#inputLabel {
border-color: rgba(85, 217, 233, 0.55);
}
#outputLabel {
border-color: rgba(255, 180, 92, 0.55);
}
.camera-hint {
position: absolute;
z-index: 3;
left: 18px;
bottom: 18px;
color: #718da2;
font-size: 11px;
pointer-events: none;
}
@media (max-width: 760px) {
html,
body {
overflow: auto;
}
#app {
grid-template-columns: 1fr;
grid-template-rows: auto 62vh;
height: auto;
min-height: 100%;
}
#panel {
overflow: visible;
border-right: 0;
border-bottom: 1px solid var(--border);
}
#stage {
min-height: 62vh;
}
.metric-grid {
grid-template-columns: 1fr 1fr;
}
}
</style>
</head>
<body>
<main id="app">
<aside id="panel">
<div style="color: var(--cyan); font-size: 11px; font-weight: 700; letter-spacing: 0.14em;">
IDEAL ACTUATOR LAB
</div>
<h1>Hydraulic / Pneumatic<br />Force & Motion</h1>
<p class="subtitle">
Explore area ratio, piston displacement, and ideal force multiplication
in a lossless incompressible model.
</p>
<div class="section-title">Input Parameters</div>
<div class="control">
<div class="control-heading">
Input piston area <span>A<sub>in</sub></span>
</div>
<div class="number-row">
<input
id="ainRange"
type="range"
min="1"
max="100"
step="0.1"
value="20"
aria-label="Input piston area"
/>
<input
id="ainNumber"
type="number"
min="1"
max="100"
step="0.1"
value="20"
aria-label="Input piston area in square centimeters"
/>
</div>
<div class="metric-note">cm²</div>
</div>
<div class="control">
<div class="control-heading">
Output piston area <span>A<sub>out</sub></span>
</div>
<div class="number-row">
<input
id="aoutRange"
type="range"
min="1"
max="100"
step="0.1"
value="50"
aria-label="Output piston area"
/>
<input
id="aoutNumber"
type="number"
min="1"
max="100"
step="0.1"
value="50"
aria-label="Output piston area in square centimeters"
/>
</div>
<div class="metric-note">cm²</div>
</div>
<div class="control">
<div class="control-heading">
Input displacement <span>d<sub>in</sub></span>
</div>
<div class="number-row">
<input
id="displacementRange"
type="range"
min="0"
max="100"
step="0.1"
value="30"
aria-label="Input displacement"
/>
<input
id="displacementNumber"
type="number"
min="0"
max="100"
step="0.1"
value="30"
aria-label="Input displacement in centimeters"
/>
</div>
<div class="metric-note">cm</div>
</div>
<div class="section-title">Calculated Results</div>
<div class="metric-grid">
<div class="metric wide">
<div class="metric-label">OUTPUT MOVEMENT</div>
<div class="metric-value orange" id="outputMovement">60.0 cm</div>
<div class="metric-note">Output piston displacement d<sub>out</sub></div>
</div>
<div class="metric">
<div class="metric-label">IDEAL FORCE RATIO</div>
<div class="metric-value cyan" id="forceRatio">2.50 ×</div>
<div class="metric-note">F<sub>out</sub> / F<sub>in</sub></div>
</div>
<div class="metric">
<div class="metric-label">DISPLACEMENT RATIO</div>
<div class="metric-value" id="displacementRatio">0.40 ×</div>
<div class="metric-note">d<sub>out</sub> / d<sub>in</sub></div>
</div>
<div class="metric wide">
<div class="metric-label">TRANSFERRED FLUID VOLUME</div>
<div class="metric-value" id="sweptVolume">600.0 cm³</div>
<div class="metric-note">A<sub>in</sub>d<sub>in</sub> = A<sub>out</sub>d<sub>out</sub></div>
</div>
</div>
<div class="section-title">Ideal Model</div>
<div class="formula">
V = A<sub>in</sub>d<sub>in</sub> = A<sub>out</sub>d<sub>out</sub><br />
d<sub>out</sub> = d<sub>in</sub>A<sub>in</sub> / A<sub>out</sub><br />
F<sub>out</sub>/F<sub>in</sub> = A<sub>out</sub>/A<sub>in</sub>
</div>
<div class="assumption">
<strong>Explicit model exclusions</strong>
This is an ideal kinematic and force-ratio model only. It deliberately
excludes leakage, fluid or gas compressibility, structural limits,
friction, pressure losses, elastic deformation, and cavitation.
The calculated values are not safety ratings or hardware limits.
</div>
</aside>
<section id="stage" aria-label="Interactive low-poly actuator scene">
<div class="hud">
<div class="pill"><strong>Drag</strong> to orbit</div>
<div class="pill"><strong>Scroll</strong> to zoom</div>
<div class="pill">Ideal volume conservation</div>
</div>
<div class="scene-label" id="inputLabel">
<strong>INPUT CYLINDER</strong>
<span id="inputLabelText"></span>
</div>
<div class="scene-label" id="outputLabel">
<strong>OUTPUT CYLINDER</strong>
<span id="outputLabelText"></span>
</div>
<div class="legend">
<div class="legend-row">
<span class="swatch cyan"></span> Input piston and motion
</div>
<div class="legend-row">
<span class="swatch orange"></span> Output piston and motion
</div>
<div class="legend-row">
<span class="swatch yellow"></span> Ideal fluid path
</div>
</div>
<div class="camera-hint">Scene motion is visually compressed; numbers remain exact.</div>
</section>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const state = {
ain: 20,
aout: 50,
displacement: 30
};
const stage = document.getElementById("stage");
// ------------------------------------------------------------
// Renderer, scene, camera, and lights
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
stage.insertBefore(renderer.domElement, stage.firstChild);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 15, 32);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.15, 0);
let azimuth = 0.72;
let elevation = 0.35;
let cameraDistance = 13.5;
function updateCamera() {
const horizontal = cameraDistance * Math.cos(elevation);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(azimuth),
cameraTarget.y + cameraDistance * Math.sin(elevation),
cameraTarget.z + horizontal * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xbfe8ff,
0x14202c,
2.1
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x55d9e9, 1.1);
fillLight.position.set(-7, 4, -4);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffb45c, 1.25);
rimLight.position.set(6, 3, -6);
scene.add(rimLight);
// ------------------------------------------------------------
// Low-poly environment
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
new THREE.MeshStandardMaterial({
color: 0x0b1a29,
roughness: 0.92,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x23445e, 0x162c40);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.45;
scene.add(grid);
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x8193a3,
roughness: 0.38,
metalness: 0.78,
flatShading: true
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x243746,
roughness: 0.5,
metalness: 0.65,
flatShading: true
});
const inputMaterial = new THREE.MeshStandardMaterial({
color: 0x55d9e9,
roughness: 0.4,
metalness: 0.45,
flatShading: true
});
const outputMaterial = new THREE.MeshStandardMaterial({
color: 0xffb45c,
roughness: 0.4,
metalness: 0.45,
flatShading: true
});
const glassMaterial = new THREE.MeshStandardMaterial({
color: 0x4aa9bd,
roughness: 0.3,
metalness: 0.1,
transparent: true,
opacity: 0.25,
depthWrite: false,
side: THREE.DoubleSide
});
const fluidMaterial = new THREE.MeshStandardMaterial({
color: 0xf5d36b,
roughness: 0.38,
metalness: 0.18,
flatShading: true
});
function makeBox(width, height, depth, material, x, y, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// Pedestals
makeBox(4.5, 0.28, 2.25, darkMetalMaterial, -3, 0.18);
makeBox(4.5, 0.28, 2.25, darkMetalMaterial, 3, 0.18);
function createCylinderAssembly(centerX, pistonMaterial) {
const group = new THREE.Group();
group.position.x = centerX;
scene.add(group);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(1, 1, 4, 12, 1, false),
glassMaterial
);
body.rotation.z = Math.PI / 2;
body.position.y = 1.65;
body.castShadow = true;
group.add(body);
// End rings and structural feet.
[-2.02, 2.02].forEach((x) => {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.03, 0.14, 6, 12),
metalMaterial
);
ring.rotation.y = Math.PI / 2;
ring.position.set(x, 1.65, 0);
ring.castShadow = true;
group.add(ring);
});
const piston = new THREE.Mesh(
new THREE.CylinderGeometry(0.82, 0.82, 0.28, 12),
pistonMaterial
);
piston.rotation.z = Math.PI / 2;
piston.position.y = 1.65;
piston.castShadow = true;
group.add(piston);
const pistonSeal = new THREE.Mesh(
new THREE.TorusGeometry(0.82, 0.07, 6, 12),
darkMetalMaterial
);
pistonSeal.rotation.y = Math.PI / 2;
pistonSeal.position.y = 1.65;
group.add(pistonSeal);
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 2.2, 10),
metalMaterial
);
rod.rotation.z = Math.PI / 2;
rod.position.y = 1.65;
rod.castShadow = true;
group.add(rod);
const rodEnd = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.55, 0.55),
pistonMaterial
);
rodEnd.position.y = 1.65;
rodEnd.castShadow = true;
group.add(rodEnd);
return { group, piston, pistonSeal, rod, rodEnd };
}
const inputAssembly = createCylinderAssembly(-3, inputMaterial);
const outputAssembly = createCylinderAssembly(3, outputMaterial);
// Fluid connection between the two cylinders.
const pipe = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.14, 2.2, 8),
fluidMaterial
);
pipe.rotation.z = Math.PI / 2;
pipe.position.set(0, 1.65, 0);
pipe.castShadow = true;
scene.add(pipe);
[-1.05, 1.05].forEach((x) => {
const port = new THREE.Mesh(
new THREE.CylinderGeometry(0.23, 0.23, 0.35, 8),
fluidMaterial
);
port.rotation.z = Math.PI / 2;
port.position.set(x, 1.65, 0);
port.castShadow = true;
scene.add(port);
});
// Center support for the fluid line.
makeBox(0.45, 1.05, 0.45, darkMetalMaterial, 0, 0.55);
const inputArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-5.25, 0.78, 0),
0.01,
0x55d9e9,
0.22,
0.13
);
scene.add(inputArrow);
const outputArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(5.35, 0.78, 0),
0.01,
0xffb45c,
0.22,
0.13
);
scene.add(outputArrow);
// ------------------------------------------------------------
// UI and calculation
// ------------------------------------------------------------
const ainRange = document.getElementById("ainRange");
const ainNumber = document.getElementById("ainNumber");
const aoutRange = document.getElementById("aoutRange");
const aoutNumber = document.getElementById("aoutNumber");
const displacementRange = document.getElementById("displacementRange");
const displacementNumber = document.getElementById("displacementNumber");
const outputMovement = document.getElementById("outputMovement");
const forceRatio = document.getElementById("forceRatio");
const displacementRatio = document.getElementById("displacementRatio");
const sweptVolume = document.getElementById("sweptVolume");
const inputLabelText = document.getElementById("inputLabelText");
const outputLabelText = document.getElementById("outputLabelText");
const numberFormatter = new Intl.NumberFormat(undefined, {
maximumFractionDigits: 2
});
function format(value) {
return numberFormatter.format(value);
}
function calculate() {
const volume = state.ain * state.displacement;
const movement = volume / state.aout;
const forceMultiplier = state.aout / state.ain;
const displacementMultiplier =
state.displacement > 0 ? movement / state.displacement : state.ain / state.aout;
return {
volume,
movement,
forceMultiplier,
displacementMultiplier
};
}
function updateScene(results) {
// The scene uses a compressed visual scale only. Numerical results
// are never clamped by this visual limit.
const visualScale = 0.022;
const visualInput = Math.min(2.2, state.displacement * visualScale);
const visualOutput = Math.min(2.2, results.movement * visualScale);
const inputPistonX = -0.2 + visualInput;
const outputPistonX = 0.2 + visualOutput;
inputAssembly.piston.position.x = inputPistonX;
inputAssembly.pistonSeal.position.x = inputPistonX;
inputAssembly.rod.position.x = inputPistonX - 1.1;
inputAssembly.rodEnd.position.x = inputPistonX - 2.2;
outputAssembly.piston.position.x = outputPistonX;
outputAssembly.pistonSeal.position.x = outputPistonX;
outputAssembly.rod.position.x = outputPistonX + 1.1;
outputAssembly.rodEnd.position.x = outputPistonX + 2.2;
const inputArrowLength = Math.max(0.01, visualInput);
const outputArrowLength = Math.max(0.01, visualOutput);
inputArrow.setLength(
inputArrowLength,
Math.min(0.22, inputArrowLength * 0.35),
Math.min(0.13, inputArrowLength * 0.22)
);
outputArrow.setLength(
outputArrowLength,
Math.min(0.22, outputArrowLength * 0.35),
Math.min(0.13, outputArrowLength * 0.22)
);
inputLabelText.textContent =
`Ain ${format(state.ain)} cm² · din ${format(state.displacement)} cm`;
outputLabelText.textContent =
`Aout ${format(state.aout)} cm² · dout ${format(results.movement)} cm`;
}
function refresh() {
const results = calculate();
outputMovement.textContent = `${format(results.movement)} cm`;
forceRatio.textContent = `${format(results.forceMultiplier)} ×`;
displacementRatio.textContent = `${format(results.displacementMultiplier)} ×`;
sweptVolume.textContent = `${format(results.volume)} cm³`;
updateScene(results);
}
function applyValue(key, rawValue, source) {
const value = Number.parseFloat(rawValue);
if (!Number.isFinite(value)) {
return;
}
const limits = {
ain: [1, 100],
aout: [1, 100],
displacement: [0, 100]
};
const [min, max] = limits[key];
state[key] = Math.min(max, Math.max(min, value));
const controls = {
ain: [ainRange, ainNumber],
aout: [aoutRange, aoutNumber],
displacement: [displacementRange, displacementNumber]
};
controls[key][0].value = state[key];
if (source !== "number") {
controls[key][1].value = state[key];
}
refresh();
}
function bindControl(key, range, number) {
range.addEventListener("input", () => {
applyValue(key, range.value, "range");
});
number.addEventListener("input", () => {
if (number.value !== "") {
applyValue(key, number.value, "number");
}
});
number.addEventListener("change", () => {
if (number.value === "") {
number.value = state[key];
} else {
applyValue(key, number.value, "number");
number.value = state[key];
}
});
}
bindControl("ain", ainRange, ainNumber);
bindControl("aout", aoutRange, aoutNumber);
bindControl("displacement", displacementRange, displacementNumber);
// ------------------------------------------------------------
// Direct camera controls: no Three.js addons
// ------------------------------------------------------------
let pointerActive = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerActive = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive) {
return;
}
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
azimuth -= deltaX * 0.008;
elevation = Math.min(
1.25,
Math.max(-0.05, elevation + deltaY * 0.006)
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function endPointer(event) {
pointerActive = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = Math.min(
23,
Math.max(7.5, cameraDistance + event.deltaY * 0.008)
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Labels, resize, and render loop
// ------------------------------------------------------------
const inputLabel = document.getElementById("inputLabel");
const outputLabel = document.getElementById("outputLabel");
const projected = new THREE.Vector3();
function positionLabel(element, worldX, worldY, worldZ) {
projected.set(worldX, worldY, worldZ).project(camera);
const x = (projected.x * 0.5 + 0.5) * stage.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * stage.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(stage);
resize();
refresh();
function animate() {
requestAnimationFrame(animate);
positionLabel(inputLabel, -3, 3.05, 0);
positionLabel(outputLabel, 3, 3.05, 0);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```