Prompt specification
Build or customize the Three.js 3D scene "Calibrate a water clock from sample readings" 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: Calibrate a water clock from sample readings
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/calibrate-a-water-clock-from-sample-readings-x2556
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring water clocks and flow-based timekeeping. Let users place time marks using observed model readings. Compare a naive evenly spaced scale with the calibrated scale while preserving the same vessel and flow law. Low-poly.
Working Three.js Starter (calibrate-a-water-clock-from-sample-readings-x2556):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Water Clock Studio</title>
<style>
:root {
--ink: #18211f;
--muted: #78837f;
--paper: #f4f0e7;
--panel: #17201e;
--panel-2: #202b28;
--line: rgba(255, 255, 255, 0.11);
--water: #55b9cf;
--green: #72c9a9;
--orange: #f08a62;
--white: #f8f5ec;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button {
cursor: pointer;
}
#app {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
}
#viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 36%, #faf8f2 0%, #e8e2d7 72%);
}
#scene {
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.scene-hud {
position: absolute;
inset: 20px 20px auto;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 14px;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
width: 42px;
height: 42px;
border: 1px solid rgba(24, 33, 31, 0.2);
border-radius: 50%;
display: grid;
place-items: center;
background: rgba(255, 255, 255, 0.55);
backdrop-filter: blur(8px);
font-size: 20px;
}
.brand h1 {
margin: 0;
font-size: 17px;
letter-spacing: -0.02em;
font-weight: 750;
}
.brand p {
margin: 3px 0 0;
color: #68736f;
font-size: 11px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.hud-actions {
display: flex;
gap: 8px;
pointer-events: auto;
}
.glass-button {
border: 1px solid rgba(24, 33, 31, 0.16);
border-radius: 999px;
padding: 9px 12px;
color: var(--ink);
background: rgba(255, 255, 255, 0.68);
backdrop-filter: blur(10px);
font-size: 12px;
font-weight: 650;
box-shadow: 0 5px 20px rgba(40, 45, 42, 0.07);
}
.glass-button:hover {
background: rgba(255, 255, 255, 0.92);
}
.scene-badges {
position: absolute;
left: 20px;
bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100% - 40px);
pointer-events: none;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.72);
border: 1px solid rgba(24, 33, 31, 0.11);
color: #4e5b57;
font-size: 11px;
font-weight: 650;
backdrop-filter: blur(8px);
}
.badge i {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--water);
}
.badge.green i {
background: var(--green);
}
.badge.orange i {
background: var(--orange);
}
#panel {
min-height: 0;
overflow-y: auto;
color: var(--white);
background:
linear-gradient(180deg, #192320 0%, #141c1a 100%);
border-left: 1px solid rgba(255, 255, 255, 0.07);
box-shadow: -15px 0 50px rgba(36, 41, 38, 0.12);
}
#panel::-webkit-scrollbar {
width: 7px;
}
#panel::-webkit-scrollbar-thumb {
background: rgba(255, 255, 255, 0.15);
border-radius: 20px;
}
.panel-inner {
padding: 25px 24px 32px;
}
.eyebrow {
margin: 0 0 7px;
color: #83a99c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h2 {
margin: 0;
font-size: 25px;
line-height: 1.05;
letter-spacing: -0.045em;
font-weight: 740;
}
.intro {
margin: 10px 0 22px;
color: #aab4b0;
font-size: 12px;
line-height: 1.55;
}
.section {
padding: 19px 0;
border-top: 1px solid var(--line);
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.section-heading h3 {
margin: 0;
font-size: 12px;
letter-spacing: 0.02em;
font-weight: 760;
}
.section-heading span {
color: #76837e;
font-size: 10px;
}
.clock-card {
padding: 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(255, 255, 255, 0.035);
}
.clock-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 14px;
}
.large-clock {
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 37px;
line-height: 1;
font-weight: 600;
letter-spacing: -0.055em;
}
.live-dot {
display: inline-flex;
align-items: center;
gap: 6px;
margin-bottom: 5px;
color: #9ba7a2;
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.09em;
}
.live-dot::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: #73c8a7;
box-shadow: 0 0 0 4px rgba(115, 200, 167, 0.1);
}
.readings {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 17px;
}
.reading {
padding-top: 11px;
border-top: 1px solid var(--line);
}
.reading span {
display: block;
color: #7e8b86;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.reading strong {
display: block;
margin-top: 4px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 15px;
font-weight: 600;
}
.transport {
display: flex;
gap: 8px;
margin-top: 16px;
}
.button {
min-height: 38px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--panel-2);
color: var(--white);
font-size: 11px;
font-weight: 700;
}
.button:hover {
border-color: rgba(255, 255, 255, 0.23);
background: #27332f;
}
.button.primary {
flex: 1;
color: #10201b;
background: var(--green);
border-color: transparent;
}
.button.primary:hover {
background: #88d8ba;
}
.button.orange {
color: #24140e;
background: var(--orange);
border-color: transparent;
}
.button.quiet {
background: transparent;
color: #9ca8a3;
}
.button.small {
min-height: 31px;
padding: 6px 9px;
font-size: 10px;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 16px 0 8px;
accent-color: var(--green);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
color: #697670;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
}
.speed-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 13px;
color: #82908a;
font-size: 10px;
}
select {
padding: 5px 8px;
border: 1px solid var(--line);
border-radius: 7px;
color: #dce2de;
background: #202a27;
font-size: 10px;
}
.formula {
padding: 13px;
border-left: 2px solid var(--green);
border-radius: 0 9px 9px 0;
background: rgba(114, 201, 169, 0.06);
}
.formula code {
display: block;
color: #c8eadb;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
line-height: 1.65;
white-space: normal;
}
.formula small {
display: block;
margin-top: 7px;
color: #81918a;
font-size: 9px;
line-height: 1.5;
}
.law-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 11px;
}
.law-stat {
padding: 10px 8px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
}
.law-stat span {
display: block;
color: #74827c;
font-size: 8px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.law-stat strong {
display: block;
margin-top: 4px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
font-weight: 600;
}
.hint {
margin: 11px 0 14px;
color: #8f9c96;
font-size: 10px;
line-height: 1.55;
}
.field-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.field label {
display: block;
margin-bottom: 6px;
color: #7d8b85;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.field input {
width: 100%;
height: 39px;
padding: 0 10px;
border: 1px solid var(--line);
border-radius: 9px;
outline: none;
color: var(--white);
background: #111917;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
}
.field input:focus {
border-color: rgba(114, 201, 169, 0.6);
box-shadow: 0 0 0 3px rgba(114, 201, 169, 0.08);
}
.field-actions {
display: flex;
gap: 8px;
margin-top: 11px;
}
.field-actions .button {
flex: 1;
}
.demo-row {
display: flex;
justify-content: flex-end;
margin-top: 8px;
}
.text-button {
padding: 0;
border: 0;
color: #86bca8;
background: transparent;
font-size: 10px;
font-weight: 650;
}
.text-button:hover {
color: #b0e3d1;
}
.observation-list {
display: grid;
gap: 8px;
margin-top: 13px;
}
.empty-state {
padding: 15px;
border: 1px dashed rgba(255, 255, 255, 0.14);
border-radius: 10px;
color: #71807a;
font-size: 10px;
line-height: 1.5;
text-align: center;
}
.observation {
display: grid;
grid-template-columns: 1fr auto;
gap: 10px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.028);
}
.observation-main strong {
display: block;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
font-weight: 600;
}
.observation-main span {
display: block;
margin-top: 3px;
color: #84918b;
font-size: 9px;
}
.comparison {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-top: 8px;
}
.comparison b {
padding: 3px 5px;
border-radius: 5px;
color: #aab5b0;
background: rgba(255, 255, 255, 0.055);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 8px;
font-weight: 500;
}
.comparison b.naive {
color: #e8a98f;
background: rgba(240, 138, 98, 0.08);
}
.comparison b.law {
color: #9cddc1;
background: rgba(114, 201, 169, 0.08);
}
.remove-button {
width: 24px;
height: 24px;
padding: 0;
border: 0;
border-radius: 50%;
color: #7d8984;
background: rgba(255, 255, 255, 0.045);
font-size: 15px;
line-height: 1;
}
.remove-button:hover {
color: #f0a083;
background: rgba(240, 138, 98, 0.12);
}
.accuracy {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: #7e8b86;
font-size: 9px;
}
.accuracy strong {
color: #e4e9e5;
font-family: "SFMono-Regular", Consolas, monospace;
font-weight: 600;
}
.legend-note {
display: grid;
gap: 8px;
margin-top: 13px;
color: #899690;
font-size: 9px;
line-height: 1.45;
}
.legend-note div {
display: flex;
align-items: center;
gap: 8px;
}
.swatch {
width: 22px;
height: 3px;
flex: 0 0 auto;
border-radius: 3px;
background: #6b716e;
}
.swatch.green {
background: var(--green);
}
.swatch.orange {
background: var(--orange);
}
.status {
min-height: 32px;
margin-top: 12px;
color: #91aa9f;
font-size: 9px;
line-height: 1.45;
}
#loading {
position: absolute;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #52605b;
background: #ece6dc;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 860px) {
html,
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100%;
display: block;
}
#viewport {
height: 58vh;
min-height: 420px;
}
#panel {
overflow: visible;
border-left: 0;
border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.panel-inner {
max-width: 620px;
margin: 0 auto;
}
.scene-hud {
inset: 14px 14px auto;
}
.brand p {
display: none;
}
.brand h1 {
font-size: 14px;
}
.brand-mark {
width: 36px;
height: 36px;
}
.glass-button {
padding: 8px 10px;
font-size: 10px;
}
.scene-badges {
left: 14px;
bottom: 14px;
}
.badge {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="app">
<section id="viewport">
<canvas id="scene" aria-label="Interactive 3D comparison of two water clocks"></canvas>
<div class="scene-hud">
<div class="brand">
<div class="brand-mark">◷</div>
<div>
<h1>Water Clock Studio</h1>
<p>Flow-calibrated timekeeping</p>
</div>
</div>
<div class="hud-actions">
<button class="glass-button" id="resetView">Reset view</button>
</div>
</div>
<div class="scene-badges">
<div class="badge"><i></i>One shared water level</div>
<div class="badge green"><i></i>Identical vessels & flow law</div>
<div class="badge orange"><i></i>Observed model marks</div>
</div>
<div id="loading">Preparing the flow model…</div>
</section>
<aside id="panel">
<div class="panel-inner">
<p class="eyebrow">Experiment / 01</p>
<h2>Read time from falling water.</h2>
<p class="intro">
The left ruler assumes equal height intervals represent equal time.
The right ruler converts height to time using the shared nonlinear flow law.
</p>
<section class="section">
<div class="section-heading">
<h3>Shared clock model</h3>
<span id="modelState">paused</span>
</div>
<div class="clock-card">
<div class="clock-row">
<div>
<div class="live-dot">Model time</div>
<div class="large-clock" id="liveTime">00:00</div>
</div>
<button class="button orange" id="playButton">Play</button>
</div>
<div class="readings">
<div class="reading">
<span>Remaining level</span>
<strong id="liveLevel">72.0 cm</strong>
</div>
<div class="reading">
<span>Outflow</span>
<strong id="liveFlow">180 mL/min</strong>
</div>
</div>
<input
id="timeSlider"
type="range"
min="0"
max="58.48"
step="0.01"
value="0"
aria-label="Model time"
/>
<div class="range-labels">
<span>FULL</span>
<span>EMPTY</span>
</div>
<div class="speed-row">
<span>Simulation speed</span>
<select id="speedSelect" aria-label="Simulation speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
</div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h3>Shared flow law</h3>
<span>outflow</span>
</div>
<div class="formula">
<code>
Q(H) = Q₀ · (H / H₀)<sup>β</sup><br />
t(H) = AH₀ / [(1−β)Q₀] · [1−(H/H₀)<sup>1−β</sup>]
</code>
<small>
Both vessels use this exact law. Only the interpretation of the
physical level changes.
</small>
</div>
<div class="law-stats">
<div class="law-stat">
<span>Vessel area</span>
<strong id="areaStat">95.0 cm²</strong>
</div>
<div class="law-stat">
<span>Q₀</span>
<strong>180 mL/min</strong>
</div>
<div class="law-stat">
<span>Full → empty</span>
<strong id="emptyStat">58:29</strong>
</div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h3>Place an observed time mark</h3>
<span id="markCount">0 marks</span>
</div>
<p class="hint">
Enter an observed model reading and its elapsed model time. The orange
mark is placed at the observed height on both rulers. You can also click
a ruler to sample its height.
</p>
<div class="field-grid">
<div class="field">
<label for="readingInput">Observed reading</label>
<input
id="readingInput"
type="number"
min="0"
max="72"
step="0.1"
value="36.0"
/>
</div>
<div class="field">
<label for="modelTimeInput">Model time</label>
<input
id="modelTimeInput"
type="text"
value="21:13"
inputmode="numeric"
aria-label="Observed model time in minutes or minutes and seconds"
/>
</div>
</div>
<div class="field-actions">
<button class="button" id="useLiveButton">Use live reading</button>
<button class="button primary" id="placeButton">Place on both scales</button>
</div>
<div class="demo-row">
<button class="text-button" id="demoButton">Add three example readings</button>
</div>
<div class="observation-list" id="observationList"></div>
<div class="accuracy">
<span>Mean naive-scale miss</span>
<strong id="accuracyStat">—</strong>
</div>
<div class="legend-note">
<div><span class="swatch"></span>Naive ruler: equal physical height intervals</div>
<div><span class="swatch green"></span>Calibrated ruler: inverse of the shared flow law</div>
<div><span class="swatch orange"></span>Orange tick: your observed model mark</div>
</div>
<div class="status" id="statusText">
Drag to orbit · scroll to zoom · click a ruler to sample a reading.
</div>
</section>
</div>
</aside>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
// ---------------------------------------------------------------------
// Physical model
// ---------------------------------------------------------------------
const H0 = 72; // cm
const RADIUS = 5.5; // cm
const AREA = Math.PI * RADIUS * RADIUS; // cm²
const Q0 = 180; // cm³/min at full height
const FLOW_EXP = 0.35;
const T_EMPTY =
(AREA * H0) / ((1 - FLOW_EXP) * Q0);
const BODY_BOTTOM = 0.72;
const BODY_HEIGHT = 4.2;
const WORLD_SCALE = BODY_HEIGHT / H0;
let simTime = 0;
let playing = false;
let speed = 1;
let observations = [];
function levelFromTime(t) {
const u = Math.max(
0,
1 - ((1 - FLOW_EXP) * Q0 * t) / (AREA * H0)
);
return H0 * Math.pow(u, 1 / (1 - FLOW_EXP));
}
function timeFromLevel(level) {
const x = THREE.MathUtils.clamp(level / H0, 0, 1);
return T_EMPTY * (1 - Math.pow(x, 1 - FLOW_EXP));
}
function naiveTimeFromLevel(level) {
return (AREA * (H0 - level)) / Q0;
}
function flowFromLevel(level) {
return Q0 * Math.pow(THREE.MathUtils.clamp(level / H0, 0, 1), FLOW_EXP);
}
function formatClock(minutes) {
const totalSeconds = Math.max(0, Math.round(minutes * 60));
const m = Math.floor(totalSeconds / 60);
const s = totalSeconds % 60;
return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`;
}
function parseTime(value) {
const text = String(value).trim();
if (!text) return NaN;
if (text.includes(":")) {
const parts = text.split(":");
const minutes = Number(parts[0]);
const seconds = Number(parts[1] || 0);
if (!Number.isFinite(minutes) || !Number.isFinite(seconds)) return NaN;
return minutes + seconds / 60;
}
return Number(text);
}
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const viewport = $("viewport");
const canvas = $("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeae4da);
scene.fog = new THREE.Fog(0xeae4da, 13, 27);
const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 80);
const target = new THREE.Vector3(0, 2.45, 0);
let azimuth = 0.34;
let polar = 1.22;
let cameraRadius = 12.2;
let cameraTouched = false;
function updateCamera() {
const sinPolar = Math.sin(polar);
camera.position.set(
target.x + cameraRadius * sinPolar * Math.sin(azimuth),
target.y + cameraRadius * Math.cos(polar),
target.z + cameraRadius * sinPolar * Math.cos(azimuth)
);
camera.lookAt(target);
}
function resetView() {
azimuth = 0.34;
polar = 1.22;
cameraRadius = viewport.clientWidth / viewport.clientHeight < 0.95 ? 14.5 : 12.2;
cameraTouched = false;
updateCamera();
}
$("resetView").addEventListener("click", resetView);
updateCamera();
scene.add(new THREE.HemisphereLight(0xfff6e6, 0x5b6c68, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff0d8, 3.2);
keyLight.position.set(-4, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xbde8e0, 1.6);
rimLight.position.set(5, 5, -6);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xd8d1c4,
roughness: 0.97,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(22, 22, 0x9ea39d, 0xb9b5ab);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const brass = new THREE.MeshStandardMaterial({
color: 0xa98245,
roughness: 0.42,
metalness: 0.68,
flatShading: true
});
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x37413d,
roughness: 0.58,
metalness: 0.48,
flatShading: true
});
const glass = new THREE.MeshPhongMaterial({
color: 0xc7eef0,
transparent: true,
opacity: 0.22,
shininess: 75,
specular: 0xffffff,
side: THREE.DoubleSide,
depthWrite: false
});
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x3da9c2,
emissive: 0x0b3540,
emissiveIntensity: 0.15,
roughness: 0.26,
metalness: 0.02,
transparent: true,
opacity: 0.79,
depthWrite: false,
flatShading: true
});
// ---------------------------------------------------------------------
// Low-poly vessels
// ---------------------------------------------------------------------
const vessels = [];
function makeVessel(x, index) {
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.02, 1.12, 0.22, 12),
new THREE.MeshStandardMaterial({
color: index === 0 ? 0xaaa89e : 0x9ca99f,
roughness: 0.85,
flatShading: true
})
);
pedestal.position.y = 0.11;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.83, 0.87, 0.16, 12),
darkMetal
);
foot.position.y = 0.29;
foot.castShadow = true;
group.add(foot);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(
0.78,
0.68,
BODY_HEIGHT,
12,
1,
true
),
glass
);
body.position.y = BODY_BOTTOM + BODY_HEIGHT / 2;
body.renderOrder = 3;
group.add(body);
const water = new THREE.Mesh(
new THREE.CylinderGeometry(0.70, 0.64, 1, 12),
waterMaterial
);
water.position.y = BODY_BOTTOM + BODY_HEIGHT / 2;
water.renderOrder = 2;
water.castShadow = true;
group.add(water);
const topRing = new THREE.Mesh(
new THREE.TorusGeometry(0.78, 0.035, 4, 12),
brass
);
topRing.rotation.x = Math.PI / 2;
topRing.position.y = BODY_BOTTOM + BODY_HEIGHT;
topRing.castShadow = true;
group.add(topRing);
const bottomRing = new THREE.Mesh(
new THREE.TorusGeometry(0.68, 0.04, 4, 12),
brass
);
bottomRing.rotation.x = Math.PI / 2;
bottomRing.position.y = BODY_BOTTOM;
bottomRing.castShadow = true;
group.add(bottomRing);
for (const angle of [0.05, 2.12, 4.18]) {
const strut = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, BODY_HEIGHT - 0.08, 5),
brass
);
strut.position.set(
Math.cos(angle) * 0.73,
BODY_BOTTOM + BODY_HEIGHT / 2,
Math.sin(angle) * 0.73
);
strut.castShadow = true;
group.add(strut);
}
const outlet = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.14, 0.32, 8),
brass
);
outlet.position.y = 0.47;
outlet.castShadow = true;
group.add(outlet);
const spout = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.09, 0.2, 8),
darkMetal
);
spout.rotation.z = Math.PI / 2;
spout.position.set(0.11, 0.38, 0);
group.add(spout);
const basin = new THREE.Mesh(
new THREE.TorusGeometry(0.45, 0.07, 4, 12),
darkMetal
);
basin.rotation.x = Math.PI / 2;
basin.position.y = 0.07;
basin.castShadow = true;
group.add(basin);
const drops = [];
const dropGeometry = new THREE.OctahedronGeometry(0.045, 0);
const dropMaterial = new THREE.MeshStandardMaterial({
color: 0x58bfd2,
roughness: 0.2,
transparent: true,
opacity: 0.85
});
for (let i = 0; i < 8; i++) {
const drop = new THREE.Mesh(dropGeometry, dropMaterial);
drop.visible = false;
group.add(drop);
drops.push(drop);
}
vessels.push({ group, water, drops, x });
}
makeVessel(-1.72, 0);
makeVessel(1.72, 1);
// Shared level guide.
const levelGuide = new THREE.Mesh(
new THREE.BoxGeometry(5.65, 0.012, 0.018),
new THREE.MeshBasicMaterial({
color: 0x38a8bf,
transparent: true,
opacity: 0.5,
depthTest: false
})
);
levelGuide.position.z = 0.86;
levelGuide.renderOrder = 8;
scene.add(levelGuide);
// ---------------------------------------------------------------------
// Canvas text sprites
// ---------------------------------------------------------------------
function makeTextSprite(
text,
{
color = "#f5f3e9",
background = null,
width = 0.6,
height = 0.15,
fontSize = 58
} = {}
) {
const c = document.createElement("canvas");
c.width = 512;
c.height = 128;
const ctx = c.getContext("2d");
if (background) {
ctx.fillStyle = background;
ctx.beginPath();
ctx.roundRect(5, 5, 502, 118, 20);
ctx.fill();
}
ctx.fillStyle = color;
ctx.font = `700 ${fontSize}px ui-monospace, SFMono-Regular, Menlo, monospace`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
function makePlaque(title, subtitle, accent) {
const c = document.createElement("canvas");
c.width = 768;
c.height = 180;
const ctx = c.getContext("2d");
ctx.fillStyle = "rgba(23, 32, 29, 0.91)";
ctx.beginPath();
ctx.roundRect(8, 8, 752, 164, 28);
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 6;
ctx.beginPath();
ctx.roundRect(8, 8, 752, 164, 28);
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = "#f7f4eb";
ctx.font = "800 54px ui-sans-serif, system-ui, sans-serif";
ctx.fillText(title, 384, 76);
ctx.fillStyle = "#a9b5af";
ctx.font = "500 27px ui-sans-serif, system-ui, sans-serif";
ctx.fillText(subtitle, 384, 126);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(2.75, 0.645, 1);
sprite.renderOrder = 25;
return sprite;
}
const naivePlaque = makePlaque(
"NAIVE SCALE",
"Equal height · constant-flow assumption",
"#8f9692"
);
naivePlaque.position.set(-1.72, 5.46, 0);
scene.add(naivePlaque);
const calibratedPlaque = makePlaque(
"CALIBRATED SCALE",
"Same flow law · inverse level-to-time",
"#72c9a9"
);
calibratedPlaque.position.set(1.72, 5.46, 0);
scene.add(calibratedPlaque);
// ---------------------------------------------------------------------
// Rulers
// ---------------------------------------------------------------------
const naiveX = -2.66;
const calibratedX = 2.66;
const hitTargets = [];
function levelToY(level) {
return BODY_BOTTOM + (level / H0) * BODY_HEIGHT;
}
function addVerticalRule(x, color) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.8
});
const line = new THREE.Mesh(
new THREE.BoxGeometry(0.022, BODY_HEIGHT, 0.025),
material
);
line.position.set(x, BODY_BOTTOM + BODY_HEIGHT / 2, 0.7);
scene.add(line);
const hit = new THREE.Mesh(
new THREE.BoxGeometry(0.42, BODY_HEIGHT, 0.28),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
hit.position.set(x, BODY_BOTTOM + BODY_HEIGHT / 2, 0.71);
hit.userData.readingScale = true;
scene.add(hit);
hitTargets.push(hit);
}
function addTick(x, y, direction, length, color, opacity = 1) {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.018, 0.024),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity
})
);
tick.position.set(x + direction * length * 0.5, y, 0.72);
tick.renderOrder = 9;
scene.add(tick);
return tick;
}
addVerticalRule(naiveX, 0x68706d);
addVerticalRule(calibratedX, 0x2f8d78);
// Naive ruler: physically equal 6 cm intervals.
for (let level = H0; level >= 0; level -= 6) {
const y = levelToY(level);
addTick(naiveX, y, -1, 0.18, 0x626b67);
const label = makeTextSprite(formatClock(naiveTimeFromLevel(level)), {
color: "#4c5652",
width: 0.53,
height: 0.1325
});
label.position.set(naiveX - 0.42, y, 0.76);
scene.add(label);
}
// Calibrated ruler: equal time intervals, non-uniform physical positions.
for (let minute = 0; minute <= 55; minute += 5) {
const level = H0 *
Math.pow(
Math.max(
0,
1 - ((1 - FLOW_EXP) * Q0 * minute) / (AREA * H0)
),
1 / (1 - FLOW_EXP)
);
const y = levelToY(level);
addTick(calibratedX, y, 1, 0.18, 0x2f8d78);
const label = makeTextSprite(formatClock(minute), {
color: "#247a68",
width: 0.53,
height: 0.1325
});
label.position.set(calibratedX + 0.42, y, 0.76);
scene.add(label);
}
const endLabel = makeTextSprite(formatClock(T_EMPTY), {
color: "#247a68",
width: 0.6,
height: 0.15
});
endLabel.position.set(calibratedX + 0.43, levelToY(0), 0.76);
scene.add(endLabel);
// Separate groups for user-added observations.
const naiveObservationGroup = new THREE.Group();
const calibratedObservationGroup = new THREE.Group();
scene.add(naiveObservationGroup, calibratedObservationGroup);
function disposeGroup(group) {
while (group.children.length) {
const child = group.children[0];
group.remove(child);
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => {
if (material.map) material.map.dispose();
material.dispose();
});
}
});
}
}
function renderObservationMarks() {
disposeGroup(naiveObservationGroup);
disposeGroup(calibratedObservationGroup);
observations.forEach((observation) => {
const y = levelToY(observation.reading);
const naiveTick = new THREE.Mesh(
new THREE.BoxGeometry(0.4, 0.028, 0.035),
new THREE.MeshBasicMaterial({
color: 0xf08a62,
depthTest: false
})
);
naiveTick.position.set(naiveX - 0.18, y, 0.75);
naiveTick.renderOrder = 22;
naiveObservationGroup.add(naiveTick);
const naiveLabel = makeTextSprite(
`N ${formatClock(naiveTimeFromLevel(observation.reading))}`,
{
color: "#c75f3c",
width: 0.72,
height: 0.18
}
);
naiveLabel.position.set(naiveX - 0.63, y + 0.13, 0.78);
naiveObservationGroup.add(naiveLabel);
const calibratedTick = new THREE.Mesh(
new THREE.BoxGeometry(0.4, 0.028, 0.035),
new THREE.MeshBasicMaterial({
color: 0xf08a62,
depthTest: false
})
);
calibratedTick.position.set(calibratedX + 0.18, y, 0.75);
calibratedTick.renderOrder = 22;
calibratedObservationGroup.add(calibratedTick);
const calibratedLabel = makeTextSprite(
`O ${formatClock(observation.time)}`,
{
color: "#c75f3c",
width: 0.72,
height: 0.18
}
);
calibratedLabel.position.set(calibratedX + 0.65, y + 0.13, 0.78);
calibratedObservationGroup.add(calibratedLabel);
});
}
// ---------------------------------------------------------------------
// Observation UI
// ---------------------------------------------------------------------
function renderObservationList() {
const container = $("observationList");
container.innerHTML = "";
$("markCount").textContent =
`${observations.length} ${observations.length === 1 ? "mark" : "marks"}`;
if (!observations.length) {
container.innerHTML =
'<div class="empty-state">No observed marks yet. Add a reading or sample a ruler in the scene.</div>';
$("accuracyStat").textContent = "—";
return;
}
const sorted = [...observations].sort((a, b) => a.time - b.time);
sorted.forEach((observation) => {
const naive = naiveTimeFromLevel(observation.reading);
const law = timeFromLevel(observation.reading);
const error = naive - observation.time;
const item = document.createElement("div");
item.className = "observation";
item.innerHTML = `
<div class="observation-main">
<strong>${observation.reading.toFixed(1)} cm · observed ${formatClock(observation.time)}</strong>
<span>${error >= 0 ? "+" : "−"}${formatClock(Math.abs(error))} naive miss</span>
<div class="comparison">
<b class="naive">N ${formatClock(naive)}</b>
<b class="law">Law ${formatClock(law)}</b>
</div>
</div>
<button class="remove-button" data-id="${observation.id}" aria-label="Remove observation">×</button>
`;
container.appendChild(item);
});
const meanError =
observations.reduce(
(sum, observation) =>
sum +
Math.abs(
naiveTimeFromLevel(observation.reading) - observation.time
),
0
) / observations.length;
$("accuracyStat").textContent = formatClock(meanError);
}
function placeObservation() {
const reading = Number($("readingInput").value);
const time = parseTime($("modelTimeInput").value);
if (!Number.isFinite(reading) || reading < 0 || reading > H0) {
$("statusText").textContent =
"Enter a reading between 0 and 72 cm.";
return;
}
if (!Number.isFinite(time) || time < 0) {
$("statusText").textContent =
"Enter a valid model time, for example 12:30 or 12.5 minutes.";
return;
}
const duplicate = observations.find(
(observation) => Math.abs(observation.reading - reading) < 0.05
);
if (duplicate) {
observations = observations.filter(
(observation) => observation.id !== duplicate.id
);
}
observations.push({
id: Date.now() + Math.random(),
reading,
time
});
renderObservationMarks();
renderObservationList();
const lawTime = timeFromLevel(reading);
const residual = Math.abs(lawTime - time);
$("statusText").textContent =
residual < 0.08
? `Placed at ${reading.toFixed(1)} cm. The observed time agrees with the shared flow law.`
: `Placed at ${reading.toFixed(1)} cm. This observation differs from the law-implied time by ${formatClock(residual)}.`;
}
$("placeButton").addEventListener("click", placeObservation);
$("useLiveButton").addEventListener("click", () => {
const level = levelFromTime(simTime);
$("readingInput").value = level.toFixed(1);
$("modelTimeInput").value = formatClock(simTime);
$("statusText").textContent =
"Live reading captured. Adjust the model time if the external observation differs, then place the mark.";
});
$("demoButton").addEventListener("click", () => {
observations = [54, 36, 18].map((reading, index) => ({
id: Date.now() + index,
reading,
time: timeFromLevel(reading)
}));
renderObservationMarks();
renderObservationList();
$("statusText").textContent =
"Three example observations added. Scrub the clock or add your own readings.";
});
$("observationList").addEventListener("click", (event) => {
const button = event.target.closest("[data-id]");
if (!button) return;
observations = observations.filter(
(observation) => observation.id !== button.dataset.id
);
renderObservationMarks();
renderObservationList();
});
// ---------------------------------------------------------------------
// Simulation controls
// ---------------------------------------------------------------------
function setPlaying(value) {
if (value && simTime >= T_EMPTY) {
simTime = 0;
}
playing = value;
$("playButton").textContent = playing ? "Pause" : "Play";
$("modelState").textContent = playing ? "running" : "paused";
}
$("playButton").addEventListener("click", () => setPlaying(!playing));
$("timeSlider").addEventListener("input", (event) => {
simTime = Number(event.target.value);
setPlaying(false);
updateSimulationVisuals();
updateSimulationUI();
});
$("speedSelect").addEventListener("change", (event) => {
speed = Number(event.target.value);
});
function updateSimulationVisuals() {
const level = levelFromTime(simTime);
const waterHeight = (level / H0) * BODY_HEIGHT;
vessels.forEach((vessel) => {
vessel.water.visible = level > 0.01;
vessel.water.scale.y = Math.max(0.001, waterHeight);
vessel.water.position.y = BODY_BOTTOM + waterHeight / 2;
});
levelGuide.position.y = levelToY(level);
levelGuide.visible = level > 0.05;
}
function updateSimulationUI() {
const level = levelFromTime(simTime);
$("liveTime").textContent = formatClock(simTime);
$("liveLevel").textContent = `${level.toFixed(1)} cm`;
$("liveFlow").textContent = `${flowFromLevel(level).toFixed(0)} mL/min`;
$("timeSlider").value = simTime;
}
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, pinch, ruler picking
// ---------------------------------------------------------------------
const pointers = new Map();
let moved = false;
let lastPointer = { x: 0, y: 0 };
let pinchDistance = 0;
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function pickRuler(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);
const hits = raycaster.intersectObjects(hitTargets, false);
if (!hits.length) return;
const worldY = hits[0].point.y;
const level = THREE.MathUtils.clamp(
((worldY - BODY_BOTTOM) / BODY_HEIGHT) * H0,
0,
H0
);
$("readingInput").value = level.toFixed(1);
$("statusText").textContent =
`Sampled ${level.toFixed(1)} cm from the ruler. Enter the observed model time, then place the mark.`;
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
lastPointer = { x: event.clientX, y: event.clientY };
moved = false;
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
moved = true;
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
if (Math.abs(dx) + Math.abs(dy) > 1) {
moved = true;
}
azimuth -= dx * 0.006;
polar = THREE.MathUtils.clamp(polar + dy * 0.005, 0.38, 1.48);
cameraTouched = true;
lastPointer = { x: event.clientX, y: event.clientY };
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (pinchDistance / distance),
7.5,
19
);
cameraTouched = true;
}
pinchDistance = distance;
moved = true;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
pinchDistance = 0;
}
if (!moved && pointers.size === 0) {
pickRuler(event);
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
7.5,
19
);
cameraTouched = true;
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resize() {
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();
if (!cameraTouched) {
cameraRadius = width / height < 0.95 ? 14.5 : 12.2;
}
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
$("areaStat").textContent = `${AREA.toFixed(1)} cm²`;
$("emptyStat").textContent = formatClock(T_EMPTY);
$("timeSlider").max = T_EMPTY;
renderObservationMarks();
renderObservationList();
updateSimulationVisuals();
updateSimulationUI();
let lastFrame = performance.now();
let lastUIUpdate = 0;
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min(0.05, (now - lastFrame) / 1000);
lastFrame = now;
if (playing) {
simTime = Math.min(T_EMPTY, simTime + dt * speed);
if (simTime >= T_EMPTY) {
setPlaying(false);
$("statusText").textContent =
"The vessel is empty. Reset or scrub the model time to begin again.";
}
}
updateSimulationVisuals();
const level = levelFromTime(simTime);
const flowRatio = flowFromLevel(level) / Q0;
const elapsed = now / 1000;
vessels.forEach((vessel) => {
vessel.drops.forEach((drop, index) => {
drop.visible = playing && level > 0.05;
if (drop.visible) {
const progress =
(elapsed * (0.35 + flowRatio * 0.8) + index / 8) % 1;
drop.position.set(
vessel.x + 0.13 + Math.sin(index * 2.1) * 0.025,
0.37 - progress * 0.28,
Math.cos(index * 1.7) * 0.025
);
const scale = 0.9 - progress * 0.4;
drop.scale.setScalar(scale);
}
});
});
if (now - lastUIUpdate > 80) {
updateSimulationUI();
lastUIUpdate = now;
}
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
$("loading").remove();
</script>
</body>
</html>
```