Prompt specification
Build or customize the Three.js 3D scene "Recombination-position exploration table" 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: Recombination-position exploration table
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/recombination-position-exploration-table-x0932
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring meiotic recombination. Let users choose a single crossover location on two homologs; display resulting chromatids and explain the simplification without implying that arbitrary real loci recombine equally. Low-poly.
Working Three.js Starter (recombination-position-exploration-table-x0932):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>One Crossover — Meiotic Recombination</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: #0c192a;
--panel-2: #101f33;
--line: #263a54;
--text: #e9f1fb;
--muted: #9eb0c8;
--a: #3ad7c3;
--b: #ff7d73;
--gold: #ffd166;
--cream: #f3e8c9;
}
* {
box-sizing: border-box;
}
html,
body {
min-height: 100%;
margin: 0;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
min-width: 320px;
}
button,
input {
font: inherit;
}
.app {
min-height: 100vh;
display: grid;
grid-template-rows: auto 1fr;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
padding: 1rem 1.25rem;
border-bottom: 1px solid var(--line);
background: rgba(7, 17, 31, 0.94);
}
.eyebrow {
margin: 0 0 0.2rem;
color: var(--gold);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.25rem, 2.2vw, 1.9rem);
letter-spacing: -0.035em;
}
.topbar p {
max-width: 38rem;
margin: 0.35rem 0 0;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.45;
}
.model-badge {
flex: 0 0 auto;
padding: 0.5rem 0.7rem;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
font-size: 0.76rem;
white-space: nowrap;
}
.layout {
min-height: 0;
display: grid;
grid-template-columns: minmax(0, 1fr) 350px;
}
.stage {
position: relative;
min-height: 650px;
overflow: hidden;
background:
radial-gradient(circle at 50% 45%, #10223a 0%, #07111f 70%);
border-right: 1px solid var(--line);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel-title,
.panel-status,
.scene-callout {
position: absolute;
z-index: 2;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.panel-title {
padding: 0.38rem 0.68rem;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 999px;
background: rgba(7, 17, 31, 0.76);
color: #f5f8ff;
font-size: 0.76rem;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.panel-status {
color: var(--muted);
font-size: 0.76rem;
}
.scene-callout {
padding: 0.28rem 0.48rem;
border: 1px solid rgba(255, 209, 102, 0.5);
border-radius: 6px;
background: rgba(7, 17, 31, 0.88);
color: var(--gold);
font-size: 0.72rem;
font-weight: 700;
}
.stage-hud {
position: absolute;
z-index: 3;
inset: 0.85rem auto auto 0.85rem;
max-width: 20rem;
padding: 0.65rem 0.75rem;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(7, 17, 31, 0.78);
color: var(--muted);
font-size: 0.74rem;
line-height: 1.45;
pointer-events: none;
backdrop-filter: blur(8px);
}
.stage-hud strong {
color: var(--text);
}
.reset-view {
position: absolute;
z-index: 3;
right: 0.85rem;
bottom: 0.85rem;
border: 1px solid var(--line);
border-radius: 8px;
padding: 0.48rem 0.68rem;
background: rgba(12, 25, 42, 0.9);
color: var(--text);
cursor: pointer;
}
.reset-view:hover,
.reset-view:focus-visible {
border-color: var(--gold);
outline: none;
}
.controls {
min-height: 0;
overflow: auto;
padding: 1rem;
background: var(--panel);
}
.control-block {
padding-bottom: 1rem;
margin-bottom: 1rem;
border-bottom: 1px solid var(--line);
}
.control-block:last-child {
margin-bottom: 0;
border-bottom: 0;
}
h2,
h3 {
margin: 0;
letter-spacing: -0.02em;
}
h2 {
font-size: 1rem;
}
h3 {
font-size: 0.9rem;
}
.step {
display: inline-grid;
place-items: center;
width: 1.35rem;
height: 1.35rem;
margin-right: 0.38rem;
border-radius: 50%;
background: var(--gold);
color: #152033;
font-size: 0.76rem;
font-weight: 900;
}
.help {
margin: 0.45rem 0 0.75rem;
color: var(--muted);
font-size: 0.79rem;
line-height: 1.5;
}
.pair-state {
margin: 0.75rem 0;
padding: 0.65rem 0.72rem;
border: 1px solid rgba(255, 209, 102, 0.34);
border-radius: 8px;
background: rgba(255, 209, 102, 0.07);
color: var(--gold);
font-size: 0.82rem;
font-weight: 750;
}
.choice-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.5rem;
}
.chrom-button {
display: flex;
align-items: center;
gap: 0.55rem;
width: 100%;
padding: 0.58rem;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel-2);
color: var(--text);
text-align: left;
cursor: pointer;
}
.chrom-button:hover,
.chrom-button:focus-visible {
border-color: #617896;
outline: none;
}
.chrom-button.selected {
border-color: var(--gold);
background: rgba(255, 209, 102, 0.08);
box-shadow: inset 0 0 0 1px rgba(255, 209, 102, 0.12);
}
.chrom-button strong,
.chrom-button small {
display: block;
}
.chrom-button strong {
font-size: 0.82rem;
}
.chrom-button small {
margin-top: 0.12rem;
color: var(--muted);
font-size: 0.67rem;
}
.dot {
flex: 0 0 auto;
width: 0.7rem;
height: 0.7rem;
border-radius: 50%;
}
.dot.a {
background: var(--a);
}
.dot.b {
background: var(--b);
}
.position-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 1rem;
margin-top: 0.55rem;
}
#coordinateValue {
color: var(--gold);
font-variant-numeric: tabular-nums;
font-weight: 850;
}
input[type="range"] {
width: 100%;
height: 7px;
margin: 0.8rem 0 0.25rem;
border-radius: 999px;
appearance: none;
background: #293b55;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #152033;
border-radius: 50%;
appearance: none;
background: var(--gold);
box-shadow: 0 0 0 1px var(--gold);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #152033;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 1px var(--gold);
}
.range-ends {
display: flex;
justify-content: space-between;
color: #71859f;
font-size: 0.67rem;
}
#positionMeta {
min-height: 2.4em;
margin: 0.6rem 0 0;
color: var(--muted);
font-size: 0.76rem;
line-height: 1.4;
}
.warning {
display: none;
margin-top: 0.55rem;
padding: 0.55rem 0.6rem;
border-left: 3px solid var(--gold);
background: rgba(255, 209, 102, 0.07);
color: #e7d7ad;
font-size: 0.72rem;
line-height: 1.45;
}
.warning.visible {
display: block;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.45rem;
margin-top: 0.8rem;
}
.metric {
padding: 0.62rem 0.4rem;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel-2);
text-align: center;
}
.metric b {
display: block;
color: var(--gold);
font-size: 1.05rem;
}
.metric span {
display: block;
margin-top: 0.18rem;
color: var(--muted);
font-size: 0.66rem;
line-height: 1.25;
}
.legend {
display: grid;
gap: 0.55rem;
margin-top: 0.75rem;
}
.legend-row {
display: flex;
align-items: center;
gap: 0.55rem;
color: var(--muted);
font-size: 0.75rem;
}
.swatch {
width: 1.05rem;
height: 0.55rem;
border-radius: 999px;
}
.swatch.a {
background: var(--a);
}
.swatch.b {
background: var(--b);
}
.swatch.gold {
height: 0.7rem;
border: 2px solid var(--gold);
background: transparent;
}
.swatch.cream {
height: 0.7rem;
border-radius: 3px;
background: var(--cream);
}
.explanation p {
margin: 0.6rem 0 0;
color: var(--muted);
font-size: 0.77rem;
line-height: 1.55;
}
.explanation strong {
color: var(--text);
}
.caution {
padding: 0.7rem;
border: 1px solid rgba(255, 209, 102, 0.3);
border-radius: 8px;
background: rgba(255, 209, 102, 0.06);
}
.caution p {
color: #dfd3b3;
}
.footer-note {
margin-top: 0.85rem !important;
color: #8093ad !important;
font-size: 0.7rem !important;
}
@media (max-width: 900px) {
.layout {
grid-template-columns: 1fr;
}
.stage {
min-height: 590px;
border-right: 0;
border-bottom: 1px solid var(--line);
}
.controls {
overflow: visible;
}
.topbar {
align-items: flex-start;
}
.model-badge {
display: none;
}
}
@media (max-width: 520px) {
.topbar {
padding: 0.9rem;
}
.stage {
min-height: 520px;
}
.stage-hud {
right: 0.65rem;
max-width: none;
font-size: 0.68rem;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div>
<p class="eyebrow">Interactive genetics · simplified single-event model</p>
<h1>One crossover, four chromatids</h1>
<p>
Choose one nonsister chromatid from each homolog and place one shared
crossover coordinate.
</p>
</div>
<div class="model-badge">Low-poly 3D · one reciprocal exchange</div>
</header>
<main class="layout">
<section class="stage" id="stage" aria-label="Interactive 3D recombination scene">
<canvas
id="scene"
tabindex="0"
aria-label="Two homologous chromosomes before and after one crossover. Use the controls to select chromatids and move the crossover marker."
></canvas>
<div class="stage-hud">
<strong>Direct interaction:</strong> click a chromatid to select it and
place the marker. Drag the scene to orbit; scroll or pinch to zoom.
</div>
<div id="preTitle" class="panel-title">Before exchange</div>
<div id="postTitle" class="panel-title">After exchange</div>
<div id="preStatus" class="panel-status"></div>
<div id="postStatus" class="panel-status"></div>
<div id="crossoverCallout" class="scene-callout">
one shared crossover site
</div>
<div id="exchangeCallout" class="scene-callout">
reciprocal exchange
</div>
<button id="resetView" class="reset-view" type="button">
Reset view
</button>
</section>
<aside class="controls">
<section class="control-block">
<h2><span class="step">1</span>Choose participating chromatids</h2>
<p class="help">
Select exactly one chromatid from Homolog A and one from Homolog B.
Clicking a chromatid in the 3D scene does both selection and placement.
</p>
<div class="pair-state" id="pairLabel"></div>
<div class="choice-grid">
<button class="chrom-button" data-id="A1" type="button" aria-pressed="true">
<span class="dot a"></span>
<span><strong>A1</strong><small>Homolog A · chromatid 1</small></span>
</button>
<button class="chrom-button" data-id="A2" type="button" aria-pressed="false">
<span class="dot a"></span>
<span><strong>A2</strong><small>Homolog A · chromatid 2</small></span>
</button>
<button class="chrom-button" data-id="B1" type="button" aria-pressed="true">
<span class="dot b"></span>
<span><strong>B1</strong><small>Homolog B · chromatid 1</small></span>
</button>
<button class="chrom-button" data-id="B2" type="button" aria-pressed="false">
<span class="dot b"></span>
<span><strong>B2</strong><small>Homolog B · chromatid 2</small></span>
</button>
</div>
</section>
<section class="control-block">
<h2><span class="step">2</span>Place one crossover coordinate</h2>
<p class="help">
The coordinate is mirrored on both participating chromatids because
crossing over occurs between aligned homologous positions.
</p>
<div class="position-heading">
<label for="coordinate">Illustrative coordinate</label>
<output id="coordinateValue" for="coordinate">66 / 100</output>
</div>
<input
id="coordinate"
type="range"
min="10"
max="90"
step="1"
value="66"
aria-describedby="positionMeta"
>
<div class="range-ends">
<span>one chromosome end</span>
<span>other chromosome end</span>
</div>
<p id="positionMeta"></p>
<div class="warning" id="centromereWarning">
This places the marker at the schematic centromere. Centromeric
crossing over is strongly constrained in many organisms; this toy
geometry does not represent that biology.
</div>
</section>
<section class="control-block">
<h2>Result from this one event</h2>
<div class="metrics">
<div class="metric"><b>1</b><span>reciprocal<br>exchange</span></div>
<div class="metric"><b>2</b><span>recombinant<br>chromatids</span></div>
<div class="metric"><b>2</b><span>parental<br>chromatids</span></div>
</div>
<div class="legend" aria-label="Color legend">
<div class="legend-row">
<span class="swatch a"></span>
Homolog A ancestry
</div>
<div class="legend-row">
<span class="swatch b"></span>
Homolog B ancestry
</div>
<div class="legend-row">
<span class="swatch gold"></span>
Shared crossover marker
</div>
<div class="legend-row">
<span class="swatch cream"></span>
Schematic centromere
</div>
</div>
</section>
<section class="control-block explanation">
<h3>What the model shows</h3>
<p>
After replication, each homolog contains two sister chromatids.
A single reciprocal crossover between one nonsister chromatid of
Homolog A and one of Homolog B exchanges the segment farther from
the centromere. The other two chromatids remain parental.
</p>
<div class="caution">
<h3>The slider is not a probability</h3>
<p>
This is an illustrative coordinate, not a claim that arbitrary
real loci recombine equally. In genomes, crossing over is
initiated at accessible hotspots, and rates vary with sequence,
chromatin, chromosome region, alignment, and repair constraints.
</p>
</div>
<p>
The model omits multiple crossovers, crossover interference,
chromatid participation patterns, and gene conversion. Color shows
parental ancestry rather than a particular allele; a real crossover
need not change an allele at the break itself.
</p>
<p class="footer-note">
Geometry is intentionally schematic: chromosomes are linearized and
the centromere is only an approximate location.
</p>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const stage = $("#stage");
const canvas = $("#scene");
const state = {
selectedA: 0,
selectedB: 0,
t: 0.66,
azimuth: 0,
elevation: 0.08,
zoom: 1
};
const TOP = 2.25;
const BOTTOM = -2.25;
const LENGTH = TOP - BOTTOM;
const CENTROMERE = 0.46;
const UP = new THREE.Vector3(0, 1, 0);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setClearColor(0x07111f, 1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0, 0);
scene.add(new THREE.HemisphereLight(0xc7ddff, 0x142033, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.5);
keyLight.position.set(-4, 8, 10);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x72b8ff, 1.1);
rimLight.position.set(7, 1, 4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffc86a, 12, 18, 2);
warmLight.position.set(0, -1, 5);
scene.add(warmLight);
const materials = {
a: new THREE.MeshStandardMaterial({
color: 0x3ad7c3,
roughness: 0.78,
flatShading: true
}),
b: new THREE.MeshStandardMaterial({
color: 0xff7d73,
roughness: 0.78,
flatShading: true
}),
aDim: new THREE.MeshStandardMaterial({
color: 0x246c68,
roughness: 0.9,
flatShading: true
}),
bDim: new THREE.MeshStandardMaterial({
color: 0x81444b,
roughness: 0.9,
flatShading: true
}),
centromere: new THREE.MeshStandardMaterial({
color: 0xf3e8c9,
roughness: 0.8,
flatShading: true
}),
telomere: new THREE.MeshStandardMaterial({
color: 0xcbd8e8,
roughness: 0.85,
flatShading: true
}),
marker: new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x6c4700,
emissiveIntensity: 0.55,
roughness: 0.45,
flatShading: true
})
};
const preRoot = new THREE.Group();
const postRoot = new THREE.Group();
preRoot.position.x = -3.15;
postRoot.position.x = 3.15;
scene.add(preRoot, postRoot);
const definitions = [
{ id: "A1", hom: "A", index: 0, offset: -0.68, phase: 0.1, z: -0.025 },
{ id: "A2", hom: "A", index: 1, offset: -0.24, phase: 0.7, z: 0.025 },
{ id: "B1", hom: "B", index: 0, offset: 0.24, phase: 1.1, z: 0.025 },
{ id: "B2", hom: "B", index: 1, offset: 0.68, phase: 1.8, z: -0.025 }
];
const preChromatids = [];
const postChromatids = [];
const pickMeshes = [];
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeChromatidLabel() {
const c = document.createElement("canvas");
c.width = 256;
c.height = 96;
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.62, 0.23, 1);
sprite.renderOrder = 20;
sprite.userData.canvas = c;
sprite.userData.texture = texture;
sprite.userData.key = "";
return sprite;
}
function updateChromatidLabel(ch) {
const selected = isSelected(ch);
const recombinant = ch.isPost && selected;
const text = recombinant ? `${ch.def.id} · R` : ch.def.id;
const key = `${text}|${selected}`;
if (ch.label.userData.key === key) return;
ch.label.userData.key = key;
const c = ch.label.userData.canvas;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = selected
? "rgba(255, 209, 102, 0.96)"
: "rgba(7, 17, 31, 0.88)";
ctx.strokeStyle = selected
? "#fff0bd"
: "rgba(190, 207, 229, 0.45)";
ctx.lineWidth = selected ? 7 : 3;
roundedRect(ctx, 25, 10, 206, 76, 18);
ctx.fill();
ctx.stroke();
ctx.fillStyle = selected ? "#172033" : "#e9f1fb";
ctx.font = "800 43px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 49);
ch.label.userData.texture.needsUpdate = true;
}
function makeTextSprite(text, color = "#aebed3") {
const c = document.createElement("canvas");
c.width = 512;
c.height = 128;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = color;
ctx.font = "800 34px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 64);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.2, 0.3, 1);
sprite.renderOrder = 19;
return sprite;
}
function makePanel(root) {
const plateGeometry = new THREE.PlaneGeometry(4.85, 5.95);
const plate = new THREE.Mesh(
plateGeometry,
new THREE.MeshBasicMaterial({
color: 0x0c1b2e,
transparent: true,
opacity: 0.72,
side: THREE.DoubleSide,
depthWrite: false
})
);
plate.position.z = -0.58;
root.add(plate);
const border = new THREE.LineSegments(
new THREE.EdgesGeometry(plateGeometry),
new THREE.LineBasicMaterial({
color: 0x29415f,
transparent: true,
opacity: 0.85
})
);
border.position.copy(plate.position);
root.add(border);
const homologA = makeTextSprite("HOMOLOG A", "#8de9dc");
homologA.position.set(-0.47, 3.02, 0.03);
homologA.scale.set(0.9, 0.225, 1);
root.add(homologA);
const homologB = makeTextSprite("HOMOLOG B", "#ffaaa3");
homologB.position.set(0.47, 3.02, 0.03);
homologB.scale.set(0.9, 0.225, 1);
root.add(homologB);
}
makePanel(preRoot);
makePanel(postRoot);
function pathPoint(ch, t) {
const sway = Math.sin(t * Math.PI * 1.45 + ch.def.phase) * 0.075;
const depth = Math.sin(t * Math.PI * 2 + ch.def.phase) * 0.025;
return new THREE.Vector3(
sway,
TOP - LENGTH * t,
ch.def.z + depth
);
}
function radiusAt(t) {
const centromerePinch = Math.exp(-Math.pow((t - CENTROMERE) / 0.038, 2));
return 0.092 * (1 - 0.42 * centromerePinch);
}
function makeCollar(radius, height, material) {
return new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, 10, 1, false),
material
);
}
function orientAlongY(mesh, tangent) {
mesh.quaternion.setFromUnitVectors(UP, tangent.clone().normalize());
}
function createChromatid(def, root, isPost) {
const group = new THREE.Group();
group.position.x = def.offset;
root.add(group);
const geometryGroup = new THREE.Group();
group.add(geometryGroup);
const label = makeChromatidLabel();
const topPoint = pathPoint({ def }, 0);
label.position.set(topPoint.x, TOP + 0.42, topPoint.z + 0.04);
group.add(label);
const marker = makeCollar(0.18, 0.075, materials.marker);
marker.visible = false;
marker.renderOrder = 4;
group.add(marker);
const centromere = makeCollar(0.115, 0.09, materials.centromere);
const centromerePoint = pathPoint({ def }, CENTROMERE);
const centromereTangent = pathPoint({ def }, CENTROMERE + 0.005)
.sub(pathPoint({ def }, CENTROMERE - 0.005));
centromere.position.copy(centromerePoint);
orientAlongY(centromere, centromereTangent);
group.add(centromere);
const topCap = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105, 0),
materials.telomere
);
topCap.position.copy(pathPoint({ def }, 0));
group.add(topCap);
const bottomCap = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105, 0),
materials.telomere
);
bottomCap.position.copy(pathPoint({ def }, 1));
group.add(bottomCap);
const pickMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
});
pickMaterial.colorWrite = false;
const pick = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 4.72, 0.36),
pickMaterial
);
pick.position.set(0, 0, def.z);
group.add(pick);
const ch = {
def,
group,
geometryGroup,
label,
marker,
isPost
};
pick.userData.chromatid = ch;
pickMeshes.push(pick);
return ch;
}
for (const def of definitions) {
preChromatids.push(createChromatid(def, preRoot, false));
postChromatids.push(createChromatid(def, postRoot, true));
}
const preConnector = new THREE.Line(
new THREE.BufferGeometry(),
new THREE.LineDashedMaterial({
color: 0xffd166,
dashSize: 0.12,
gapSize: 0.085,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
preConnector.renderOrder = 12;
scene.add(preConnector);
function isSelected(ch) {
return ch.def.hom === "A"
? state.selectedA === ch.def.index
: state.selectedB === ch.def.index;
}
function disposeGeometry(group) {
for (const child of [...group.children]) {
if (child.geometry) child.geometry.dispose();
group.remove(child);
}
}
function addPiece(ch, t0, t1, material) {
const steps = Math.max(2, Math.ceil(Math.abs(t1 - t0) * 18));
for (let i = 0; i < steps; i++) {
const a = t0 + ((t1 - t0) * i) / steps;
const b = t0 + ((t1 - t0) * (i + 1)) / steps;
const p0 = pathPoint(ch, a);
const p1 = pathPoint(ch, b);
const direction = p1.clone().sub(p0);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radiusAt(b),
radiusAt(a),
length * 1.08,
7,
1,
false
);
const segment = new THREE.Mesh(geometry, material);
segment.position.copy(p0).add(p1).multiplyScalar(0.5);
orientAlongY(segment, direction);
ch.geometryGroup.add(segment);
}
}
function updateChromatid(ch) {
const selected = isSelected(ch);
const ownMaterial = ch.def.hom === "A" ? materials.a : materials.b;
const foreignMaterial = ch.def.hom === "A" ? materials.b : materials.a;
const dimMaterial = ch.def.hom === "A" ? materials.aDim : materials.bDim;
disposeGeometry(ch.geometryGroup);
if (ch.isPost && selected) {
// The piece farther from the centromere is exchanged.
if (state.t < CENTROMERE) {
addPiece(ch, 0, state.t, foreignMaterial);
addPiece(ch, state.t, 1, ownMaterial);
} else {
addPiece(ch, 0, state.t, ownMaterial);
addPiece(ch, state.t, 1, foreignMaterial);
}
} else if (ch.isPost) {
addPiece(ch, 0, 1, dimMaterial);
} else {
addPiece(ch, 0, 1, ownMaterial);
}
ch.marker.visible = selected;
if (selected) {
const point = pathPoint(ch, state.t);
const tangent = pathPoint(ch, Math.min(1, state.t + 0.006))
.sub(pathPoint(ch, Math.max(0, state.t - 0.006)));
ch.marker.position.copy(point);
orientAlongY(ch.marker, tangent);
}
updateChromatidLabel(ch);
}
function selectedPreChromatid() {
return preChromatids.find((ch) => ch.def.hom === "A" && isSelected(ch));
}
function selectedPostChromatid(hom) {
return postChromatids.find(
(ch) => ch.def.hom === hom && isSelected(ch)
);
}
function updateConnector() {
const a = selectedPreChromatid();
const b = selectedPreChromatid("B");
a.group.updateWorldMatrix(true, false);
b.group.updateWorldMatrix(true, false);
const pointA = a.group.localToWorld(pathPoint(a, state.t).clone());
const pointB = b.group.localToWorld(pathPoint(b, state.t).clone());
preConnector.geometry.dispose();
preConnector.geometry = new THREE.BufferGeometry().setFromPoints([
pointA,
pointB
]);
preConnector.computeLineDistances();
}
function updateUI() {
const buttons = [...document.querySelectorAll(".chrom-button")];
for (const button of buttons) {
const [hom, number] = button.dataset.id.split("");
const index = Number(number) - 1;
const selected = hom === "A"
? state.selectedA === index
: state.selectedB === index;
button.classList.toggle("selected", selected);
button.setAttribute("aria-pressed", String(selected));
}
const aId = `A${state.selectedA + 1}`;
const bId = `B${state.selectedB + 1}`;
const coordinate = Math.round(state.t * 100);
$("#pairLabel").textContent = `Participating pair: ${aId} × ${bId}`;
$("#coordinateValue").textContent = `${coordinate} / 100`;
$("#coordinate").value = String(coordinate);
const slider = $("#coordinate");
slider.style.background =
`linear-gradient(90deg, #ffd166 ${coordinate}%, #293b55 ${coordinate}%)`;
const side = coordinate < CENTROMERE * 100
? "above"
: "below";
$("#positionMeta").textContent =
`Coordinate ${coordinate} is ${side} the schematic centromere. ` +
`The same marker is shown on ${aId} and ${bId}.`;
$("#centromereWarning").classList.toggle(
"visible",
Math.abs(coordinate - CENTROMERE * 100) < 4
);
$("#preStatus").textContent = `${aId} × ${bId} will participate`;
$("#postStatus").textContent = "2 recombinant chromatids + 2 parental chromatids";
}
function updateScene() {
for (const ch of preChromatids) updateChromatid(ch);
for (const ch of postChromatids) updateChromatid(ch);
updateConnector();
updateUI();
}
function selectChromatid(def) {
if (def.hom === "A") state.selectedA = def.index;
else state.selectedB = def.index;
updateScene();
}
for (const button of document.querySelectorAll(".chrom-button")) {
button.addEventListener("click", () => {
const [hom, number] = button.dataset.id.split("");
const index = Number(number) - 1;
const def = definitions.find(
(d) => d.hom === hom && d.index === index
);
selectChromatid(def);
});
}
$("#coordinate").addEventListener("input", (event) => {
state.t = Number(event.target.value) / 100;
updateScene();
});
// Direct camera controls, implemented without Three.js addons.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let fitDistance = 14.5;
let drag = null;
function updateCamera() {
const radius = fitDistance * state.zoom;
const horizontal = radius * Math.cos(state.elevation);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(state.azimuth),
cameraTarget.y + radius * Math.sin(state.elevation),
cameraTarget.z + horizontal * Math.cos(state.azimuth)
);
camera.lookAt(cameraTarget);
}
function pickChromatid(event) {
const rect = canvas.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 hits = raycaster.intersectObjects(pickMeshes, false);
if (!hits.length) return;
const ch = hits[0].object.userData.chromatid;
ch.group.updateWorldMatrix(true, false);
const localPoint = ch.group.worldToLocal(hits[0].point.clone());
const t = THREE.MathUtils.clamp((TOP - localPoint.y) / LENGTH, 0.1, 0.9);
state.t = Math.round(t * 100) / 100;
if (ch.def.hom === "A") state.selectedA = ch.def.index;
else state.selectedB = ch.def.index;
updateScene();
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
drag = {
pointerId: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!drag || drag.pointerId !== event.pointerId) return;
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
const totalDistance = Math.hypot(
event.clientX - drag.startX,
event.clientY - drag.startY
);
if (totalDistance > 4) {
drag.moved = true;
canvas.style.cursor = "grabbing";
}
if (drag.moved) {
state.azimuth = THREE.MathUtils.clamp(
state.azimuth - dx * 0.0055,
-1.05,
1.05
);
state.elevation = THREE.MathUtils.clamp(
state.elevation + dy * 0.0045,
-0.42,
0.55
);
}
});
canvas.addEventListener("pointerup", (event) => {
if (!drag || drag.pointerId !== event.pointerId) return;
if (!drag.moved) pickChromatid(event);
drag = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointercancel", () => {
drag = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
state.zoom = THREE.MathUtils.clamp(
state.zoom * Math.exp(event.deltaY * 0.001),
0.72,
1.65
);
}, { passive: false });
canvas.style.cursor = "grab";
$("#resetView").addEventListener("click", () => {
state.azimuth = 0;
state.elevation = 0.08;
state.zoom = 1;
});
canvas.addEventListener("dblclick", () => {
state.azimuth = 0;
state.elevation = 0.08;
state.zoom = 1;
});
function resize() {
const rect = stage.getBoundingClientRect();
const width = Math.max(1, rect.width);
const height = Math.max(1, rect.height);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
// Keep both panels framed on narrow screens.
fitDistance = 14.5 * Math.max(1, 1.28 / camera.aspect);
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
const projected = new THREE.Vector3();
function projectOverlay(element, worldPosition, verticalOffset = 0) {
projected.copy(worldPosition);
projected.y += verticalOffset;
projected.project(camera);
const rect = stage.getBoundingClientRect();
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * rect.width}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * rect.height}px`;
}
function updateOverlays() {
projectOverlay(
$("#preTitle"),
new THREE.Vector3(preRoot.position.x, 3.48, 0)
);
projectOverlay(
$("#postTitle"),
new THREE.Vector3(postRoot.position.x, 3.48, 0)
);
projectOverlay(
$("#preStatus"),
new THREE.Vector3(preRoot.position.x, -2.92, 0)
);
projectOverlay(
$("#postStatus"),
new THREE.Vector3(postRoot.position.x, -2.92, 0)
);
const preA = selectedPreChromatid();
const preB = selectedPreChromatid("B");
preA.group.updateWorldMatrix(true, false);
preB.group.updateWorldMatrix(true, false);
const prePointA = preA.group.localToWorld(pathPoint(preA, state.t).clone());
const prePointB = preB.group.localToWorld(pathPoint(preB, state.t).clone());
const preMidpoint = prePointA.clone().add(prePointB).multiplyScalar(0.5);
projectOverlay($("#crossoverCallout"), preMidpoint, 0.43);
const postA = selectedPostChromatid("A");
const postB = selectedPostChromatid("B");
postA.group.updateWorldMatrix(true, false);
postB.group.updateWorldMatrix(true, false);
const postPointA = postA.group.localToWorld(pathPoint(postA, state.t).clone());
const postPointB = postB.group.localToWorld(pathPoint(postB, state.t).clone());
const postMidpoint = postPointA.clone().add(postPointB).multiplyScalar(0.5);
projectOverlay($("#exchangeCallout"), postMidpoint, 0.43);
}
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateOverlays();
renderer.render(scene, camera);
}
resize();
updateScene();
animate();
</script>
</body>
</html>
```