Prompt specification
Build or customize the Three.js 3D scene "Water column with depth probes" 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: Water column with depth probes
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/water-column-with-depth-probes-x1485
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring hydrostatic pressure at different depths. Build a transparent water tank with three pressure markers at different depths; display gauge pressure proportional to depth under constant density and gravity assumptions. Low-poly.
Working Three.js Starter (water-column-with-depth-probes-x1485):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hydrostatic Pressure — Depth Study</title>
<style>
:root {
--bg: #07131f;
--panel: rgba(9, 25, 38, 0.82);
--line: rgba(148, 210, 235, 0.22);
--text: #eaf8ff;
--muted: #8eafbf;
--cyan: #53d9ed;
--mint: #46e0c4;
--amber: #ffca58;
--coral: #ff6b6b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene-container {
position: fixed;
inset: 0;
}
#scene-container canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene-container canvas.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
background:
radial-gradient(circle at 52% 48%, transparent 38%, rgba(2, 9, 15, 0.2) 76%),
linear-gradient(180deg, rgba(2, 10, 17, 0.12), transparent 25%, transparent 72%, rgba(2, 9, 15, 0.32));
}
.hero {
position: fixed;
top: 30px;
left: 34px;
z-index: 5;
width: min(440px, calc(100vw - 410px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 25px;
height: 2px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(83, 217, 237, 0.7);
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 55px);
font-weight: 780;
line-height: 0.98;
letter-spacing: -0.045em;
text-transform: uppercase;
}
.hero p {
max-width: 390px;
margin: 14px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
.model-card {
position: fixed;
top: 28px;
right: 28px;
z-index: 5;
width: 294px;
padding: 19px 20px 17px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
pointer-events: none;
}
.card-kicker {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.equation {
margin: 9px 0 17px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 27px;
font-weight: 700;
letter-spacing: -0.04em;
}
.equation sub {
font-size: 13px;
}
.assumption {
display: flex;
align-items: baseline;
justify-content: space-between;
padding: 8px 0;
border-top: 1px solid rgba(148, 210, 235, 0.11);
color: var(--muted);
font-size: 12px;
}
.assumption strong {
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
font-weight: 600;
}
.pressure-note {
margin-top: 12px;
padding: 11px 12px;
border-left: 2px solid var(--mint);
background: rgba(70, 224, 196, 0.08);
color: #c7e8e3;
font-size: 11px;
line-height: 1.55;
}
.pressure-note strong {
color: var(--mint);
}
#label-layer {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.pressure-label {
position: absolute;
top: 0;
left: 0;
width: 174px;
padding: 10px 11px 9px;
border: 1px solid rgba(151, 207, 229, 0.22);
border-left: 3px solid var(--accent);
border-radius: 10px;
background: rgba(7, 20, 31, 0.88);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.27);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
will-change: transform;
}
.pressure-label::before {
content: "";
position: absolute;
top: 50%;
right: 100%;
width: 20px;
height: 1px;
background: var(--accent);
opacity: 0.85;
box-shadow: 0 0 8px var(--accent);
}
.pressure-label.to-left::before {
right: auto;
left: 100%;
}
.label-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.label-row + .label-row {
margin-top: 6px;
}
.label-row span {
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.label-row strong {
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
font-weight: 700;
white-space: nowrap;
}
.label-row.pressure strong {
color: var(--accent);
font-size: 14px;
}
.pressure-bar {
height: 3px;
margin-top: 8px;
overflow: hidden;
border-radius: 3px;
background: rgba(255, 255, 255, 0.1);
}
.pressure-bar i {
display: block;
height: 100%;
border-radius: inherit;
background: var(--accent);
box-shadow: 0 0 9px var(--accent);
}
.controls {
position: fixed;
bottom: 25px;
left: 32px;
z-index: 8;
display: flex;
align-items: center;
gap: 10px;
}
.control-hint {
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(7, 20, 31, 0.72);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
.control-hint span {
color: var(--text);
}
.control-hint i {
width: 4px;
height: 4px;
border-radius: 50%;
background: var(--cyan);
}
#reset-view {
height: 38px;
padding: 0 14px;
border: 1px solid rgba(83, 217, 237, 0.3);
border-radius: 10px;
background: rgba(14, 43, 57, 0.85);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}
#reset-view:hover {
border-color: var(--cyan);
background: rgba(22, 65, 80, 0.95);
transform: translateY(-1px);
}
#reset-view:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
@media (max-width: 760px) {
.hero {
top: 19px;
left: 19px;
width: calc(100vw - 38px);
}
h1 {
max-width: 330px;
font-size: 34px;
}
.hero p {
max-width: 310px;
font-size: 11px;
}
.model-card {
top: auto;
right: 14px;
bottom: 72px;
width: 236px;
padding: 13px 14px;
}
.equation {
margin: 5px 0 9px;
font-size: 22px;
}
.assumption {
padding: 5px 0;
font-size: 10px;
}
.assumption strong {
font-size: 10px;
}
.pressure-note {
display: none;
}
.pressure-label {
width: 145px;
padding: 8px 9px;
}
.label-row span {
font-size: 7px;
}
.label-row strong {
font-size: 10px;
}
.label-row.pressure strong {
font-size: 12px;
}
.controls {
bottom: 17px;
left: 16px;
}
.control-hint {
padding: 9px 11px;
font-size: 10px;
}
.control-hint .secondary {
display: none;
}
}
</style>
</head>
<body>
<div id="scene-container"></div>
<div class="vignette"></div>
<header class="hero">
<div class="eyebrow">Fluid Mechanics / Tank 01</div>
<h1>Hydrostatic<br>Pressure</h1>
<p>
Gauge pressure increases linearly with depth in a fluid of constant density.
Three submerged markers compare pressure at 0.50 m, 1.50 m, and 2.50 m.
</p>
</header>
<aside class="model-card">
<div class="card-kicker">Gauge Pressure Model</div>
<div class="equation">P<sub>g</sub> = ρgh</div>
<div class="assumption">
<span>Density, ρ</span>
<strong>1,000 kg/m³</strong>
</div>
<div class="assumption">
<span>Gravity, g</span>
<strong>9.81 m/s²</strong>
</div>
<div class="assumption">
<span>Water depth</span>
<strong>3.60 m</strong>
</div>
<div class="pressure-note">
Pressure at the free surface is <strong>0 kPa gauge</strong>.
At 1 m depth, P<sub>g</sub> = <strong>9.81 kPa</strong>.
</div>
</aside>
<div id="label-layer"></div>
<div class="controls">
<div class="control-hint">
<i></i>
<span>Drag to orbit</span>
<span class="secondary">• Scroll to zoom</span>
</div>
<button id="reset-view" type="button" aria-label="Reset camera view">
Reset view
</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Constants and physical model
// ------------------------------------------------------------
const DENSITY = 1000; // kg/m^3
const GRAVITY = 9.81; // m/s^2
const TANK_WIDTH = 3.2; // m
const TANK_DEPTH = 2.2; // m
const TANK_HEIGHT = 4.0; // m
const TANK_BOTTOM = -2.0;
const WATER_LEVEL = 1.6;
const WATER_DEPTH = WATER_LEVEL - TANK_BOTTOM;
const markerDefinitions = [
{ depth: 0.5, color: 0x46e0c4 },
{ depth: 1.5, color: 0xffca58 },
{ depth: 2.5, color: 0xff6b6b }
];
const maxGaugePressure = DENSITY * GRAVITY * WATER_DEPTH;
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const container = document.querySelector("#scene-container");
const labelLayer = document.querySelector("#label-layer");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D tank showing hydrostatic pressure at three depths"
);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131f);
scene.fog = new THREE.Fog(0x07131f, 12, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, -0.15, 0);
const spherical = new THREE.Spherical();
function resetCamera() {
const narrow = window.innerWidth < 760;
spherical.set(narrow ? 11.6 : 9.4, 1.14, narrow ? 0.34 : 0.47);
cameraTarget.set(0, -0.15, 0);
updateCamera();
}
function updateCamera() {
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
resetCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xb9edff, 0x07111a, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3dc7ff, 1.15);
rimLight.position.set(-4, 2, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb85c, 18, 8, 2);
warmLight.position.set(3.5, 0.8, -2);
scene.add(warmLight);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x294654,
roughness: 0.42,
metalness: 0.72,
flatShading: true
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x102a37,
roughness: 0.58,
metalness: 0.55,
flatShading: true
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x142b38,
roughness: 0.78,
metalness: 0.18,
flatShading: true
});
const glassMaterial = new THREE.MeshPhongMaterial({
color: 0xa4e9ff,
transparent: true,
opacity: 0.09,
side: THREE.DoubleSide,
depthWrite: false,
shininess: 110
});
const waterMaterial = new THREE.MeshPhongMaterial({
color: 0x28b9dd,
transparent: true,
opacity: 0.24,
side: THREE.DoubleSide,
depthWrite: false,
shininess: 85
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x163544,
roughness: 0.68,
metalness: 0.25,
flatShading: true
});
// ------------------------------------------------------------
// Low-poly environment and platform
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CircleGeometry(18, 16),
new THREE.MeshStandardMaterial({
color: 0x081722,
roughness: 0.95,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.34;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(18, 18, 0x2a7186, 0x183a48);
grid.position.y = -2.325;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(2.48, 2.62, 0.24, 8),
baseMaterial
);
plinth.position.y = -2.2;
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const plinthInset = new THREE.Mesh(
new THREE.CylinderGeometry(2.1, 2.1, 0.035, 8),
darkMetalMaterial
);
plinthInset.position.y = -2.065;
plinthInset.receiveShadow = true;
scene.add(plinthInset);
// ------------------------------------------------------------
// Tank frame
// ------------------------------------------------------------
function addBeam(x, y, z, sizeX, sizeY, sizeZ, material = metalMaterial) {
const beam = new THREE.Mesh(
new THREE.BoxGeometry(sizeX, sizeY, sizeZ),
material
);
beam.position.set(x, y, z);
beam.castShadow = true;
beam.receiveShadow = true;
scene.add(beam);
return beam;
}
const halfWidth = TANK_WIDTH / 2;
const halfDepth = TANK_DEPTH / 2;
const beamSize = 0.09;
// Corner uprights
for (const x of [-halfWidth, halfWidth]) {
for (const z of [-halfDepth, halfDepth]) {
addBeam(
x,
0,
z,
beamSize,
TANK_HEIGHT + beamSize,
beamSize
);
}
}
// Top and bottom horizontal rails
for (const y of [-2.02, 2.02]) {
for (const z of [-halfDepth, halfDepth]) {
addBeam(0, y, z, TANK_WIDTH + beamSize, beamSize, beamSize);
}
for (const x of [-halfWidth, halfWidth]) {
addBeam(x, y, 0, beamSize, beamSize, TANK_DEPTH + beamSize);
}
}
// Interior floor
const tankFloor = new THREE.Mesh(
new THREE.BoxGeometry(TANK_WIDTH - 0.08, 0.06, TANK_DEPTH - 0.08),
floorMaterial
);
tankFloor.position.y = -1.97;
tankFloor.receiveShadow = true;
scene.add(tankFloor);
// ------------------------------------------------------------
// Transparent glass panels
// ------------------------------------------------------------
function addGlassPanel(sizeX, sizeY, sizeZ, x, y, z, renderOrder) {
const panel = new THREE.Mesh(
new THREE.BoxGeometry(sizeX, sizeY, sizeZ),
glassMaterial
);
panel.position.set(x, y, z);
panel.renderOrder = renderOrder;
scene.add(panel);
return panel;
}
addGlassPanel(
TANK_WIDTH - 0.06, TANK_HEIGHT - 0.08, 0.025,
0, 0, halfDepth, 7
);
addGlassPanel(
TANK_WIDTH - 0.06, TANK_HEIGHT - 0.08, 0.025,
0, 0, -halfDepth, 1
);
addGlassPanel(
0.025, TANK_HEIGHT - 0.08, TANK_DEPTH - 0.06,
-halfWidth, 0, 0, 5
);
addGlassPanel(
0.025, TANK_HEIGHT - 0.08, TANK_DEPTH - 0.06,
halfWidth, 0, 0, 5
);
// ------------------------------------------------------------
// Water body and subtle depth gradient
// ------------------------------------------------------------
const waterHeight = WATER_LEVEL - TANK_BOTTOM - 0.025;
const waterCenterY = TANK_BOTTOM + waterHeight / 2;
const waterBody = new THREE.Mesh(
new THREE.BoxGeometry(
TANK_WIDTH - 0.14,
waterHeight,
TANK_DEPTH - 0.14
),
waterMaterial
);
waterBody.position.y = waterCenterY;
waterBody.renderOrder = 2;
scene.add(waterBody);
const gradientGeometry = new THREE.PlaneGeometry(
TANK_WIDTH - 0.16,
waterHeight,
1,
8
);
const gradientPositions = gradientGeometry.attributes.position;
const gradientColors = [];
const shallowColor = new THREE.Color(0x55d8e8);
const deepColor = new THREE.Color(0x075f91);
for (let i = 0; i < gradientPositions.count; i++) {
const localHeight = gradientPositions.getY(i) + waterHeight / 2;
const t = THREE.MathUtils.clamp(localHeight / waterHeight, 0, 1);
const color = deepColor.clone().lerp(shallowColor, t);
gradientColors.push(color.r, color.g, color.b);
}
gradientGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(gradientColors, 3)
);
const gradientSheet = new THREE.Mesh(
gradientGeometry,
new THREE.MeshBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.16,
side: THREE.DoubleSide,
depthWrite: false
})
);
gradientSheet.position.set(
0,
waterCenterY,
-halfDepth + 0.075
);
gradientSheet.renderOrder = 1;
scene.add(gradientSheet);
// ------------------------------------------------------------
// Faceted animated water surface
// ------------------------------------------------------------
let surfaceGeometry = new THREE.PlaneGeometry(
TANK_WIDTH - 0.14,
TANK_DEPTH - 0.14,
6,
4
);
surfaceGeometry.rotateX(-Math.PI / 2);
surfaceGeometry = surfaceGeometry.toNonIndexed();
const surfacePositions = surfaceGeometry.attributes.position;
const surfacePalette = [
new THREE.Color(0x53d9ed),
new THREE.Color(0x3fc6df),
new THREE.Color(0x6be5ed),
new THREE.Color(0x2eb2d2)
];
const surfaceColors = [];
for (let i = 0; i < surfacePositions.count; i += 3) {
const color =
surfacePalette[Math.floor(Math.random() * surfacePalette.length)];
for (let j = 0; j < 3; j++) {
surfaceColors.push(color.r, color.g, color.b);
}
}
surfaceGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(surfaceColors, 3)
);
const surfaceMaterial = new THREE.MeshPhongMaterial({
vertexColors: true,
transparent: true,
opacity: 0.62,
side: THREE.DoubleSide,
flatShading: true,
shininess: 120,
depthWrite: false
});
const surface = new THREE.Mesh(surfaceGeometry, surfaceMaterial);
surface.position.y = WATER_LEVEL;
surface.renderOrder = 3;
scene.add(surface);
function updateSurface(time) {
for (let i = 0; i < surfacePositions.count; i += 3) {
const x = surfacePositions.getX(i);
const z = surfacePositions.getZ(i);
const wave =
Math.sin(x * 2.3 + time * 1.15) * 0.012 +
Math.cos(z * 3.1 - time * 0.85) * 0.009;
for (let j = 0; j < 3; j++) {
surfacePositions.setY(i + j, wave);
}
}
surfacePositions.needsUpdate = true;
surfaceGeometry.computeVertexNormals();
}
// ------------------------------------------------------------
// Sprite text helper for the depth ruler
// ------------------------------------------------------------
function makeTextSprite(text, color = "#bfefff", width = 0.48) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 64;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = "700 31px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(text, 128, 32);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, width / 4, 1);
sprite.renderOrder = 20;
return sprite;
}
// ------------------------------------------------------------
// Depth ruler and guide lines
// ------------------------------------------------------------
const rulerX = -1.84;
const rulerZ = 0.46;
const rulerVertices = [];
// Vertical ruler
rulerVertices.push(
rulerX, WATER_LEVEL, rulerZ,
rulerX, TANK_BOTTOM, rulerZ
);
// Half-meter tick marks
for (let depth = 0; depth <= WATER_DEPTH + 0.001; depth += 0.5) {
const y = WATER_LEVEL - depth;
const tickLength = depth % 1 === 0 ? 0.19 : 0.12;
rulerVertices.push(
rulerX, y, rulerZ,
rulerX + tickLength, y, rulerZ
);
}
const rulerGeometry = new THREE.BufferGeometry();
rulerGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(rulerVertices, 3)
);
const ruler = new THREE.LineSegments(
rulerGeometry,
new THREE.LineBasicMaterial({
color: 0x8dc8d9,
transparent: true,
opacity: 0.62
})
);
ruler.renderOrder = 8;
scene.add(ruler);
// Meter labels
for (let depth = 0; depth <= 3; depth++) {
const label = makeTextSprite(`${depth} m`, "#a9d8e5", 0.45);
label.position.set(rulerX - 0.27, WATER_LEVEL - depth, rulerZ);
scene.add(label);
}
const surfaceLabel = makeTextSprite("0 kPa", "#65e5ee", 0.54);
surfaceLabel.position.set(1.08, WATER_LEVEL + 0.15, rulerZ);
scene.add(surfaceLabel);
// ------------------------------------------------------------
// Three pressure markers and projected HTML labels
// ------------------------------------------------------------
const markerLabels = [];
markerDefinitions.forEach((definition, index) => {
const { depth, color } = definition;
const pressurePa = DENSITY * GRAVITY * depth;
const pressureKPa = pressurePa / 1000;
const y = WATER_LEVEL - depth;
const accentCSS = `#${color.toString(16).padStart(6, "0")}`;
// Depth guide from ruler toward the marker
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(rulerX, y, rulerZ),
new THREE.Vector3(-0.28, y, rulerZ)
]);
const guide = new THREE.Line(
guideGeometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.42
})
);
guide.renderOrder = 8;
scene.add(guide);
const group = new THREE.Group();
group.position.set(0, y, 0.46);
const puckMaterial = new THREE.MeshStandardMaterial({
color: 0x102a38,
roughness: 0.38,
metalness: 0.62,
flatShading: true
});
const accentMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.24,
roughness: 0.3,
metalness: 0.38,
flatShading: true
});
const puck = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.19, 0.075, 8),
puckMaterial
);
puck.rotation.x = Math.PI / 2;
puck.castShadow = true;
group.add(puck);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.235, 0.035, 6, 12),
accentMaterial
);
ring.position.z = 0.055;
ring.castShadow = true;
group.add(ring);
const core = new THREE.Mesh(
new THREE.OctahedronGeometry(0.105, 0),
new THREE.MeshStandardMaterial({
color: 0xe8fbff,
emissive: color,
emissiveIntensity: 0.35,
roughness: 0.25,
metalness: 0.2,
flatShading: true
})
);
core.position.z = 0.085;
core.castShadow = true;
group.add(core);
const markerLight = new THREE.PointLight(color, 5.5, 2.2, 2);
markerLight.position.set(0, 0, 0.16);
group.add(markerLight);
scene.add(group);
// HTML label projected from the 3D marker each frame
const element = document.createElement("div");
element.className = "pressure-label";
element.style.setProperty("--accent", accentCSS);
element.innerHTML = `
<div class="label-row">
<span>Depth below surface</span>
<strong>${depth.toFixed(2)} m</strong>
</div>
<div class="label-row pressure">
<span>Gauge pressure</span>
<strong>${pressureKPa.toFixed(2)} kPa</strong>
</div>
<div class="pressure-bar">
<i style="width:${(pressurePa / maxGaugePressure * 100).toFixed(1)}%"></i>
</div>
`;
labelLayer.appendChild(element);
markerLabels.push({
element,
anchor: new THREE.Vector3(0.31, y, 0.48)
});
});
// ------------------------------------------------------------
// Direct camera controls — no addons required
// ------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
spherical.theta -= deltaX * 0.006;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi - deltaY * 0.005,
0.34,
1.48
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius * Math.exp(event.deltaY * 0.001),
6.2,
15
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
document.querySelector("#reset-view").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "r") {
resetCamera();
} else if (key === "arrowleft") {
spherical.theta += 0.1;
updateCamera();
} else if (key === "arrowright") {
spherical.theta -= 0.1;
updateCamera();
} else if (key === "arrowup") {
spherical.phi = THREE.MathUtils.clamp(spherical.phi - 0.08, 0.34, 1.48);
updateCamera();
} else if (key === "arrowdown") {
spherical.phi = THREE.MathUtils.clamp(spherical.phi + 0.08, 0.34, 1.48);
updateCamera();
}
});
// ------------------------------------------------------------
// Project marker labels to screen coordinates
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const marker of markerLabels) {
projected.copy(marker.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
marker.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const toLeft = x > width - (width < 760 ? 160 : 205);
marker.element.classList.toggle("to-left", toLeft);
if (toLeft) {
marker.element.style.transform =
`translate3d(${x}px, ${y}px, 0) ` +
`translateX(calc(-100% - 20px)) translateY(-50%)`;
} else {
marker.element.style.transform =
`translate3d(${x}px, ${y}px, 0) ` +
`translateX(20px) translateY(-50%)`;
}
}
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", onResize);
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
updateSurface(elapsed);
// Gentle pressure-marker ring pulse.
scene.traverse((object) => {
if (object.userData.pressureRing) {
const pulse = 1 + Math.sin(elapsed * 2.1 + object.userData.phase) * 0.035;
object.scale.setScalar(pulse);
}
});
updateCamera();
renderer.render(scene, camera);
updateLabels();
});
</script>
</body>
</html>
```