Prompt specification
Build or customize the Three.js 3D scene "Directional transport across a cell sheet" 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: Directional transport across a cell sheet
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/directional-transport-across-a-cell-sheet-x0822
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring epithelial cell polarity. Build a row of polarized cells between two compartments; animate a labeled toy transport route from one side to the other without reversing cell orientation. Stylized solid forms.
Working Three.js Starter (directional-transport-across-a-cell-sheet-x0822):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Epithelial Polarity Lab</title>
<style>
:root {
--ink: #17324d;
--muted: #5e7288;
--panel: rgba(255, 255, 255, 0.88);
--border: rgba(23, 50, 77, 0.12);
--apical: #ff6b6b;
--basal: #22b8a7;
--route: #ffb000;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #edf4f6;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 14px 40px rgba(23, 50, 77, 0.12);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#intro {
top: 22px;
left: 22px;
width: min(430px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 20px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #268b85;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1.08;
letter-spacing: -0.035em;
}
#intro p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
#legend {
left: 22px;
bottom: 22px;
width: min(340px, calc(100vw - 44px));
padding: 15px 17px;
border-radius: 17px;
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin: 7px 0;
color: #496176;
font-size: 12px;
line-height: 1.3;
}
.swatch {
width: 16px;
height: 16px;
flex: 0 0 auto;
border-radius: 5px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.swatch.apical {
background: var(--apical);
}
.swatch.basal {
background: var(--basal);
}
.swatch.route {
height: 5px;
border-radius: 99px;
background: var(--route);
}
#progress-wrap {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--border);
}
#progress-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: #496176;
font-size: 11px;
font-weight: 700;
}
#progress-track {
height: 7px;
overflow: hidden;
border-radius: 99px;
background: #dfe9ec;
}
#progress-fill {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #16a6a0, #ffb000, #ff6b6b);
}
#controls {
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border-radius: 15px;
}
button {
min-height: 38px;
padding: 0 13px;
border: 0;
border-radius: 10px;
background: #edf4f6;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
background: #dfecee;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.primary {
background: #17324d;
color: white;
}
button.primary:hover {
background: #244968;
}
#hint {
position: fixed;
z-index: 9;
top: 22px;
right: 22px;
padding: 9px 12px;
border-radius: 12px;
background: rgba(255, 255, 255, 0.76);
color: #62778a;
font-size: 11px;
pointer-events: none;
}
@media (max-width: 700px) {
#intro {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
}
#intro p {
display: none;
}
#legend {
left: 14px;
bottom: 72px;
width: calc(100vw - 28px);
padding: 11px 13px;
}
.legend-row {
margin: 5px 0;
font-size: 11px;
}
#controls {
right: 14px;
bottom: 14px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section id="intro" class="panel">
<div class="eyebrow">Polarized epithelial barrier</div>
<h1>Epithelial Polarity Lab</h1>
<p>
A one-way toy cargo route crosses the fixed-polarity cell row from
Compartment A to Compartment B. The apical and basal faces never flip.
</p>
</section>
<section id="legend" class="panel">
<div class="legend-row">
<span class="swatch apical"></span>
<span><strong>Apical face</strong> — always directed toward Compartment A</span>
</div>
<div class="legend-row">
<span class="swatch basal"></span>
<span><strong>Basal face</strong> — always directed toward Compartment B</span>
</div>
<div class="legend-row">
<span class="swatch route"></span>
<span><strong>Toy transport route</strong> — A → B, shown as a schematic overlay</span>
</div>
<div id="progress-wrap">
<div id="progress-label">
<span id="route-state">Cargo moving A → B</span>
<span id="progress-number">0%</span>
</div>
<div id="progress-track">
<span id="progress-fill"></span>
</div>
</div>
</section>
<div id="hint">Drag to orbit · Wheel to zoom · Arrow keys to pan</div>
<nav id="controls" class="panel" aria-label="Animation controls">
<button id="pause" class="primary" type="button">Pause</button>
<button id="restart" type="button">Restart route</button>
<button id="reset-camera" type="button">Reset view</button>
</nav>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xedf4f6);
scene.fog = new THREE.Fog(0xedf4f6, 18, 34);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
radius: 14.2,
theta: 0.52,
phi: 1.05,
target: new THREE.Vector3(0, -0.05, 0)
};
function updateCamera() {
const { radius, theta, phi, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
// Lighting
const hemisphere = new THREE.HemisphereLight(0xf9fdff, 0x8aa0aa, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x8bd9df, 1.05);
fillLight.position.set(-7, 4, -5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffc27d, 0.85);
rimLight.position.set(4, 2, -8);
scene.add(rimLight);
// Helpers
function roundedBoxGeometry(width, height, depth, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
const bevel = Math.min(0.07, radius * 0.28);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: bevel,
bevelThickness: bevel,
curveSegments: 8
});
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
return geometry;
}
function makeLabel(text, options = {}) {
const {
foreground = "#17324d",
background = "rgba(255,255,255,0.94)",
border = "rgba(23,50,77,0.16)",
width = 2.4
} = options;
const lines = text.split("\n");
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const fontSize = lines.length > 1 ? 32 : 36;
const lineHeight = fontSize + 8;
const font = `800 ${fontSize}px Inter, Arial, sans-serif`;
context.font = font;
const widest = Math.max(
...lines.map((line) => context.measureText(line).width)
);
canvas.width = Math.ceil(Math.max(260, Math.min(1000, widest + 64)));
canvas.height = lines.length > 1 ? 112 : 76;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
const radius = 18;
const x = 3;
const y = 3;
const w = canvas.width - 6;
const h = canvas.height - 6;
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + w - radius, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + radius);
ctx.lineTo(x + w, y + h - radius);
ctx.quadraticCurveTo(x + w, y + h, x + w - radius, y + h);
ctx.lineTo(x + radius, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = border;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = foreground;
ctx.font = font;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
const startY = canvas.height / 2 -
((lines.length - 1) * lineHeight) / 2;
lines.forEach((line, index) => {
ctx.fillText(line, canvas.width / 2, startY + index * lineHeight);
});
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(
width,
width * canvas.height / canvas.width,
1
);
sprite.renderOrder = 30;
return sprite;
}
function makeTextSprite(text, options = {}) {
const sprite = makeLabel(text, options);
return sprite;
}
// Ground
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xdfe9eb,
roughness: 0.96,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(32, 26),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.92;
ground.receiveShadow = true;
scene.add(ground);
// Two solid compartments
const compartmentMaterials = {
a: new THREE.MeshStandardMaterial({
color: 0xb9e4e7,
roughness: 0.7
}),
b: new THREE.MeshStandardMaterial({
color: 0xf6d3b3,
roughness: 0.72
})
};
const compartmentTopMaterials = {
a: new THREE.MeshStandardMaterial({
color: 0xd7f1f2,
roughness: 0.62
}),
b: new THREE.MeshStandardMaterial({
color: 0xfae2ca,
roughness: 0.64
})
};
const compartmentGeometry = roundedBoxGeometry(4.4, 0.5, 6.8, 0.2);
const compartmentTopGeometry = roundedBoxGeometry(4.05, 0.08, 6.45, 0.12);
function addCompartment(x, side) {
const group = new THREE.Group();
const base = new THREE.Mesh(
compartmentGeometry,
compartmentMaterials[side]
);
base.position.set(x, -1.56, 0);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const top = new THREE.Mesh(
compartmentTopGeometry,
compartmentTopMaterials[side]
);
top.position.set(x, -1.27, 0);
top.receiveShadow = true;
group.add(top);
scene.add(group);
}
addCompartment(-3.25, "a");
addCompartment(3.25, "b");
const membraneMaterial = new THREE.MeshStandardMaterial({
color: 0x5ba7aa,
roughness: 0.56
});
const leftMembrane = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 1.25, 6.65),
membraneMaterial
);
leftMembrane.position.set(-1.04, -0.68, 0);
leftMembrane.castShadow = true;
scene.add(leftMembrane);
const rightMembrane = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 1.25, 6.65),
new THREE.MeshStandardMaterial({
color: 0xd49a6a,
roughness: 0.58
})
);
rightMembrane.position.set(1.04, -0.68, 0);
rightMembrane.castShadow = true;
scene.add(rightMembrane);
// Decorative solid molecules inside each compartment
const moleculeGeometry = new THREE.SphereGeometry(0.18, 18, 14);
const moleculeMaterials = [
new THREE.MeshStandardMaterial({ color: 0x49b6ba, roughness: 0.5 }),
new THREE.MeshStandardMaterial({ color: 0x78c9cd, roughness: 0.5 }),
new THREE.MeshStandardMaterial({ color: 0xeaa56c, roughness: 0.5 }),
new THREE.MeshStandardMaterial({ color: 0xf2c18e, roughness: 0.5 })
];
const moleculePositions = [
[-4.75, -0.91, -2.45],
[-3.75, -0.87, -1.55],
[-2.55, -0.92, 1.85],
[-4.05, -0.88, 2.45],
[-2.05, -0.93, -0.35],
[4.55, -0.91, -2.35],
[3.55, -0.88, -1.45],
[2.35, -0.93, 1.95],
[4.05, -0.89, 2.35],
[2.15, -0.93, -0.35]
];
moleculePositions.forEach((position, index) => {
const molecule = new THREE.Mesh(
moleculeGeometry,
moleculeMaterials[index % moleculeMaterials.length]
);
molecule.position.set(...position);
const scale = 0.8 + (index % 3) * 0.14;
molecule.scale.set(scale, scale * 0.86, scale);
molecule.castShadow = true;
scene.add(molecule);
});
// Cell-row support
const tray = new THREE.Mesh(
roundedBoxGeometry(1.5, 0.18, 7.05, 0.08),
new THREE.MeshStandardMaterial({
color: 0x38566b,
roughness: 0.72
})
);
tray.position.set(0, -1.22, 0);
tray.castShadow = true;
tray.receiveShadow = true;
scene.add(tray);
// Fixed-polarity epithelial row.
// Apical is always on the left (-X), basal is always on the right (+X).
// This group is never rotated, flipped, or animated.
const epithelialRow = new THREE.Group();
scene.add(epithelialRow);
const cellBodyGeometry = roundedBoxGeometry(1.08, 2.08, 0.82, 0.25);
const apicalCapGeometry = roundedBoxGeometry(0.16, 1.62, 0.70, 0.07);
const basalCapGeometry = roundedBoxGeometry(0.16, 1.62, 0.70, 0.07);
const cellBodyMaterials = [
new THREE.MeshStandardMaterial({
color: 0x9186d5,
roughness: 0.58
}),
new THREE.MeshStandardMaterial({
color: 0xa092df,
roughness: 0.58
}),
new THREE.MeshStandardMaterial({
color: 0x8279ca,
roughness: 0.58
})
];
const apicalMaterial = new THREE.MeshStandardMaterial({
color: 0xff6b6b,
roughness: 0.52
});
const basalMaterial = new THREE.MeshStandardMaterial({
color: 0x22b8a7,
roughness: 0.56
});
const nucleusMaterial = new THREE.MeshStandardMaterial({
color: 0xfff0c7,
roughness: 0.48
});
const microvillusGeometry = new THREE.CylinderGeometry(
0.025,
0.025,
0.32,
10
);
const cellPositions = [-2.4, -1.2, 0, 1.2, 2.4];
cellPositions.forEach((z, index) => {
const cell = new THREE.Group();
cell.position.set(0, -0.05, z);
const body = new THREE.Mesh(
cellBodyGeometry,
cellBodyMaterials[index % cellBodyMaterials.length]
);
body.castShadow = true;
body.receiveShadow = true;
cell.add(body);
// Fixed apical face, directed toward Compartment A.
const apicalCap = new THREE.Mesh(apicalCapGeometry, apicalMaterial);
apicalCap.position.set(-0.535, 0.02, 0);
apicalCap.castShadow = true;
cell.add(apicalCap);
// Fixed basal face, directed toward Compartment B.
const basalCap = new THREE.Mesh(basalCapGeometry, basalMaterial);
basalCap.position.set(0.535, 0.02, 0);
basalCap.castShadow = true;
cell.add(basalCap);
// Small apical projections make the direction easy to read.
[-0.43, 0, 0.43].forEach((y) => {
const projection = new THREE.Mesh(
microvillusGeometry,
apicalMaterial
);
projection.rotation.z = Math.PI / 2;
projection.position.set(-0.77, y, 0);
projection.castShadow = true;
cell.add(projection);
});
// Basal attachment pads.
const basalFoot = new THREE.Mesh(
new THREE.SphereGeometry(0.18, 16, 12),
basalMaterial
);
basalFoot.scale.set(0.9, 0.38, 1.35);
basalFoot.position.set(0.67, -0.54, 0);
basalFoot.castShadow = true;
cell.add(basalFoot);
// Solid nucleus.
const nucleus = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 20, 16),
nucleusMaterial
);
nucleus.scale.set(0.88, 1.12, 0.48);
nucleus.position.set(0.13, 0.03, 0.41);
nucleus.castShadow = true;
cell.add(nucleus);
epithelialRow.add(cell);
});
// Labels
const compartmentALabel = makeTextSprite("COMPARTMENT A\nLEFT SIDE", {
foreground: "#167c80",
background: "rgba(231,250,250,0.96)",
border: "rgba(22,124,128,0.22)",
width: 3.05
});
compartmentALabel.position.set(-3.3, 0.76, -2.35);
scene.add(compartmentALabel);
const compartmentBLabel = makeTextSprite("COMPARTMENT B\nRIGHT SIDE", {
foreground: "#9b5d25",
background: "rgba(255,244,229,0.96)",
border: "rgba(155,93,37,0.22)",
width: 3.05
});
compartmentBLabel.position.set(3.3, 0.76, -2.35);
scene.add(compartmentBLabel);
const apicalLabel = makeTextSprite("APICAL → A", {
foreground: "#a9343d",
background: "rgba(255,235,236,0.96)",
border: "rgba(255,107,107,0.45)",
width: 1.85
});
apicalLabel.position.set(-1.05, 1.33, 2.42);
scene.add(apicalLabel);
const basalLabel = makeTextSprite("BASAL → B", {
foreground: "#147c70",
background: "rgba(226,250,245,0.96)",
border: "rgba(34,184,167,0.45)",
width: 1.85
});
basalLabel.position.set(1.05, 1.33, 2.42);
scene.add(basalLabel);
const rowLabel = makeTextSprite("FIXED-POLARITY CELL ROW", {
foreground: "#17324d",
background: "rgba(255,255,255,0.94)",
border: "rgba(23,50,77,0.18)",
width: 3.3
});
rowLabel.position.set(0, -0.78, 3.43);
scene.add(rowLabel);
// Toy transport route from A to B
const routeCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-4.85, 0.18, 0),
new THREE.Vector3(-3.2, 0.2, 0),
new THREE.Vector3(-1.35, 0.18, 0),
new THREE.Vector3(0, 0.22, 0),
new THREE.Vector3(1.35, 0.18, 0),
new THREE.Vector3(3.25, 0.2, 0),
new THREE.Vector3(4.85, 0.18, 0)
]);
const routeMaterial = new THREE.MeshBasicMaterial({
color: 0xffb000,
transparent: true,
opacity: 0.92,
depthTest: false,
depthWrite: false
});
const route = new THREE.Mesh(
new THREE.TubeGeometry(routeCurve, 120, 0.045, 8, false),
routeMaterial
);
route.renderOrder = 20;
scene.add(route);
// Direction arrows along the route.
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0xffb000,
transparent: true,
opacity: 0.96,
depthTest: false,
depthWrite: false
});
[0.16, 0.36, 0.64, 0.84].forEach((t) => {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.15, 0.36, 18),
arrowMaterial
);
arrow.position.copy(routeCurve.getPointAt(t));
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(1, 0, 0)
);
arrow.renderOrder = 22;
scene.add(arrow);
});
const pickupLabel = makeTextSprite("1 PICKUP", {
foreground: "#956000",
background: "rgba(255,247,215,0.96)",
border: "rgba(255,176,0,0.45)",
width: 1.75
});
pickupLabel.position.set(-4.15, -0.55, 0);
scene.add(pickupLabel);
const crossingLabel = makeTextSprite("2 TRANS-CELLULAR", {
foreground: "#7f5600",
background: "rgba(255,247,215,0.96)",
border: "rgba(255,176,0,0.45)",
width: 2.85
});
crossingLabel.position.set(0, 0.83, 0);
scene.add(crossingLabel);
const releaseLabel = makeTextSprite("3 RELEASE", {
foreground: "#956000",
background: "rgba(255,247,215,0.96)",
border: "rgba(255,176,0,0.45)",
width: 1.75
});
releaseLabel.position.set(4.15, -0.55, 0);
scene.add(releaseLabel);
// Labeled cargo
const cargoGroup = new THREE.Group();
const cargoMaterial = new THREE.MeshStandardMaterial({
color: 0xf04464,
emissive: 0x7d142d,
emissiveIntensity: 0.25,
roughness: 0.35,
transparent: true,
depthTest: false,
depthWrite: false
});
const cargo = new THREE.Mesh(
new THREE.SphereGeometry(0.23, 24, 18),
cargoMaterial
);
cargo.castShadow = true;
cargo.renderOrder = 25;
cargoGroup.add(cargo);
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0xffe18a,
transparent: true,
opacity: 0.82,
depthTest: false,
depthWrite: false
});
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.032, 10, 36),
haloMaterial
);
halo.rotation.x = Math.PI / 2;
halo.renderOrder = 26;
cargoGroup.add(halo);
const cargoLabel = makeTextSprite("CARGO", {
foreground: "#9f1836",
background: "rgba(255,239,242,0.97)",
border: "rgba(240,68,100,0.5)",
width: 1.25
});
cargoLabel.position.set(0, 0.55, 0);
cargoGroup.add(cargoLabel);
scene.add(cargoGroup);
// UI references
const pauseButton = document.getElementById("pause");
const restartButton = document.getElementById("restart");
const resetCameraButton = document.getElementById("reset-camera");
const progressFill = document.getElementById("progress-fill");
const progressNumber = document.getElementById("progress-number");
const routeState = document.getElementById("route-state");
let elapsed = 0;
let paused = false;
const cycleDuration = 10;
function resetRoute() {
elapsed = 0;
}
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Play" : "Pause";
routeState.textContent = paused ? "Animation paused" : "Cargo moving A → B";
}
pauseButton.addEventListener("click", togglePause);
restartButton.addEventListener("click", resetRoute);
resetCameraButton.addEventListener("click", () => {
cameraState.radius = 14.2;
cameraState.theta = 0.52;
cameraState.phi = 1.05;
cameraState.target.set(0, -0.05, 0);
updateCamera();
});
// Direct camera controls, implemented without external addons.
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.005,
0.48,
1.42
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8,
23
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
const panStep = 0.2;
if (event.key === "ArrowLeft") {
cameraState.target.x -= panStep;
} else if (event.key === "ArrowRight") {
cameraState.target.x += panStep;
} else if (event.key === "ArrowUp") {
cameraState.target.z -= panStep;
} else if (event.key === "ArrowDown") {
cameraState.target.z += panStep;
} else if (event.key === "+" || event.key === "=") {
cameraState.radius = Math.max(8, cameraState.radius - 0.6);
} else if (event.key === "-") {
cameraState.radius = Math.min(23, cameraState.radius + 0.6);
} else if (event.code === "Space") {
event.preventDefault();
togglePause();
} else if (event.key.toLowerCase() === "r") {
resetRoute();
}
updateCamera();
});
// Resize
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
elapsed += delta;
}
const phase = (elapsed % cycleDuration) / cycleDuration;
let routeProgress = 0;
let cargoOpacity = 1;
if (phase < 0.08) {
cargoOpacity = phase / 0.08;
routeProgress = 0;
} else if (phase > 0.92) {
cargoOpacity = (1 - phase) / 0.08;
routeProgress = 1;
} else {
cargoOpacity = 1;
routeProgress = (phase - 0.08) / 0.84;
}
cargoGroup.position.copy(
routeCurve.getPointAt(THREE.MathUtils.clamp(routeProgress, 0, 1))
);
cargoMaterial.opacity = cargoOpacity;
haloMaterial.opacity = 0.82 * cargoOpacity;
cargoLabel.material.opacity = cargoOpacity;
halo.rotation.z += delta * 1.8;
cargo.rotation.y += delta * 1.4;
const percent = Math.round(routeProgress * 100);
progressFill.style.width = `${percent}%`;
progressNumber.textContent = `${percent}%`;
if (!paused) {
routeState.textContent =
phase < 0.08
? "Cargo entering at A"
: phase > 0.92
? "Cargo released at B"
: "Cargo moving A → B";
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```