Prompt specification
Build or customize the Three.js 3D scene "Depth-pressure exploration tank" 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: Depth-pressure exploration tank
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/depth-pressure-exploration-tank-x1488
Scene Description Prompt:
Depth-pressure exploration tank. Allow changing liquid depth and density in an ideal hydrostatic tank; update pressure profiles consistently and keep atmospheric pressure separate from gauge pressure. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (depth-pressure-exploration-tank-x1488):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Depth / Pressure Explorer</title>
<style>
:root {
color-scheme: dark;
--bg: #050b12;
--panel: rgba(10, 21, 33, 0.92);
--panel-strong: #0c1927;
--line: rgba(137, 190, 218, 0.16);
--line-strong: rgba(137, 190, 218, 0.28);
--text: #e9f6ff;
--muted: #86a1b3;
--cyan: #54e6ff;
--mint: #64f2c3;
--violet: #ad91ff;
--amber: #ffbd5c;
--danger: #ff7f91;
--range-accent: var(--cyan);
}
* {
box-sizing: border-box;
}
html {
min-width: 320px;
background: var(--bg);
}
body {
margin: 0;
min-height: 100vh;
color: var(--text);
background:
radial-gradient(circle at 14% 4%, rgba(37, 126, 166, 0.16), transparent 34rem),
radial-gradient(circle at 90% 90%, rgba(100, 77, 168, 0.10), transparent 32rem),
var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
width: min(1540px, calc(100% - 32px));
margin: 0 auto;
padding: 20px 0 26px;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 4px 2px 20px;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
display: grid;
grid-template-columns: repeat(3, 8px);
grid-template-rows: repeat(3, 8px);
gap: 3px;
padding: 9px;
border: 1px solid rgba(84, 230, 255, 0.36);
border-radius: 11px;
background: rgba(84, 230, 255, 0.07);
box-shadow: 0 0 28px rgba(84, 230, 255, 0.10);
}
.brand-mark i {
display: block;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 8px rgba(84, 230, 255, 0.55);
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(4),
.brand-mark i:nth-child(8) {
opacity: 0.42;
}
.eyebrow,
.section-kicker,
.metric-label,
.badge,
.mini-label {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
text-transform: uppercase;
letter-spacing: 0.12em;
}
.eyebrow {
margin-bottom: 4px;
color: var(--cyan);
font-size: 10px;
font-weight: 700;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.2vw, 31px);
line-height: 1.1;
font-weight: 680;
letter-spacing: -0.035em;
}
.header-meta {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.badge {
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
background: rgba(10, 22, 34, 0.62);
font-size: 9px;
font-weight: 700;
}
.badge.live {
color: var(--mint);
border-color: rgba(100, 242, 195, 0.24);
background: rgba(100, 242, 195, 0.06);
}
.badge.live::before {
content: "";
display: inline-block;
width: 6px;
height: 6px;
margin-right: 7px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 9px var(--mint);
}
.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
gap: 16px;
align-items: start;
}
.viewer-card {
position: relative;
height: calc(100vh - 138px);
min-height: 640px;
max-height: 880px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 18px;
background:
radial-gradient(circle at 50% 38%, rgba(40, 101, 133, 0.14), transparent 45%),
linear-gradient(160deg, #091522, #050c14 72%);
box-shadow:
0 24px 80px rgba(0, 0, 0, 0.35),
inset 0 1px 0 rgba(255, 255, 255, 0.025);
}
.viewer-card::before {
content: "";
position: absolute;
inset: 0;
z-index: 1;
pointer-events: none;
background:
linear-gradient(rgba(84, 230, 255, 0.018) 1px, transparent 1px),
linear-gradient(90deg, rgba(84, 230, 255, 0.018) 1px, transparent 1px);
background-size: 40px 40px;
mask-image: linear-gradient(to bottom, black, transparent 78%);
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.viewer-top {
position: absolute;
z-index: 3;
top: 20px;
left: 20px;
right: 20px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.viewer-title {
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(5, 13, 22, 0.66);
backdrop-filter: blur(12px);
}
.section-kicker {
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.viewer-title strong {
display: block;
margin-top: 5px;
font-size: 14px;
font-weight: 600;
}
.viewer-title span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
}
.camera-reset {
pointer-events: auto;
border: 1px solid var(--line-strong);
border-radius: 9px;
padding: 9px 11px;
color: #bdd1df;
background: rgba(5, 13, 22, 0.72);
cursor: pointer;
font-size: 11px;
backdrop-filter: blur(10px);
}
.camera-reset:hover {
color: var(--cyan);
border-color: rgba(84, 230, 255, 0.45);
}
.viewer-bottom {
position: absolute;
z-index: 3;
left: 20px;
right: 20px;
bottom: 18px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.scene-legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.scene-chip {
display: flex;
align-items: center;
gap: 7px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(5, 13, 22, 0.66);
color: #b7cbd9;
font-size: 10px;
backdrop-filter: blur(10px);
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 8px currentColor;
}
.dot.amber {
background: var(--amber);
}
.dot.blue {
background: #438dff;
}
.navigation-hint {
color: #7892a4;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
text-align: right;
}
.world-label {
position: absolute;
z-index: 2;
display: none;
transform: translate(-50%, -50%);
pointer-events: none;
white-space: nowrap;
padding: 6px 8px;
border: 1px solid var(--line-strong);
border-radius: 6px;
background: rgba(4, 11, 18, 0.78);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
letter-spacing: 0.035em;
backdrop-filter: blur(7px);
}
.world-label.atmosphere {
color: var(--amber);
border-color: rgba(255, 189, 92, 0.35);
}
.world-label.surface {
color: var(--cyan);
border-color: rgba(84, 230, 255, 0.34);
}
.world-label.bottom {
color: var(--mint);
border-color: rgba(100, 242, 195, 0.32);
}
.world-label.ruler {
padding: 3px 5px;
color: #8ca5b6;
background: rgba(4, 11, 18, 0.54);
font-size: 8px;
}
.panel {
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 24px 80px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.025);
overflow: hidden;
}
.panel-section {
padding: 19px 20px;
border-bottom: 1px solid var(--line);
}
.panel-section:last-child {
border-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 17px;
}
.section-heading h2 {
margin: 0;
font-size: 14px;
font-weight: 650;
letter-spacing: -0.015em;
}
.section-number {
color: #496579;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
}
.control {
margin-bottom: 21px;
}
.control:last-child {
margin-bottom: 0;
}
.control-header {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.control-header label {
color: #c8d9e4;
font-size: 12px;
font-weight: 550;
}
.control-header output {
color: var(--cyan);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
font-weight: 700;
}
input[type="range"] {
--fill: 50%;
width: 100%;
height: 4px;
margin: 7px 0;
border-radius: 99px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--range-accent) 0%,
var(--range-accent) var(--fill),
rgba(130, 171, 194, 0.16) var(--fill),
rgba(130, 171, 194, 0.16) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid #08131e;
border-radius: 50%;
appearance: none;
background: var(--range-accent);
box-shadow: 0 0 0 1px var(--range-accent), 0 0 14px color-mix(in srgb, var(--range-accent) 50%, transparent);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #08131e;
border-radius: 50%;
background: var(--range-accent);
box-shadow: 0 0 0 1px var(--range-accent);
}
input[type="range"]:focus-visible {
box-shadow: 0 0 0 3px rgba(84, 230, 255, 0.14);
}
.range-scale {
display: flex;
justify-content: space-between;
color: #607b8d;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
}
.control-note {
margin-top: 8px;
color: #7892a4;
font-size: 10px;
line-height: 1.5;
}
.separation-note {
padding: 10px 11px;
border: 1px solid rgba(255, 189, 92, 0.18);
border-radius: 9px;
color: #c6ad83;
background: rgba(255, 189, 92, 0.045);
font-size: 10px;
line-height: 1.55;
}
.presets {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 7px;
margin-top: 13px;
}
.preset {
min-height: 34px;
padding: 8px 9px;
border: 1px solid var(--line);
border-radius: 8px;
color: #9fb5c5;
background: rgba(6, 15, 24, 0.60);
cursor: pointer;
font-size: 10px;
transition: 150ms ease;
}
.preset:hover,
.preset.active {
color: var(--cyan);
border-color: rgba(84, 230, 255, 0.32);
background: rgba(84, 230, 255, 0.065);
}
.metrics {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 9px;
}
.metric {
min-height: 100px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(5, 13, 21, 0.48);
}
.metric.primary {
border-color: rgba(84, 230, 255, 0.26);
background: linear-gradient(145deg, rgba(84, 230, 255, 0.09), rgba(5, 13, 21, 0.46));
}
.metric.absolute {
border-color: rgba(173, 145, 255, 0.25);
background: linear-gradient(145deg, rgba(173, 145, 255, 0.08), rgba(5, 13, 21, 0.46));
}
.metric-label {
color: #7791a3;
font-size: 8px;
font-weight: 700;
}
.metric strong {
display: block;
margin-top: 8px;
color: var(--cyan);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 18px;
font-weight: 700;
}
.metric.absolute strong {
color: var(--violet);
}
.metric small {
display: block;
margin-top: 5px;
color: #617b8c;
font-size: 9px;
}
.equation-box {
margin-top: 11px;
padding: 12px 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: #07111b;
}
.equation-line {
display: flex;
justify-content: space-between;
gap: 12px;
padding: 5px 0;
color: #839baa;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
}
.equation-line strong {
color: #cce0eb;
font-weight: 600;
text-align: right;
}
.equation-line.gauge strong {
color: var(--cyan);
}
.equation-line.absolute strong {
color: var(--violet);
}
.profile-wrap {
position: relative;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 11px;
background: #07111b;
}
#profileCanvas {
display: block;
width: 100%;
height: 245px;
}
.profile-legend {
display: flex;
flex-wrap: wrap;
gap: 14px;
padding: 10px 12px;
border-top: 1px solid var(--line);
color: #829cac;
font-size: 9px;
}
.profile-legend span {
display: flex;
align-items: center;
gap: 6px;
}
.profile-legend .line {
width: 16px;
height: 2px;
background: var(--cyan);
}
.profile-legend .line.absolute {
background: var(--violet);
}
.profile-legend .line.atmosphere {
height: 0;
border-top: 1px dashed var(--amber);
background: transparent;
}
.sample-tooltip {
position: absolute;
z-index: 4;
display: none;
min-width: 160px;
padding: 8px 9px;
border: 1px solid rgba(84, 230, 255, 0.34);
border-radius: 7px;
color: #d8e9f3;
background: rgba(4, 11, 18, 0.94);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
pointer-events: none;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
line-height: 1.55;
}
.profile-hint {
margin-top: 9px;
color: #668294;
font-size: 9px;
line-height: 1.5;
}
.assumptions {
color: #728c9d;
font-size: 10px;
line-height: 1.65;
}
.assumptions strong {
color: #aec3d1;
font-weight: 600;
}
.footer {
display: flex;
justify-content: space-between;
gap: 18px;
padding: 13px 4px 0;
color: #526d7f;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
}
@media (max-width: 1050px) {
.workspace {
grid-template-columns: minmax(0, 1fr) 350px;
}
.panel-section {
padding: 17px;
}
}
@media (max-width: 860px) {
.workspace {
grid-template-columns: 1fr;
}
.viewer-card {
height: 66vh;
min-height: 520px;
}
.panel {
width: 100%;
}
.header-meta .badge:not(.live) {
display: none;
}
}
@media (max-width: 540px) {
.app {
width: min(100% - 18px, 1540px);
padding-top: 12px;
}
.header {
padding-bottom: 14px;
}
.header-meta {
display: none;
}
.viewer-card {
min-height: 500px;
border-radius: 14px;
}
.viewer-top {
top: 12px;
left: 12px;
right: 12px;
}
.viewer-title {
padding: 9px 10px;
}
.viewer-title span {
display: none;
}
.viewer-bottom {
left: 12px;
right: 12px;
bottom: 12px;
}
.navigation-hint {
display: none;
}
.scene-chip {
padding: 6px 8px;
font-size: 9px;
}
.metrics {
grid-template-columns: 1fr 1fr;
}
.footer {
flex-direction: column;
gap: 5px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="header">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
<i></i><i></i><i></i>
<i></i><i></i><i></i>
</div>
<div>
<div class="eyebrow">Hydrostatic Systems / Voxel Lab</div>
<h1>Depth / Pressure Explorer</h1>
</div>
</div>
<div class="header-meta">
<div class="badge live">Live physical model</div>
<div class="badge">Static · incompressible</div>
<div class="badge">g = 9.81 m/s²</div>
</div>
</header>
<main class="workspace">
<section class="viewer-card" aria-label="Interactive 3D voxel tank">
<div id="viewport"></div>
<div class="viewer-top">
<div class="viewer-title">
<div class="section-kicker">Virtual test chamber</div>
<strong>Visible voxel liquid field</strong>
<span id="viewerState">2.40 m water column · 1,000 kg/m³</span>
</div>
<button class="camera-reset" id="resetCamera" type="button">Reset view</button>
</div>
<div class="world-label atmosphere" id="atmosphereLabel">
ATMOSPHERE · 101.3 kPa absolute
</div>
<div class="world-label surface" id="surfaceLabel">
SURFACE · 0.00 kPa gauge
</div>
<div class="world-label bottom" id="bottomLabel">
BOTTOM · 23.54 kPa gauge
</div>
<div class="world-label ruler" id="ruler0">0 m</div>
<div class="world-label ruler" id="ruler2">2 m</div>
<div class="world-label ruler" id="ruler4">4 m</div>
<div class="viewer-bottom">
<div class="scene-legend">
<div class="scene-chip"><span class="dot"></span> Liquid voxels</div>
<div class="scene-chip"><span class="dot amber"></span> Atmospheric load</div>
<div class="scene-chip"><span class="dot blue"></span> Tank structure</div>
</div>
<div class="navigation-hint">DRAG TO ORBIT · SCROLL / PINCH TO ZOOM · DOUBLE-CLICK TO RESET</div>
</div>
</section>
<aside class="panel">
<section class="panel-section">
<div class="section-heading">
<h2>Experiment controls</h2>
<span class="section-number">01 / INPUT</span>
</div>
<div class="control">
<div class="control-header">
<label for="depthInput">Liquid depth</label>
<output id="depthOutput" for="depthInput">2.40 m</output>
</div>
<input
id="depthInput"
type="range"
min="0.25"
max="4"
step="0.01"
value="2.4"
aria-label="Liquid depth in meters"
/>
<div class="range-scale"><span>0.25 m</span><span>4.00 m</span></div>
</div>
<div class="control">
<div class="control-header">
<label for="densityInput">Liquid density</label>
<output id="densityOutput" for="densityInput">1,000 kg/m³</output>
</div>
<input
id="densityInput"
type="range"
min="500"
max="1500"
step="10"
value="1000"
style="--range-accent: var(--mint)"
aria-label="Liquid density in kilograms per cubic meter"
/>
<div class="range-scale"><span>500 kg/m³</span><span>1,500 kg/m³</span></div>
</div>
<div class="control">
<div class="control-header">
<label for="atmosphereInput">Atmospheric pressure</label>
<output id="atmosphereOutput" for="atmosphereInput">101.3 kPa</output>
</div>
<input
id="atmosphereInput"
type="range"
min="50"
max="120"
step="0.1"
value="101.3"
style="--range-accent: var(--amber)"
aria-label="Atmospheric pressure in kilopascals"
/>
<div class="range-scale"><span>50 kPa</span><span>120 kPa</span></div>
<div class="control-note">
Surface absolute pressure. This shifts only the absolute-pressure curve.
</div>
</div>
<div class="separation-note">
<strong>Gauge ≠ absolute.</strong> Gauge pressure excludes the atmosphere.
Changing atmospheric pressure must not change <em>ρgh</em>.
</div>
<div class="presets" aria-label="Experiment presets">
<button class="preset active" type="button" data-preset="fresh">Fresh water</button>
<button class="preset" type="button" data-preset="salt">Salt water</button>
<button class="preset" type="button" data-preset="light">Light liquid</button>
<button class="preset" type="button" data-preset="deep">Deep column</button>
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Pressure readout</h2>
<span class="section-number">02 / RESULT</span>
</div>
<div class="metrics">
<div class="metric primary">
<div class="metric-label">Gauge at bottom</div>
<strong id="gaugeBottom">23.54 kPa</strong>
<small>Hydrostatic pressure above surface</small>
</div>
<div class="metric absolute">
<div class="metric-label">Absolute at bottom</div>
<strong id="absoluteBottom">124.84 kPa</strong>
<small>Atmospheric + hydrostatic</small>
</div>
<div class="metric">
<div class="metric-label">Surface absolute</div>
<strong id="surfaceAbsolute">101.30 kPa</strong>
<small>Equal to atmosphere</small>
</div>
<div class="metric">
<div class="metric-label">Pressure gradient</div>
<strong id="pressureGradient">9.81 kPa/m</strong>
<small>Constant for one liquid</small>
</div>
</div>
<div class="equation-box">
<div class="equation-line gauge">
<span>p<sub>g</sub>(h) = ρgh</span>
<strong id="gaugeEquation">23,542 Pa</strong>
</div>
<div class="equation-line absolute">
<span>p<sub>abs</sub>(h) = p<sub>atm</sub> + p<sub>g</sub>(h)</span>
<strong id="absoluteEquation">124,842 Pa</strong>
</div>
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Pressure profile</h2>
<span class="section-number">03 / PROFILE</span>
</div>
<div class="profile-wrap" id="profileWrap">
<canvas
id="profileCanvas"
aria-label="Pressure versus depth graph showing gauge and absolute pressure"
></canvas>
<div class="profile-legend">
<span><i class="line"></i> Gauge</span>
<span><i class="line absolute"></i> Absolute</span>
<span><i class="line atmosphere"></i> Atmosphere</span>
</div>
<div class="sample-tooltip" id="sampleTooltip"></div>
</div>
<div class="profile-hint">
Hover over the graph to sample pressure at any depth. Gauge starts at zero;
absolute starts at atmospheric pressure.
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Model assumptions</h2>
<span class="section-number">04 / INFO</span>
</div>
<div class="assumptions">
<strong>Hydrostatic model:</strong> a stationary, incompressible liquid with constant
density. Pressure varies linearly with vertical depth. The tank is treated as
open to the selected atmosphere, so the free surface has zero gauge pressure and
atmospheric absolute pressure.
</div>
</section>
</aside>
</main>
<footer class="footer">
<span>THREE.JS CORE / VOXEL HYDROSTATICS / NO EXTERNAL ADDONS</span>
<span>Pa = N/m² · kPa = 1000 Pa · h measured downward from the free surface</span>
</footer>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const state = {
depth: 2.40,
density: 1000,
atmosphere: 101.3
};
const GRAVITY = 9.81;
const MAX_DEPTH = 4.0;
const ui = {
depth: document.getElementById("depthInput"),
density: document.getElementById("densityInput"),
atmosphere: document.getElementById("atmosphereInput"),
depthOutput: document.getElementById("depthOutput"),
densityOutput: document.getElementById("densityOutput"),
atmosphereOutput: document.getElementById("atmosphereOutput"),
gaugeBottom: document.getElementById("gaugeBottom"),
absoluteBottom: document.getElementById("absoluteBottom"),
surfaceAbsolute: document.getElementById("surfaceAbsolute"),
pressureGradient: document.getElementById("pressureGradient"),
gaugeEquation: document.getElementById("gaugeEquation"),
absoluteEquation: document.getElementById("absoluteEquation"),
viewerState: document.getElementById("viewerState"),
atmosphereLabel: document.getElementById("atmosphereLabel"),
surfaceLabel: document.getElementById("surfaceLabel"),
bottomLabel: document.getElementById("bottomLabel"),
ruler0: document.getElementById("ruler0"),
ruler2: document.getElementById("ruler2"),
ruler4: document.getElementById("ruler4"),
resetCamera: document.getElementById("resetCamera"),
profileCanvas: document.getElementById("profileCanvas"),
profileWrap: document.getElementById("profileWrap"),
sampleTooltip: document.getElementById("sampleTooltip")
};
// ---------- Three.js scene ----------
const viewport = document.getElementById("viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional voxel tank. Drag to orbit and scroll to zoom."
);
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07111c, 10, 21);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.92, 0);
const cameraState = {
theta: 0.78,
phi: 1.12,
radius: 8.5
};
const cameraGoal = {
theta: cameraState.theta,
phi: cameraState.phi,
radius: cameraState.radius
};
function resetCamera() {
cameraGoal.theta = 0.78;
cameraGoal.phi = 1.12;
cameraGoal.radius = 8.5;
}
const hemisphere = new THREE.HemisphereLight(0x9cdcff, 0x07101a, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd9f5ff, 3.25);
keyLight.position.set(5.5, 8.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x32cfff, 18, 8, 2);
cyanLight.position.set(-3.8, 3.2, 2.8);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffa84a, 11, 6, 2);
amberLight.position.set(2.4, 5.2, -1.8);
scene.add(amberLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x061019,
roughness: 0.9,
metalness: 0.12
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(28, 28),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.26;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(16, 32, 0x2a5369, 0x143040);
grid.position.y = -0.25;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
// Tank structure built from separated voxel-like blocks.
const frameTransforms = [];
function addBlock(x, y, z, sx, sy, sz) {
frameTransforms.push({ x, y, z, sx, sy, sz });
}
// Floor blocks.
for (let ix = 0; ix < 5; ix++) {
for (let iz = 0; iz < 3; iz++) {
addBlock(
-1.26 + ix * 0.63,
-0.115,
-0.59 + iz * 0.59,
0.58,
0.20,
0.54
);
}
}
// Corner columns.
for (const x of [-1.73, 1.73]) {
for (const z of [-0.92, 0.92]) {
for (let level = 0; level < 12; level++) {
addBlock(x, 0.12 + level * 0.36, z, 0.16, 0.32, 0.16);
}
}
}
// Top side rails.
for (const x of [-1.73, 1.73]) {
for (let iz = 0; iz < 3; iz++) {
addBlock(x, 4.13, -0.59 + iz * 0.59, 0.16, 0.12, 0.54);
}
}
// Top front and back rails.
for (const z of [-0.92, 0.92]) {
for (let ix = 0; ix < 5; ix++) {
addBlock(-1.26 + ix * 0.63, 4.13, z, 0.58, 0.12, 0.14);
}
}
// Rear intermediate posts.
for (const x of [-0.63, 0.63]) {
for (let level = 0; level < 12; level++) {
addBlock(x, 0.12 + level * 0.36, -0.92, 0.10, 0.32, 0.10);
}
}
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x294459,
roughness: 0.46,
metalness: 0.58
});
const frameMesh = new THREE.InstancedMesh(
unitBox,
frameMaterial,
frameTransforms.length
);
frameTransforms.forEach((transform, index) => {
dummy.position.set(transform.x, transform.y, transform.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(transform.sx, transform.sy, transform.sz);
dummy.updateMatrix();
frameMesh.setMatrixAt(index, dummy.matrix);
});
frameMesh.instanceMatrix.needsUpdate = true;
frameMesh.castShadow = true;
frameMesh.receiveShadow = true;
scene.add(frameMesh);
// Subtle glass shell.
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x8bdfff,
transparent: true,
opacity: 0.055,
roughness: 0.12,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
});
function addGlassPanel(width, height, depth, x, y, z) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const panel = new THREE.Mesh(geometry, glassMaterial);
panel.position.set(x, y, z);
scene.add(panel);
}
addGlassPanel(3.60, 4.20, 0.035, 0, 2.00, -0.96);
addGlassPanel(0.035, 4.20, 1.88, -1.82, 2.00, 0);
addGlassPanel(0.035, 4.20, 1.88, 1.82, 2.00, 0);
const tankOutline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(3.62, 4.18, 1.92)),
new THREE.LineBasicMaterial({
color: 0x78b5d2,
transparent: true,
opacity: 0.22
})
);
tankOutline.position.y = 2.0;
scene.add(tankOutline);
// Depth ruler.
const rulerGroup = new THREE.Group();
const rulerStem = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 4.02, 0.025),
new THREE.MeshStandardMaterial({
color: 0x416276,
roughness: 0.6,
metalness: 0.35
})
);
rulerStem.position.set(2.08, 2.0, 0.93);
rulerGroup.add(rulerStem);
const tickMaterial = new THREE.MeshStandardMaterial({
color: 0x6e9aae,
roughness: 0.55,
metalness: 0.3
});
for (let depth = 0; depth <= MAX_DEPTH + 0.001; depth += 0.5) {
const major = Math.abs(depth % 1) < 0.001;
const tick = new THREE.Mesh(
new THREE.BoxGeometry(major ? 0.23 : 0.14, 0.028, 0.055),
tickMaterial
);
tick.position.set(2.08, depth, 0.93);
rulerGroup.add(tick);
}
scene.add(rulerGroup);
// Voxel liquid.
const liquidMaterial = new THREE.MeshPhysicalMaterial({
color: 0xffffff,
roughness: 0.2,
metalness: 0.04,
clearcoat: 0.65,
clearcoatRoughness: 0.22,
transparent: true,
opacity: 0.78
});
const liquidMesh = new THREE.InstancedMesh(
unitBox,
liquidMaterial,
400
);
liquidMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
liquidMesh.count = 0;
liquidMesh.castShadow = true;
liquidMesh.receiveShadow = true;
liquidMesh.frustumCulled = false;
scene.add(liquidMesh);
const COLS_X = 5;
const COLS_Z = 3;
const CELL_X = 0.56;
const CELL_Z = 0.50;
const CELL_HEIGHT = 0.215;
const LAYER_PITCH = 0.24;
const surfaceMaterial = new THREE.MeshPhysicalMaterial({
color: 0x6ee9ff,
emissive: 0x0c5670,
emissiveIntensity: 0.35,
transparent: true,
opacity: 0.28,
roughness: 0.15,
metalness: 0,
depthWrite: false
});
const surfaceMesh = new THREE.Mesh(
new THREE.BoxGeometry(3.10, 0.025, 1.62),
surfaceMaterial
);
scene.add(surfaceMesh);
const surfaceEdge = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(3.10, 0.025, 1.62)),
new THREE.LineBasicMaterial({
color: 0xa4f3ff,
transparent: true,
opacity: 0.78
})
);
scene.add(surfaceEdge);
// Atmospheric load plate and downward arrows, intentionally amber.
const atmosphereGroup = new THREE.Group();
const atmospherePlate = new THREE.Mesh(
new THREE.BoxGeometry(3.02, 0.025, 1.55),
new THREE.MeshStandardMaterial({
color: 0xffbd5c,
emissive: 0x6f3900,
emissiveIntensity: 0.5,
transparent: true,
opacity: 0.17,
depthWrite: false
})
);
atmospherePlate.position.y = 0.045;
atmosphereGroup.add(atmospherePlate);
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0xffbd5c,
emissive: 0x7a3d00,
emissiveIntensity: 0.65,
transparent: true,
opacity: 0.76,
roughness: 0.42,
metalness: 0.25
});
for (const x of [-1.1, -0.55, 0, 0.55, 1.1]) {
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.017, 0.017, 0.17, 8),
arrowMaterial
);
shaft.position.set(x, 0.19, 0.24);
atmosphereGroup.add(shaft);
const cone = new THREE.Mesh(
new THREE.ConeGeometry(0.065, 0.13, 8),
arrowMaterial
);
cone.position.set(x, 0.335, 0.24);
cone.rotation.z = Math.PI;
atmosphereGroup.add(cone);
}
scene.add(atmosphereGroup);
// ---------- Physical calculations ----------
function gaugePressureAt(depth, density = state.density) {
return density * GRAVITY * depth;
}
function absolutePressureAt(depth, density = state.density) {
return state.atmosphere * 1000 + gaugePressureAt(depth, density);
}
function formatKPa(pascals, digits = 2) {
return `${(pascals / 1000).toFixed(digits)} kPa`;
}
function updateLiquidVoxels() {
let instanceIndex = 0;
const color = new THREE.Color();
for (let layer = 0; layer < 20; layer++) {
const layerBottom = layer * LAYER_PITCH;
if (layerBottom >= state.depth) break;
const layerHeight = Math.min(CELL_HEIGHT, state.depth - layerBottom);
if (layerHeight <= 0.001) continue;
for (let ix = 0; ix < COLS_X; ix++) {
for (let iz = 0; iz < COLS_Z; iz++) {
const x = (ix - (COLS_X - 1) / 2) * 0.63;
const z = (iz - (COLS_Z - 1) / 2) * 0.59;
const y = layerBottom + layerHeight / 2;
dummy.position.set(x, y, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(CELL_X, layerHeight, CELL_Z);
dummy.updateMatrix();
liquidMesh.setMatrixAt(instanceIndex, dummy.matrix);
const normalizedDepth = THREE.MathUtils.clamp(y / MAX_DEPTH, 0, 1);
color.setHSL(
0.53 - normalizedDepth * 0.37,
0.84,
0.57 - normalizedDepth * 0.17
);
liquidMesh.setColorAt(instanceIndex, color);
instanceIndex++;
}
}
}
liquidMesh.count = instanceIndex;
liquidMesh.instanceMatrix.needsUpdate = true;
if (liquidMesh.instanceColor) {
liquidMesh.instanceColor.needsUpdate = true;
}
}
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function updatePresetState() {
const presets = document.querySelectorAll("[data-preset]");
presets.forEach((button) => {
const type = button.dataset.preset;
let active = false;
if (type === "fresh") {
active = state.density === 1000;
} else if (type === "salt") {
active = state.density === 1025;
} else if (type === "light") {
active = state.density === 800;
} else if (type === "deep") {
active = state.density === 1025 && Math.abs(state.depth - 3.6) < 0.001;
}
button.classList.toggle("active", active);
});
}
function updateModel() {
updateLiquidVoxels();
surfaceMesh.position.y = state.depth + 0.014;
surfaceEdge.position.y = state.depth + 0.017;
atmosphereGroup.position.y = state.depth;
const gaugeBottom = gaugePressureAt(state.depth);
const absoluteBottom = absolutePressureAt(state.depth);
ui.depthOutput.textContent = `${state.depth.toFixed(2)} m`;
ui.densityOutput.textContent = `${state.density.toLocaleString()} kg/m³`;
ui.atmosphereOutput.textContent = `${state.atmosphere.toFixed(1)} kPa`;
ui.gaugeBottom.textContent = formatKPa(gaugeBottom);
ui.absoluteBottom.textContent = formatKPa(absoluteBottom);
ui.surfaceAbsolute.textContent = `${state.atmosphere.toFixed(2)} kPa`;
ui.pressureGradient.textContent = `${((state.density * GRAVITY) / 1000).toFixed(2)} kPa/m`;
ui.gaugeEquation.textContent = `${Math.round(gaugeBottom).toLocaleString()} Pa`;
ui.absoluteEquation.textContent = `${Math.round(absoluteBottom).toLocaleString()} Pa`;
ui.viewerState.textContent =
`${state.depth.toFixed(2)} m water column · ` +
`${state.density.toLocaleString()} kg/m³`;
ui.atmosphereLabel.textContent =
`ATMOSPHERE · ${state.atmosphere.toFixed(1)} kPa absolute`;
ui.surfaceLabel.textContent =
`SURFACE · 0.00 kPa gauge`;
ui.bottomLabel.textContent =
`BOTTOM · ${(gaugeBottom / 1000).toFixed(2)} kPa gauge`;
updateRangeFill(ui.depth);
updateRangeFill(ui.density);
updateRangeFill(ui.atmosphere);
updatePresetState();
drawProfile();
}
// ---------- Pressure profile chart ----------
const profileContext = ui.profileCanvas.getContext("2d");
let plotMap = null;
function resizeProfileCanvas() {
const rect = ui.profileCanvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio, 2);
const width = Math.max(1, Math.round(rect.width * dpr));
const height = Math.max(1, Math.round(rect.height * dpr));
if (ui.profileCanvas.width !== width || ui.profileCanvas.height !== height) {
ui.profileCanvas.width = width;
ui.profileCanvas.height = height;
}
drawProfile();
}
function drawProfile() {
const rect = ui.profileCanvas.getBoundingClientRect();
if (rect.width < 10 || rect.height < 10) return;
const width = rect.width;
const height = rect.height;
const dpr = Math.min(window.devicePixelRatio, 2);
profileContext.setTransform(dpr, 0, 0, dpr, 0, 0);
profileContext.clearRect(0, 0, width, height);
const margin = {
left: 42,
right: 17,
top: 24,
bottom: 34
};
const plotWidth = width - margin.left - margin.right;
const plotHeight = height - margin.top - margin.bottom;
const gaugeBottomKPa = gaugePressureAt(state.depth) / 1000;
const absoluteBottomKPa = absolutePressureAt(state.depth) / 1000;
const requiredMaximum = Math.max(state.atmosphere, absoluteBottomKPa) * 1.12;
const maxPressure = Math.max(150, Math.ceil(requiredMaximum / 50) * 50);
const pressureStep = maxPressure <= 200 ? 25 : maxPressure <= 400 ? 50 : 100;
const x = pressure => margin.left + (pressure / maxPressure) * plotWidth;
const y = depth => margin.top + (depth / MAX_DEPTH) * plotHeight;
plotMap = { margin, plotWidth, plotHeight, maxPressure, x, y };
// Plot background.
profileContext.fillStyle = "#07111b";
profileContext.fillRect(0, 0, width, height);
profileContext.fillStyle = "rgba(84, 230, 255, 0.018)";
profileContext.fillRect(
margin.left,
margin.top,
plotWidth,
plotHeight
);
// Pressure grid.
profileContext.font = '9px "SFMono-Regular", Consolas, monospace';
profileContext.textAlign = "center";
profileContext.textBaseline = "top";
for (let pressure = 0; pressure <= maxPressure + 0.1; pressure += pressureStep) {
const px = x(pressure);
profileContext.strokeStyle = "rgba(128, 171, 194, 0.10)";
profileContext.lineWidth = 1;
profileContext.beginPath();
profileContext.moveTo(px, margin.top);
profileContext.lineTo(px, margin.top + plotHeight);
profileContext.stroke();
profileContext.fillStyle = "#607b8d";
profileContext.fillText(String(pressure), px, margin.top + plotHeight + 8);
}
// Depth grid.
profileContext.textAlign = "right";
profileContext.textBaseline = "middle";
for (let depth = 0; depth <= MAX_DEPTH + 0.001; depth += 0.5) {
const py = y(depth);
profileContext.strokeStyle = "rgba(128, 171, 194, 0.10)";
profileContext.beginPath();
profileContext.moveTo(margin.left, py);
profileContext.lineTo(margin.left + plotWidth, py);
profileContext.stroke();
profileContext.fillStyle = "#607b8d";
profileContext.fillText(
depth.toFixed(1),
margin.left - 8,
py
);
}
// Atmospheric pressure is a constant vertical line on a pressure/depth plot.
const atmosphereX = x(state.atmosphere);
profileContext.save();
profileContext.setLineDash([4, 4]);
profileContext.strokeStyle = "rgba(255, 189, 92, 0.72)";
profileContext.lineWidth = 1;
profileContext.beginPath();
profileContext.moveTo(atmosphereX, margin.top);
profileContext.lineTo(atmosphereX, margin.top + plotHeight);
profileContext.stroke();
profileContext.restore();
profileContext.fillStyle = "#ffbd5c";
profileContext.font = '8px "SFMono-Regular", Consolas, monospace';
profileContext.textAlign = "left";
profileContext.textBaseline = "top";
profileContext.fillText(
"p_atm",
Math.min(atmosphereX + 5, width - 35),
margin.top + 4
);
// Gauge-pressure area.
const fillGradient = profileContext.createLinearGradient(
margin.left,
margin.top,
x(gaugeBottomKPa),
margin.top + plotHeight
);
fillGradient.addColorStop(0, "rgba(84, 230, 255, 0.25)");
fillGradient.addColorStop(1, "rgba(84, 230, 255, 0.025)");
profileContext.beginPath();
profileContext.moveTo(x(0), y(0));
for (let i = 0; i <= 100; i++) {
const depth = state.depth * (i / 100);
profileContext.lineTo(
x(gaugePressureAt(depth) / 1000),
y(depth)
);
}
profileContext.lineTo(x(0), y(state.depth));
profileContext.closePath();
profileContext.fillStyle = fillGradient;
profileContext.fill();
// Gauge curve.
profileContext.beginPath();
profileContext.moveTo(x(0), y(0));
for (let i = 0; i <= 100; i++) {
const depth = state.depth * (i / 100);
profileContext.lineTo(
x(gaugePressureAt(depth) / 1000),
y(depth)
);
}
profileContext.strokeStyle = "#54e6ff";
profileContext.lineWidth = 2.25;
profileContext.stroke();
// Absolute curve.
profileContext.beginPath();
profileContext.moveTo(x(state.atmosphere), y(0));
for (let i = 0; i <= 100; i++) {
const depth = state.depth * (i / 100);
profileContext.lineTo(
x(absolutePressureAt(depth) / 1000),
y(depth)
);
}
profileContext.strokeStyle = "#ad91ff";
profileContext.lineWidth = 2.25;
profileContext.stroke();
// Current bottom marker.
const bottomY = y(state.depth);
profileContext.save();
profileContext.setLineDash([3, 4]);
profileContext.strokeStyle = "rgba(221, 239, 248, 0.30)";
profileContext.lineWidth = 1;
profileContext.beginPath();
profileContext.moveTo(margin.left, bottomY);
profileContext.lineTo(margin.left + plotWidth, bottomY);
profileContext.stroke();
profileContext.restore();
for (const [pressure, color] of [
[gaugeBottomKPa, "#54e6ff"],
[absoluteBottomKPa, "#ad91ff"]
]) {
profileContext.beginPath();
profileContext.arc(x(pressure), bottomY, 3.5, 0, Math.PI * 2);
profileContext.fillStyle = color;
profileContext.fill();
profileContext.strokeStyle = "#07111b";
profileContext.lineWidth = 1.5;
profileContext.stroke();
}
// Axis labels.
profileContext.fillStyle = "#7892a4";
profileContext.font = '8px "SFMono-Regular", Consolas, monospace';
profileContext.textAlign = "center";
profileContext.textBaseline = "bottom";
profileContext.fillText(
"PRESSURE (kPa)",
margin.left + plotWidth / 2,
height - 1
);
profileContext.save();
profileContext.translate(10, margin.top + plotHeight / 2);
profileContext.rotate(-Math.PI / 2);
profileContext.fillText("DEPTH (m)", 0, 0);
profileContext.restore();
profileContext.fillStyle = "#9cb2c1";
profileContext.textAlign = "right";
profileContext.textBaseline = "bottom";
profileContext.fillText(
`current bottom: ${state.depth.toFixed(2)} m`,
margin.left + plotWidth,
bottomY - 4
);
}
ui.profileCanvas.addEventListener("pointermove", event => {
if (!plotMap) return;
const rect = ui.profileCanvas.getBoundingClientRect();
const px = event.clientX - rect.left;
const py = event.clientY - rect.top;
const { margin, plotWidth, plotHeight, maxPressure, x, y } = plotMap;
if (
px < margin.left ||
px > margin.left + plotWidth ||
py < margin.top ||
py > margin.top + plotHeight
) {
ui.sampleTooltip.style.display = "none";
return;
}
const depth = THREE.MathUtils.clamp(
((py - margin.top) / plotHeight) * MAX_DEPTH,
0,
state.depth
);
const gauge = gaugePressureAt(depth);
const absolute = absolutePressureAt(depth);
ui.sampleTooltip.style.display = "block";
ui.sampleTooltip.style.left = `${Math.min(px + 12, rect.width - 180)}px`;
ui.sampleTooltip.style.top = `${Math.max(5, py - 42)}px`;
ui.sampleTooltip.innerHTML =
`h = ${depth.toFixed(2)} m<br>` +
`gauge = ${(gauge / 1000).toFixed(2)} kPa<br>` +
`absolute = ${(absolute / 1000).toFixed(2)} kPa`;
});
ui.profileCanvas.addEventListener("pointerleave", () => {
ui.sampleTooltip.style.display = "none";
});
// ---------- Input handling ----------
ui.depth.addEventListener("input", () => {
state.depth = Number(ui.depth.value);
updateModel();
});
ui.density.addEventListener("input", () => {
state.density = Number(ui.density.value);
updateModel();
});
ui.atmosphere.addEventListener("input", () => {
state.atmosphere = Number(ui.atmosphere.value);
updateModel();
});
document.querySelectorAll("[data-preset]").forEach(button => {
button.addEventListener("click", () => {
switch (button.dataset.preset) {
case "fresh":
state.density = 1000;
break;
case "salt":
state.density = 1025;
break;
case "light":
state.density = 800;
break;
case "deep":
state.depth = 3.6;
state.density = 1025;
break;
}
ui.depth.value = String(state.depth);
ui.density.value = String(state.density);
updateModel();
});
});
// ---------- Direct camera controls ----------
const pointers = new Map();
let lastPointer = null;
let lastPinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", event => {
if (event.pointerType === "mouse" && event.button !== 0) return;
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
lastPointer = { x: event.clientX, y: event.clientY };
} else if (pointers.size === 2) {
lastPinchDistance = getPinchDistance();
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1 && lastPointer) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
cameraGoal.theta -= dx * 0.006;
cameraGoal.phi = THREE.MathUtils.clamp(
cameraGoal.phi - dy * 0.005,
0.46,
1.46
);
lastPointer = { x: event.clientX, y: event.clientY };
} else if (pointers.size === 2) {
const distance = getPinchDistance();
if (lastPinchDistance > 0 && distance > 0) {
cameraGoal.radius = THREE.MathUtils.clamp(
cameraGoal.radius * (lastPinchDistance / distance),
5.5,
14
);
}
lastPinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
lastPointer = null;
lastPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("lostpointercapture", endPointer);
function getPinchDistance() {
const values = Array.from(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(
"wheel",
event => {
event.preventDefault();
cameraGoal.radius = THREE.MathUtils.clamp(
cameraGoal.radius * Math.exp(event.deltaY * 0.001),
5.5,
14
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
ui.resetCamera.addEventListener("click", resetCamera);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
// ---------- World-space annotation labels ----------
const projectedPoint = new THREE.Vector3();
function placeLabel(element, position) {
projectedPoint.copy(position).project(camera);
const visible =
projectedPoint.z > -1 &&
projectedPoint.z < 1 &&
Math.abs(projectedPoint.x) < 1.15 &&
Math.abs(projectedPoint.y) < 1.15;
if (!visible) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left =
`${(projectedPoint.x * 0.5 + 0.5) * viewport.clientWidth}px`;
element.style.top =
`${(-projectedPoint.y * 0.5 + 0.5) * viewport.clientHeight}px`;
}
// ---------- Resize and render loop ----------
function resizeRenderer() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.fov = camera.aspect < 1 ? 52 : 42;
camera.updateProjectionMatrix();
}
const viewportObserver = new ResizeObserver(() => {
resizeRenderer();
});
viewportObserver.observe(viewport);
const profileObserver = new ResizeObserver(() => {
resizeProfileCanvas();
});
profileObserver.observe(ui.profileWrap);
function animate() {
requestAnimationFrame(animate);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraGoal.theta,
0.12
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraGoal.phi,
0.12
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraGoal.radius,
0.12
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
placeLabel(
ui.atmosphereLabel,
new THREE.Vector3(-0.35, state.depth + 0.58, 0.15)
);
placeLabel(
ui.surfaceLabel,
new THREE.Vector3(1.08, state.depth + 0.12, 0.64)
);
placeLabel(
ui.bottomLabel,
new THREE.Vector3(0.98, 0.16, 0.67)
);
placeLabel(
ui.ruler0,
new THREE.Vector3(2.24, 0.0, 0.93)
);
placeLabel(
ui.ruler2,
new THREE.Vector3(2.24, 2.0, 0.93)
);
placeLabel(
ui.ruler4,
new THREE.Vector3(2.24, 4.0, 0.93)
);
renderer.render(scene, camera);
}
resizeRenderer();
resizeProfileCanvas();
updateModel();
animate();
</script>
</body>
</html>
```