Prompt specification
Build or customize the Three.js 3D scene "Polymer chain entanglement model" 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: Polymer chain entanglement model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/polymer-chain-entanglement-model-x1981
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this cutaway scene: Build several long bead chains in a transparent volume with visible connectivity; distinguish covalent chain bonds from temporary entanglement contacts and label the scale schematic.
Working Three.js Starter (polymer-chain-entanglement-model-x1981):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Cutaway Polymer Entanglement</title>
<style>
:root {
color-scheme: dark;
--bg: #07111b;
--panel: rgba(8, 20, 32, 0.82);
--panel-border: rgba(150, 215, 255, 0.18);
--text: #eaf7ff;
--muted: #91aabd;
--cyan: #b8f4ff;
--cyan-strong: #55dcff;
--orange: #ffad66;
--violet: #b99cff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 65% 35%, #102b40 0%, transparent 42%),
linear-gradient(145deg, #07111b, #040a11);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#hud {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 19px;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.34),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px) saturate(125%);
-webkit-backdrop-filter: blur(16px) saturate(125%);
user-select: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan-strong);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
font-weight: 680;
line-height: 1.08;
letter-spacing: -0.025em;
}
.intro {
margin: 11px 0 16px;
color: var(--muted);
font-size: 12.5px;
line-height: 1.55;
}
.stats {
display: inline-flex;
margin-bottom: 14px;
padding: 6px 9px;
border: 1px solid rgba(95, 216, 255, 0.18);
border-radius: 999px;
background: rgba(65, 193, 238, 0.07);
color: #b8d8e8;
font-size: 10.5px;
letter-spacing: 0.025em;
}
.legend {
display: grid;
gap: 9px;
padding: 13px 0;
border-top: 1px solid rgba(255, 255, 255, 0.07);
border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}
.legend-row {
display: grid;
grid-template-columns: 31px 1fr;
align-items: center;
gap: 10px;
color: #c7d9e6;
font-size: 11.5px;
line-height: 1.3;
}
.legend-symbol {
position: relative;
width: 29px;
height: 18px;
}
.legend-symbol.bead {
width: 13px;
height: 13px;
margin-left: 7px;
border-radius: 50%;
background: #56d9ff;
box-shadow:
inset -3px -3px 5px rgba(0, 38, 60, 0.55),
0 0 12px rgba(86, 217, 255, 0.28);
}
.legend-symbol.bond {
height: 3px;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 8px rgba(184, 244, 255, 0.35);
}
.legend-symbol.contact {
height: 0;
border-top: 2px dashed var(--orange);
}
.legend-symbol.contact::after {
position: absolute;
top: -5px;
left: 12px;
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 8px rgba(255, 173, 102, 0.7);
content: "";
}
.legend-symbol.volume {
width: 22px;
height: 14px;
margin-left: 3px;
border: 1px solid rgba(119, 221, 255, 0.55);
background: rgba(86, 207, 255, 0.07);
}
.scale-card {
margin-top: 13px;
padding: 11px 12px;
border-left: 2px solid var(--cyan-strong);
border-radius: 0 8px 8px 0;
background: rgba(75, 196, 240, 0.065);
}
.scale-card strong {
display: block;
margin-bottom: 3px;
color: #d9f7ff;
font-size: 10px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scale-card span {
display: block;
color: var(--muted);
font-size: 10.5px;
line-height: 1.45;
}
#hud.collapsed .panel-body {
display: none;
}
#toolbar {
position: fixed;
z-index: 12;
bottom: 22px;
left: 50%;
display: flex;
gap: 6px;
padding: 6px;
transform: translateX(-50%);
border: 1px solid var(--panel-border);
border-radius: 12px;
background: rgba(8, 20, 32, 0.82);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
button {
appearance: none;
border: 1px solid transparent;
border-radius: 8px;
padding: 8px 11px;
background: transparent;
color: #9fb8c8;
font: inherit;
font-size: 10.5px;
font-weight: 650;
letter-spacing: 0.035em;
cursor: pointer;
transition:
color 140ms ease,
background 140ms ease,
border-color 140ms ease;
white-space: nowrap;
}
button:hover {
color: #ecfaff;
background: rgba(255, 255, 255, 0.055);
}
button.active {
border-color: rgba(82, 216, 255, 0.22);
background: rgba(82, 216, 255, 0.105);
color: #c9f5ff;
}
button[data-toggle="contacts"].active {
border-color: rgba(255, 173, 102, 0.26);
background: rgba(255, 173, 102, 0.1);
color: #ffd2a9;
}
#hint {
position: fixed;
z-index: 8;
right: 22px;
bottom: 25px;
color: rgba(177, 204, 220, 0.62);
font-size: 10px;
letter-spacing: 0.025em;
pointer-events: none;
}
#labelLayer {
position: fixed;
z-index: 5;
inset: 0;
overflow: hidden;
pointer-events: none;
}
#labelLayer.off {
display: none;
}
.scene-label {
position: absolute;
top: 0;
left: 0;
padding: 5px 8px;
transform: translate(-50%, -50%);
border: 1px solid rgba(132, 210, 244, 0.22);
border-radius: 6px;
background: rgba(5, 16, 26, 0.78);
color: #cce9f7;
font-size: 10px;
font-weight: 600;
letter-spacing: 0.035em;
white-space: nowrap;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(7px);
-webkit-backdrop-filter: blur(7px);
will-change: transform;
}
.scene-label.contact {
border-color: rgba(255, 173, 102, 0.46);
color: #ffd0a5;
box-shadow: 0 0 18px rgba(255, 145, 55, 0.12);
}
.scene-label.scale {
border-color: rgba(211, 247, 255, 0.35);
color: #effcff;
}
.scene-label.cutaway {
border-color: rgba(80, 214, 255, 0.38);
color: #9beaff;
text-transform: uppercase;
letter-spacing: 0.11em;
}
.scene-label.chain {
padding: 3px 6px;
border-color: color-mix(in srgb, var(--chain-color) 42%, transparent);
color: var(--chain-color);
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.1em;
}
@media (max-width: 760px) {
#hud {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
h1 {
font-size: 21px;
}
.intro {
margin: 8px 0 11px;
}
.legend {
grid-template-columns: 1fr 1fr;
gap: 8px 12px;
}
.legend-row {
grid-template-columns: 27px 1fr;
gap: 7px;
font-size: 10.5px;
}
.scale-card {
margin-top: 10px;
}
#toolbar {
bottom: 12px;
max-width: calc(100vw - 24px);
overflow-x: auto;
}
button {
padding: 8px 9px;
}
#hint {
display: none;
}
.scene-label.chain {
display: none !important;
}
}
@media (max-height: 650px) {
.intro,
.stats {
display: none;
}
.legend {
padding: 9px 0;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive cutaway diagram of several bead polymer chains inside a transparent volume"
></canvas>
<aside id="hud">
<div class="eyebrow">Confinement / Polymer topology</div>
<h1>Entangled bead chains</h1>
<div class="panel-body">
<p class="intro">
Five independent chains occupy a transparent confinement volume.
Solid backbone connectors show permanent covalent connectivity;
orange dashed arcs mark temporary entanglement contacts.
</p>
<div id="stats" class="stats" aria-live="polite">
Preparing chains…
</div>
<div class="legend" aria-label="Scene legend">
<div class="legend-row">
<span class="legend-symbol bead"></span>
<span>Chain-colored spheres are individual monomers.</span>
</div>
<div class="legend-row">
<span class="legend-symbol bond"></span>
<span><strong>Solid rod:</strong> covalent chain bond</span>
</div>
<div class="legend-row">
<span class="legend-symbol contact"></span>
<span><strong>Dashed arc:</strong> temporary contact</span>
</div>
<div class="legend-row">
<span class="legend-symbol volume"></span>
<span>Transparent confinement volume</span>
</div>
</div>
<div class="scale-card">
<strong>Scale schematic · not to scale</strong>
<span>
The 10 nm bar indicates the intended molecular scale only.
Bead spacing, curvature, and box proportions are illustrative.
</span>
</div>
</div>
</aside>
<div id="toolbar" aria-label="Scene controls">
<button id="toggleBonds" class="active" data-toggle="bonds" aria-pressed="true">
Backbone
</button>
<button
id="toggleContacts"
class="active"
data-toggle="contacts"
aria-pressed="true"
>
Contacts
</button>
<button
id="toggleLabels"
class="active"
data-toggle="labels"
aria-pressed="true"
>
Labels
</button>
<button id="toggleInfo" data-toggle="info" aria-pressed="false">
Info
</button>
<button id="resetView">Reset view</button>
</div>
<div id="hint">
Drag to orbit · Scroll / pinch to zoom · Shift-drag to pan · R to reset
</div>
<div id="labelLayer" aria-hidden="true"></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.querySelector("#scene");
const hud = document.querySelector("#hud");
const labelLayer = document.querySelector("#labelLayer");
const stats = document.querySelector("#stats");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: 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.2;
renderer.setClearColor(0x07111b, 0);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111b, 0.018);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// ------------------------------------------------------------
// Direct camera controls: no examples or addons are imported.
// ------------------------------------------------------------
const controls = {
azimuth: 0.68,
polar: 1.06,
radius: 22,
target: new THREE.Vector3(0, -0.05, 0),
minRadius: 11,
maxRadius: 36
};
const defaultControls = {
azimuth: 0.68,
polar: 1.06,
radius: 22,
target: new THREE.Vector3(0, -0.05, 0)
};
function updateCamera() {
const sinPolar = Math.sin(controls.polar);
camera.position.set(
controls.target.x +
controls.radius * sinPolar * Math.sin(controls.azimuth),
controls.target.y + controls.radius * Math.cos(controls.polar),
controls.target.z +
controls.radius * sinPolar * Math.cos(controls.azimuth)
);
camera.lookAt(controls.target);
}
function resetView() {
controls.azimuth = defaultControls.azimuth;
controls.polar = defaultControls.polar;
controls.radius = defaultControls.radius;
controls.target.copy(defaultControls.target);
updateCamera();
}
function zoomBy(factor) {
controls.radius = THREE.MathUtils.clamp(
controls.radius * factor,
controls.minRadius,
controls.maxRadius
);
}
const pointers = new Map();
let dragX = 0;
let dragY = 0;
let pinchDistance = 0;
let shiftPressed = false;
function pointerPairDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragX = event.clientX;
dragY = event.clientY;
} else if (pointers.size === 2) {
pinchDistance = pointerPairDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
if (shiftPressed) {
const right = new THREE.Vector3();
const up = new THREE.Vector3();
camera.matrixWorld.extractBasis(right, up, new THREE.Vector3());
controls.target.addScaledVector(right, -dx * 0.012);
controls.target.addScaledVector(up, dy * 0.012);
controls.target.x = THREE.MathUtils.clamp(
controls.target.x,
-6,
6
);
controls.target.y = THREE.MathUtils.clamp(
controls.target.y,
-4,
4
);
} else {
controls.azimuth -= dx * 0.006;
controls.polar = THREE.MathUtils.clamp(
controls.polar - dy * 0.006,
0.22,
Math.PI - 0.22
);
}
dragX = event.clientX;
dragY = event.clientY;
} else if (pointers.size === 2) {
const distance = pointerPairDistance();
if (pinchDistance > 0 && distance > 0) {
zoomBy(pinchDistance / distance);
}
pinchDistance = distance;
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
dragX = remaining.x;
dragY = remaining.y;
} else {
pinchDistance = 0;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoomBy(Math.exp(event.deltaY * 0.001));
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key === "Shift") shiftPressed = true;
switch (event.key.toLowerCase()) {
case "r":
resetView();
break;
case "arrowleft":
controls.azimuth += 0.08;
break;
case "arrowright":
controls.azimuth -= 0.08;
break;
case "arrowup":
controls.polar = Math.max(
0.22,
controls.polar - 0.06
);
break;
case "arrowdown":
controls.polar = Math.min(
Math.PI - 0.22,
controls.polar + 0.06
);
break;
case "+":
case "=":
zoomBy(0.9);
break;
case "-":
zoomBy(1.1);
break;
}
updateCamera();
});
window.addEventListener("keyup", (event) => {
if (event.key === "Shift") shiftPressed = false;
});
// ------------------------------------------------------------
// Lighting.
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa9ddff, 0x08101a, 1.65));
const keyLight = new THREE.DirectionalLight(0xd9f5ff, 3.1);
keyLight.position.set(5, 10, 7);
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x2abfff, 55, 35, 2);
rimLight.position.set(-7, 4, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9e50, 28, 28, 2);
warmLight.position.set(7, -2, 6);
scene.add(warmLight);
// ------------------------------------------------------------
// Transparent cutaway confinement volume.
// Front and top faces are omitted to expose the interior.
// ------------------------------------------------------------
const volumeWidth = 14;
const volumeHeight = 9;
const volumeDepth = 10;
const volumeGroup = new THREE.Group();
scene.add(volumeGroup);
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x70d8ff,
transparent: true,
opacity: 0.055,
roughness: 0.15,
metalness: 0,
side: THREE.DoubleSide,
depthWrite: false
});
function addWall(width, height, position, rotation) {
const wall = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
glassMaterial
);
wall.position.copy(position);
wall.rotation.set(rotation.x, rotation.y, rotation.z);
wall.renderOrder = 0;
volumeGroup.add(wall);
return wall;
}
addWall(
volumeWidth,
volumeHeight,
new THREE.Vector3(0, 0, -volumeDepth / 2),
new THREE.Euler(0, 0, 0)
);
addWall(
volumeDepth,
volumeHeight,
new THREE.Vector3(-volumeWidth / 2, 0, 0),
new THREE.Euler(0, Math.PI / 2, 0)
);
addWall(
volumeWidth,
volumeDepth,
new THREE.Vector3(0, -volumeHeight / 2, 0),
new THREE.Euler(Math.PI / 2, 0, 0)
);
const boxGeometry = new THREE.BoxGeometry(
volumeWidth,
volumeHeight,
volumeDepth
);
const boxEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(boxGeometry),
new THREE.LineBasicMaterial({
color: 0x70d7ff,
transparent: true,
opacity: 0.24,
depthWrite: false
})
);
volumeGroup.add(boxEdges);
const cutawayPoints = [
new THREE.Vector3(
-volumeWidth / 2,
-volumeHeight / 2,
volumeDepth / 2
),
new THREE.Vector3(
volumeWidth / 2,
-volumeHeight / 2,
volumeDepth / 2
),
new THREE.Vector3(
volumeWidth / 2,
volumeHeight / 2,
volumeDepth / 2
),
new THREE.Vector3(
-volumeWidth / 2,
volumeHeight / 2,
volumeDepth / 2
)
];
const cutawayRim = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(cutawayPoints),
new THREE.LineBasicMaterial({
color: 0x75e2ff,
transparent: true,
opacity: 0.78
})
);
volumeGroup.add(cutawayRim);
// Small section marks emphasize the exposed cut boundary.
const sectionVertices = [];
for (let y = -3.8; y <= 3.8; y += 0.75) {
sectionVertices.push(
volumeWidth / 2,
y,
volumeDepth / 2,
volumeWidth / 2 - 0.22,
y + 0.22,
volumeDepth / 2
);
}
const sectionGeometry = new THREE.BufferGeometry();
sectionGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(sectionVertices, 3)
);
const sectionMarks = new THREE.LineSegments(
sectionGeometry,
new THREE.LineBasicMaterial({
color: 0x75e2ff,
transparent: true,
opacity: 0.27
})
);
volumeGroup.add(sectionMarks);
const floorGrid = new THREE.GridHelper(
volumeWidth,
28,
0x32758d,
0x17374a
);
floorGrid.scale.z = volumeDepth / volumeWidth;
floorGrid.position.y = -volumeHeight / 2 + 0.012;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.32;
floorGrid.material.depthWrite = false;
volumeGroup.add(floorGrid);
// ------------------------------------------------------------
// Scene groups.
// ------------------------------------------------------------
const beadGroup = new THREE.Group();
const bondGroup = new THREE.Group();
const contactGroup = new THREE.Group();
scene.add(beadGroup, bondGroup, contactGroup);
const beadGeometry = new THREE.SphereGeometry(0.17, 18, 12);
const bondGeometry = new THREE.CylinderGeometry(
0.052,
0.052,
1,
8,
1,
false
);
const chainDefinitions = [
{
name: "Chain A",
color: 0x59ddff,
count: 35,
points: [
[-5.5, -2.5, -2.4],
[-3.8, -1.2, -0.4],
[-2.0, -2.1, 1.5],
[0.0, -1.0, 0.4],
[2.1, -2.2, -1.8],
[3.8, -1.0, -0.2],
[5.4, -2.4, 1.2]
]
},
{
name: "Chain B",
color: 0xb99cff,
count: 38,
points: [
[-5.2, 1.8, -3.2],
[-3.6, 1.0, -1.0],
[-2.0, 2.1, 1.3],
[0.0, 0.8, 2.2],
[2.2, 2.0, 0.3],
[4.0, 0.8, -2.3],
[5.3, 1.8, -1.0]
]
},
{
name: "Chain C",
color: 0x72f0b3,
count: 40,
points: [
[-4.8, 0.3, 2.8],
[-3.2, 2.6, 1.7],
[-1.4, 2.9, -0.5],
[-0.4, 1.2, -2.3],
[1.2, 2.4, -2.8],
[2.8, 2.7, -0.5],
[4.8, 0.4, 1.9]
]
},
{
name: "Chain D",
color: 0xff8e9f,
count: 37,
points: [
[-5.0, 2.8, 1.5],
[-3.2, 1.5, -2.8],
[-1.3, 0.1, -1.6],
[0.8, -0.2, 1.3],
[2.7, 0.5, 2.8],
[4.2, 1.9, 1.8],
[5.0, 2.9, -0.8]
]
},
{
name: "Chain E",
color: 0xffd166,
count: 34,
points: [
[-5.0, -3.0, 3.3],
[-3.3, -2.7, 2.5],
[-1.7, -3.1, 0.2],
[0.0, -2.8, -2.5],
[1.7, -3.1, -1.1],
[3.5, -2.6, 1.9],
[5.2, -3.0, 3.1]
]
}
];
const chains = chainDefinitions.map((definition) => {
const curve = new THREE.CatmullRomCurve3(
definition.points.map(
(point) => new THREE.Vector3(...point)
),
false,
"centripetal",
0.35
);
return {
...definition,
curve,
beads: curve.getSpacedPoints(definition.count - 1)
};
});
// Light collision separation prevents monomer spheres from passing
// directly through one another while retaining each chain's order.
const minimumSeparation = 0.37;
for (let iteration = 0; iteration < 5; iteration++) {
for (let a = 0; a < chains.length; a++) {
for (let b = a + 1; b < chains.length; b++) {
for (const p of chains[a].beads) {
for (const q of chains[b].beads) {
const delta = new THREE.Vector3().subVectors(p, q);
const distance = delta.length();
if (distance > 0.0001 && distance < minimumSeparation) {
delta.multiplyScalar(
(minimumSeparation - distance) /
distance /
2
);
p.add(delta);
q.sub(delta);
}
}
}
}
}
}
// Covalent monomer bonds: solid, pale cyan cylinders.
const bondMaterial = new THREE.MeshStandardMaterial({
color: 0xc8f7ff,
roughness: 0.32,
metalness: 0.18,
emissive: 0x174d60,
emissiveIntensity: 0.42
});
const upAxis = new THREE.Vector3(0, 1, 0);
for (const chain of chains) {
const beadMaterial = new THREE.MeshPhysicalMaterial({
color: chain.color,
roughness: 0.25,
metalness: 0.08,
clearcoat: 0.5,
clearcoatRoughness: 0.24,
emissive: chain.color,
emissiveIntensity: 0.07
});
for (const beadPosition of chain.beads) {
const bead = new THREE.Mesh(beadGeometry, beadMaterial);
bead.position.copy(beadPosition);
bead.renderOrder = 2;
beadGroup.add(bead);
}
for (let i = 0; i < chain.beads.length - 1; i++) {
const start = chain.beads[i];
const end = chain.beads[i + 1];
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const bond = new THREE.Mesh(bondGeometry, bondMaterial);
bond.position.copy(start).add(end).multiplyScalar(0.5);
bond.quaternion.setFromUnitVectors(
upAxis,
direction.normalize()
);
bond.scale.y = length;
bond.renderOrder = 1;
bondGroup.add(bond);
}
}
// ------------------------------------------------------------
// Temporary entanglement contacts.
// These are deliberately dashed and orange, unlike covalent rods.
// ------------------------------------------------------------
const contactCandidates = [];
for (let a = 0; a < chains.length; a++) {
for (let b = a + 1; b < chains.length; b++) {
for (let i = 0; i < chains[a].beads.length; i++) {
for (let j = 0; j < chains[b].beads.length; j++) {
const distance = chains[a].beads[i].distanceTo(
chains[b].beads[j]
);
if (distance > 0.46 && distance < 1.42) {
contactCandidates.push({
chainA: a,
chainB: b,
indexA: i,
indexB: j,
distance
});
}
}
}
}
}
contactCandidates.sort(
(a, b) => a.distance - b.distance
);
const usedBeads = new Set();
const selectedContacts = [];
for (const candidate of contactCandidates) {
if (selectedContacts.length >= 12) break;
const keyA = `${candidate.chainA}:${candidate.indexA}`;
const keyB = `${candidate.chainB}:${candidate.indexB}`;
if (usedBeads.has(keyA) || usedBeads.has(keyB)) {
continue;
}
usedBeads.add(keyA);
usedBeads.add(keyB);
selectedContacts.push(candidate);
}
const animatedHalos = [];
const dashedContactMaterial = new THREE.LineDashedMaterial({
color: 0xffa45d,
dashSize: 0.14,
gapSize: 0.10,
transparent: true,
opacity: 0.96,
depthWrite: false
});
const contactHaloGeometry = new THREE.SphereGeometry(
0.34,
14,
10
);
const contactMarkerGeometry = new THREE.SphereGeometry(
0.058,
10,
8
);
selectedContacts.forEach((contact, index) => {
const start =
chains[contact.chainA].beads[contact.indexA];
const end =
chains[contact.chainB].beads[contact.indexB];
const direction = new THREE.Vector3().subVectors(end, start);
const midpoint = start.clone().add(end).multiplyScalar(0.5);
let side = new THREE.Vector3().crossVectors(
direction,
upAxis
);
if (side.lengthSq() < 0.05) {
side.set(1, 0, 0);
}
side.normalize();
const lift = 0.15 + (index % 3) * 0.055;
midpoint.addScaledVector(side, lift);
midpoint.y += 0.09;
const curve = new THREE.QuadraticBezierCurve3(
start,
midpoint,
end
);
const points = curve.getPoints(32);
const dashedLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
dashedContactMaterial
);
dashedLine.computeLineDistances();
dashedLine.renderOrder = 3;
contactGroup.add(dashedLine);
const halo = new THREE.Mesh(
contactHaloGeometry,
new THREE.MeshBasicMaterial({
color: 0xff9f55,
transparent: true,
opacity: 0.07,
depthWrite: false
})
);
halo.position.copy(midpoint);
halo.renderOrder = 3;
contactGroup.add(halo);
const marker = new THREE.Mesh(
contactMarkerGeometry,
new THREE.MeshBasicMaterial({
color: 0xffb36f,
transparent: true,
opacity: 0.92,
depthWrite: false
})
);
marker.position.copy(midpoint);
marker.renderOrder = 4;
contactGroup.add(marker);
animatedHalos.push({
halo,
phase: index * 0.7
});
});
// ------------------------------------------------------------
// 3D scale bar. Its length is illustrative, not geometrically
// calibrated to the scene coordinates.
// ------------------------------------------------------------
const scaleGroup = new THREE.Group();
scene.add(scaleGroup);
const scaleStartX = -volumeWidth / 2 + 0.8;
const scaleEndX = scaleStartX + 3.5;
const scaleY = -volumeHeight / 2 + 0.18;
const scaleZ = volumeDepth / 2 - 0.12;
const scaleVertices = [
scaleStartX, scaleY, scaleZ,
scaleEndX, scaleY, scaleZ,
scaleStartX, scaleY - 0.16, scaleZ,
scaleStartX, scaleY + 0.16, scaleZ,
scaleEndX, scaleY - 0.16, scaleZ,
scaleEndX, scaleY + 0.16, scaleZ,
(scaleStartX + scaleEndX) / 2, scaleY - 0.11, scaleZ,
(scaleStartX + scaleEndX) / 2, scaleY + 0.11, scaleZ
];
const scaleGeometry = new THREE.BufferGeometry();
scaleGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(scaleVertices, 3)
);
const scaleBar = new THREE.LineSegments(
scaleGeometry,
new THREE.LineBasicMaterial({
color: 0xe7faff,
transparent: true,
opacity: 0.9
})
);
scaleGroup.add(scaleBar);
const scaleAnchor = new THREE.Vector3(
(scaleStartX + scaleEndX) / 2,
scaleY + 0.28,
scaleZ
);
// ------------------------------------------------------------
// HTML labels projected from 3D anchors.
// ------------------------------------------------------------
const labels = [];
function addLabel({
text,
className,
anchor,
kind = "normal",
chainColor = null
}) {
const element = document.createElement("div");
element.className = `scene-label ${className}`;
element.textContent = text;
if (chainColor !== null) {
const hex = `#${new THREE.Color(chainColor).getHexString()}`;
element.style.setProperty("--chain-color", hex);
}
labelLayer.appendChild(element);
labels.push({
element,
anchor: anchor.clone(),
kind
});
return element;
}
addLabel({
text: "Cutaway confinement volume",
className: "cutaway",
anchor: new THREE.Vector3(
-volumeWidth / 2 + 1.7,
volumeHeight / 2 - 0.25,
volumeDepth / 2
)
});
addLabel({
text: "10 nm · schematic scale",
className: "scale",
anchor: scaleAnchor
});
chains.forEach((chain, index) => {
addLabel({
text: chain.name,
className: "chain",
anchor: chain.beads[
Math.floor(chain.beads.length * (0.58 + index * 0.025))
],
chainColor: chain.color
});
});
if (selectedContacts.length > 0) {
const contact = selectedContacts[0];
const anchor = chains[contact.chainA].beads[
contact.indexA
].clone().add(
chains[contact.chainB].beads[contact.indexB]
).multiplyScalar(0.5);
addLabel({
text: "Temporary entanglement contact",
className: "contact",
anchor,
kind: "contact"
});
}
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
if (
label.kind === "contact" &&
!contactGroup.visible
) {
label.element.style.visibility = "hidden";
continue;
}
const projected = label.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
label.element.style.visibility = visible
? "visible"
: "hidden";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// ------------------------------------------------------------
// UI toggles.
// ------------------------------------------------------------
document
.querySelector("#toggleBonds")
.addEventListener("click", (event) => {
bondGroup.visible = !bondGroup.visible;
event.currentTarget.classList.toggle(
"active",
bondGroup.visible
);
event.currentTarget.setAttribute(
"aria-pressed",
String(bondGroup.visible)
);
});
document
.querySelector("#toggleContacts")
.addEventListener("click", (event) => {
contactGroup.visible = !contactGroup.visible;
event.currentTarget.classList.toggle(
"active",
contactGroup.visible
);
event.currentTarget.setAttribute(
"aria-pressed",
String(contactGroup.visible)
);
});
document
.querySelector("#toggleLabels")
.addEventListener("click", (event) => {
labelLayer.classList.toggle("off");
event.currentTarget.classList.toggle(
"active",
!labelLayer.classList.contains("off")
);
event.currentTarget.setAttribute(
"aria-pressed",
String(!labelLayer.classList.contains("off"))
);
});
document
.querySelector("#toggleInfo")
.addEventListener("click", (event) => {
hud.classList.toggle("collapsed");
const collapsed = hud.classList.contains("collapsed");
event.currentTarget.classList.toggle("active", collapsed);
event.currentTarget.setAttribute(
"aria-pressed",
String(collapsed)
);
});
document
.querySelector("#resetView")
.addEventListener("click", resetView);
const totalBeads = chains.reduce(
(sum, chain) => sum + chain.beads.length,
0
);
stats.textContent =
`${chains.length} chains · ${totalBeads} monomers · ` +
`${selectedContacts.length} transient contacts`;
// ------------------------------------------------------------
// Resize and render loop.
// ------------------------------------------------------------
function resize() {
camera.aspect =
window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
}
window.addEventListener("resize", resize);
resetView();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
for (const item of animatedHalos) {
const pulse =
1 + 0.08 * Math.sin(elapsed * 2.1 + item.phase);
item.halo.scale.setScalar(pulse);
item.halo.material.opacity =
0.055 +
0.025 *
(0.5 + 0.5 * Math.sin(elapsed * 2.1 + item.phase));
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```