Prompt specification
Build or customize the Three.js 3D scene "Same volume different mass comparison" 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: Same volume different mass comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/same-volume-different-mass-comparison-x1442
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this stylized scene: Arrange equal-volume blocks with different assigned masses in identical tanks; show floating, neutral, or sinking behavior under a simplified rigid-body model.
Working Three.js Starter (same-volume-different-mass-comparison-x1442):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hydrostatic Lab — Equal Volume, Different Mass</title>
<style>
:root {
color-scheme: dark;
--bg: #071116;
--panel: rgba(10, 24, 31, 0.78);
--panel-strong: rgba(9, 21, 27, 0.92);
--line: rgba(158, 214, 226, 0.18);
--text: #e8f5f6;
--muted: #8da9b1;
--cyan: #54dce5;
--green: #52e3b7;
--yellow: #ffd166;
--red: #ff6b7a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
touch-action: none;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
#app::after {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
z-index: 1;
background:
radial-gradient(circle at 50% 45%, transparent 42%, rgba(2, 7, 10, 0.28) 100%),
linear-gradient(180deg, rgba(3, 10, 14, 0.16), transparent 24%, transparent 72%, rgba(3, 9, 12, 0.3));
}
.hud {
position: absolute;
z-index: 3;
pointer-events: none;
}
.brand {
top: 26px;
left: 30px;
max-width: 390px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 1px;
background: var(--cyan);
box-shadow: 0 0 10px rgba(84, 220, 229, 0.8);
}
h1 {
margin: 0;
font-size: clamp(27px, 3vw, 48px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 720;
}
h1 span {
color: var(--muted);
font-weight: 430;
}
.brand p {
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.model-card {
left: 30px;
bottom: 30px;
width: 265px;
padding: 17px 18px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(16px);
box-shadow: 0 18px 45px rgba(0, 0, 0, 0.2);
}
.card-heading {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 13px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.model-card dl {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px 16px;
margin: 0;
font-size: 11px;
}
.model-card dt {
color: var(--muted);
}
.model-card dd {
margin: 0;
color: var(--text);
font-variant-numeric: tabular-nums;
font-weight: 650;
}
.formula {
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: #b8d0d5;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.55;
}
.spec-panel {
top: 26px;
right: 26px;
width: 300px;
padding: 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel-strong);
backdrop-filter: blur(18px);
box-shadow: 0 20px 55px rgba(0, 0, 0, 0.26);
pointer-events: auto;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 13px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.panel-title span:last-child {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
}
.spec-card {
--accent: var(--green);
padding: 13px 13px 11px;
margin-top: 9px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.spec-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.spec-id {
display: flex;
align-items: center;
gap: 8px;
color: #c5d7db;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
}
.spec-id::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 10px var(--accent);
}
.status-badge {
padding: 4px 7px;
border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
border-radius: 999px;
color: var(--accent);
background: color-mix(in srgb, var(--accent) 9%, transparent);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.12em;
}
.mass-row {
display: flex;
align-items: baseline;
justify-content: space-between;
margin: 10px 0 7px;
}
.mass-value {
font-size: 22px;
font-weight: 720;
letter-spacing: -0.035em;
font-variant-numeric: tabular-nums;
}
.mass-value small {
color: var(--muted);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
}
.ratio {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 5px 0 10px;
appearance: none;
border-radius: 99px;
outline: none;
background: rgba(255, 255, 255, 0.1);
accent-color: var(--accent);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 13px;
height: 13px;
appearance: none;
border: 2px solid #dff7f7;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 12px color-mix(in srgb, var(--accent) 55%, transparent);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #dff7f7;
border-radius: 50%;
background: var(--accent);
}
.live-data {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.live-data b {
color: #c7dade;
font-weight: 650;
}
.controls {
left: 50%;
bottom: 27px;
display: flex;
align-items: center;
gap: 7px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel-strong);
backdrop-filter: blur(18px);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.3);
transform: translateX(-50%);
pointer-events: auto;
}
.controls button {
min-height: 34px;
padding: 0 12px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 8px;
background: rgba(255, 255, 255, 0.045);
color: #c8dade;
font: inherit;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
cursor: pointer;
transition: 160ms ease;
}
.controls button:hover {
border-color: rgba(84, 220, 229, 0.4);
color: white;
background: rgba(84, 220, 229, 0.08);
}
.controls button.active {
border-color: rgba(84, 220, 229, 0.42);
color: var(--cyan);
background: rgba(84, 220, 229, 0.1);
}
.speed-control {
display: flex;
align-items: center;
gap: 8px;
padding: 0 9px 0 7px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
}
.speed-control input {
width: 74px;
margin: 0;
accent-color: var(--cyan);
}
.run-state {
display: flex;
align-items: center;
gap: 7px;
padding: 0 9px 0 5px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
}
.run-state::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 9px var(--green);
}
.run-state.paused::before {
background: var(--yellow);
box-shadow: 0 0 9px var(--yellow);
}
.force-legend {
right: 29px;
bottom: 31px;
display: flex;
gap: 13px;
color: var(--muted);
font-size: 9px;
pointer-events: none;
}
.force-legend span {
display: flex;
align-items: center;
gap: 6px;
}
.force-legend i {
width: 15px;
height: 2px;
display: inline-block;
background: currentColor;
}
.force-legend .buoyancy {
color: var(--cyan);
}
.force-legend .weight {
color: var(--red);
}
.hint {
left: 50%;
top: 26px;
padding: 7px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 20, 26, 0.5);
color: var(--muted);
font-size: 9px;
letter-spacing: 0.05em;
transform: translateX(-50%);
backdrop-filter: blur(10px);
}
@media (max-width: 1050px) {
.spec-panel {
width: 270px;
}
.model-card {
display: none;
}
.force-legend {
display: none;
}
}
@media (max-width: 760px) {
.brand {
top: 18px;
left: 18px;
}
.brand p,
.hint,
.spec-panel {
display: none;
}
h1 {
font-size: 29px;
}
.controls {
bottom: 15px;
width: calc(100% - 24px);
justify-content: center;
flex-wrap: wrap;
}
.controls button {
padding: 0 9px;
}
.speed-control {
display: none;
}
.run-state {
display: none;
}
}
</style>
</head>
<body>
<main id="app">
<header class="hud brand">
<div class="eyebrow">Hydrostatic Lab / Test 03</div>
<h1>Equal volume.<br /><span>Different mass.</span></h1>
<p>
Three identical tanks, one fluid, and blocks with the same displacement.
Adjust each assigned mass to change its buoyancy outcome.
</p>
</header>
<aside class="hud spec-panel">
<div class="panel-title">
<span>Assigned Mass</span>
<span>Volume locked · 0.125 m³</span>
</div>
<div id="specimens"></div>
</aside>
<aside class="hud model-card">
<div class="card-heading">
<span>Simplified Model</span>
<span>1-DOF / Vertical</span>
</div>
<dl>
<dt>Fluid density</dt><dd>1,000 kg/m³</dd>
<dt>Block volume</dt><dd>0.125 m³</dd>
<dt>Neutral mass</dt><dd>125 kg</dd>
<dt>Gravity</dt><dd>9.81 m/s²</dd>
</dl>
<div class="formula">
F<sub>B</sub> = ρgV f<sub>sub</sub><br />
F<sub>D</sub> = −(c₁v + c₂v|v|)<br />
mÿ = F<sub>B</sub> − mg + F<sub>D</sub>
</div>
</aside>
<div class="hud hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<nav class="hud controls" aria-label="Simulation controls">
<button id="resetDrop" type="button">RESET DROP</button>
<button id="pause" type="button">PAUSE</button>
<button id="forces" class="active" type="button" aria-pressed="true">FORCES</button>
<button id="resetView" type="button">RESET VIEW</button>
<label class="speed-control">
SPEED
<input id="speed" type="range" min="0.25" max="2" step="0.25" value="1" />
</label>
<div id="runState" class="run-state">RUNNING</div>
</nav>
<div class="hud force-legend">
<span class="buoyancy"><i></i> Buoyancy</span>
<span class="weight"><i></i> Weight</span>
</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.getElementById("app");
const specimensContainer = document.getElementById("specimens");
// -------------------------------------------------------------------------
// Physical model
// -------------------------------------------------------------------------
const FLUID_DENSITY = 1000; // kg/m^3
const BLOCK_VOLUME = 0.125; // m^3
const BLOCK_SIDE = 0.5; // m
const TANK_DEPTH = 1.0; // m
const GRAVITY = 9.81;
const DISPLACED_MASS = FLUID_DENSITY * BLOCK_VOLUME; // 125 kg
const INITIAL_Y = 1.12;
const WORLD_SCALE = 2.9;
const FLOOR_Y = 0.62;
const WATER_DEPTH_WORLD = TANK_DEPTH * WORLD_SCALE;
const WATER_LINE_Y = FLOOR_Y + WATER_DEPTH_WORLD;
const BLOCK_WORLD_SIZE = BLOCK_SIDE * WORLD_SCALE;
const STATUS_COLORS = {
float: 0x52e3b7,
neutral: 0xffd166,
sink: 0xff6b7a
};
const specimens = [
{ index: 0, x: -5.2, mass: 70, y: INITIAL_Y, velocity: 0 },
{ index: 1, x: 0, mass: 125, y: INITIAL_Y, velocity: 0 },
{ index: 2, x: 5.2, mass: 180, y: INITIAL_Y, velocity: 0 }
];
function getBehavior(mass) {
const ratio = mass / DISPLACED_MASS;
if (Math.abs(ratio - 1) < 0.0001) return "neutral";
return ratio < 1 ? "float" : "sink";
}
function getStatusColor(mass) {
return STATUS_COLORS[getBehavior(mass)];
}
function submergedFraction(y) {
const bottom = y - BLOCK_SIDE / 2;
const top = y + BLOCK_SIDE / 2;
const overlap = Math.max(
0,
Math.min(top, TANK_DEPTH) - Math.max(bottom, 0)
);
return overlap / BLOCK_SIDE;
}
function stepSpecimen(specimen, dt) {
const submerged = submergedFraction(specimen.y);
const buoyancyForce =
FLUID_DENSITY * GRAVITY * BLOCK_VOLUME * submerged;
const weightForce = specimen.mass * GRAVITY;
const dragInfluence = Math.min(1, submerged / 0.2);
const dragForce =
submerged > 0
? -(70 * specimen.velocity +
260 * specimen.velocity * Math.abs(specimen.velocity)) *
dragInfluence
: 0;
const acceleration =
(buoyancyForce - weightForce + dragForce) / specimen.mass;
specimen.velocity += acceleration * dt;
specimen.y += specimen.velocity * dt;
// Rigid floor contact. The block itself never deforms.
const minimumCenterY = BLOCK_SIDE / 2;
if (specimen.y < minimumCenterY) {
specimen.y = minimumCenterY;
if (specimen.velocity < 0) specimen.velocity = 0;
}
specimen.submerged = submerged;
specimen.buoyancyForce = buoyancyForce;
specimen.weightForce = weightForce;
}
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071116);
scene.fog = new THREE.FogExp2(0x071116, 0.021);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
app.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraTarget = new THREE.Vector3(0, 2.45, 0);
const defaultCamera = {
radius: 20.5,
yaw: 0.34,
pitch: 0.31
};
const cameraState = { ...defaultCamera };
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const portraitFit = Math.max(1, Math.min(2.35, 1.25 / aspect));
const radius = cameraState.radius * portraitFit;
camera.position.set(
cameraTarget.x + radius * Math.sin(cameraState.yaw) * Math.cos(cameraState.pitch),
cameraTarget.y + radius * Math.sin(cameraState.pitch),
cameraTarget.z + radius * Math.cos(cameraState.yaw) * Math.cos(cameraState.pitch)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0xbcefff, 0x102026, 1.65));
const keyLight = new THREE.DirectionalLight(0xe8fbff, 3.1);
keyLight.position.set(-5, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x45cddd, 2.1);
rimLight.position.set(8, 6, -8);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb86b, 1.2, 16);
warmLight.position.set(0, 3, 5);
scene.add(warmLight);
// Ground
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x09171c,
roughness: 0.86,
metalness: 0.12
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.01;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(42, 42, 0x23434a, 0x132a31);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x10252c,
roughness: 0.52,
metalness: 0.45
});
const platform = new THREE.Mesh(
new THREE.BoxGeometry(16.3, 0.22, 5.1),
platformMaterial
);
platform.position.set(0, 0.11, 0);
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
// -------------------------------------------------------------------------
// Canvas texture helpers
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createCanvasTexture(width, height) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return { canvas, ctx, texture };
}
function colorToCss(color) {
return `#${color.toString(16).padStart(6, "0")}`;
}
function drawTankLabel(label, specimen) {
const { ctx, canvas, texture } = label;
const accent = colorToCss(getStatusColor(specimen.mass));
const behavior = getBehavior(specimen.mass);
const ratio = specimen.mass / DISPLACED_MASS;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, canvas.width - 16, canvas.height - 16, 30);
ctx.fillStyle = "rgba(7, 19, 25, 0.90)";
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = accent;
ctx.globalAlpha = 0.65;
ctx.stroke();
ctx.globalAlpha = 1;
ctx.fillStyle = "#8ca8b0";
ctx.font = "800 30px system-ui";
ctx.letterSpacing = "3px";
ctx.fillText(`SPECIMEN 0${specimen.index + 1}`, 42, 61);
ctx.fillStyle = accent;
ctx.font = "800 60px system-ui";
ctx.fillText(behavior.toUpperCase(), 39, 132);
ctx.textAlign = "right";
ctx.fillStyle = "#edf9fa";
ctx.font = "750 55px system-ui";
ctx.fillText(`${specimen.mass} kg`, canvas.width - 42, 91);
ctx.fillStyle = "#89a4ac";
ctx.font = "500 27px ui-monospace, monospace";
ctx.fillText(`ρ / ρfluid ${ratio.toFixed(2)}`, canvas.width - 42, 137);
ctx.textAlign = "left";
texture.needsUpdate = true;
}
function drawMassBadge(label, specimen) {
const { ctx, canvas, texture } = label;
const accent = colorToCss(getStatusColor(specimen.mass));
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 5, 5, canvas.width - 10, canvas.height - 10, 26);
ctx.fillStyle = "rgba(5, 14, 18, 0.88)";
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f2fbfb";
ctx.font = "750 65px system-ui";
ctx.fillText(`${specimen.mass} kg`, canvas.width / 2, canvas.height / 2 + 3);
texture.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Tank construction
// -------------------------------------------------------------------------
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb8edf1,
roughness: 0.08,
metalness: 0,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x29434b,
roughness: 0.35,
metalness: 0.72
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x101f25,
roughness: 0.6,
metalness: 0.38
});
const waterVolumeMaterial = new THREE.MeshStandardMaterial({
color: 0x1ba9bd,
roughness: 0.22,
metalness: 0.02,
transparent: true,
opacity: 0.105,
depthWrite: false,
side: THREE.DoubleSide
});
const waterVertexShader = `
uniform float uTime;
varying vec2 vUv;
varying float vWave;
void main() {
vUv = uv;
vec3 p = position;
float wave =
sin(p.x * 2.7 + uTime * 1.45) * 0.028 +
cos(p.y * 3.2 - uTime * 1.1) * 0.021;
p.z += wave;
vWave = wave;
gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0);
}
`;
const waterFragmentShader = `
uniform float uTime;
uniform vec3 uColor;
varying vec2 vUv;
varying float vWave;
void main() {
float ripple = sin((vUv.x + vUv.y) * 22.0 + uTime * 0.65) * 0.5 + 0.5;
float edge = smoothstep(0.0, 0.12, vUv.x) *
smoothstep(0.0, 0.12, 1.0 - vUv.x) *
smoothstep(0.0, 0.12, vUv.y) *
smoothstep(0.0, 0.12, 1.0 - vUv.y);
vec3 color = uColor + ripple * 0.025 + vWave * 0.18;
gl_FragColor = vec4(color, 0.23 + ripple * 0.035 + edge * 0.025);
}
`;
const tankData = [];
function buildTank(specimen) {
const group = new THREE.Group();
group.position.x = specimen.x;
scene.add(group);
// Tank base
const base = new THREE.Mesh(
new THREE.BoxGeometry(3.82, 0.36, 3.82),
darkMaterial
);
base.position.y = 0.42;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
// Feet
const footGeometry = new THREE.BoxGeometry(0.36, 0.3, 0.36);
for (const x of [-1.55, 1.55]) {
for (const z of [-1.55, 1.55]) {
const foot = new THREE.Mesh(footGeometry, metalMaterial);
foot.position.set(x, 0.15, z);
foot.castShadow = true;
group.add(foot);
}
}
// Glass walls
const wallHeight = 4.55;
const wallY = 2.88;
const sideGeometry = new THREE.BoxGeometry(0.11, wallHeight, 3.66);
const frontGeometry = new THREE.BoxGeometry(3.62, wallHeight, 0.11);
for (const x of [-1.81, 1.81]) {
const wall = new THREE.Mesh(sideGeometry, glassMaterial);
wall.position.set(x, wallY, 0);
wall.renderOrder = 3;
group.add(wall);
}
for (const z of [-1.81, 1.81]) {
const wall = new THREE.Mesh(frontGeometry, glassMaterial);
wall.position.set(0, wallY, z);
wall.renderOrder = 3;
group.add(wall);
}
// Structural outline
const tankOutline = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(3.72, wallHeight, 3.72)
),
new THREE.LineBasicMaterial({
color: 0x91c9d1,
transparent: true,
opacity: 0.31
})
);
tankOutline.position.y = wallY;
tankOutline.renderOrder = 4;
group.add(tankOutline);
// Top rim
const rimLong = new THREE.BoxGeometry(3.8, 0.08, 0.1);
const rimShort = new THREE.BoxGeometry(0.1, 0.08, 3.8);
for (const z of [-1.83, 1.83]) {
const rim = new THREE.Mesh(rimLong, metalMaterial);
rim.position.set(0, 5.14, z);
group.add(rim);
}
for (const x of [-1.83, 1.83]) {
const rim = new THREE.Mesh(rimShort, metalMaterial);
rim.position.set(x, 5.14, 0);
group.add(rim);
}
// Water body
const waterBody = new THREE.Mesh(
new THREE.BoxGeometry(3.36, WATER_DEPTH_WORLD, 3.34),
waterVolumeMaterial
);
waterBody.position.y = FLOOR_Y + WATER_DEPTH_WORLD / 2;
waterBody.renderOrder = 1;
group.add(waterBody);
// Animated surface
const waterMaterial = new THREE.ShaderMaterial({
uniforms: {
uTime: { value: 0 },
uColor: { value: new THREE.Color(0x26b8c6) }
},
vertexShader: waterVertexShader,
fragmentShader: waterFragmentShader,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const waterSurface = new THREE.Mesh(
new THREE.PlaneGeometry(3.36, 3.34, 26, 26),
waterMaterial
);
waterSurface.rotation.x = -Math.PI / 2;
waterSurface.position.y = WATER_LINE_Y;
waterSurface.renderOrder = 2;
group.add(waterSurface);
// Small bubbles
const bubbles = [];
const bubbleGeometry = new THREE.SphereGeometry(1, 8, 8);
const bubbleMaterial = new THREE.MeshBasicMaterial({
color: 0xbdf8ff,
transparent: true,
opacity: 0.28,
depthWrite: false
});
for (let i = 0; i < 13; i++) {
const seed = specimen.index * 17.7 + i * 8.13;
const randomA = Math.abs(Math.sin(seed * 1.71));
const randomB = Math.abs(Math.sin(seed * 2.37));
const randomC = Math.abs(Math.sin(seed * 3.19));
const size = 0.024 + randomC * 0.036;
const bubble = new THREE.Mesh(bubbleGeometry, bubbleMaterial);
bubble.scale.setScalar(size);
bubble.position.set(
(randomA - 0.5) * 2.7,
FLOOR_Y + randomB * WATER_DEPTH_WORLD,
(randomC - 0.5) * 2.65
);
bubble.userData.phase = randomB;
bubble.userData.speed = 0.055 + randomA * 0.055;
bubbles.push(bubble);
group.add(bubble);
}
// Floating label above tank
const label = createCanvasTexture(1024, 190);
const labelMaterial = new THREE.SpriteMaterial({
map: label.texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const labelSprite = new THREE.Sprite(labelMaterial);
labelSprite.position.set(0, 5.85, 0);
labelSprite.scale.set(3.45, 0.64, 1);
labelSprite.renderOrder = 20;
group.add(labelSprite);
drawTankLabel(label, specimen);
tankData.push({
group,
waterMaterial,
bubbles,
label,
labelSprite
});
}
specimens.forEach(buildTank);
// -------------------------------------------------------------------------
// Blocks and force arrows
// -------------------------------------------------------------------------
const blockGeometry = new THREE.BoxGeometry(
BLOCK_WORLD_SIZE,
BLOCK_WORLD_SIZE,
BLOCK_WORLD_SIZE
);
const edgeGeometry = new THREE.EdgesGeometry(blockGeometry);
specimens.forEach((specimen) => {
const behavior = getBehavior(specimen.mass);
const color = getStatusColor(specimen.mass);
const group = new THREE.Group();
group.position.x = specimen.x;
scene.add(group);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.075,
roughness: 0.3,
metalness: 0.08
});
const block = new THREE.Mesh(blockGeometry, material);
block.castShadow = true;
block.receiveShadow = true;
group.add(block);
const edges = new THREE.LineSegments(
edgeGeometry,
new THREE.LineBasicMaterial({
color: 0xf2ffff,
transparent: true,
opacity: 0.48
})
);
group.add(edges);
const badge = createCanvasTexture(512, 180);
const badgeMesh = new THREE.Mesh(
new THREE.PlaneGeometry(BLOCK_WORLD_SIZE * 0.79, BLOCK_WORLD_SIZE * 0.31),
new THREE.MeshBasicMaterial({
map: badge.texture,
transparent: true,
depthWrite: false,
toneMapped: false
})
);
badgeMesh.position.z = BLOCK_WORLD_SIZE / 2 + 0.009;
badgeMesh.renderOrder = 6;
group.add(badgeMesh);
drawMassBadge(badge, specimen);
const buoyancyArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(),
1,
0x54dce5,
0.18,
0.1
);
const weightArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(),
1,
0xff6b7a,
0.18,
0.1
);
for (const arrow of [buoyancyArrow, weightArrow]) {
arrow.line.material.depthTest = false;
arrow.cone.material.depthTest = false;
arrow.renderOrder = 15;
scene.add(arrow);
}
specimen.blockGroup = group;
specimen.blockMaterial = material;
specimen.badge = badge;
specimen.badgeMesh = badgeMesh;
specimen.buoyancyArrow = buoyancyArrow;
specimen.weightArrow = weightArrow;
});
// -------------------------------------------------------------------------
// UI cards
// -------------------------------------------------------------------------
const cardRefs = [];
specimens.forEach((specimen) => {
const card = document.createElement("section");
card.className = "spec-card";
card.innerHTML = `
<div class="spec-top">
<div class="spec-id">SPECIMEN 0${specimen.index + 1}</div>
<div class="status-badge"></div>
</div>
<div class="mass-row">
<div class="mass-value"><span></span> <small>kg</small></div>
<div class="ratio"></div>
</div>
<input
type="range"
min="40"
max="220"
step="1"
value="${specimen.mass}"
aria-label="Assigned mass for specimen ${specimen.index + 1}, in kilograms"
/>
<div class="live-data">
<span>Submerged <b class="submerged-value">0%</b></span>
<span>F<sub>B</sub> <b class="force-value">0 N</b></span>
<span class="motion-value">SETTLING</span>
</div>
`;
specimensContainer.appendChild(card);
const refs = {
card,
status: card.querySelector(".status-badge"),
mass: card.querySelector(".mass-value span"),
ratio: card.querySelector(".ratio"),
slider: card.querySelector("input"),
submerged: card.querySelector(".submerged-value"),
force: card.querySelector(".force-value"),
motion: card.querySelector(".motion-value")
};
cardRefs.push(refs);
refs.slider.addEventListener("input", () => {
specimen.mass = Number(refs.slider.value);
updateAppearance(specimen);
updateCardStatic(refs, specimen);
});
});
function updateCardStatic(refs, specimen) {
const behavior = getBehavior(specimen.mass);
const color = colorToCss(getStatusColor(specimen.mass));
refs.card.style.setProperty("--accent", color);
refs.status.textContent = behavior.toUpperCase();
refs.mass.textContent = specimen.mass;
refs.ratio.textContent = `ρ/ρfluid ${(specimen.mass / DISPLACED_MASS).toFixed(2)}`;
refs.slider.setAttribute(
"aria-valuetext",
`${specimen.mass} kilograms, ${behavior}`
);
}
function updateAppearance(specimen) {
const color = getStatusColor(specimen.mass);
specimen.blockMaterial.color.setHex(color);
specimen.blockMaterial.emissive.setHex(color);
specimen.blockMaterial.emissiveIntensity = 0.075;
drawTankLabel(tankData[specimen.index].label, specimen);
drawMassBadge(specimen.badge, specimen);
}
specimens.forEach((specimen, i) => {
updateAppearance(specimen);
updateCardStatic(cardRefs[i], specimen);
});
// -------------------------------------------------------------------------
// Simulation controls
// -------------------------------------------------------------------------
let paused = false;
let simulationSpeed = 1;
let showForces = true;
const pauseButton = document.getElementById("pause");
const forcesButton = document.getElementById("forces");
const runState = document.getElementById("runState");
const speedInput = document.getElementById("speed");
function resetDrop() {
specimens.forEach((specimen) => {
specimen.y = INITIAL_Y;
specimen.velocity = 0;
specimen.submerged = submergedFraction(specimen.y);
specimen.buoyancyForce =
FLUID_DENSITY * GRAVITY * BLOCK_VOLUME * specimen.submerged;
specimen.weightForce = specimen.mass * GRAVITY;
});
}
function setPaused(value) {
paused = value;
pauseButton.textContent = paused ? "RESUME" : "PAUSE";
pauseButton.classList.toggle("active", paused);
runState.textContent = paused ? "PAUSED" : "RUNNING";
runState.classList.toggle("paused", paused);
}
document.getElementById("resetDrop").addEventListener("click", () => {
resetDrop();
setPaused(false);
});
pauseButton.addEventListener("click", () => setPaused(!paused));
forcesButton.addEventListener("click", () => {
showForces = !showForces;
forcesButton.classList.toggle("active", showForces);
forcesButton.setAttribute("aria-pressed", String(showForces));
});
speedInput.addEventListener("input", () => {
simulationSpeed = Number(speedInput.value);
});
document.getElementById("resetView").addEventListener("click", () => {
cameraState.radius = defaultCamera.radius;
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
updateCamera();
});
// -------------------------------------------------------------------------
// Direct camera controls: no external controls addon
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.004,
0.11,
0.82
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
canvas.style.cursor = "grab";
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("pointerleave", (event) => {
if (dragging) endDrag(event);
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
14,
29
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", () => {
cameraState.radius = defaultCamera.radius;
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
updateCamera();
});
canvas.style.cursor = "grab";
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const FIXED_STEP = 1 / 120;
let accumulator = 0;
let previousTime = performance.now();
let visualTime = 0;
let lastTelemetryTime = 0;
function updateVisuals() {
tankData.forEach((tank) => {
tank.waterMaterial.uniforms.uTime.value = visualTime;
tank.bubbles.forEach((bubble) => {
const progress =
(bubble.userData.phase + visualTime * bubble.userData.speed) % 1;
bubble.position.y =
FLOOR_Y + 0.1 + progress * (WATER_DEPTH_WORLD - 0.24);
});
});
specimens.forEach((specimen) => {
const worldY = FLOOR_Y + specimen.y * WORLD_SCALE;
specimen.blockGroup.position.y = worldY;
const submerged = submergedFraction(specimen.y);
const buoyancyForce =
FLUID_DENSITY * GRAVITY * BLOCK_VOLUME * submerged;
const weightForce = specimen.mass * GRAVITY;
specimen.buoyancyArrow.visible = showForces;
specimen.weightArrow.visible = showForces;
if (showForces) {
const buoyancyLength = 0.45 + submerged * 1.25;
const weightLength = 0.45 + (specimen.mass / 220) * 1.45;
specimen.buoyancyArrow.position.set(
specimen.x + 0.42,
worldY - 0.38,
1.02
);
specimen.buoyancyArrow.setLength(
buoyancyLength,
0.18,
0.1
);
specimen.weightArrow.position.set(
specimen.x - 0.42,
worldY + 0.38,
1.02
);
specimen.weightArrow.setLength(
weightLength,
0.18,
0.1
);
}
});
}
function updateTelemetry(now) {
if (now - lastTelemetryTime < 90) return;
lastTelemetryTime = now;
specimens.forEach((specimen, i) => {
const refs = cardRefs[i];
const submerged = submergedFraction(specimen.y);
const buoyancyForce =
FLUID_DENSITY * GRAVITY * BLOCK_VOLUME * submerged;
refs.submerged.textContent = `${Math.round(submerged * 100)}%`;
refs.force.textContent = `${Math.round(buoyancyForce)} N`;
const tolerance = 0.018;
if (Math.abs(specimen.velocity) < tolerance) {
refs.motion.textContent =
specimen.mass > DISPLACED_MASS &&
specimen.y <= BLOCK_SIDE / 2 + 0.002
? "ON FLOOR"
: "SUSPENDED";
} else {
refs.motion.textContent =
specimen.velocity > 0 ? "RISING" : "DESCENDING";
}
});
}
function animate(now) {
const frameDelta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
visualTime += frameDelta;
if (!paused) {
accumulator += frameDelta * simulationSpeed;
let safetyCounter = 0;
while (accumulator >= FIXED_STEP && safetyCounter < 20) {
specimens.forEach((specimen) =>
stepSpecimen(specimen, FIXED_STEP)
);
accumulator -= FIXED_STEP;
safetyCounter++;
}
}
updateVisuals();
updateTelemetry(now);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
updateCamera();
});
resetDrop();
updateVisuals();
requestAnimationFrame(animate);
</script>
</body>
</html>
```