Prompt specification
Build or customize the Three.js 3D scene "Small boat cargo teaching dock" 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: Small boat cargo teaching dock
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/small-boat-cargo-teaching-dock-x1443
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts buoyancy and displaced fluid. Compose a box-shaped boat beside a dock with movable model cargo; calculate draft from displacement while avoiding claims about realistic stability or seaworthiness. Voxel style.
Acceptance checks: Use a rectangular hull, mass in kilograms and water density in kg/m³. Calculate draft from (boat mass + cargo mass) / (water density × rectangular waterplane area). Choose synthetic dimensions and masses that give a plausible positive empty draft smaller than the hull height. Show empty and loaded values and test two cargo moves. Model only displacement, without a safety or stability verdict.
Return a single complete HTML document. It must work in a sandbox that allows scripts, with the cdnjs Three.js r160 module and inline CSS. Use procedural geometry and local button handlers; no native form submission, pointer lock, remote assets, or runtime network requests besides Three.js.
Working Three.js Starter (small-boat-cargo-teaching-dock-x1443):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#101923">
<link rel="icon" href="data:,">
<title>Voxel Displacement Dock</title>
<style>
:root {
color-scheme: dark;
--bg: #0c141c;
--panel: #101923;
--panel-2: #16232e;
--line: #293a47;
--text: #e8f2f4;
--muted: #91a6b1;
--cyan: #59e0e8;
--cyan-soft: rgba(89, 224, 232, 0.14);
--orange: #f28a42;
--yellow: #ffd45f;
--green: #79d6a6;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
body {
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
code {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
button {
border: 0;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
width: 100%;
height: 100vh;
height: 100dvh;
}
#viewport {
position: relative;
min-width: 0;
overflow: hidden;
background: #b9dce3;
}
#sceneCanvas {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
.scene-heading {
position: absolute;
z-index: 4;
top: 22px;
left: 24px;
max-width: min(430px, calc(100% - 48px));
pointer-events: none;
color: #102b38;
}
.scene-eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
padding: 6px 9px;
border: 1px solid rgba(16, 43, 56, 0.18);
border-radius: 6px;
background: rgba(241, 253, 255, 0.72);
backdrop-filter: blur(8px);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
}
.scene-eyebrow::before {
width: 7px;
height: 7px;
content: "";
background: #13aabd;
box-shadow: 0 0 0 3px rgba(19, 170, 189, 0.15);
}
.scene-heading h1 {
margin: 0;
font-size: clamp(24px, 3vw, 42px);
font-weight: 800;
line-height: 0.98;
letter-spacing: -0.045em;
}
.scene-heading p {
margin: 10px 0 0;
max-width: 370px;
color: rgba(16, 43, 56, 0.75);
font-size: 13px;
line-height: 1.45;
}
.scene-legend,
.camera-hint {
position: absolute;
z-index: 4;
bottom: 18px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 7px;
background: rgba(9, 24, 32, 0.76);
color: #d8edf1;
backdrop-filter: blur(8px);
font: 600 10px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
pointer-events: none;
}
.scene-legend {
left: 18px;
display: flex;
align-items: center;
gap: 8px;
}
.legend-swatch {
width: 12px;
height: 12px;
border: 1px solid rgba(255, 255, 255, 0.72);
background: rgba(89, 224, 232, 0.35);
box-shadow: inset 0 0 0 2px rgba(89, 224, 232, 0.22);
}
.camera-hint {
right: 18px;
color: #afc7d0;
}
#worldLabels {
position: absolute;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
top: 0;
left: 0;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 5px;
background: rgba(9, 24, 32, 0.82);
color: #effbff;
box-shadow: 0 5px 16px rgba(2, 15, 20, 0.18);
backdrop-filter: blur(5px);
white-space: nowrap;
transform: translate(-50%, -100%);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.045em;
}
#labelA {
border-color: rgba(242, 138, 66, 0.82);
}
#labelB {
border-color: rgba(255, 212, 95, 0.82);
}
#labelVolume {
border-color: rgba(89, 224, 232, 0.82);
color: #b9fbff;
}
#labelDraft {
border-color: rgba(242, 138, 66, 0.9);
color: #ffd8b8;
}
#panel {
z-index: 6;
overflow-y: auto;
padding: 24px 22px 30px;
border-left: 1px solid #263945;
background:
radial-gradient(circle at 100% 0%, rgba(55, 126, 145, 0.16), transparent 34%),
var(--panel);
scrollbar-color: #38505e transparent;
}
.panel-kicker {
color: var(--cyan);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
}
#panel h2 {
margin: 8px 0 8px;
font-size: 28px;
line-height: 1;
letter-spacing: -0.035em;
}
.panel-intro {
margin: 0 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.formula-card {
padding: 14px;
border: 1px solid #2c4854;
border-radius: 9px;
background: linear-gradient(135deg, rgba(89, 224, 232, 0.11), rgba(89, 224, 232, 0.025));
}
.formula-label {
margin-bottom: 9px;
color: #a9c0c9;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
.formula-card code {
display: block;
color: #d9fbff;
font-size: 13px;
line-height: 1.55;
}
.formula-sub {
margin-top: 8px;
color: var(--muted);
font: 500 10px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.section-label {
margin: 19px 0 9px;
color: #a7bac3;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.metric-pair {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.metric {
min-width: 0;
padding: 12px;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel-2);
}
.metric span,
.current-grid span {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin: 7px 0 5px;
color: #f2fbfc;
font: 800 22px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.05em;
}
.metric small {
display: block;
color: #8da5af;
font: 500 9px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.metric.loaded strong {
color: var(--cyan);
}
.current-card {
margin-top: 9px;
padding: 14px;
border: 1px solid #2f6872;
border-radius: 9px;
background:
linear-gradient(115deg, rgba(89, 224, 232, 0.13), rgba(89, 224, 232, 0.025)),
#13212b;
}
.current-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.current-heading span {
color: #b8cdd4;
font-size: 10px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
#currentDraft {
color: var(--cyan);
font: 800 30px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.055em;
}
.current-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 13px;
}
.current-grid strong {
display: block;
margin-top: 5px;
color: #edf8fa;
font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.draft-meter {
margin-top: 13px;
}
.draft-track {
position: relative;
height: 9px;
overflow: hidden;
border-radius: 99px;
background: #071119;
}
#draftFill {
width: 36%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #238d9a, var(--cyan));
transition: width 180ms ease-out;
}
.draft-scale {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 6px;
color: #829aa5;
font: 500 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.parameter-list {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--line);
}
.parameter {
padding: 10px 11px;
background: #111d26;
}
.parameter span {
display: block;
color: #8199a4;
font-size: 9px;
text-transform: uppercase;
}
.parameter strong {
display: block;
margin-top: 5px;
color: #dce9ec;
font: 700 11px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.cargo-list {
display: grid;
gap: 7px;
}
.cargo-row {
display: grid;
grid-template-columns: 12px minmax(0, 1fr) auto auto;
align-items: center;
gap: 9px;
padding: 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: #121e27;
}
.cargo-dot {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.07);
}
.cargo-dot.a {
background: var(--orange);
}
.cargo-dot.b {
background: var(--yellow);
}
.cargo-name {
color: #e3eef1;
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.cargo-mass {
margin-top: 4px;
color: #8098a3;
font: 500 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.cargo-state {
min-width: 48px;
padding: 5px 6px;
border-radius: 4px;
background: rgba(89, 224, 232, 0.09);
color: #9debf0;
text-align: center;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
}
.cargo-state.moving {
background: rgba(255, 212, 95, 0.12);
color: #ffe28e;
}
.small-button,
.reset-button {
border: 1px solid #36505d;
border-radius: 5px;
background: #1b2b35;
color: #d8e8ec;
cursor: pointer;
transition: background 130ms ease, border-color 130ms ease, transform 130ms ease;
}
.small-button {
padding: 6px 8px;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.small-button:hover:not(:disabled),
.reset-button:hover:not(:disabled) {
border-color: #5a8793;
background: #243a46;
}
.small-button:active:not(:disabled),
.reset-button:active:not(:disabled) {
transform: translateY(1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
button:disabled {
cursor: not-allowed;
opacity: 0.36;
}
.move-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
.move-button {
min-height: 62px;
padding: 11px;
border: 1px solid #35636d;
border-radius: 8px;
background: linear-gradient(145deg, rgba(89, 224, 232, 0.14), rgba(89, 224, 232, 0.04));
color: #e8fbfc;
text-align: left;
cursor: pointer;
transition: border-color 130ms ease, background 130ms ease, transform 130ms ease;
}
.move-button:hover:not(:disabled) {
border-color: var(--cyan);
background: linear-gradient(145deg, rgba(89, 224, 232, 0.22), rgba(89, 224, 232, 0.07));
}
.move-button:active:not(:disabled) {
transform: translateY(1px);
}
.button-title {
display: block;
font: 800 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.button-sub {
display: block;
margin-top: 6px;
color: #8fb4bd;
font: 500 9px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#transferNote {
min-height: 29px;
margin: 8px 0 0;
color: #8da5af;
font: 500 9px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.log-wrap {
overflow-x: auto;
border: 1px solid var(--line);
border-radius: 8px;
background: #101b23;
}
#logTable {
width: 100%;
border-collapse: collapse;
font: 500 9px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#logTable th,
#logTable td {
padding: 8px 7px;
border-bottom: 1px solid #243641;
text-align: right;
white-space: nowrap;
}
#logTable th {
color: #78919c;
background: #14232c;
font-weight: 700;
letter-spacing: 0.04em;
}
#logTable th:first-child,
#logTable td:first-child {
text-align: left;
}
#logTable tbody tr:last-child td {
border-bottom: 0;
}
#logTable td {
color: #c6d8dd;
}
#logTable td:nth-child(3),
#logTable td:nth-child(4) {
color: var(--cyan);
font-weight: 700;
}
.reset-button {
width: 100%;
margin-top: 9px;
padding: 9px;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.model-note {
margin: 18px 0 0;
padding-top: 14px;
border-top: 1px solid var(--line);
color: #78919c;
font-size: 10px;
line-height: 1.55;
}
.model-note strong {
color: #a8bec6;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
#app {
grid-template-columns: 1fr;
grid-template-rows: minmax(470px, 68dvh) auto;
height: auto;
min-height: 100dvh;
}
#panel {
overflow: visible;
border-top: 1px solid #263945;
border-left: 0;
}
.scene-heading p,
.camera-hint {
display: none;
}
}
@media (max-width: 480px) {
#panel {
padding: 20px 15px 26px;
}
.metric strong {
font-size: 19px;
}
#currentDraft {
font-size: 25px;
}
.move-actions {
grid-template-columns: 1fr;
}
.scene-legend {
right: 18px;
left: 18px;
justify-content: center;
}
}
</style>
</head>
<body>
<main id="app">
<section id="viewport" aria-label="Interactive voxel boat and dock scene">
<canvas
id="sceneCanvas"
tabindex="0"
aria-label="Voxel boat beside a dock. Drag to orbit, use the mouse wheel to zoom, and click cargo to move it."
></canvas>
<div class="scene-heading">
<div class="scene-eyebrow">VOXEL HYDROSTATICS</div>
<h1>Buoyancy by<br>Displacement</h1>
<p>A rectangular boat settles to the draft calculated from its boat-plus-cargo mass and the fixed water density.</p>
</div>
<div id="worldLabels" aria-hidden="true">
<div id="labelA" class="world-label">CARGO A · 800 kg</div>
<div id="labelB" class="world-label">CARGO B · 1,200 kg</div>
<div id="labelVolume" class="world-label">DISPLACED FLUID</div>
<div id="labelDraft" class="world-label">DRAFT</div>
</div>
<div class="scene-legend">
<span class="legend-swatch"></span>
<span>Cyan x-ray cells = displaced-fluid volume</span>
</div>
<div class="camera-hint">DRAG TO ORBIT · WHEEL TO ZOOM · CLICK A CRATE TO MOVE IT</div>
</section>
<aside id="panel" aria-label="Displacement calculations and cargo controls">
<div class="panel-kicker">DISPLACEMENT DOCK</div>
<h2>Mass → Draft</h2>
<p class="panel-intro">
Synthetic dimensions and masses keep the calculation explicit.
Cargo contributes to displacement only while it is aboard the boat.
</p>
<div class="formula-card">
<div class="formula-label">RECTANGULAR-WATERPLANE EQUATION</div>
<code>d = (m<sub>boat</sub> + Σm<sub>cargo aboard</sub>) / (ρ × L × B)</code>
<div class="formula-sub">
V<sub>displaced</sub> = (m<sub>boat</sub> + m<sub>cargo</sub>) / ρ<br>
ρ = 1,000 kg/m³ · A = L × B = 8.00 m²
</div>
</div>
<div class="section-label">Reference drafts</div>
<div class="metric-pair">
<div class="metric">
<span>Empty boat</span>
<strong id="emptyDraft">0.350 m</strong>
<small id="emptyEquation">2,800 ÷ (1,000 × 8.00) = 0.350 m</small>
</div>
<div class="metric loaded">
<span>Both cargo aboard</span>
<strong id="loadedDraft">0.600 m</strong>
<small id="loadedEquation">4,800 ÷ (1,000 × 8.00) = 0.600 m</small>
</div>
</div>
<div class="section-label">Current displacement</div>
<div class="current-card" aria-live="polite">
<div class="current-heading">
<span>Calculated draft</span>
<strong id="currentDraft">0.450 m</strong>
</div>
<div class="current-grid">
<div>
<span>Cargo aboard</span>
<strong id="currentCargoMass">800 kg</strong>
</div>
<div>
<span>Total mass</span>
<strong id="currentTotalMass">3,600 kg</strong>
</div>
<div>
<span>Displaced fluid</span>
<strong id="currentDisplaced">3.600 m³</strong>
</div>
</div>
<div class="draft-meter" aria-hidden="true">
<div class="draft-track"><div id="draftFill"></div></div>
<div class="draft-scale">
<span>0 m</span>
<span id="draftScaleText">0.350 / 1.25 m hull height</span>
<span>1.25 m</span>
</div>
</div>
</div>
<div class="section-label">Hull and water</div>
<div class="parameter-list">
<div class="parameter">
<span>Hull length × width</span>
<strong>4.00 × 2.00 m</strong>
</div>
<div class="parameter">
<span>Rectangular hull height</span>
<strong>1.25 m</strong>
</div>
<div class="parameter">
<span>Water density</span>
<strong>1,000 kg/m³</strong>
</div>
<div class="parameter">
<span>Waterplane area</span>
<strong>8.00 m²</strong>
</div>
</div>
<div class="section-label">Movable cargo</div>
<div class="cargo-list">
<div class="cargo-row">
<span class="cargo-dot a"></span>
<div>
<div class="cargo-name">CARGO A</div>
<div class="cargo-mass">800 kg</div>
</div>
<span id="cargoAState" class="cargo-state">boat</span>
<button id="moveA" class="small-button" type="button" aria-label="Move cargo A between boat and dock">Move</button>
</div>
<div class="cargo-row">
<span class="cargo-dot b"></span>
<div>
<div class="cargo-name">CARGO B</div>
<div class="cargo-mass">1,200 kg</div>
</div>
<span id="cargoBState" class="cargo-state">dock</span>
<button id="moveB" class="small-button" type="button" aria-label="Move cargo B between boat and dock">Move</button>
</div>
</div>
<div class="section-label">Two-move test</div>
<div class="move-actions">
<button id="loadB" class="move-button" type="button">
<span class="button-title">1 · Load cargo B</span>
<span class="button-sub">Dock → boat · +1,200 kg</span>
</button>
<button id="unloadA" class="move-button" type="button">
<span class="button-title">2 · Unload cargo A</span>
<span class="button-sub">Boat → dock · −800 kg</span>
</button>
</div>
<p id="transferNote" aria-live="polite"></p>
<div class="section-label">Move log</div>
<div class="log-wrap">
<table id="logTable">
<thead>
<tr>
<th>Event</th>
<th>Aboard</th>
<th>Total</th>
<th>Draft</th>
</tr>
</thead>
<tbody id="logBody"></tbody>
</table>
</div>
<button id="resetButton" class="reset-button" type="button">RESET TWO-MOVE TEST</button>
<p class="model-note">
<strong>Displacement-only model.</strong> The cyan x-ray volume has rectangular
footprint <strong>L × B</strong> and height equal to the calculated draft.
Only mass balance, displaced volume, and draft are modeled.
</p>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("sceneCanvas");
const viewport = document.getElementById("viewport");
// Synthetic values in SI units.
const WATER_DENSITY_KG_M3 = 1000;
const BOAT_MASS_KG = 2800;
const HULL_LENGTH_M = 4.00;
const HULL_WIDTH_M = 2.00;
const HULL_HEIGHT_M = 1.25;
const WATERPLANE_AREA_M2 = HULL_LENGTH_M * HULL_WIDTH_M;
const CARGO_SIZE_M = 0.72;
const BOAT_X = -2.55;
const BOAT_Z = 0;
const DOCK_X = 2.35;
const DOCK_TOP_Y = 1.00;
const DECK_TOP_LOCAL_Y = HULL_HEIGHT_M + 0.12;
const cargoList = [
{
id: "A",
massKg: 800,
location: "boat",
destination: "boat",
moving: false,
transition: null,
root: null
},
{
id: "B",
massKg: 1200,
location: "dock",
destination: "dock",
moving: false,
transition: null,
root: null
}
];
const cargoById = Object.fromEntries(cargoList.map((cargo) => [cargo.id, cargo]));
const boatSlots = {
A: { x: -0.85, z: 0.28 },
B: { x: 0.35, z: 0.30 }
};
const dockSlots = {
A: { x: DOCK_X + 0.10, z: 0.92 },
B: { x: DOCK_X - 0.10, z: -0.92 }
};
const massFormatter = new Intl.NumberFormat("en-US");
const fmtMass = (value) => massFormatter.format(value);
function draftForCargoMass(cargoMassKg) {
return (
(BOAT_MASS_KG + cargoMassKg) /
(WATER_DENSITY_KG_M3 * WATERPLANE_AREA_M2)
);
}
const emptyDraftM = draftForCargoMass(0);
const allCargoMassKg = cargoList.reduce((sum, cargo) => sum + cargo.massKg, 0);
const bothCargoDraftM = draftForCargoMass(allCargoMassKg);
function calculateDisplacement() {
let cargoMassKg = 0;
const aboardIds = [];
for (const cargo of cargoList) {
if (cargo.location === "boat") {
cargoMassKg += cargo.massKg;
aboardIds.push(cargo.id);
}
}
const totalMassKg = BOAT_MASS_KG + cargoMassKg;
const displacedVolumeM3 = totalMassKg / WATER_DENSITY_KG_M3;
// Required rectangular-hull displacement calculation:
// draft = (boat mass + cargo mass) / (water density × waterplane area)
const draftM =
(BOAT_MASS_KG + cargoMassKg) /
(WATER_DENSITY_KG_M3 * WATERPLANE_AREA_M2);
return {
cargoMassKg,
totalMassKg,
displacedVolumeM3,
draftM,
aboardIds
};
}
// Renderer, scene, and camera.
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9dce3);
scene.fog = new THREE.Fog(0xb9dce3, 19, 39);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 80);
const cameraControls = {
target: new THREE.Vector3(-0.25, 0.18, 0),
azimuth: 0.62,
elevation: 0.48,
radius: 16.2
};
function syncCamera() {
const horizontal = cameraControls.radius * Math.cos(cameraControls.elevation);
camera.position.set(
cameraControls.target.x + horizontal * Math.sin(cameraControls.azimuth),
cameraControls.target.y + cameraControls.radius * Math.sin(cameraControls.elevation),
cameraControls.target.z + horizontal * Math.cos(cameraControls.azimuth)
);
camera.lookAt(cameraControls.target);
camera.updateMatrixWorld();
}
syncCamera();
// Lighting.
scene.add(new THREE.HemisphereLight(0xeaffff, 0x536b63, 2.0));
const sun = new THREE.DirectionalLight(0xfff2d8, 2.8);
sun.position.set(-6, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8ee9f4, 0.65);
fill.position.set(8, 5, -8);
scene.add(fill);
// All scene geometry is procedural and built from box primitives.
function addBox(parent, size, position, material, castShadow = true, receiveShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
const materials = {
sand: new THREE.MeshStandardMaterial({
color: 0x8c9b83,
roughness: 1,
flatShading: true
}),
rock: new THREE.MeshStandardMaterial({
color: 0x6f8378,
roughness: 0.95,
flatShading: true
}),
water: new THREE.MeshStandardMaterial({
color: 0x2b9ec4,
roughness: 0.3,
metalness: 0.05,
transparent: true,
opacity: 0.72,
depthWrite: false,
flatShading: true
}),
wave: new THREE.MeshBasicMaterial({
color: 0xd8fbff,
transparent: true,
opacity: 0.25,
depthWrite: false
}),
dock: new THREE.MeshStandardMaterial({
color: 0xa76f43,
roughness: 0.9,
flatShading: true
}),
dockEdge: new THREE.MeshStandardMaterial({
color: 0x6c4631,
roughness: 0.95,
flatShading: true
}),
piling: new THREE.MeshStandardMaterial({
color: 0x584535,
roughness: 1,
flatShading: true
}),
hull: new THREE.MeshStandardMaterial({
color: 0x315f73,
roughness: 0.72,
metalness: 0.06,
flatShading: true
}),
hullTrim: new THREE.MeshStandardMaterial({
color: 0x173b4a,
roughness: 0.78,
flatShading: true
}),
deck: new THREE.MeshStandardMaterial({
color: 0xc28a55,
roughness: 0.86,
flatShading: true
}),
cabin: new THREE.MeshStandardMaterial({
color: 0xe8e0c8,
roughness: 0.82,
flatShading: true
}),
window: new THREE.MeshStandardMaterial({
color: 0x173947,
roughness: 0.3,
metalness: 0.15,
flatShading: true
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf1d9a6,
roughness: 0.85,
flatShading: true
}),
orange: new THREE.MeshStandardMaterial({
color: 0xf28a42,
roughness: 0.8,
flatShading: true
}),
yellow: new THREE.MeshStandardMaterial({
color: 0xffd45f,
roughness: 0.8,
flatShading: true
}),
strap: new THREE.MeshStandardMaterial({
color: 0x244653,
roughness: 0.75,
flatShading: true
}),
tick: new THREE.MeshBasicMaterial({
color: 0xe9fbff,
transparent: true,
opacity: 0.78,
depthTest: false
})
};
// Water and seabed.
addBox(scene, [38, 0.35, 28], [0, -3.25, 0], materials.sand, false, true);
const waterSurface = addBox(
scene,
[38, 0.08, 28],
[0, -0.04, 0],
materials.water,
false,
true
);
waterSurface.renderOrder = 1;
const rockSpecs = [
[-10.0, -3.00, -6.0, 1.8, 0.65, 1.2],
[-7.0, -2.95, 7.5, 1.2, 0.80, 1.0],
[-1.5, -3.05, -8.2, 2.3, 0.45, 1.4],
[6.0, -2.98, 7.0, 1.7, 0.70, 1.3],
[11.0, -3.00, -4.5, 2.0, 0.65, 1.5],
[13.5, -3.05, 3.0, 1.4, 0.45, 1.0]
];
for (const [x, y, z, sx, sy, sz] of rockSpecs) {
addBox(scene, [sx, sy, sz], [x, y, z], materials.rock, false, true);
}
// Small square wave marks keep the water treatment voxel-like.
for (let i = 0; i < 34; i++) {
const x = -17 + ((i * 7.31) % 34);
const z = -12 + ((i * 4.37) % 24);
if (x > -5.2 && x < 4.1 && z > -2 && z < 2) continue;
const wave = addBox(
scene,
[0.65 + (i % 3) * 0.18, 0.012, 0.12],
[x, 0.012, z],
materials.wave,
false,
false
);
wave.rotation.y = (i % 4) * Math.PI * 0.25;
wave.renderOrder = 2;
}
// Voxel dock.
const dock = new THREE.Group();
dock.position.x = DOCK_X;
scene.add(dock);
for (let i = 0; i < 11; i++) {
const z = (i - 5) * 0.57;
addBox(dock, [2.40, 0.22, 0.52], [0, 0.89, z], materials.dock);
}
addBox(dock, [0.22, 0.30, 6.30], [-0.88, 0.59, 0], materials.dockEdge);
addBox(dock, [0.22, 0.30, 6.30], [0.88, 0.59, 0], materials.dockEdge);
for (const x of [-0.90, 0.90]) {
for (const z of [-2.72, 2.72]) {
addBox(dock, [0.34, 3.00, 0.34], [x, 0.00, z], materials.piling);
addBox(dock, [0.50, 0.18, 0.50], [x, 1.43, z], materials.dockEdge);
}
}
for (const z of [-2.45, 2.45]) {
addBox(dock, [0.18, 0.44, 0.18], [0.72, 1.22, z], materials.piling);
addBox(dock, [0.30, 0.10, 0.30], [0.72, 1.47, z], materials.cream);
}
// Rectangular voxel boat. The hull's lower face is local y = 0,
// so positioning the boat at y = -draft places it exactly at the waterline.
const boat = new THREE.Group();
boat.position.set(BOAT_X, 0, BOAT_Z);
scene.add(boat);
addBox(
boat,
[HULL_LENGTH_M, HULL_HEIGHT_M, HULL_WIDTH_M],
[0, HULL_HEIGHT_M * 0.5, 0],
materials.hull
);
addBox(
boat,
[HULL_LENGTH_M + 0.14, 0.12, HULL_WIDTH_M + 0.14],
[0, HULL_HEIGHT_M + 0.06, 0],
materials.deck
);
for (const z of [-HULL_WIDTH_M * 0.5 - 0.035, HULL_WIDTH_M * 0.5 + 0.035]) {
addBox(
boat,
[HULL_LENGTH_M + 0.18, 0.11, 0.09],
[0, HULL_HEIGHT_M - 0.17, z],
materials.hullTrim
);
}
// Squared cabin and window blocks.
addBox(boat, [0.66, 0.54, 0.70], [1.28, DECK_TOP_LOCAL_Y + 0.27, -0.48], materials.cabin);
addBox(boat, [0.78, 0.12, 0.82], [1.28, DECK_TOP_LOCAL_Y + 0.60, -0.48], materials.hullTrim);
addBox(boat, [0.025, 0.25, 0.47], [1.615, DECK_TOP_LOCAL_Y + 0.30, -0.48], materials.window);
addBox(boat, [0.47, 0.25, 0.025], [1.28, DECK_TOP_LOCAL_Y + 0.30, -0.125], materials.window);
// Short voxel mast and flag.
addBox(boat, [0.08, 0.68, 0.08], [-1.56, DECK_TOP_LOCAL_Y + 0.34, -0.56], materials.hullTrim);
addBox(boat, [0.34, 0.18, 0.05], [-1.38, DECK_TOP_LOCAL_Y + 0.54, -0.56], materials.orange);
// Square fenders.
for (const x of [-1.35, 0, 1.35]) {
addBox(boat, [0.22, 0.30, 0.16], [x, 0.62, HULL_WIDTH_M * 0.5 + 0.12], materials.cream);
}
// Draft tick blocks attached to the hull.
for (let i = 0; i <= 10; i++) {
addBox(
boat,
[0.055, 0.024, 0.055],
[-1.55 + i * 0.31, (HULL_HEIGHT_M / 10) * i, HULL_WIDTH_M * 0.5 + 0.065],
materials.tick,
false,
false
);
}
const draftMarker = addBox(
boat,
[0.34, 0.045, 0.085],
[-1.78, 0, HULL_WIDTH_M * 0.5 + 0.085],
materials.orange,
false,
false
);
// Cargo crates are also procedural box assemblies.
const cargoPickables = [];
function createCargo(cargo) {
const root = new THREE.Group();
scene.add(root);
const bodyMaterial = cargo.id === "A" ? materials.orange : materials.yellow;
const strapMaterial = cargo.id === "A" ? materials.cream : materials.hullTrim;
addBox(root, [CARGO_SIZE_M, CARGO_SIZE_M, CARGO_SIZE_M], [0, 0, 0], bodyMaterial);
addBox(root, [0.075, CARGO_SIZE_M + 0.025, CARGO_SIZE_M + 0.025], [0, 0, 0], strapMaterial);
addBox(root, [CARGO_SIZE_M + 0.025, 0.075, CARGO_SIZE_M + 0.025], [0, 0, 0], strapMaterial);
addBox(
root,
[0.27, 0.035, 0.20],
[0, CARGO_SIZE_M * 0.5 + 0.025, 0],
cargo.id === "A" ? materials.cream : materials.orange
);
root.traverse((object) => {
if (object.isMesh) {
object.userData.cargoId = cargo.id;
cargoPickables.push(object);
}
});
cargo.root = root;
}
cargoList.forEach(createCargo);
function cargoTarget(cargo, location = cargo.location) {
if (location === "boat") {
const slot = boatSlots[cargo.id];
return new THREE.Vector3(
BOAT_X + slot.x,
boat.position.y + DECK_TOP_LOCAL_Y + CARGO_SIZE_M * 0.5,
BOAT_Z + slot.z
);
}
const slot = dockSlots[cargo.id];
return new THREE.Vector3(
slot.x,
DOCK_TOP_Y + CARGO_SIZE_M * 0.5,
slot.z
);
}
cargoList.forEach((cargo) => {
cargo.root.position.copy(cargoTarget(cargo));
cargo.root.rotation.y = 0;
});
// Exact rectangular displaced-fluid volume, rendered as an x-ray voxel field.
// Unit-height cells are vertically scaled by the current calculated draft.
const displacementGroup = new THREE.Group();
displacementGroup.position.set(BOAT_X, 0, BOAT_Z);
scene.add(displacementGroup);
const cellsX = 8;
const cellsZ = 4;
const cellLength = HULL_LENGTH_M / cellsX;
const cellWidth = HULL_WIDTH_M / cellsZ;
const displacementMaterialA = new THREE.MeshBasicMaterial({
color: 0x5ce7ef,
transparent: true,
opacity: 0.16,
depthWrite: false,
depthTest: false
});
const displacementMaterialB = new THREE.MeshBasicMaterial({
color: 0x28bfd4,
transparent: true,
opacity: 0.25,
depthWrite: false,
depthTest: false
});
for (let ix = 0; ix < cellsX; ix++) {
for (let iz = 0; iz < cellsZ; iz++) {
const cell = addBox(
displacementGroup,
[cellLength, 1, cellWidth],
[
-HULL_LENGTH_M * 0.5 + (ix + 0.5) * cellLength,
-0.5,
-HULL_WIDTH_M * 0.5 + (iz + 0.5) * cellWidth
],
(ix + iz) % 2 ? displacementMaterialA : displacementMaterialB,
false,
false
);
cell.renderOrder = 5;
}
}
const displacementEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(HULL_LENGTH_M, 1, HULL_WIDTH_M)
),
new THREE.LineBasicMaterial({
color: 0x9cf8ff,
transparent: true,
opacity: 0.92,
depthTest: false,
depthWrite: false
})
);
displacementEdges.position.y = -0.5;
displacementEdges.renderOrder = 6;
displacementGroup.add(displacementEdges);
// A bright waterline rectangle remains at y = 0 as the hull moves.
const waterline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-HULL_LENGTH_M * 0.5 - 0.07, 0, -HULL_WIDTH_M * 0.5 - 0.07),
new THREE.Vector3(HULL_LENGTH_M * 0.5 + 0.07, 0, -HULL_WIDTH_M * 0.5 - 0.07),
new THREE.Vector3(HULL_LENGTH_M * 0.5 + 0.07, 0, HULL_WIDTH_M * 0.5 + 0.07),
new THREE.Vector3(-HULL_LENGTH_M * 0.5 - 0.07, 0, HULL_WIDTH_M * 0.5 + 0.07)
]),
new THREE.LineBasicMaterial({
color: 0xeaffff,
transparent: true,
opacity: 0.9,
depthTest: false,
depthWrite: false
})
);
waterline.position.set(BOAT_X, 0.025, BOAT_Z);
waterline.renderOrder = 7;
scene.add(waterline);
// UI references and state.
const ui = {
emptyDraft: document.getElementById("emptyDraft"),
emptyEquation: document.getElementById("emptyEquation"),
loadedDraft: document.getElementById("loadedDraft"),
loadedEquation: document.getElementById("loadedEquation"),
currentDraft: document.getElementById("currentDraft"),
currentCargoMass: document.getElementById("currentCargoMass"),
currentTotalMass: document.getElementById("currentTotalMass"),
currentDisplaced: document.getElementById("currentDisplaced"),
draftFill: document.getElementById("draftFill"),
draftScaleText: document.getElementById("draftScaleText"),
cargoAState: document.getElementById("cargoAState"),
cargoBState: document.getElementById("cargoBState"),
moveA: document.getElementById("moveA"),
moveB: document.getElementById("moveB"),
loadB: document.getElementById("loadB"),
unloadA: document.getElementById("unloadA"),
transferNote: document.getElementById("transferNote"),
logBody: document.getElementById("logBody"),
reset: document.getElementById("resetButton"),
labelA: document.getElementById("labelA"),
labelB: document.getElementById("labelB"),
labelVolume: document.getElementById("labelVolume"),
labelDraft: document.getElementById("labelDraft")
};
ui.emptyDraft.textContent = `${emptyDraftM.toFixed(3)} m`;
ui.emptyEquation.textContent =
`${fmtMass(BOAT_MASS_KG)} ÷ (${fmtMass(WATER_DENSITY_KG_M3)} × ${WATERPLANE_AREA_M2.toFixed(2)}) = ${emptyDraftM.toFixed(3)} m`;
ui.loadedDraft.textContent = `${bothCargoDraftM.toFixed(3)} m`;
ui.loadedEquation.textContent =
`${fmtMass(BOAT_MASS_KG + allCargoMassKg)} ÷ (${fmtMass(WATER_DENSITY_KG_M3)} × ${WATERPLANE_AREA_M2.toFixed(2)}) = ${bothCargoDraftM.toFixed(3)} m`;
let moveCount = 0;
let currentDraft = 0.45;
let targetDraft = 0.45;
function updateCargoUI() {
const pending = cargoList.some((cargo) => cargo.moving);
for (const cargo of cargoList) {
const element = cargo.id === "A" ? ui.cargoAState : ui.cargoBState;
const moveButton = cargo.id === "A" ? ui.moveA : ui.moveB;
element.textContent = cargo.moving
? `${cargo.transition.from} → ${cargo.destination}`
: cargo.location;
element.classList.toggle("moving", cargo.moving);
moveButton.disabled = pending;
moveButton.textContent = cargo.moving ? "Moving…" : "Move";
}
ui.loadB.disabled = pending || cargoById.B.location === "boat";
ui.unloadA.disabled = pending || cargoById.A.location === "dock";
const movingCargo = cargoList.find((cargo) => cargo.moving);
if (movingCargo) {
ui.transferNote.textContent =
`Cargo ${movingCargo.id}: ${movingCargo.transition.from} → ${movingCargo.destination}. Mass transfers when the crate reaches its destination.`;
} else if (moveCount === 0) {
ui.transferNote.textContent =
"Test order: load cargo B, then unload cargo A. Initial cargo aboard: A.";
} else {
ui.transferNote.textContent =
`${moveCount} cargo move${moveCount === 1 ? "" : "s"} logged. Reset repeats the two-move test.`;
}
}
function updateReadout(calculation) {
ui.currentDraft.textContent = `${calculation.draftM.toFixed(3)} m`;
ui.currentCargoMass.textContent = `${fmtMass(calculation.cargoMassKg)} kg`;
ui.currentTotalMass.textContent = `${fmtMass(calculation.totalMassKg)} kg`;
ui.currentDisplaced.textContent = `${calculation.displacedVolumeM3.toFixed(3)} m³`;
const percentage = THREE.MathUtils.clamp(
(calculation.draftM / HULL_HEIGHT_M) * 100,
0,
100
);
ui.draftFill.style.width = `${percentage}%`;
ui.draftScaleText.textContent =
`${calculation.draftM.toFixed(3)} / ${HULL_HEIGHT_M.toFixed(2)} m hull height`;
updateCargoUI();
}
function addLogRow(eventLabel, direction, calculation) {
const row = document.createElement("tr");
const eventCell = document.createElement("td");
eventCell.textContent = direction ? `${eventLabel} · ${direction}` : eventLabel;
const aboardCell = document.createElement("td");
aboardCell.textContent = calculation.aboardIds.length
? calculation.aboardIds.join(" + ")
: "none";
const totalCell = document.createElement("td");
totalCell.textContent = `${fmtMass(calculation.totalMassKg)} kg`;
const draftCell = document.createElement("td");
draftCell.textContent = `${calculation.draftM.toFixed(3)} m`;
row.append(eventCell, aboardCell, totalCell, draftCell);
ui.logBody.appendChild(row);
}
function requestCargoMove(id, destination) {
const cargo = cargoById[id];
if (cargo.moving || cargo.location === destination) return;
cargo.destination = destination;
cargo.moving = true;
cargo.transition = {
from: cargo.location,
start: cargo.root.position.clone(),
startRotation: cargo.root.rotation.y,
startTime: performance.now(),
duration: 1000
};
updateCargoUI();
}
function completeCargoMove(cargo) {
const from = cargo.transition.from;
cargo.location = cargo.destination;
cargo.moving = false;
cargo.transition = null;
cargo.root.position.copy(cargoTarget(cargo));
cargo.root.rotation.y = 0;
moveCount += 1;
const calculation = calculateDisplacement();
targetDraft = calculation.draftM;
updateReadout(calculation);
const direction = from === "boat" ? "boat → dock" : "dock → boat";
addLogRow(`Move ${moveCount}`, `Cargo ${cargo.id} ${direction}`, calculation);
}
function resetScene() {
moveCount = 0;
cargoList.forEach((cargo) => {
cargo.location = cargo.id === "A" ? "boat" : "dock";
cargo.destination = cargo.location;
cargo.moving = false;
cargo.transition = null;
cargo.root.rotation.y = 0;
});
const calculation = calculateDisplacement();
currentDraft = calculation.draftM;
targetDraft = calculation.draftM;
boat.position.y = -currentDraft;
displacementGroup.scale.y = currentDraft;
cargoList.forEach((cargo) => {
cargo.root.position.copy(cargoTarget(cargo));
});
ui.logBody.replaceChildren();
addLogRow("Start", null, calculation);
updateReadout(calculation);
}
// Local button handlers only; there is no form submission.
document.getElementById("loadB").addEventListener("click", () => {
requestCargoMove("B", "boat");
});
document.getElementById("unloadA").addEventListener("click", () => {
requestCargoMove("A", "dock");
});
ui.moveA.addEventListener("click", () => {
requestCargoMove("A", cargoById.A.location === "boat" ? "dock" : "boat");
});
ui.moveB.addEventListener("click", () => {
requestCargoMove("B", cargoById.B.location === "boat" ? "dock" : "boat");
});
ui.reset.addEventListener("click", resetScene);
// Custom camera controls implemented directly, without Three.js addons.
const pointer = {
down: false,
id: -1,
x: 0,
y: 0,
moved: 0
};
const raycaster = new THREE.Raycaster();
const pointerNdc = new THREE.Vector2();
function cargoHitAt(event) {
const rect = canvas.getBoundingClientRect();
pointerNdc.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNdc.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNdc, camera);
return raycaster.intersectObjects(cargoPickables, false)[0] || null;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
pointer.down = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.moved = 0;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (pointer.down && event.pointerId === pointer.id) {
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.moved += Math.abs(dx) + Math.abs(dy);
cameraControls.azimuth -= dx * 0.006;
cameraControls.elevation = THREE.MathUtils.clamp(
cameraControls.elevation + dy * 0.004,
0.16,
1.22
);
syncCamera();
} else {
canvas.style.cursor = cargoHitAt(event) ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
if (pointer.down && event.pointerId === pointer.id && pointer.moved < 7) {
const hit = cargoHitAt(event);
if (hit && hit.object.userData.cargoId) {
const cargo = cargoById[hit.object.userData.cargoId];
requestCargoMove(
cargo.id,
cargo.location === "boat" ? "dock" : "boat"
);
}
}
pointer.down = false;
pointer.id = -1;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointercancel", () => {
pointer.down = false;
pointer.id = -1;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControls.radius = THREE.MathUtils.clamp(
cameraControls.radius * Math.exp(event.deltaY * 0.001),
7.5,
24
);
syncCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
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.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(viewport);
window.addEventListener("resize", resizeRenderer);
resizeRenderer();
// World-space HTML labels.
const projected = new THREE.Vector3();
const worldPosition = new THREE.Vector3();
function placeLabel(element, position) {
projected.copy(position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.left =
`${(projected.x * 0.5 + 0.5) * viewport.clientWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * viewport.clientHeight}px`;
element.style.opacity = visible ? "1" : "0";
}
function updateWorldLabels() {
for (const cargo of cargoList) {
cargo.root.getWorldPosition(worldPosition);
worldPosition.y += CARGO_SIZE_M * 0.5 + 0.18;
const label = cargo.id === "A" ? ui.labelA : ui.labelB;
label.textContent = `CARGO ${cargo.id} · ${fmtMass(cargo.massKg)} kg`;
placeLabel(label, worldPosition);
}
worldPosition.set(
BOAT_X + 0.55,
0.02,
BOAT_Z - HULL_WIDTH_M * 0.5 - 0.42
);
ui.labelVolume.textContent =
`DISPLACED FLUID · ${(WATERPLANE_AREA_M2 * currentDraft).toFixed(3)} m³`;
placeLabel(ui.labelVolume, worldPosition);
worldPosition.set(
BOAT_X - HULL_LENGTH_M * 0.5 - 0.25,
-currentDraft * 0.52,
BOAT_Z + HULL_WIDTH_M * 0.5 + 0.18
);
ui.labelDraft.textContent = `DRAFT · ${currentDraft.toFixed(3)} m`;
placeLabel(ui.labelDraft, worldPosition);
}
resetScene();
let previousTime = performance.now();
function animate(now) {
const deltaSeconds = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
const smoothing = 1 - Math.exp(-deltaSeconds * 7);
currentDraft += (targetDraft - currentDraft) * smoothing;
if (Math.abs(targetDraft - currentDraft) < 0.00002) {
currentDraft = targetDraft;
}
boat.position.y = -currentDraft;
draftMarker.position.y = currentDraft;
displacementGroup.scale.y = Math.max(currentDraft, 0.0001);
for (const cargo of cargoList) {
if (!cargo.moving) {
cargo.root.position.copy(cargoTarget(cargo));
continue;
}
const transition = cargo.transition;
const progress = THREE.MathUtils.clamp(
(now - transition.startTime) / transition.duration,
0,
1
);
const eased = progress * progress * (3 - 2 * progress);
const end = cargoTarget(cargo, cargo.destination);
cargo.root.position.lerpVectors(transition.start, end, eased);
cargo.root.position.y += Math.sin(Math.PI * progress) * 1.15;
cargo.root.rotation.y =
transition.startRotation + Math.sin(Math.PI * progress) * 0.20;
if (progress >= 1) {
completeCargoMove(cargo);
}
}
syncCamera();
renderer.render(scene, camera);
updateWorldLabels();
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```