Prompt specification
Build or customize the Three.js 3D scene "Floating markers between density layers" 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: Floating markers between density layers
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/floating-markers-between-density-layers-x1462
Scene Description Prompt:
Floating markers between density layers. Place several solid markers with assigned densities in a layered tank; show their equilibrium locations under simplified assumptions and keep density separate from object color. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (floating-markers-between-density-layers-x1462):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Density Equilibrium — Layered Tank</title>
<style>
:root {
color-scheme: dark;
--bg: #06111d;
--panel: rgba(8, 20, 34, 0.94);
--border: rgba(167, 211, 235, 0.15);
--muted: #8ea8bb;
--text: #eef7ff;
--accent: #62e6dc;
--accent-soft: rgba(98, 230, 220, 0.12);
}
* {
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;
}
button {
cursor: pointer;
}
#app {
display: grid;
grid-template-columns: 350px minmax(0, 1fr);
width: 100%;
height: 100%;
}
#panel {
position: relative;
z-index: 10;
overflow-y: auto;
padding: 25px 22px 30px;
background:
linear-gradient(180deg, rgba(15, 35, 54, 0.98), rgba(7, 18, 31, 0.98));
border-right: 1px solid var(--border);
box-shadow: 18px 0 60px rgba(0, 0, 0, 0.25);
scrollbar-width: thin;
scrollbar-color: rgba(130, 180, 210, 0.3) transparent;
}
.panel-kicker {
margin: 0 0 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 26px;
line-height: 1.1;
letter-spacing: -0.035em;
}
.intro {
margin: 12px 0 22px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.section {
padding: 19px 0;
border-top: 1px solid var(--border);
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.section-heading h2 {
margin: 0;
font-size: 13px;
letter-spacing: 0.02em;
}
.section-note {
color: #718da2;
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.layer-control {
display: grid;
grid-template-columns: 86px 1fr 76px;
align-items: center;
gap: 9px;
margin: 9px 0;
}
.layer-name {
display: flex;
align-items: center;
gap: 8px;
color: #c8d8e5;
font-size: 12px;
white-space: nowrap;
}
.layer-swatch {
width: 9px;
height: 26px;
border-radius: 5px;
box-shadow: 0 0 12px currentColor;
}
.layer-swatch.l1 {
color: #58deeb;
background: #58deeb;
}
.layer-swatch.l2 {
color: #398dde;
background: #398dde;
}
.layer-swatch.l3 {
color: #4d55bd;
background: #4d55bd;
}
input[type="number"] {
width: 100%;
height: 34px;
padding: 0 8px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
border: 1px solid rgba(161, 200, 224, 0.16);
border-radius: 8px;
outline: none;
}
input[type="number"]:focus {
border-color: rgba(98, 230, 220, 0.65);
box-shadow: 0 0 0 3px rgba(98, 230, 220, 0.08);
}
.density-number {
position: relative;
}
.density-number span {
position: absolute;
top: 50%;
right: 7px;
transform: translateY(-50%);
color: #688397;
font-size: 9px;
pointer-events: none;
}
.density-number input {
padding-right: 30px;
text-align: right;
}
.marker-control {
margin: 10px 0;
padding: 12px;
background: rgba(255, 255, 255, 0.025);
border: 1px solid rgba(167, 211, 235, 0.09);
border-radius: 12px;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.marker-control.selected {
background: rgba(98, 230, 220, 0.065);
border-color: rgba(98, 230, 220, 0.45);
transform: translateX(2px);
}
.marker-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 10px;
}
.identity {
display: flex;
align-items: center;
min-width: 0;
gap: 8px;
font-size: 12px;
}
.identity-glyph {
display: grid;
width: 25px;
height: 25px;
place-items: center;
color: var(--marker-color);
background: color-mix(in srgb, var(--marker-color) 13%, transparent);
border: 1px solid color-mix(in srgb, var(--marker-color) 45%, transparent);
border-radius: 7px;
font-size: 13px;
}
.status {
padding: 4px 7px;
color: #91aabd;
background: rgba(255, 255, 255, 0.045);
border-radius: 999px;
font-size: 9px;
font-weight: 700;
white-space: nowrap;
}
.status.surface {
color: #83eced;
background: rgba(82, 216, 235, 0.09);
}
.status.interface {
color: #b9d3ff;
background: rgba(91, 132, 235, 0.11);
}
.status.floor {
color: #ffbd8a;
background: rgba(255, 151, 83, 0.1);
}
.marker-density-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 92px;
align-items: center;
gap: 12px;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
accent-color: var(--marker-color);
cursor: pointer;
}
.assumption {
margin-top: 18px;
padding: 14px;
color: #9ab0c0;
background: rgba(255, 255, 255, 0.025);
border: 1px solid rgba(167, 211, 235, 0.09);
border-radius: 12px;
font-size: 11px;
line-height: 1.55;
}
.assumption strong {
color: #d9e8f2;
}
.rule {
display: grid;
grid-template-columns: 22px 1fr;
gap: 8px;
margin-top: 9px;
}
.rule-number {
display: grid;
width: 20px;
height: 20px;
place-items: center;
color: var(--accent);
background: var(--accent-soft);
border-radius: 50%;
font-size: 10px;
font-weight: 800;
}
.color-note {
margin-top: 13px;
padding-left: 12px;
color: #7894a9;
border-left: 2px solid var(--accent);
font-size: 11px;
line-height: 1.5;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 18px;
}
.button {
min-height: 36px;
padding: 8px 10px;
color: #d9e8f2;
background: rgba(255, 255, 255, 0.045);
border: 1px solid rgba(167, 211, 235, 0.14);
border-radius: 9px;
font-size: 11px;
transition:
background 150ms ease,
border-color 150ms ease;
}
.button:hover {
background: rgba(255, 255, 255, 0.08);
border-color: rgba(167, 211, 235, 0.3);
}
.button.primary {
color: #06201e;
background: var(--accent);
border-color: var(--accent);
font-weight: 800;
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 13px;
color: #91aabd;
font-size: 11px;
}
.toggle-row input {
accent-color: var(--accent);
}
#viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 28%, rgba(31, 78, 111, 0.34), transparent 42%),
linear-gradient(180deg, #071827 0%, #06111d 100%);
}
#canvasHost,
#labels {
position: absolute;
inset: 0;
}
#canvasHost canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#canvasHost canvas:active {
cursor: grabbing;
}
#labels {
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.hud {
position: absolute;
z-index: 5;
top: 25px;
left: 27px;
max-width: 430px;
pointer-events: none;
}
.hud-kicker {
margin-bottom: 8px;
color: #62e6dc;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.hud h2 {
margin: 0;
font-size: clamp(24px, 3vw, 39px);
line-height: 1.03;
letter-spacing: -0.05em;
text-shadow: 0 3px 20px rgba(0, 0, 0, 0.35);
}
.hud p {
max-width: 360px;
margin: 11px 0 0;
color: #91aabd;
font-size: 12px;
line-height: 1.5;
}
.marker-label {
position: absolute;
min-width: 142px;
padding: 8px 10px;
background: rgba(7, 18, 31, 0.88);
border: 1px solid rgba(186, 218, 236, 0.15);
border-left: 3px solid var(--marker-color);
border-radius: 9px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
}
.marker-label-title {
display: flex;
align-items: center;
gap: 6px;
color: #d9e8f2;
font-size: 10px;
font-weight: 750;
}
.marker-label-dot {
width: 6px;
height: 6px;
background: var(--marker-color);
border-radius: 50%;
box-shadow: 0 0 8px var(--marker-color);
}
.marker-label-data {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 5px;
}
.marker-label-density {
color: white;
font-size: 12px;
font-weight: 800;
white-space: nowrap;
}
.marker-label-outcome {
color: #8faabd;
font-size: 8px;
white-space: nowrap;
}
.layer-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
color: #a9c0d0;
background: rgba(5, 15, 26, 0.8);
border: 1px solid rgba(167, 211, 235, 0.13);
border-radius: 7px;
transform: translate(-100%, -50%);
font-size: 9px;
white-space: nowrap;
backdrop-filter: blur(6px);
}
.layer-label strong {
color: white;
font-size: 10px;
}
.layer-label span {
opacity: 0.72;
}
.view-help {
position: absolute;
z-index: 5;
right: 23px;
bottom: 20px;
padding: 8px 11px;
color: #849eb2;
background: rgba(7, 18, 31, 0.72);
border: 1px solid rgba(167, 211, 235, 0.12);
border-radius: 9px;
font-size: 10px;
pointer-events: none;
}
.legend-card {
position: absolute;
z-index: 5;
left: 25px;
bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 9px 15px;
max-width: 430px;
padding: 10px 12px;
color: #8fa8bb;
background: rgba(7, 18, 31, 0.76);
border: 1px solid rgba(167, 211, 235, 0.12);
border-radius: 10px;
font-size: 9px;
pointer-events: none;
backdrop-filter: blur(8px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.identity-dot {
width: 9px;
height: 9px;
background: linear-gradient(135deg, #ff7b72, #ffd166 35%, #55d6be 60%, #ae8bff 80%, #63b3ff);
border-radius: 3px;
}
.density-gradient {
width: 36px;
height: 7px;
background: linear-gradient(90deg, #58deeb, #398dde, #4d55bd);
border-radius: 5px;
}
@media (max-width: 850px) {
#app {
grid-template-columns: 1fr;
grid-template-rows: minmax(0, 58%) minmax(0, 42%);
}
#viewport {
grid-row: 1;
}
#panel {
grid-row: 2;
padding: 18px;
border-top: 1px solid var(--border);
border-right: 0;
}
.hud {
top: 17px;
left: 18px;
}
.hud p {
display: none;
}
.legend-card {
left: 14px;
bottom: 13px;
}
.view-help {
right: 13px;
bottom: 13px;
}
}
</style>
</head>
<body>
<div id="app">
<aside id="panel">
<p class="panel-kicker">Stratified fluid lab</p>
<h1>Density equilibrium</h1>
<p class="intro">
Assign each solid marker a density and compare it with the three fluid
layers. The markers settle at the simplified equilibrium positions.
</p>
<section class="section">
<div class="section-heading">
<h2>Fluid layers</h2>
<span class="section-note">kg/m³</span>
</div>
<div id="layerControls"></div>
</section>
<section class="section">
<div class="section-heading">
<h2>Marker densities</h2>
<span class="section-note">kg/m³</span>
</div>
<div id="markerControls"></div>
</section>
<section class="section">
<div class="section-heading">
<h2>Simplified rule</h2>
</div>
<div class="assumption">
A marker moves downward through every layer less dense than itself.
<div class="rule">
<span class="rule-number">1</span>
<span>Find the first layer whose density is greater than or equal to the marker.</span>
</div>
<div class="rule">
<span class="rule-number">2</span>
<span>Rest at that layer’s upper boundary, or at the water surface for L1.</span>
</div>
<div class="rule">
<span class="rule-number">3</span>
<span>If denser than all layers, sink to the tank floor.</span>
</div>
</div>
<div class="color-note">
<strong>Marker colors identify objects only.</strong>
Density is communicated by the numeric values, not by object hue.
</div>
<div class="buttons">
<button class="button primary" id="resetButton" type="button">
Reset setup
</button>
<button class="button" id="motionButton" type="button">
Pause settling
</button>
</div>
<label class="toggle-row">
Show floating labels
<input id="labelsToggle" type="checkbox" checked />
</label>
</section>
</aside>
<main id="viewport">
<div id="canvasHost"></div>
<div id="labels"></div>
<div class="hud">
<div class="hud-kicker">Layered tank / live equilibrium</div>
<h2>Where does each marker settle?</h2>
<p>
Solid shapes move vertically through a density-stratified fluid.
Colored rings indicate the calculated equilibrium boundary.
</p>
</div>
<div class="legend-card">
<div class="legend-item">
<span class="identity-dot"></span>
<span>Marker color = identity</span>
</div>
<div class="legend-item">
<span class="density-gradient"></span>
<span>Fluid tint = relative layer density</span>
</div>
</div>
<div class="view-help">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene and renderer
// ------------------------------------------------------------
const viewport = document.getElementById("viewport");
const canvasHost = document.getElementById("canvasHost");
const labelsHost = document.getElementById("labels");
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x000000, 0);
canvasHost.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D visualization of markers settling in density layers"
);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const hemiLight = new THREE.HemisphereLight(0xc9efff, 0x142033, 1.9);
scene.add(hemiLight);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ccfff, 1.4);
rimLight.position.set(-7, 5, -6);
scene.add(rimLight);
const ambient = new THREE.AmbientLight(0x9dc9df, 0.28);
scene.add(ambient);
// ------------------------------------------------------------
// Tank dimensions and density layers
// ------------------------------------------------------------
const TANK = {
width: 8,
depth: 4.6,
height: 6.35,
bottom: 0.35,
innerBottom: 0.62,
layerHeight: 1.72,
surfaceY: 5.78,
topY: 6.7
};
const defaultLayerDensities = [900, 1150, 1350];
const layers = [
{ name: "L1 · Low", density: defaultLayerDensities[0], color: 0x58deeb },
{ name: "L2 · Middle", density: defaultLayerDensities[1], color: 0x398dde },
{ name: "L3 · Dense", density: defaultLayerDensities[2], color: 0x4d55bd }
];
// Geometry of the three layers, ordered from top to bottom.
layers.forEach((layer, index) => {
layer.bottomY = TANK.surfaceY - (index + 1) * TANK.layerHeight;
layer.topY = TANK.surfaceY - index * TANK.layerHeight;
layer.midY = (layer.bottomY + layer.topY) * 0.5;
});
const tankGroup = new THREE.Group();
scene.add(tankGroup);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(35, 35),
new THREE.MeshStandardMaterial({
color: 0x07111d,
roughness: 0.95,
metalness: 0,
transparent: true,
opacity: 0.68
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = 0.29;
ground.receiveShadow = true;
scene.add(ground);
const base = new THREE.Mesh(
new THREE.BoxGeometry(TANK.width + 0.25, 0.2, TANK.depth + 0.25),
new THREE.MeshStandardMaterial({
color: 0x13283a,
roughness: 0.44,
metalness: 0.25
})
);
base.position.y = TANK.bottom;
base.castShadow = true;
base.receiveShadow = true;
tankGroup.add(base);
function makeGlassPanel(width, height, depth, x, y, z, opacity) {
const panel = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
new THREE.MeshPhysicalMaterial({
color: 0xbceeff,
transparent: true,
opacity,
roughness: 0.16,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
panel.position.set(x, y, z);
panel.renderOrder = 5;
tankGroup.add(panel);
return panel;
}
const panelCenterY = (TANK.bottom + TANK.topY) * 0.5;
makeGlassPanel(
TANK.width,
TANK.height,
0.045,
0,
panelCenterY,
-TANK.depth * 0.5,
0.065
);
makeGlassPanel(
TANK.width,
TANK.height,
0.035,
0,
panelCenterY,
TANK.depth * 0.5,
0.025
);
makeGlassPanel(
0.045,
TANK.height,
TANK.depth,
-TANK.width * 0.5,
panelCenterY,
0,
0.04
);
makeGlassPanel(
0.045,
TANK.height,
TANK.depth,
TANK.width * 0.5,
panelCenterY,
0,
0.04
);
const tankEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(TANK.width, TANK.height, TANK.depth)
),
new THREE.LineBasicMaterial({
color: 0xa7d9ef,
transparent: true,
opacity: 0.3
})
);
tankEdges.position.y = panelCenterY;
tankGroup.add(tankEdges);
layers.forEach((layer, index) => {
const layerHeight = layer.topY - layer.bottomY - 0.025;
const layerMesh = new THREE.Mesh(
new THREE.BoxGeometry(
TANK.width - 0.22,
layerHeight,
TANK.depth - 0.22
),
new THREE.MeshPhysicalMaterial({
color: layer.color,
transparent: true,
opacity: 0.13,
roughness: 0.48,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false,
clearcoat: 0.2
})
);
layerMesh.position.set(0, layer.midY, 0);
layerMesh.renderOrder = 2 + index;
tankGroup.add(layerMesh);
// A subtle boundary plate at the bottom of every layer.
const boundaryPlate = new THREE.Mesh(
new THREE.PlaneGeometry(TANK.width - 0.24, TANK.depth - 0.24),
new THREE.MeshBasicMaterial({
color: layer.color,
transparent: true,
opacity: 0.055,
side: THREE.DoubleSide,
depthWrite: false
})
);
boundaryPlate.rotation.x = -Math.PI / 2;
boundaryPlate.position.y = layer.bottomY + 0.006;
boundaryPlate.renderOrder = 4;
tankGroup.add(boundaryPlate);
});
function addHorizontalLine(y, color, opacity = 0.6, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-TANK.width * 0.5 + 0.08, y, TANK.depth * 0.5 + 0.025),
new THREE.Vector3(TANK.width * 0.5 - 0.08, y, TANK.depth * 0.5 + 0.025)
]);
const material = dashed
? new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.16,
gapSize: 0.1
})
: new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const line = new THREE.Line(geometry, material);
if (dashed) line.computeLineDistances();
line.renderOrder = 6;
tankGroup.add(line);
}
layers.forEach((layer) => {
addHorizontalLine(layer.bottomY, layer.color, 0.45, true);
});
addHorizontalLine(TANK.surfaceY, 0x8af4ff, 0.9);
// Surface outline.
const surfacePoints = [
new THREE.Vector3(-TANK.width * 0.5 + 0.1, TANK.surfaceY, -TANK.depth * 0.5 + 0.1),
new THREE.Vector3(TANK.width * 0.5 - 0.1, TANK.surfaceY, -TANK.depth * 0.5 + 0.1),
new THREE.Vector3(TANK.width * 0.5 - 0.1, TANK.surfaceY, TANK.depth * 0.5 - 0.1),
new THREE.Vector3(-TANK.width * 0.5 + 0.1, TANK.surfaceY, TANK.depth * 0.5 - 0.1)
];
const surfaceLine = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(surfacePoints),
new THREE.LineBasicMaterial({
color: 0x8af4ff,
transparent: true,
opacity: 0.8
})
);
surfaceLine.renderOrder = 7;
tankGroup.add(surfaceLine);
// Small scale ticks on the front-left frame.
const tickMaterial = new THREE.LineBasicMaterial({
color: 0x91b9cf,
transparent: true,
opacity: 0.35
});
for (
let y = TANK.innerBottom;
y <= TANK.surfaceY + 0.001;
y += TANK.layerHeight / 4
) {
const tickGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-TANK.width * 0.5, y, TANK.depth * 0.5 + 0.035),
new THREE.Vector3(-TANK.width * 0.5 + 0.2, y, TANK.depth * 0.5 + 0.035)
]);
tankGroup.add(new THREE.Line(tickGeometry, tickMaterial));
}
// ------------------------------------------------------------
// DOM layer labels
// ------------------------------------------------------------
layers.forEach((layer) => {
const element = document.createElement("div");
element.className = "layer-label";
labelsHost.appendChild(element);
layer.labelElement = element;
});
function updateLayerLabels() {
layers.forEach((layer) => {
layer.labelElement.innerHTML = `
<span>${layer.name}</span>
<strong>${layer.density}</strong>
<span>kg/m³</span>
`;
});
}
// ------------------------------------------------------------
// Solid markers
// ------------------------------------------------------------
const markerDefinitions = [
{
id: "block",
name: "Block",
glyph: "■",
color: "#ff7b72",
density: 1260,
halfHeight: 0.46,
x: -2.7,
z: -0.72,
type: "box"
},
{
id: "ball",
name: "Ball",
glyph: "●",
color: "#ffd166",
density: 1040,
halfHeight: 0.47,
x: -1.35,
z: 0.72,
type: "sphere"
},
{
id: "drum",
name: "Drum",
glyph: "▰",
color: "#55d6be",
density: 1480,
halfHeight: 0.45,
x: 0,
z: -0.65,
type: "cylinder"
},
{
id: "gem",
name: "Gem",
glyph: "◆",
color: "#ae8bff",
density: 1110,
halfHeight: 0.51,
x: 1.35,
z: 0.68,
type: "octahedron"
},
{
id: "cone",
name: "Cone",
glyph: "▲",
color: "#63b3ff",
density: 820,
halfHeight: 0.46,
x: 2.7,
z: -0.6,
type: "cone"
}
];
const markerMeshes = [];
const markers = [];
function makeMarkerGeometry(type) {
switch (type) {
case "box":
return new THREE.BoxGeometry(0.9, 0.9, 0.9);
case "sphere":
return new THREE.SphereGeometry(0.47, 28, 20);
case "cylinder":
return new THREE.CylinderGeometry(0.45, 0.45, 0.9, 30);
case "octahedron":
return new THREE.OctahedronGeometry(0.55, 0);
case "cone":
return new THREE.ConeGeometry(0.48, 0.92, 28);
default:
return new THREE.BoxGeometry(0.8, 0.8, 0.8);
}
}
markerDefinitions.forEach((definition, index) => {
const group = new THREE.Group();
group.position.set(
definition.x,
TANK.surfaceY + 0.8 + index * 0.08,
definition.z
);
scene.add(group);
const geometry = makeMarkerGeometry(definition.type);
const material = new THREE.MeshStandardMaterial({
color: definition.color,
roughness: 0.29,
metalness: 0.08,
emissive: new THREE.Color(definition.color).multiplyScalar(0.08)
});
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.markerId = definition.id;
group.add(mesh);
markerMeshes.push(mesh);
// Crisp edge accents on the angular shapes.
if (definition.type !== "sphere") {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 28),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.28
})
);
group.add(edges);
}
// The ring stays at the calculated equilibrium boundary.
const targetRing = new THREE.Mesh(
new THREE.TorusGeometry(0.61, 0.025, 8, 52),
new THREE.MeshBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
targetRing.rotation.x = Math.PI / 2;
targetRing.renderOrder = 8;
scene.add(targetRing);
const label = document.createElement("div");
label.className = "marker-label";
label.style.setProperty("--marker-color", definition.color);
label.innerHTML = `
<div class="marker-label-title">
<span class="marker-label-dot"></span>
<span>${definition.name}</span>
</div>
<div class="marker-label-data">
<span class="marker-label-density"></span>
<span class="marker-label-outcome"></span>
</div>
`;
labelsHost.appendChild(label);
const marker = {
...definition,
group,
mesh,
targetRing,
label,
labelDensity: label.querySelector(".marker-label-density"),
labelOutcome: label.querySelector(".marker-label-outcome"),
velocityY: 0,
targetY: group.position.y,
boundaryY: TANK.surfaceY,
outcome: "Surface",
outcomeClass: "surface",
selected: false
};
markers.push(marker);
});
// ------------------------------------------------------------
// Equilibrium calculation
// ------------------------------------------------------------
function calculateEquilibrium(marker) {
// A marker denser than every fluid layer sinks to the bottom.
if (marker.density > layers[2].density) {
return {
boundaryY: TANK.innerBottom,
targetY: TANK.innerBottom + marker.halfHeight + 0.035,
outcome: "Tank floor",
outcomeClass: "floor"
};
}
// Layers are ordered from low density at the top to high at the bottom.
const supportingIndex = layers.findIndex(
(layer) => layer.density >= marker.density
);
const boundaryY = layers[supportingIndex].topY;
let outcome = "Surface";
let outcomeClass = "surface";
if (supportingIndex > 0) {
outcome = `L${supportingIndex + 1} interface`;
outcomeClass = "interface";
}
return {
boundaryY,
targetY: boundaryY + marker.halfHeight + 0.045,
outcome,
outcomeClass
};
}
function updateMarkerTarget(marker, resetVelocity = false) {
const equilibrium = calculateEquilibrium(marker);
marker.targetY = equilibrium.targetY;
marker.boundaryY = equilibrium.boundaryY;
marker.outcome = equilibrium.outcome;
marker.outcomeClass = equilibrium.outcomeClass;
marker.targetRing.position.set(
marker.x,
equilibrium.boundaryY + 0.018,
marker.z
);
if (resetVelocity) marker.velocityY = 0;
}
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
const layerControlsHost = document.getElementById("layerControls");
const markerControlsHost = document.getElementById("markerControls");
const layerInputs = [];
layers.forEach((layer, index) => {
const row = document.createElement("div");
row.className = "layer-control";
row.innerHTML = `
<div class="layer-name">
<span class="layer-swatch l${index + 1}"></span>
<span>${layer.name}</span>
</div>
<div class="density-number">
<input
type="number"
min="${400 + index * 50}"
max="${1150 + index * 550}"
step="10"
value="${layer.density}"
aria-label="${layer.name} fluid density"
/>
<span>kg/m³</span>
</div>
`;
const input = row.querySelector("input");
layerInputs.push(input);
input.addEventListener("change", () => {
setLayerDensity(index, Number(input.value));
});
layerControlsHost.appendChild(row);
});
const markerRows = new Map();
markers.forEach((marker) => {
const row = document.createElement("div");
row.className = "marker-control";
row.style.setProperty("--marker-color", marker.color);
row.innerHTML = `
<div class="marker-head">
<div class="identity">
<span class="identity-glyph">${marker.glyph}</span>
<strong>${marker.name}</strong>
</div>
<span class="status"></span>
</div>
<div class="marker-density-row">
<input
type="range"
min="400"
max="2200"
step="10"
value="${marker.density}"
aria-label="${marker.name} density slider"
/>
<div class="density-number">
<input
type="number"
min="400"
max="2200"
step="10"
value="${marker.density}"
aria-label="${marker.name} density"
/>
<span>kg/m³</span>
</div>
</div>
`;
const range = row.querySelector('input[type="range"]');
const number = row.querySelector('input[type="number"]');
const status = row.querySelector(".status");
markerRows.set(marker.id, { row, range, number, status });
range.addEventListener("input", () => {
setMarkerDensity(marker, Number(range.value));
});
number.addEventListener("change", () => {
setMarkerDensity(marker, Number(number.value));
});
row.addEventListener("pointerdown", () => {
selectMarker(marker.id, false);
});
markerControlsHost.appendChild(row);
});
function updateLayerInputs() {
layerInputs.forEach((input, index) => {
input.value = layers[index].density;
});
updateLayerLabels();
}
function setLayerDensity(index, rawValue) {
if (!Number.isFinite(rawValue)) {
updateLayerInputs();
return;
}
if (index === 0) {
layers[0].density = THREE.MathUtils.clamp(rawValue, 400, 1150);
if (layers[1].density <= layers[0].density) {
layers[1].density = layers[0].density + 50;
}
} else if (index === 1) {
layers[1].density = THREE.MathUtils.clamp(
rawValue,
layers[0].density + 50,
1700
);
} else {
layers[2].density = THREE.MathUtils.clamp(
rawValue,
layers[1].density + 50,
2200
);
}
if (layers[2].density <= layers[1].density) {
layers[2].density = layers[1].density + 50;
}
updateLayerInputs();
markers.forEach((marker) => {
updateMarkerTarget(marker, true);
updateMarkerUI(marker);
});
}
function setMarkerDensity(marker, rawValue) {
if (!Number.isFinite(rawValue)) {
updateMarkerUI(marker);
return;
}
marker.density = Math.round(
THREE.MathUtils.clamp(rawValue, 400, 2200)
);
updateMarkerTarget(marker, true);
updateMarkerUI(marker);
}
function updateMarkerUI(marker) {
const controls = markerRows.get(marker.id);
controls.range.value = marker.density;
controls.number.value = marker.density;
controls.status.textContent = marker.outcome;
controls.status.className = `status ${marker.outcomeClass}`;
marker.labelDensity.textContent = `${marker.density} kg/m³`;
marker.labelOutcome.textContent = marker.outcome;
}
// ------------------------------------------------------------
// Selection
// ------------------------------------------------------------
let selectedMarkerId = null;
function selectMarker(id, scrollPanel = true) {
selectedMarkerId = id;
markers.forEach((marker) => {
marker.selected = marker.id === id;
marker.targetRing.material.opacity = marker.selected ? 0.92 : 0.42;
});
markerRows.forEach(({ row }, markerId) => {
row.classList.toggle("selected", markerId === id);
});
if (scrollPanel && markerRows.has(id)) {
markerRows
.get(id)
.row.scrollIntoView({ behavior: "smooth", block: "nearest" });
}
}
// ------------------------------------------------------------
// Buttons and labels
// ------------------------------------------------------------
const motionButton = document.getElementById("motionButton");
const resetButton = document.getElementById("resetButton");
const labelsToggle = document.getElementById("labelsToggle");
let paused = false;
let labelsVisible = true;
motionButton.addEventListener("click", () => {
paused = !paused;
motionButton.textContent = paused ? "Resume settling" : "Pause settling";
});
labelsToggle.addEventListener("change", () => {
labelsVisible = labelsToggle.checked;
});
function resetSetup() {
layers.forEach((layer, index) => {
layer.density = defaultLayerDensities[index];
});
markers.forEach((marker, index) => {
marker.density = markerDefinitions[index].density;
marker.group.position.y =
TANK.surfaceY + 0.8 + index * 0.08;
marker.velocityY = 0;
updateMarkerTarget(marker, true);
updateMarkerUI(marker);
});
updateLayerInputs();
paused = false;
motionButton.textContent = "Pause settling";
resetCamera();
}
resetButton.addEventListener("click", resetSetup);
// ------------------------------------------------------------
// Custom camera controls — no addons
// ------------------------------------------------------------
const cameraState = {
theta: 0.78,
phi: 1.08,
radius: 13.5,
target: new THREE.Vector3(0, 3.0, 0)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.theta = 0.78;
cameraState.phi = 1.08;
cameraState.radius = 13.5;
updateCamera();
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let pointerDownX = 0;
let pointerDownY = 0;
let previousX = 0;
let previousY = 0;
let pointerTravel = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerTravel = 0;
pointerDownX = event.clientX;
pointerDownY = event.clientY;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
pointerTravel += Math.abs(dx) + Math.abs(dy);
cameraState.theta -= dx * 0.0065;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0055,
0.48,
1.42
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (pointerTravel < 5) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
markerMeshes,
false
);
if (intersections.length > 0) {
selectMarker(intersections[0].object.userData.markerId, true);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8.5,
20
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition, visible = true) {
projected.copy(worldPosition).project(camera);
const width = canvasHost.clientWidth;
const height = canvasHost.clientHeight;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const onScreen =
projected.z >= -1 &&
projected.z <= 1 &&
x > -80 &&
x < width + 80 &&
y > -80 &&
y < height + 80;
element.style.display = visible && onScreen ? "block" : "none";
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
const markerLabelPosition = new THREE.Vector3();
const layerLabelPosition = new THREE.Vector3();
function updateProjectedLabels() {
markers.forEach((marker) => {
markerLabelPosition.copy(marker.group.position);
markerLabelPosition.y += marker.halfHeight + 0.55;
positionLabel(marker.label, markerLabelPosition, labelsVisible);
});
layers.forEach((layer) => {
layerLabelPosition.set(
-TANK.width * 0.5 - 0.12,
layer.midY,
TANK.depth * 0.5
);
positionLabel(
layer.labelElement,
layerLabelPosition,
labelsVisible
);
});
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
const width = Math.max(1, canvasHost.clientWidth);
const height = Math.max(1, canvasHost.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.033);
const elapsed = clock.elapsedTime;
if (!paused) {
markers.forEach((marker) => {
const error = marker.targetY - marker.group.position.y;
marker.velocityY += error * 24 * dt;
marker.velocityY *= Math.exp(-9.2 * dt);
marker.group.position.y += marker.velocityY * dt;
if (
Math.abs(error) < 0.001 &&
Math.abs(marker.velocityY) < 0.01
) {
marker.group.position.y = marker.targetY;
marker.velocityY = 0;
}
});
}
markers.forEach((marker) => {
marker.group.rotation.y += dt * 0.16;
const pulse = 1 + Math.sin(elapsed * 2.1 + marker.x) * 0.055;
marker.targetRing.scale.setScalar(pulse);
});
updateProjectedLabels();
renderer.render(scene, camera);
}
// Initialize state.
updateLayerInputs();
markers.forEach((marker) => {
updateMarkerTarget(marker, true);
updateMarkerUI(marker);
});
updateCamera();
resize();
animate();
</script>
</body>
</html>
```