Prompt specification
Build or customize the Three.js 3D scene "Buoyancy density exploration lab" 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: Buoyancy density exploration lab
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/buoyancy-density-exploration-lab-x1444
Scene Description Prompt:
Change a low-poly object’s mass and volume independently in a buoyancy tank; show submerged fraction or sinking. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (buoyancy-density-exploration-lab-x1444):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Archimedes Lab — Buoyancy Tank</title>
<style>
:root {
color-scheme: dark;
--bg: #07121d;
--panel: rgba(9, 25, 39, 0.88);
--border: rgba(166, 220, 238, 0.16);
--text: #edf8fb;
--muted: #91aab6;
--cyan: #55e0df;
--orange: #ffad66;
--green: #62e6b3;
--red: #ff6f69;
}
* {
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;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.topbar {
position: fixed;
top: 22px;
left: 24px;
right: 24px;
z-index: 5;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 11px;
color: rgba(233, 249, 253, 0.86);
letter-spacing: 0.12em;
font-size: 11px;
font-weight: 750;
text-transform: uppercase;
}
.brand-mark {
width: 31px;
height: 31px;
display: grid;
place-items: center;
border: 1px solid rgba(85, 224, 223, 0.36);
border-radius: 9px;
color: var(--cyan);
background: rgba(85, 224, 223, 0.08);
box-shadow: inset 0 0 16px rgba(85, 224, 223, 0.08);
font-size: 17px;
}
.live-badge {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(8, 22, 34, 0.62);
backdrop-filter: blur(12px);
color: #bfd1d8;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.live-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 10px var(--green);
}
.panel {
position: fixed;
z-index: 10;
top: 78px;
left: 24px;
width: 338px;
max-height: calc(100vh - 102px);
overflow: auto;
padding: 20px;
border: 1px solid var(--border);
border-radius: 20px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.035), transparent 45%),
var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.34),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(20px);
scrollbar-width: thin;
scrollbar-color: rgba(85, 224, 223, 0.35) transparent;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 7px;
font-size: 25px;
line-height: 1.1;
letter-spacing: -0.035em;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.status-block {
margin-top: 18px;
padding: 14px;
border: 1px solid rgba(98, 230, 179, 0.2);
border-radius: 14px;
background: rgba(98, 230, 179, 0.07);
}
.status-block[data-state="sinking"],
.status-block[data-state="sunk"] {
border-color: rgba(255, 111, 105, 0.25);
background: rgba(255, 111, 105, 0.07);
}
.status-block[data-state="neutral"] {
border-color: rgba(85, 224, 223, 0.24);
background: rgba(85, 224, 223, 0.07);
}
.status-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.status-name {
display: flex;
align-items: center;
gap: 8px;
color: var(--green);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
[data-state="sinking"] .status-name,
[data-state="sunk"] .status-name {
color: var(--red);
}
[data-state="neutral"] .status-name {
color: var(--cyan);
}
.status-indicator {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.fraction {
margin-top: 12px;
font-size: 10px;
color: var(--muted);
text-transform: uppercase;
letter-spacing: 0.1em;
}
.fraction-number {
margin-top: 1px;
font-size: 42px;
font-weight: 760;
line-height: 1;
letter-spacing: -0.06em;
}
.fraction-number small {
margin-left: 4px;
color: var(--muted);
font-size: 15px;
font-weight: 600;
letter-spacing: 0;
}
.fraction-track {
height: 5px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.fraction-track i {
display: block;
width: 75%;
height: 100%;
border-radius: inherit;
background: var(--green);
transition: width 180ms ease, background 180ms ease;
}
.status-note {
min-height: 34px;
margin: 10px 0 0;
color: #a9bec7;
font-size: 11px;
line-height: 1.5;
}
.control-section {
margin-top: 19px;
padding-top: 17px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
color: #c9d8de;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.control + .control {
margin-top: 16px;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 9px;
}
.control-label label {
color: #b8cbd3;
font-size: 12px;
}
.control-value {
min-width: 91px;
color: white;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
font-weight: 700;
text-align: right;
}
.control-value.mass {
color: var(--orange);
}
.control-value.volume {
color: var(--cyan);
}
input[type="range"] {
--pct: 50%;
width: 100%;
height: 4px;
margin: 4px 0;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
to right,
var(--slider-color) 0%,
var(--slider-color) var(--pct),
rgba(255, 255, 255, 0.1) var(--pct),
rgba(255, 255, 255, 0.1) 100%
);
}
#massSlider {
--slider-color: var(--orange);
}
#volumeSlider {
--slider-color: var(--cyan);
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid #122634;
border-radius: 50%;
background: var(--slider-color);
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #122634;
border-radius: 50%;
background: var(--slider-color);
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
cursor: pointer;
}
.range-limits {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #657d89;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.derived {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 9px;
margin-top: 18px;
}
.derived-card {
padding: 11px;
border: 1px solid rgba(255, 255, 255, 0.065);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.derived-card span {
display: block;
color: #718b97;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.derived-card strong {
display: block;
margin-top: 5px;
color: #dcebf0;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
}
.force-legend {
display: flex;
gap: 15px;
margin-top: 15px;
color: #92aab4;
font-size: 10px;
}
.force-legend span {
display: flex;
align-items: center;
gap: 6px;
}
.force-legend i {
width: 16px;
height: 2px;
background: currentColor;
}
.force-legend .buoyancy {
color: var(--cyan);
}
.force-legend .weight {
color: var(--orange);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 17px;
}
button {
min-height: 39px;
border: 1px solid rgba(85, 224, 223, 0.26);
border-radius: 11px;
background: rgba(85, 224, 223, 0.11);
color: #baf5f1;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease,
border-color 150ms ease;
}
button:hover {
border-color: rgba(85, 224, 223, 0.5);
background: rgba(85, 224, 223, 0.19);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.ghost {
border-color: rgba(255, 255, 255, 0.09);
background: rgba(255, 255, 255, 0.035);
color: #9eb3bc;
}
.hint {
position: fixed;
z-index: 4;
right: 24px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
color: rgba(185, 211, 220, 0.55);
font-size: 10px;
letter-spacing: 0.05em;
pointer-events: none;
}
.hint kbd {
padding: 4px 6px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 5px;
background: rgba(8, 22, 34, 0.58);
color: rgba(225, 244, 249, 0.7);
font: inherit;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #07121d;
color: var(--cyan);
font-size: 12px;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
width: 36px;
height: 36px;
margin: 0 auto 13px;
border: 2px solid rgba(85, 224, 223, 0.14);
border-top-color: var(--cyan);
border-radius: 50%;
animation: spin 800ms linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (max-width: 700px) {
.topbar {
top: 14px;
left: 14px;
right: 14px;
}
.brand {
font-size: 9px;
}
.live-badge {
padding: 7px 9px;
font-size: 8px;
}
.panel {
top: auto;
left: 12px;
right: 12px;
bottom: 12px;
width: auto;
max-height: 46vh;
padding: 16px;
border-radius: 16px;
}
h1 {
font-size: 21px;
}
.fraction-number {
font-size: 34px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div class="topbar">
<div class="brand">
<span class="brand-mark">≈</span>
<span>Archimedes Lab / Buoyancy Tank</span>
</div>
<div class="live-badge">
<span class="live-dot"></span>
Live simulation
</div>
</div>
<aside class="panel">
<div class="eyebrow">Experiment 01</div>
<h1>Will it float?</h1>
<p class="intro">
Change the object's mass and volume independently, then release it into
fresh water at 1,000 kg/m³.
</p>
<div class="status-block" id="statusBlock" data-state="floating">
<div class="status-top">
<div class="status-name">
<span class="status-indicator"></span>
<span id="statusText">Floating</span>
</div>
<span style="color:#728c97;font-size:10px">ρ water = 1,000 kg/m³</span>
</div>
<div class="fraction">Submerged fraction</div>
<div class="fraction-number">
<span id="fractionValue">75</span><small>%</small>
</div>
<div class="fraction-track">
<i id="fractionBar"></i>
</div>
<p class="status-note" id="statusNote">
75% of the object's volume must be underwater to float.
</p>
</div>
<section class="control-section">
<div class="control-heading">
<span>Object properties</span>
<span>Independent controls</span>
</div>
<div class="control">
<div class="control-label">
<label for="massSlider">Mass</label>
<output class="control-value mass" id="massOutput">600 kg</output>
</div>
<input
id="massSlider"
type="range"
min="50"
max="3000"
step="25"
value="600"
aria-label="Object mass in kilograms"
/>
<div class="range-limits">
<span>50 kg</span>
<span>3,000 kg</span>
</div>
</div>
<div class="control">
<div class="control-label">
<label for="volumeSlider">Volume</label>
<output class="control-value volume" id="volumeOutput">0.80 m³</output>
</div>
<input
id="volumeSlider"
type="range"
min="0.1"
max="2"
step="0.05"
value="0.8"
aria-label="Object volume in cubic metres"
/>
<div class="range-limits">
<span>0.10 m³</span>
<span>2.00 m³</span>
</div>
</div>
</section>
<div class="derived">
<div class="derived-card">
<span>Displaced-water mass</span>
<strong id="displacedMass">800 kg</strong>
</div>
<div class="derived-card">
<span>Net force</span>
<strong id="netForce">↑ 1.96 kN</strong>
</div>
<div class="derived-card">
<span>Average density</span>
<strong id="densityValue">750 kg/m³</strong>
</div>
<div class="derived-card">
<span>Maximum buoyancy</span>
<strong id="buoyancyValue">7.85 kN</strong>
</div>
</div>
<div class="force-legend">
<span class="buoyancy"><i></i> Buoyant force</span>
<span class="weight"><i></i> Weight</span>
</div>
<div class="buttons">
<button id="dropButton">Drop test</button>
<button id="viewButton" class="ghost">Reset view</button>
</div>
</aside>
<div class="hint">
<kbd>Drag</kbd> orbit
<span>·</span>
<kbd>Wheel</kbd> zoom
</div>
<div id="loading">
<div>
<div class="loader"></div>
Preparing tank
</div>
</div>
<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("scene");
const loading = document.getElementById("loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07121d);
scene.fog = new THREE.FogExp2(0x07121d, 0.032);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Custom camera controls — no external examples/addons.
const cameraTarget = new THREE.Vector3(0, -1.75, 0);
let cameraYaw = 0.72;
let cameraPitch = 0.34;
let cameraDistance = 12.4;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.005,
-0.08,
1.08
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event?.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.008,
8,
20
);
updateCamera();
},
{ passive: false }
);
// Lighting.
scene.add(new THREE.HemisphereLight(0xbcefff, 0x18212a, 1.8));
const keyLight = new THREE.DirectionalLight(0xfff1dc, 3.2);
keyLight.position.set(5, 9, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55dfff, 1.45);
rimLight.position.set(-5, 3, -4);
scene.add(rimLight);
const underwaterLight = new THREE.PointLight(0x36d9ff, 28, 12, 2);
underwaterLight.position.set(0, -2, 0);
scene.add(underwaterLight);
// Tank dimensions.
const TANK = {
width: 8,
depth: 5,
height: 5,
bottom: -5,
surface: 0
};
// Tank floor.
const floor = new THREE.Mesh(
new THREE.BoxGeometry(TANK.width + 0.35, 0.28, TANK.depth + 0.35),
new THREE.MeshStandardMaterial({
color: 0x102a37,
roughness: 0.86,
metalness: 0.08
})
);
floor.position.y = TANK.bottom - 0.15;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(8, 16, 0x2b7182, 0x204957);
grid.position.y = TANK.bottom + 0.015;
grid.scale.z = TANK.depth / TANK.width;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
// Transparent glass tank.
const glassGeometry = new THREE.BoxGeometry(
TANK.width,
TANK.height,
TANK.depth
);
const glass = new THREE.Mesh(
glassGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x8edbed,
transparent: true,
opacity: 0.045,
roughness: 0.12,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
glass.position.y = TANK.surface - TANK.height / 2;
glass.renderOrder = 4;
scene.add(glass);
const frame = new THREE.LineSegments(
new THREE.EdgesGeometry(glassGeometry),
new THREE.LineBasicMaterial({
color: 0x74c9d8,
transparent: true,
opacity: 0.42
})
);
frame.position.copy(glass.position);
frame.renderOrder = 6;
scene.add(frame);
// Water body.
const waterHeight = TANK.depth - 0.08;
const water = new THREE.Mesh(
new THREE.BoxGeometry(
TANK.width - 0.18,
waterHeight,
TANK.depth - 0.18
),
new THREE.MeshPhongMaterial({
color: 0x2ba8c3,
transparent: true,
opacity: 0.17,
shininess: 85,
side: THREE.DoubleSide,
depthWrite: false
})
);
water.position.y = TANK.surface - waterHeight / 2;
water.renderOrder = 3;
scene.add(water);
// Slightly animated water surface.
const surfaceGeometry = new THREE.PlaneGeometry(
TANK.width - 0.2,
TANK.depth - 0.2,
24,
16
);
surfaceGeometry.rotateX(-Math.PI / 2);
const surface = new THREE.Mesh(
surfaceGeometry,
new THREE.MeshStandardMaterial({
color: 0x4bd4df,
transparent: true,
opacity: 0.33,
roughness: 0.2,
metalness: 0.08,
side: THREE.DoubleSide,
depthWrite: false
})
);
surface.position.y = TANK.surface;
surface.renderOrder = 5;
scene.add(surface);
const waterlinePoints = [
new THREE.Vector3(-4, 0.015, -2.5),
new THREE.Vector3(4, 0.015, -2.5),
new THREE.Vector3(4, 0.015, 2.5),
new THREE.Vector3(-4, 0.015, 2.5)
];
const waterline = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(waterlinePoints),
new THREE.LineBasicMaterial({
color: 0x8df8f2,
transparent: true,
opacity: 0.8
})
);
scene.add(waterline);
// Low-poly faceted object.
const sourceGeometry = new THREE.IcosahedronGeometry(1, 0);
const objectGeometry = sourceGeometry.index
? sourceGeometry.toNonIndexed()
: sourceGeometry.clone();
if (sourceGeometry.index) sourceGeometry.dispose();
const palette = [
new THREE.Color(0xf5a14c),
new THREE.Color(0xff704f),
new THREE.Color(0xe7c56d),
new THREE.Color(0xd97b43),
new THREE.Color(0xf4bd69)
];
const colorArray = new Float32Array(
objectGeometry.attributes.position.count * 3
);
for (let i = 0; i < objectGeometry.attributes.position.count; i += 3) {
const a = objectGeometry.attributes.position;
const centerX = (a.getX(i) + a.getX(i + 1) + a.getX(i + 2)) / 3;
const centerY = (a.getY(i) + a.getY(i + 1) + a.getY(i + 2)) / 3;
const centerZ = (a.getZ(i) + a.getZ(i + 1) + a.getZ(i + 2)) / 3;
const colorIndex =
Math.floor(Math.abs(centerX * 13 + centerY * 17 + centerZ * 19) * 37) %
palette.length;
const color = palette[colorIndex];
for (let j = 0; j < 3; j++) {
colorArray[(i + j) * 3] = color.r;
colorArray[(i + j) * 3 + 1] = color.g;
colorArray[(i + j) * 3 + 2] = color.b;
}
}
objectGeometry.setAttribute(
"color",
new THREE.BufferAttribute(colorArray, 3)
);
objectGeometry.computeVertexNormals();
objectGeometry.computeBoundingBox();
const objectPivot = new THREE.Group();
objectPivot.rotation.y = 0.42;
scene.add(objectPivot);
const objectMesh = new THREE.Mesh(
objectGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.52,
metalness: 0.12
})
);
objectMesh.castShadow = true;
objectMesh.receiveShadow = true;
objectPivot.add(objectMesh);
const objectEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(objectGeometry, 12),
new THREE.LineBasicMaterial({
color: 0x6e321f,
transparent: true,
opacity: 0.55
})
);
objectPivot.add(objectEdges);
// Volume of a radius-1 regular icosahedron.
const ICOSAHEDRON_VOLUME = 2.536;
// Force arrows.
const buoyancyArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
1,
0x55e0df,
0.25,
0.12
);
const weightArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(),
1,
0xffad66,
0.25,
0.12
);
for (const arrow of [buoyancyArrow, weightArrow]) {
arrow.line.material.depthTest = false;
arrow.cone.material.depthTest = false;
arrow.line.renderOrder = 10;
arrow.cone.renderOrder = 10;
scene.add(arrow);
}
// Small suspended bubbles.
const bubbleCount = 42;
const bubblePositions = new Float32Array(bubbleCount * 3);
const bubbleMetadata = [];
for (let i = 0; i < bubbleCount; i++) {
const x = (Math.random() - 0.5) * (TANK.width - 1);
const z = (Math.random() - 0.5) * (TANK.depth - 1);
const speed = 0.16 + Math.random() * 0.32;
const offset = Math.random() * (TANK.depth - 0.5);
bubbleMetadata.push({ x, z, speed, offset });
bubblePositions[i * 3] = x;
bubblePositions[i * 3 + 1] = TANK.bottom + offset;
bubblePositions[i * 3 + 2] = z;
}
const bubbleGeometry = new THREE.BufferGeometry();
bubbleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(bubblePositions, 3)
);
const bubbles = new THREE.Points(
bubbleGeometry,
new THREE.PointsMaterial({
color: 0xc4fbff,
size: 0.055,
transparent: true,
opacity: 0.42,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(bubbles);
// Surface ripple used by the drop button.
const ripple = new THREE.Mesh(
new THREE.RingGeometry(0.48, 0.51, 48),
new THREE.MeshBasicMaterial({
color: 0x9cfff5,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
ripple.rotation.x = -Math.PI / 2;
ripple.position.y = TANK.surface + 0.035;
ripple.renderOrder = 7;
scene.add(ripple);
// Simulation state.
const WATER_DENSITY = 1000;
const GRAVITY = 9.81;
const state = {
mass: 600,
volume: 0.8,
y: 0,
velocityY: 0,
halfHeight: 0.6,
rippleAge: 10
};
const massSlider = document.getElementById("massSlider");
const volumeSlider = document.getElementById("volumeSlider");
const massOutput = document.getElementById("massOutput");
const volumeOutput = document.getElementById("volumeOutput");
const statusBlock = document.getElementById("statusBlock");
const statusText = document.getElementById("statusText");
const statusNote = document.getElementById("statusNote");
const fractionValue = document.getElementById("fractionValue");
const fractionBar = document.getElementById("fractionBar");
const displacedMass = document.getElementById("displacedMass");
const netForce = document.getElementById("netForce");
const densityValue = document.getElementById("densityValue");
const buoyancyValue = document.getElementById("buoyancyValue");
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percentage = ((value - min) / (max - min)) * 100;
input.style.setProperty("--pct", `${percentage}%`);
}
function updateObjectScale() {
const scale = Math.cbrt(state.volume / ICOSAHEDRON_VOLUME);
objectPivot.scale.setScalar(scale);
const bounds = objectGeometry.boundingBox;
const verticalSize = bounds.max.y - bounds.min.y;
state.halfHeight = (verticalSize * scale) / 2;
}
function getDensityRatio() {
return state.mass / (WATER_DENSITY * state.volume);
}
function getVisualSubmersion() {
const bottomY = state.y - state.halfHeight;
const topY = state.y + state.halfHeight;
if (topY <= TANK.surface) return 1;
if (bottomY >= TANK.surface) return 0;
return THREE.MathUtils.clamp(
(TANK.surface - bottomY) / (state.halfHeight * 2),
0,
1
);
}
function isOnBottom() {
return (
state.y <=
TANK.bottom + state.halfHeight + 0.025
);
}
function getFloatingTarget(ratio, time) {
const gentleBob = Math.sin(time * 1.8) * 0.017;
return (
TANK.surface +
state.halfHeight -
2 * ratio * state.halfHeight +
gentleBob
);
}
function updatePhysics(delta, time) {
const ratio = getDensityRatio();
if (ratio <= 1) {
// Stable spring around the ideal submerged-volume equilibrium.
const target = getFloatingTarget(ratio, time);
const omega = Math.sqrt(22 * (700 / state.mass));
const damping = 2 * 0.64 * omega;
const acceleration =
omega * omega * (target - state.y) -
damping * state.velocityY;
state.velocityY += acceleration * delta;
state.y += state.velocityY * delta;
} else {
// Real downward force: gravity minus the maximum buoyant force.
const submerged = getVisualSubmersion();
const submergedDownAcceleration =
Math.max(0, 1 - (WATER_DENSITY * state.volume) / state.mass) *
GRAVITY;
const downwardAcceleration =
(1 - submerged) * GRAVITY +
submerged * submergedDownAcceleration;
state.velocityY -= downwardAcceleration * delta;
state.velocityY *= Math.exp(-0.58 * delta);
state.y += state.velocityY * delta;
const minimumY = TANK.bottom + state.halfHeight + 0.025;
if (state.y <= minimumY) {
state.y = minimumY;
state.velocityY = 0;
}
}
objectPivot.position.y = state.y;
}
function forceLength(force) {
return 0.28 + Math.min(1.8, force / 9000);
}
function updateForceArrows() {
const submersion = getVisualSubmersion();
const weightForce = state.mass * GRAVITY;
const buoyantForce =
WATER_DENSITY * state.volume * GRAVITY * submersion;
const horizontalOffset = state.halfHeight * 1.2 + 0.55;
buoyancyArrow.position.set(
-horizontalOffset,
state.y,
0
);
buoyancyArrow.setLength(
forceLength(buoyantForce),
0.24,
0.12
);
weightArrow.position.set(
horizontalOffset,
state.y,
0
);
weightArrow.setLength(
forceLength(weightForce),
0.24,
0.12
);
}
function updateReadouts() {
const displacedWaterMass = WATER_DENSITY * state.volume;
const ratio = getDensityRatio();
const density = state.mass / state.volume;
const upwardForce = (displacedWaterMass - state.mass) * GRAVITY;
const fraction = Math.min(ratio, 1);
let status;
if (ratio > 1.0001) {
status = isOnBottom() ? "Sunk" : "Sinking";
} else if (ratio > 0.9999) {
status = "Neutral";
} else {
status = "Floating";
}
statusText.textContent = status;
statusBlock.dataset.state = status.toLowerCase();
fractionValue.textContent = Math.round(fraction * 100);
fractionBar.style.width = `${fraction * 100}%`;
if (status === "Floating") {
fractionBar.style.background = "var(--green)";
statusNote.textContent =
`${Math.round(ratio * 100)}% of the object's volume must be ` +
`underwater to balance its ${state.mass.toLocaleString()} kg mass.`;
} else if (status === "Neutral") {
fractionBar.style.background = "var(--cyan)";
statusNote.textContent =
"Its average density matches the water, so it can hover fully submerged.";
} else if (status === "Sinking") {
fractionBar.style.background = "var(--red)";
statusNote.textContent =
`The tank can support only ${Math.round(displacedWaterMass).toLocaleString()} kg. ` +
`This object exceeds that maximum buoyant mass.`;
} else {
fractionBar.style.background = "var(--red)";
statusNote.textContent =
`The object is denser than water and has settled on the tank floor.`;
}
displacedMass.textContent =
`${Math.round(displacedWaterMass).toLocaleString()} kg`;
densityValue.textContent =
`${density.toLocaleString(undefined, {
maximumFractionDigits: 0
})} kg/m³`;
buoyancyValue.textContent =
`${(displacedWaterMass * GRAVITY / 1000).toFixed(2)} kN`;
const netForceKilonewtons = upwardForce / 1000;
const direction = netForceKilonewtons >= 0 ? "↑" : "↓";
netForce.textContent =
`${direction} ${Math.abs(netForceKilonewtons).toFixed(2)} kN`;
netForce.style.color =
netForceKilonewtons >= 0 ? "var(--green)" : "var(--red)";
}
massSlider.addEventListener("input", () => {
state.mass = Number(massSlider.value);
massOutput.textContent = `${state.mass.toLocaleString()} kg`;
updateRangeFill(massSlider);
});
volumeSlider.addEventListener("input", () => {
state.volume = Number(volumeSlider.value);
volumeOutput.textContent = `${state.volume.toFixed(2)} m³`;
updateRangeFill(volumeSlider);
updateObjectScale();
const minimumY = TANK.bottom + state.halfHeight + 0.025;
state.y = Math.max(state.y, minimumY);
objectPivot.position.y = state.y;
});
document.getElementById("dropButton").addEventListener("click", () => {
state.y = TANK.surface + state.halfHeight + 0.45;
state.velocityY = 0;
objectPivot.position.y = state.y;
state.rippleAge = 0;
});
document.getElementById("viewButton").addEventListener("click", () => {
cameraYaw = 0.72;
cameraPitch = 0.34;
cameraDistance = 12.4;
updateCamera();
});
function updateWaterSurface(time, frame) {
const positions = surfaceGeometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const z = positions.getZ(i);
const wave =
Math.sin(x * 1.65 + time * 0.85) * 0.018 +
Math.cos(z * 2.1 - time * 0.65) * 0.012;
positions.setY(i, wave);
}
positions.needsUpdate = true;
if (frame % 5 === 0) {
surfaceGeometry.computeVertexNormals();
}
}
function updateBubbles(time) {
const positions = bubbleGeometry.attributes.position;
const depth = TANK.depth - 0.45;
for (let i = 0; i < bubbleCount; i++) {
const meta = bubbleMetadata[i];
const y =
TANK.bottom +
0.2 +
((meta.offset + time * meta.speed) % depth);
positions.setXYZ(i, meta.x, y, meta.z);
}
positions.needsUpdate = true;
}
function updateRipple(delta) {
state.rippleAge += delta;
if (state.rippleAge < 1.5) {
ripple.visible = true;
ripple.scale.setScalar(1 + state.rippleAge * 2.4);
ripple.material.opacity = Math.max(
0,
0.65 * (1 - state.rippleAge / 1.5)
);
} else {
ripple.visible = false;
}
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
// Initialize.
updateObjectScale();
state.y = getFloatingTarget(getDensityRatio(), 0);
objectPivot.position.y = state.y;
massOutput.textContent = `${state.mass.toLocaleString()} kg`;
volumeOutput.textContent = `${state.volume.toFixed(2)} m³`;
updateRangeFill(massSlider);
updateRangeFill(volumeSlider);
const clock = new THREE.Clock();
let frameIndex = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.033);
const time = clock.elapsedTime;
frameIndex++;
updatePhysics(delta, time);
updateWaterSurface(time, frameIndex);
updateBubbles(time);
updateRipple(delta);
updateForceArrows();
updateReadouts();
renderer.render(scene, camera);
}
animate();
loading.classList.add("hidden");
</script>
</body>
</html>
```