Prompt specification
Build or customize the Three.js 3D scene "Epithelial wall over connective tissue" 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: Epithelial wall over connective tissue
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/epithelial-wall-over-connective-tissue-x0815
Scene Description Prompt:
Show a folded voxel epithelial sheet above connective tissue, labeling apical sides, continuous junctions, and basement membrane. Three.js from cdnjs, one HTML file.
Working Three.js Starter (epithelial-wall-over-connective-tissue-x0815):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Folded Voxel Epithelial Sheet</title>
<style>
:root {
color-scheme: dark;
--apical: #ff8a70;
--junction: #baff66;
--membrane: #ff4b9b;
--tissue: #e5a184;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 55% 28%, #173447 0%, #0b1d29 38%, #050c12 100%);
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.dragging {
cursor: grabbing;
}
.title-panel {
position: fixed;
z-index: 10;
top: 24px;
left: 26px;
max-width: min(460px, calc(100vw - 52px));
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.eyebrow {
margin-bottom: 8px;
color: #8fc7d6;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #eefaff;
font-size: clamp(25px, 3vw, 39px);
font-weight: 650;
letter-spacing: -0.035em;
line-height: 1.05;
}
.subtitle {
max-width: 420px;
margin: 12px 0 0;
color: #b8d1da;
font-size: 13px;
line-height: 1.55;
}
#annotations {
position: fixed;
z-index: 8;
inset: 0;
pointer-events: none;
}
#leaders {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
}
.annotation {
position: absolute;
display: flex;
align-items: center;
gap: 10px;
min-width: 178px;
padding: 10px 13px 10px 10px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-left: 3px solid var(--accent);
border-radius: 10px;
background: rgba(8, 20, 29, 0.84);
box-shadow:
0 12px 30px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(12px);
transform: translate(-50%, -50%);
transition: opacity 160ms ease;
}
.annotation .number {
display: grid;
width: 25px;
height: 25px;
flex: 0 0 25px;
place-items: center;
border-radius: 7px;
color: #07131b;
background: var(--accent);
font-size: 10px;
font-weight: 850;
}
.annotation strong {
display: block;
color: #f4fbff;
font-size: 12px;
font-weight: 720;
letter-spacing: 0.01em;
}
.annotation small {
display: block;
margin-top: 2px;
color: #91abb5;
font-size: 10px;
letter-spacing: 0.025em;
}
.legend {
position: fixed;
z-index: 10;
bottom: 24px;
left: 26px;
display: flex;
flex-wrap: wrap;
gap: 9px 16px;
max-width: min(570px, calc(100vw - 52px));
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
background: rgba(7, 17, 24, 0.68);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #b7cbd2;
font-size: 10px;
white-space: nowrap;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
background: var(--color);
box-shadow: 0 0 10px color-mix(in srgb, var(--color), transparent 30%);
}
.controls {
position: fixed;
z-index: 20;
right: 24px;
bottom: 24px;
display: flex;
gap: 8px;
}
button {
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 9px;
color: #d8edf4;
background: rgba(10, 24, 33, 0.84);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
font: inherit;
font-size: 11px;
cursor: pointer;
backdrop-filter: blur(10px);
}
button:hover {
border-color: rgba(161, 223, 238, 0.45);
color: white;
background: rgba(19, 42, 54, 0.92);
}
.hint {
position: fixed;
z-index: 9;
right: 26px;
top: 28px;
color: #89aab7;
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
@media (max-width: 700px) {
.title-panel {
top: 17px;
left: 18px;
}
.subtitle,
.hint,
.legend {
display: none;
}
.annotation {
min-width: 145px;
padding: 8px 10px 8px 8px;
}
.annotation small {
display: none;
}
.controls {
right: 16px;
bottom: 16px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="title-panel">
<div class="eyebrow">Epithelial architecture · voxel schematic</div>
<h1>Folded epithelial sheet</h1>
<p class="subtitle">
A single-cell-thick, folded epithelial layer suspended above voxelized
connective tissue. Surface color distinguishes the apical face,
junctional network, and basement membrane.
</p>
</header>
<div class="hint">Drag to orbit · Shift/right-drag to pan · Wheel to zoom</div>
<div id="annotations">
<svg id="leaders" aria-hidden="true"></svg>
<div id="anno-apical" class="annotation" style="--accent: var(--apical)">
<span class="number">01</span>
<div>
<strong>Apical side</strong>
<small>Free / luminal surface</small>
</div>
</div>
<div id="anno-junction" class="annotation" style="--accent: var(--junction)">
<span class="number">02</span>
<div>
<strong>Continuous junctions</strong>
<small>Intercellular seal network</small>
</div>
</div>
<div id="anno-membrane" class="annotation" style="--accent: var(--membrane)">
<span class="number">03</span>
<div>
<strong>Basement membrane</strong>
<small>Epithelial–stromal boundary</small>
</div>
</div>
<div id="anno-tissue" class="annotation" style="--accent: var(--tissue)">
<span class="number">04</span>
<div>
<strong>Connective tissue</strong>
<small>Supporting stromal layer</small>
</div>
</div>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch" style="--color: var(--apical)"></span>
Apical surface
</div>
<div class="legend-item">
<span class="swatch" style="--color: var(--junction)"></span>
Continuous junctions
</div>
<div class="legend-item">
<span class="swatch" style="--color: var(--membrane)"></span>
Basement membrane
</div>
<div class="legend-item">
<span class="swatch" style="--color: var(--tissue)"></span>
Connective tissue
</div>
</div>
<div class="controls">
<button id="reset" type="button">Reset view</button>
<button id="toggle-labels" type="button">Hide labels</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneContainer = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneContainer.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x081722, 21, 40);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
target: new THREE.Vector3(0, 0.9, 0),
yaw: 0.66,
phi: 1.04,
radius: 18
};
const defaultCamera = {
yaw: 0.66,
phi: 1.04,
radius: 18,
target: new THREE.Vector3(0, 0.9, 0)
};
function updateCamera() {
const { target, yaw, phi, radius } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(yaw),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(yaw)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xc5f2ff,
0x412b2b,
2.1
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1dd, 3.2);
keyLight.position.set(7, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 38;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.PointLight(0x66c9ff, 30, 30, 2);
fillLight.position.set(-9, 5, -8);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xff6d9e, 18, 22, 2);
rimLight.position.set(7, 3, -7);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x07131c,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.08;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------
// Geometry parameters
// ------------------------------------------------------------
const COLS = 10;
const ROWS = 14;
const SPACING = 0.92;
const CELL_SIZE = 0.84;
const CELL_HEIGHT = 0.86;
const SHEET_WIDTH = COLS * SPACING;
const SHEET_DEPTH = ROWS * SPACING;
const cellX = i => (i - (COLS - 1) / 2) * SPACING;
const cellZ = j => (j - (ROWS - 1) / 2) * SPACING;
// A smooth height field creates the broad folds and raised ridges.
function heightAt(x, z) {
const broadFold =
1.0 * Math.exp(
-(
((x + 2.1) ** 2) / 1.6 +
((z - 0.6) ** 2) / 3.2
)
);
const secondaryFold =
0.42 * Math.exp(
-(
((x - 2.4) ** 2) / 1.2 +
((z + 2.1) ** 2) / 1.8
)
);
return (
0.72 +
0.24 * Math.sin(0.9 * x + 0.45 * z) +
0.16 * Math.cos(0.75 * z - 0.2 * x) +
broadFold +
secondaryFold
);
}
function surfaceQuaternion(x, z) {
const eps = 0.025;
const dx =
(heightAt(x + eps, z) - heightAt(x - eps, z)) / (2 * eps);
const dz =
(heightAt(x, z + eps) - heightAt(x, z - eps)) / (2 * eps);
const xAxis = new THREE.Vector3(1, dx, 0).normalize();
const normal = new THREE.Vector3(-dx, 1, -dz).normalize();
const zAxis = new THREE.Vector3()
.crossVectors(xAxis, normal)
.normalize();
const basis = new THREE.Matrix4().makeBasis(
xAxis,
normal,
zAxis
);
return new THREE.Quaternion().setFromRotationMatrix(basis);
}
function makeSurfaceGeometry(yFunction) {
const segmentsX = 76;
const segmentsZ = 96;
const positions = [];
const indices = [];
for (let j = 0; j <= segmentsZ; j++) {
const z =
-SHEET_DEPTH / 2 +
(SHEET_DEPTH * j) / segmentsZ;
for (let i = 0; i <= segmentsX; i++) {
const x =
-SHEET_WIDTH / 2 +
(SHEET_WIDTH * i) / segmentsX;
positions.push(x, yFunction(x, z), z);
}
}
for (let j = 0; j < segmentsZ; j++) {
for (let i = 0; i < segmentsX; i++) {
const a = j * (segmentsX + 1) + i;
const b = a + 1;
const c = a + segmentsX + 1;
const d = c + 1;
indices.push(a, c, b);
indices.push(b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
// ------------------------------------------------------------
// Connective tissue: voxelized stroma and collagen-like fibers
// ------------------------------------------------------------
const tissueBase = new THREE.Mesh(
new THREE.BoxGeometry(9.68, 0.18, 13.48),
new THREE.MeshStandardMaterial({
color: 0x542d35,
roughness: 0.88
})
);
tissueBase.position.y = -0.97;
tissueBase.receiveShadow = true;
scene.add(tissueBase);
const tissueCols = 20;
const tissueRows = 28;
const tissueSpacing = 0.48;
const tissueCount = tissueCols * tissueRows;
const tissueGeometry = new THREE.BoxGeometry(0.44, 0.85, 0.44);
const tissueMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86,
metalness: 0,
transparent: true,
opacity: 0.76
});
const tissue = new THREE.InstancedMesh(
tissueGeometry,
tissueMaterial,
tissueCount
);
const tissueDummy = new THREE.Object3D();
const tissueColor = new THREE.Color();
let tissueIndex = 0;
for (let j = 0; j < tissueRows; j++) {
for (let i = 0; i < tissueCols; i++) {
const x =
(i - (tissueCols - 1) / 2) * tissueSpacing;
const z =
(j - (tissueRows - 1) / 2) * tissueSpacing;
const scaleY = 0.78 + Math.random() * 0.48;
tissueDummy.position.set(
x,
-0.49 + (Math.random() - 0.5) * 0.035,
z
);
tissueDummy.scale.set(1, scaleY, 1);
tissueDummy.updateMatrix();
tissue.setMatrixAt(tissueIndex, tissueDummy.matrix);
tissueColor.setHSL(
0.035 + Math.random() * 0.025,
0.34 + Math.random() * 0.13,
0.43 + Math.random() * 0.1
);
tissue.setColorAt(tissueIndex, tissueColor);
tissueIndex++;
}
}
tissue.instanceMatrix.needsUpdate = true;
tissue.instanceColor.needsUpdate = true;
tissue.receiveShadow = true;
scene.add(tissue);
const fiberPositions = [];
for (let i = 0; i < 95; i++) {
const x = (Math.random() - 0.5) * 9;
const z = (Math.random() - 0.5) * 12.5;
const y = -0.16 - Math.random() * 0.58;
fiberPositions.push(
x,
y,
z,
x + (Math.random() - 0.5) * 0.65,
y + (Math.random() - 0.5) * 0.16,
z + (Math.random() - 0.5) * 0.65
);
}
const fiberGeometry = new THREE.BufferGeometry();
fiberGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(fiberPositions, 3)
);
const fibers = new THREE.LineSegments(
fiberGeometry,
new THREE.LineBasicMaterial({
color: 0xffc4ad,
transparent: true,
opacity: 0.24
})
);
scene.add(fibers);
// ------------------------------------------------------------
// Basement membrane
// ------------------------------------------------------------
const membrane = new THREE.Mesh(
makeSurfaceGeometry((x, z) => heightAt(x, z) + 0.012),
new THREE.MeshStandardMaterial({
color: 0xff4b9b,
emissive: 0x5d0a38,
emissiveIntensity: 0.72,
roughness: 0.42,
metalness: 0.02,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.9
})
);
membrane.receiveShadow = true;
scene.add(membrane);
function addMembraneEdge(points) {
const curve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.TubeGeometry(
curve,
points.length * 2,
0.042,
8,
false
);
const material = new THREE.MeshStandardMaterial({
color: 0xff66ad,
emissive: 0x8f174e,
emissiveIntensity: 1.1,
roughness: 0.3
});
scene.add(new THREE.Mesh(geometry, material));
}
const frontPoints = [];
for (let i = 0; i <= 80; i++) {
const x = -SHEET_WIDTH / 2 + (SHEET_WIDTH * i) / 80;
const z = SHEET_DEPTH / 2;
frontPoints.push(
new THREE.Vector3(x, heightAt(x, z) + 0.045, z)
);
}
addMembraneEdge(frontPoints);
for (const sideX of [-SHEET_WIDTH / 2, SHEET_WIDTH / 2]) {
const sidePoints = [];
for (let j = 0; j <= 70; j++) {
const z = -SHEET_DEPTH / 2 + (SHEET_DEPTH * j) / 70;
sidePoints.push(
new THREE.Vector3(sideX, heightAt(sideX, z) + 0.045, z)
);
}
addMembraneEdge(sidePoints);
}
// ------------------------------------------------------------
// Folded epithelial voxels
// ------------------------------------------------------------
const cellCount = COLS * ROWS;
const bodyGeometry = new THREE.BoxGeometry(
CELL_SIZE,
CELL_HEIGHT,
CELL_SIZE
);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.38,
metalness: 0.015,
transparent: true,
opacity: 0.78,
depthWrite: false,
emissive: 0x0c3444,
emissiveIntensity: 0.16
});
const bodies = new THREE.InstancedMesh(
bodyGeometry,
bodyMaterial,
cellCount
);
bodies.castShadow = true;
bodies.renderOrder = 1;
scene.add(bodies);
const apicalGeometry = new THREE.PlaneGeometry(
CELL_SIZE * 0.91,
CELL_SIZE * 0.91
);
apicalGeometry.rotateX(-Math.PI / 2);
const apicalMaterial = new THREE.MeshStandardMaterial({
color: 0xff8a70,
emissive: 0x6d1c18,
emissiveIntensity: 0.42,
roughness: 0.52,
side: THREE.DoubleSide
});
const apicalFaces = new THREE.InstancedMesh(
apicalGeometry,
apicalMaterial,
cellCount
);
apicalFaces.renderOrder = 3;
scene.add(apicalFaces);
const nucleusGeometry = new THREE.SphereGeometry(0.14, 12, 8);
const nucleusMaterial = new THREE.MeshStandardMaterial({
color: 0x173b63,
emissive: 0x07182d,
emissiveIntensity: 0.35,
roughness: 0.6
});
const nuclei = new THREE.InstancedMesh(
nucleusGeometry,
nucleusMaterial,
cellCount
);
nuclei.renderOrder = 2;
scene.add(nuclei);
const frames = [];
const tempMatrix = new THREE.Matrix4();
const localMatrix = new THREE.Matrix4();
const cellColor = new THREE.Color();
const epithelialPalette = [
0x83d7e6,
0x6fc1d4,
0x9ce4e8,
0x76cddd
];
let cellIndex = 0;
for (let j = 0; j < ROWS; j++) {
for (let i = 0; i < COLS; i++) {
const x = cellX(i);
const z = cellZ(j);
const quaternion = surfaceQuaternion(x, z);
const position = new THREE.Vector3(
x,
heightAt(x, z) + CELL_HEIGHT / 2,
z
);
const frame = new THREE.Matrix4().compose(
position,
quaternion,
new THREE.Vector3(1, 1, 1)
);
frames[cellIndex] = { frame, position, quaternion };
bodies.setMatrixAt(cellIndex, frame);
const paletteIndex =
(i + 2 * j + Math.floor(Math.random() * 2)) %
epithelialPalette.length;
cellColor.setHex(epithelialPalette[paletteIndex]);
bodies.setColorAt(cellIndex, cellColor);
// Apical face.
localMatrix.makeTranslation(
0,
CELL_HEIGHT / 2 + 0.012,
0
);
tempMatrix.multiplyMatrices(frame, localMatrix);
apicalFaces.setMatrixAt(cellIndex, tempMatrix);
// Small nucleus visible through the translucent cell body.
localMatrix.compose(
new THREE.Vector3(0, -0.025, 0),
new THREE.Quaternion(),
new THREE.Vector3(0.72, 1.08, 0.72)
);
tempMatrix.multiplyMatrices(frame, localMatrix);
nuclei.setMatrixAt(cellIndex, tempMatrix);
cellIndex++;
}
}
bodies.instanceMatrix.needsUpdate = true;
bodies.instanceColor.needsUpdate = true;
apicalFaces.instanceMatrix.needsUpdate = true;
nuclei.instanceMatrix.needsUpdate = true;
// ------------------------------------------------------------
// Continuous apical junction network
// ------------------------------------------------------------
const junctionSegments = [];
// Boundaries running along z.
for (let i = 0; i < COLS - 1; i++) {
const x = (cellX(i) + cellX(i + 1)) / 2;
for (let j = 0; j < ROWS; j++) {
const z1 = cellZ(j) - SPACING / 2;
const z2 = cellZ(j) + SPACING / 2;
junctionSegments.push([
new THREE.Vector3(
x,
heightAt(x, z1) + CELL_HEIGHT + 0.03,
z1
),
new THREE.Vector3(
x,
heightAt(x, z2) + CELL_HEIGHT + 0.03,
z2
)
]);
}
}
// Boundaries running along x.
for (let j = 0; j < ROWS - 1; j++) {
const z = (cellZ(j) + cellZ(j + 1)) / 2;
for (let i = 0; i < COLS; i++) {
const x1 = cellX(i) - SPACING / 2;
const x2 = cellX(i) + SPACING / 2;
junctionSegments.push([
new THREE.Vector3(
x1,
heightAt(x1, z) + CELL_HEIGHT + 0.03,
z
),
new THREE.Vector3(
x2,
heightAt(x2, z) + CELL_HEIGHT + 0.03,
z
)
]);
}
}
const junctionGeometry = new THREE.CylinderGeometry(
0.026,
0.026,
1,
7
);
const junctionMaterial = new THREE.MeshStandardMaterial({
color: 0xbaff66,
emissive: 0x6bc52d,
emissiveIntensity: 1.7,
roughness: 0.3
});
const junctions = new THREE.InstancedMesh(
junctionGeometry,
junctionMaterial,
junctionSegments.length
);
junctions.renderOrder = 4;
const yAxis = new THREE.Vector3(0, 1, 0);
const segmentDummy = new THREE.Object3D();
junctionSegments.forEach(([a, b], index) => {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const midpoint = new THREE.Vector3()
.addVectors(a, b)
.multiplyScalar(0.5);
segmentDummy.position.copy(midpoint);
segmentDummy.quaternion.setFromUnitVectors(
yAxis,
direction.normalize()
);
segmentDummy.scale.set(1, length, 1);
segmentDummy.updateMatrix();
junctions.setMatrixAt(index, segmentDummy.matrix);
});
junctions.instanceMatrix.needsUpdate = true;
scene.add(junctions);
// ------------------------------------------------------------
// Annotation anchors
// ------------------------------------------------------------
function cellWorldPoint(i, j, localPoint) {
const frame = frames[j * COLS + i].frame;
return localPoint.clone().applyMatrix4(frame);
}
const apicalAnchor = cellWorldPoint(
6,
3,
new THREE.Vector3(0, CELL_HEIGHT / 2 + 0.035, 0)
);
const junctionX = (cellX(4) + cellX(5)) / 2;
const junctionZ = cellZ(7);
const junctionAnchor = new THREE.Vector3(
junctionX,
heightAt(junctionX, junctionZ) + CELL_HEIGHT + 0.04,
junctionZ
);
const membraneX = 2.75;
const membraneZ = SHEET_DEPTH / 2;
const membraneAnchor = new THREE.Vector3(
membraneX,
heightAt(membraneX, membraneZ) + 0.05,
membraneZ
);
const tissueAnchor = new THREE.Vector3(-4.1, 0.025, 3.55);
const annotationSpecs = [
{
element: document.getElementById("anno-apical"),
target: apicalAnchor,
offset: [135, -80],
color: "#ff8a70"
},
{
element: document.getElementById("anno-junction"),
target: junctionAnchor,
offset: [-155, 25],
color: "#baff66"
},
{
element: document.getElementById("anno-membrane"),
target: membraneAnchor,
offset: [145, 65],
color: "#ff4b9b"
},
{
element: document.getElementById("anno-tissue"),
target: tissueAnchor,
offset: [-115, 75],
color: "#e5a184"
}
];
const svgNS = "http://www.w3.org/2000/svg";
for (const spec of annotationSpecs) {
const line = document.createElementNS(svgNS, "line");
line.setAttribute("stroke", spec.color);
line.setAttribute("stroke-width", "1.4");
line.setAttribute("stroke-opacity", "0.9");
line.setAttribute("vector-effect", "non-scaling-stroke");
const dot = document.createElementNS(svgNS, "circle");
dot.setAttribute("r", "4");
dot.setAttribute("fill", spec.color);
dot.setAttribute("stroke", "#07131b");
dot.setAttribute("stroke-width", "2");
document.getElementById("leaders").append(line, dot);
spec.line = line;
spec.dot = dot;
}
let labelsVisible = true;
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
const responsiveFactor = width < 700 ? 0.58 : 1;
for (const spec of annotationSpecs) {
const projected = spec.target.clone().project(camera);
const visible =
labelsVisible &&
projected.z > -1 &&
projected.z < 1;
spec.element.style.opacity = visible ? "1" : "0";
spec.line.style.display = visible ? "" : "none";
spec.dot.style.display = visible ? "" : "none";
if (!visible) continue;
const targetX = (projected.x * 0.5 + 0.5) * width;
const targetY = (-projected.y * 0.5 + 0.5) * height;
const halfWidth = spec.element.offsetWidth / 2;
const halfHeight = spec.element.offsetHeight / 2;
let labelX =
targetX + spec.offset[0] * responsiveFactor;
let labelY =
targetY + spec.offset[1] * responsiveFactor;
labelX = THREE.MathUtils.clamp(
labelX,
halfWidth + 16,
width - halfWidth - 16
);
labelY = THREE.MathUtils.clamp(
labelY,
halfHeight + 92,
height - halfHeight - 80
);
spec.element.style.left = `${labelX}px`;
spec.element.style.top = `${labelY}px`;
// Find the point on the label's border nearest the target.
const dx = targetX - labelX;
const dy = targetY - labelY;
const tx = Math.abs(dx) > 0.001
? halfWidth / Math.abs(dx)
: Infinity;
const ty = Math.abs(dy) > 0.001
? halfHeight / Math.abs(dy)
: Infinity;
const edgeRatio = Math.min(tx, ty, 1);
const endX = labelX + dx * edgeRatio;
const endY = labelY + dy * edgeRatio;
spec.line.setAttribute("x1", targetX);
spec.line.setAttribute("y1", targetY);
spec.line.setAttribute("x2", endX);
spec.line.setAttribute("y2", endY);
spec.dot.setAttribute("cx", targetX);
spec.dot.setAttribute("cy", targetY);
}
}
// ------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom
// ------------------------------------------------------------
let dragging = false;
let panning = false;
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", event => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (panning) {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 1);
const panScale = cameraState.radius * 0.0014;
cameraState.target.addScaledVector(
right,
-dx * panScale
);
cameraState.target.addScaledVector(
up,
dy * panScale
);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-5,
5
);
cameraState.target.y = THREE.MathUtils.clamp(
cameraState.target.y,
0.2,
3.2
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-5,
5
);
} else {
cameraState.yaw -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.24,
Math.PI - 0.24
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
panning = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", event => {
event.preventDefault();
});
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8,
30
);
updateCamera();
},
{ passive: false }
);
function resetView() {
cameraState.yaw = defaultCamera.yaw;
cameraState.phi = defaultCamera.phi;
cameraState.radius = defaultCamera.radius;
cameraState.target.copy(defaultCamera.target);
updateCamera();
}
document.getElementById("reset").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
const labelButton = document.getElementById("toggle-labels");
labelButton.addEventListener("click", () => {
labelsVisible = !labelsVisible;
labelButton.textContent = labelsVisible
? "Hide labels"
: "Show labels";
});
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
renderer.setAnimationLoop(() => {
updateAnnotations();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```