Prompt specification
Build or customize the Three.js 3D scene "Linked versus independent assortment trays" 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: Linked versus independent assortment trays
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/linked-versus-independent-assortment-trays-x0926
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring genetic linkage. Compare symbolic inheritance outcomes for nearby loci on one chromosome and loci on separate chromosomes under explicitly stated simplified assumptions. Stylized solid forms.
Working Three.js Starter (linked-versus-independent-assortment-trays-x0926):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Genetic Linkage — Interactive Three.js Model</title>
<style>
:root {
--bg: #f2f0e9;
--ink: #17212b;
--muted: #66717c;
--line: rgba(23, 33, 43, 0.12);
--panel: rgba(255, 255, 255, 0.91);
--a: #2f6fed;
--lower-a: #7657d5;
--b: #f4a514;
--lower-b: #e76b4d;
--teal: #218b83;
--coral: #d66c5c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--ink);
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100vw;
height: 100vh;
z-index: 0;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
min-height: 78px;
padding: 15px 22px 12px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
border-bottom: 1px solid var(--line);
background: rgba(242, 240, 233, 0.93);
backdrop-filter: blur(14px);
}
.eyebrow {
margin: 0 0 3px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.1vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
}
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: flex-end;
gap: 8px;
}
.tool-button {
min-height: 36px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--ink);
background: rgba(255, 255, 255, 0.75);
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease,
border-color 150ms ease;
}
.tool-button:hover {
border-color: rgba(23, 33, 43, 0.28);
background: #fff;
transform: translateY(-1px);
}
.tool-button:focus-visible,
.gamete-row:focus-visible,
.gamete-label:focus-visible {
outline: 3px solid rgba(47, 111, 237, 0.28);
outline-offset: 2px;
}
.interaction-hint {
color: var(--muted);
font-size: 10px;
white-space: nowrap;
}
.model-note {
position: fixed;
z-index: 9;
top: 78px;
left: 0;
right: 0;
min-height: 57px;
padding: 9px 22px 10px;
border-bottom: 1px solid var(--line);
background: rgba(242, 240, 233, 0.88);
backdrop-filter: blur(12px);
color: #4e5964;
font-size: 11px;
line-height: 1.45;
}
.model-note strong {
color: var(--ink);
}
.model-note .model-label {
margin-right: 9px;
padding: 3px 7px;
border-radius: 5px;
color: #fff;
background: var(--ink);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#lab {
position: fixed;
z-index: 4;
top: 143px;
right: 18px;
bottom: 18px;
left: 18px;
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 18px;
pointer-events: none;
}
.case {
min-width: 0;
min-height: 0;
display: grid;
grid-template-rows: minmax(220px, 1fr) auto;
gap: 10px;
}
.stage {
position: relative;
min-height: 220px;
overflow: hidden;
pointer-events: none;
}
.case-head {
position: absolute;
z-index: 3;
top: 6px;
left: 6px;
right: 6px;
display: flex;
align-items: flex-start;
gap: 11px;
pointer-events: none;
}
.case-number {
width: 31px;
height: 31px;
flex: 0 0 auto;
display: grid;
place-items: center;
border: 1px solid rgba(23, 33, 43, 0.15);
border-radius: 50%;
color: #fff;
background: var(--ink);
font-size: 10px;
font-weight: 800;
}
.case-head h2 {
margin: 0;
font-size: clamp(17px, 1.55vw, 23px);
line-height: 1.05;
letter-spacing: -0.03em;
}
.case-head p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
}
.structure-pill {
margin-left: auto;
padding: 7px 9px;
border: 1px solid var(--line);
border-radius: 999px;
color: #4e5964;
background: rgba(255, 255, 255, 0.72);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.04em;
white-space: nowrap;
text-transform: uppercase;
}
.case-card {
pointer-events: auto;
padding: 14px 15px 12px;
border: 1px solid rgba(23, 33, 43, 0.1);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 12px 34px rgba(32, 40, 47, 0.08),
0 2px 5px rgba(32, 40, 47, 0.04);
backdrop-filter: blur(14px);
}
.card-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.card-title strong {
font-size: 12px;
}
.equation {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
white-space: nowrap;
}
.legend {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 4px;
}
.dot {
width: 8px;
height: 8px;
display: inline-block;
border-radius: 50%;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.dot.A { background: var(--a); }
.dot.a { background: var(--lower-a); }
.dot.B { background: var(--b); }
.dot.b { background: var(--lower-b); }
.legend-note {
margin-left: auto;
}
.gamete-row {
width: 100%;
min-height: 35px;
padding: 5px 0;
display: grid;
grid-template-columns: 61px 48px minmax(40px, 1fr) 83px;
align-items: center;
gap: 8px;
border: 0;
border-top: 1px solid rgba(23, 33, 43, 0.075);
color: var(--ink);
background: transparent;
text-align: left;
cursor: pointer;
transition: background 140ms ease;
}
.gamete-row:hover,
.gamete-row.active {
background: rgba(47, 111, 237, 0.055);
}
.gamete-key {
display: flex;
align-items: center;
gap: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 800;
}
.probability {
color: #3f4953;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.bar {
height: 6px;
overflow: hidden;
border-radius: 999px;
background: #e5e7e6;
}
.bar i {
height: 100%;
display: block;
border-radius: inherit;
background: linear-gradient(90deg, #218b83, #4bb5a8);
transition: width 450ms ease;
}
.gamete-row[data-gamete="Ab"] .bar i,
.gamete-row[data-gamete="aB"] .bar i {
background: linear-gradient(90deg, #d66c5c, #f1a050);
}
.outcome {
display: flex;
align-items: baseline;
justify-content: flex-end;
gap: 5px;
font-size: 9px;
white-space: nowrap;
}
.outcome b {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.outcome small {
color: var(--muted);
font-size: 9px;
}
.card-summary {
margin-top: 8px;
padding-top: 9px;
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 5px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
}
.card-summary strong {
color: var(--ink);
font-size: 14px;
}
.card-summary .summary-chip {
padding: 3px 7px;
border-radius: 999px;
color: #17665f;
background: #e3f1ed;
font-weight: 750;
}
.card-summary .summary-chip.split {
color: #9a493d;
background: #f8e8e2;
}
.annotation {
position: absolute;
z-index: 2;
transform: translate(-50%, -50%);
pointer-events: none;
user-select: none;
}
.gene-label {
padding: 5px 8px;
border: 1px solid rgba(23, 33, 43, 0.12);
border-radius: 8px;
color: var(--ink);
background: rgba(255, 255, 255, 0.88);
box-shadow: 0 4px 12px rgba(23, 33, 43, 0.07);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
font-weight: 800;
white-space: nowrap;
}
.gene-label .dot {
margin-right: 3px;
}
.cue-label {
padding: 4px 7px;
border: 1px solid rgba(244, 165, 20, 0.3);
border-radius: 999px;
color: #8a5c00;
background: rgba(255, 247, 220, 0.9);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
white-space: nowrap;
}
.gamete-label {
min-width: 64px;
padding: 5px 7px;
display: flex;
align-items: center;
justify-content: center;
gap: 4px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 8px;
color: #fff;
background: rgba(23, 33, 43, 0.88);
box-shadow: 0 5px 14px rgba(23, 33, 43, 0.13);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
font-weight: 800;
pointer-events: auto;
cursor: pointer;
white-space: nowrap;
transition:
transform 150ms ease,
background 150ms ease;
}
.gamete-label:hover,
.gamete-label.active {
color: var(--ink);
background: #fff;
transform: translate(-50%, -50%) scale(1.08);
}
.gamete-label b {
color: #b9c2c8;
font-size: 8px;
}
.gamete-label:hover b,
.gamete-label.active b {
color: var(--muted);
}
@media (max-width: 980px) {
.topbar {
gap: 12px;
}
.interaction-hint {
display: none;
}
#lab {
left: 10px;
right: 10px;
gap: 10px;
}
.case-card {
padding: 11px 12px;
}
.gamete-row {
grid-template-columns: 54px 43px minmax(30px, 1fr) 73px;
gap: 5px;
}
.outcome small {
display: none;
}
.structure-pill {
display: none;
}
}
@media (max-width: 700px) {
body {
overflow: auto;
}
#scene {
position: fixed;
}
.topbar,
.model-note {
position: relative;
}
.topbar {
align-items: flex-start;
flex-direction: column;
}
.toolbar {
justify-content: flex-start;
}
#lab {
position: relative;
top: auto;
right: auto;
bottom: auto;
left: auto;
margin-top: 14px;
padding: 0 10px 18px;
display: grid;
grid-template-columns: 1fr;
gap: 14px;
}
.case {
grid-template-rows: 430px auto;
}
.model-note {
padding: 10px 14px;
}
.legend-note {
width: 100%;
margin-left: 0;
}
}
@media (prefers-reduced-motion: reduce) {
* {
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive three-dimensional comparison of linked and independently assorting genes"
></canvas>
<header class="topbar">
<div>
<p class="eyebrow">Heredity / Interactive Model</p>
<h1>When do genes travel together?</h1>
<p class="subtitle">
Symbolic inheritance outcomes for nearby loci versus separate chromosomes.
</p>
</div>
<div class="toolbar">
<span class="interaction-hint">Drag to orbit · scroll to zoom · double-click to reset</span>
<button class="tool-button" id="motion-toggle" type="button" aria-pressed="false">
Pause motion
</button>
<button class="tool-button" id="reset-view" type="button">
Reset view
</button>
</div>
</header>
<div class="model-note">
<span class="model-label">Simplified model</span>
<strong>Assumptions:</strong>
one <em>AaBb</em> parent in coupling phase <em>AB/ab</em> is crossed to an
<em>aabb</em> tester.
<strong>Linked:</strong>
both loci are on one homolog pair; at most one crossover occurs in the interval
(<em>r = 0.30</em>), with no chromatid interference.
<strong>Separate chromosomes:</strong>
the loci assort independently (<em>r = 0.50</em>).
All gametes have equal viability and fertility; fertilization is random; there is
no mutation, selection, or segregation distortion.
Phenotypes use complete dominance and no epistasis:
<em>A_</em>/<em>aa</em> and <em>B_</em>/<em>bb</em>.
</div>
<main id="lab">
<section class="case" aria-labelledby="linked-title">
<div class="stage" id="stage-linked">
<div class="case-head">
<span class="case-number">01</span>
<div>
<h2 id="linked-title">Nearby loci · same chromosome</h2>
<p>Linked inheritance · recombination fraction r = 0.30</p>
</div>
<span class="structure-pill">One linked chromosome</span>
</div>
</div>
<div class="case-card">
<div class="card-title">
<strong>Gametes → tester-cross progeny</strong>
<span class="equation">AaBb × aabb</span>
</div>
<div class="legend">
<span class="legend-item"><i class="dot A"></i>A</span>
<span class="legend-item"><i class="dot a"></i>a</span>
<span class="legend-item"><i class="dot B"></i>B</span>
<span class="legend-item"><i class="dot b"></i>b</span>
<span class="legend-note">Rod size reflects relative gamete frequency</span>
</div>
<button class="gamete-row" type="button" data-case="linked" data-gamete="AB" aria-pressed="false">
<span class="gamete-key"><i class="dot A"></i><i class="dot B"></i>AB</span>
<span class="probability">35%</span>
<span class="bar"><i style="width:35%"></i></span>
<span class="outcome"><b>AaBb</b><small>A_B_</small></span>
</button>
<button class="gamete-row" type="button" data-case="linked" data-gamete="Ab" aria-pressed="false">
<span class="gamete-key"><i class="dot A"></i><i class="dot b"></i>Ab</span>
<span class="probability">15%</span>
<span class="bar"><i style="width:15%"></i></span>
<span class="outcome"><b>Aabb</b><small>A_bb</small></span>
</button>
<button class="gamete-row" type="button" data-case="linked" data-gamete="aB" aria-pressed="false">
<span class="gamete-key"><i class="dot a"></i><i class="dot B"></i>aB</span>
<span class="probability">15%</span>
<span class="bar"><i style="width:15%"></i></span>
<span class="outcome"><b>aaBb</b><small>aaB_</small></span>
</button>
<button class="gamete-row" type="button" data-case="linked" data-gamete="ab" aria-pressed="false">
<span class="gamete-key"><i class="dot a"></i><i class="dot b"></i>ab</span>
<span class="probability">35%</span>
<span class="bar"><i style="width:35%"></i></span>
<span class="outcome"><b>aabb</b><small>aabb</small></span>
</button>
<div class="card-summary">
<span class="summary-chip">70% parental gametes</span>
<span class="summary-chip split">30% recombinant gametes</span>
</div>
</div>
</section>
<section class="case" aria-labelledby="separate-title">
<div class="stage" id="stage-separate">
<div class="case-head">
<span class="case-number">02</span>
<div>
<h2 id="separate-title">Separate chromosomes</h2>
<p>Independent assortment · recombination fraction r = 0.50</p>
</div>
<span class="structure-pill">Two separate chromosomes</span>
</div>
</div>
<div class="case-card">
<div class="card-title">
<strong>Gametes → tester-cross progeny</strong>
<span class="equation">AaBb × aabb</span>
</div>
<div class="legend">
<span class="legend-item"><i class="dot A"></i>A</span>
<span class="legend-item"><i class="dot a"></i>a</span>
<span class="legend-item"><i class="dot B"></i>B</span>
<span class="legend-item"><i class="dot b"></i>b</span>
<span class="legend-note">Four combinations are equiprobable</span>
</div>
<button class="gamete-row" type="button" data-case="separate" data-gamete="AB" aria-pressed="false">
<span class="gamete-key"><i class="dot A"></i><i class="dot B"></i>AB</span>
<span class="probability">25%</span>
<span class="bar"><i style="width:25%"></i></span>
<span class="outcome"><b>AaBb</b><small>A_B_</small></span>
</button>
<button class="gamete-row" type="button" data-case="separate" data-gamete="Ab" aria-pressed="false">
<span class="gamete-key"><i class="dot A"></i><i class="dot b"></i>Ab</span>
<span class="probability">25%</span>
<span class="bar"><i style="width:25%"></i></span>
<span class="outcome"><b>Aabb</b><small>A_bb</small></span>
</button>
<button class="gamete-row" type="button" data-case="separate" data-gamete="aB" aria-pressed="false">
<span class="gamete-key"><i class="dot a"></i><i class="dot B"></i>aB</span>
<span class="probability">25%</span>
<span class="bar"><i style="width:25%"></i></span>
<span class="outcome"><b>aaBb</b><small>aaB_</small></span>
</button>
<button class="gamete-row" type="button" data-case="separate" data-gamete="ab" aria-pressed="false">
<span class="gamete-key"><i class="dot a"></i><i class="dot b"></i>ab</span>
<span class="probability">25%</span>
<span class="bar"><i style="width:25%"></i></span>
<span class="outcome"><b>aabb</b><small>aabb</small></span>
</button>
<div class="card-summary">
<span class="summary-chip">25% for each allele combination</span>
<span>No parental/recombinant imbalance</span>
</div>
</div>
</section>
</main>
<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 renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setClearColor(0xf2f0e9, 1);
renderer.autoClear = false;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
const ALLELE = {
A: 0x2f6fed,
a: 0x7657d5,
B: 0xf4a514,
b: 0xe76b4d
};
const BODY = {
linkedA: 0x218b83,
linkedB: 0xd66c5c,
pairA: 0x537da5,
pairB: 0xb18854,
neutral: 0x8f9aa1
};
const DEFAULT_ORBIT = {
theta: 0.24,
phi: 1.30,
radius: 7.8
};
const orbit = { ...DEFAULT_ORBIT };
const target = new THREE.Vector3(0, 0.05, 0);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let motionEnabled = !reducedMotion;
let elapsed = 0;
const cases = [];
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
const projected = new THREE.Vector3();
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.58,
metalness: options.metalness ?? 0.02,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
}
function addLights(scene) {
scene.add(new THREE.HemisphereLight(0xffffff, 0x798591, 2.1));
const key = new THREE.DirectionalLight(0xffffff, 3.0);
key.position.set(4.2, 7.0, 5.5);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -4;
key.shadow.camera.right = 4;
key.shadow.camera.top = 4;
key.shadow.camera.bottom = -4;
key.shadow.camera.near = 1;
key.shadow.camera.far = 18;
key.shadow.bias = -0.0006;
scene.add(key);
const fill = new THREE.DirectionalLight(0xdde8ff, 1.1);
fill.position.set(-5, 2.5, 4);
scene.add(fill);
}
function addPedestal(scene, separatePairs = false) {
const baseMaterial = material(0xfaf9f5, {
roughness: 0.82,
metalness: 0
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.98, 2.12, 0.13, 64),
baseMaterial
);
base.position.y = -2.09;
base.receiveShadow = true;
scene.add(base);
const rimMaterial = material(0xd9ded9, {
roughness: 0.72,
metalness: 0.03
});
const outerRim = new THREE.Mesh(
new THREE.TorusGeometry(2.01, 0.025, 10, 72),
rimMaterial
);
outerRim.rotation.x = Math.PI / 2;
outerRim.position.y = -2.01;
scene.add(outerRim);
if (separatePairs) {
[-0.92, 0.92].forEach((x, index) => {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.022, 10, 40),
material(index === 0 ? BODY.pairA : BODY.pairB, {
roughness: 0.65
})
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, -2.0, 0);
scene.add(ring);
});
} else {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.95, 0.022, 10, 48),
material(BODY.linkedA, { roughness: 0.65 })
);
ring.rotation.x = Math.PI / 2;
ring.position.set(0, -2.0, 0);
scene.add(ring);
}
}
function addRod(parent, options) {
const {
x = 0,
y = 0,
z = 0,
length = 2.2,
radius = 0.115,
lowerColor = BODY.neutral,
upperColor = lowerColor,
beads = [],
tilt = 0,
rotationY = 0
} = options;
const group = new THREE.Group();
group.position.set(x, y, z);
group.rotation.z = tilt;
group.rotation.y = rotationY;
const lowerMaterial = material(lowerColor, { roughness: 0.52 });
const upperMaterial = material(upperColor, { roughness: 0.52 });
if (lowerColor === upperColor) {
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 24),
lowerMaterial
);
shaft.castShadow = true;
shaft.receiveShadow = true;
group.add(shaft);
} else {
const lowerShaft = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length * 0.5 + 0.015,
24
),
lowerMaterial
);
lowerShaft.position.y = -length * 0.25;
lowerShaft.castShadow = true;
group.add(lowerShaft);
const upperShaft = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length * 0.5 + 0.015,
24
),
upperMaterial
);
upperShaft.position.y = length * 0.25;
upperShaft.castShadow = true;
group.add(upperShaft);
const seam = new THREE.Mesh(
new THREE.TorusGeometry(radius * 1.03, 0.022, 10, 28),
material(0xf4a514, {
roughness: 0.45,
emissive: 0x6f4400,
emissiveIntensity: 0.18
})
);
seam.rotation.x = Math.PI / 2;
group.add(seam);
}
const capGeometry = new THREE.SphereGeometry(radius, 20, 14);
const lowerCap = new THREE.Mesh(capGeometry, lowerMaterial);
lowerCap.position.y = -length / 2;
lowerCap.castShadow = true;
group.add(lowerCap);
const upperCap = new THREE.Mesh(capGeometry, upperMaterial);
upperCap.position.y = length / 2;
upperCap.castShadow = true;
group.add(upperCap);
beads.forEach((bead) => {
const beadMaterial = material(bead.color, {
roughness: 0.36,
metalness: 0.02
});
const beadMesh = new THREE.Mesh(
new THREE.SphereGeometry(bead.radius ?? radius * 1.42, 24, 18),
beadMaterial
);
beadMesh.position.y = bead.offset;
beadMesh.castShadow = true;
group.add(beadMesh);
});
parent.add(group);
return group;
}
function annotation(caseObject, html, className, anchor) {
const element = document.createElement("div");
element.className = `annotation ${className}`;
element.innerHTML = html;
caseObject.stage.appendChild(element);
caseObject.annotations.push({
element,
anchor,
className
});
return element;
}
function createCase(kind) {
const stage = document.querySelector(
kind === "linked" ? "#stage-linked" : "#stage-separate"
);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 50);
addLights(scene);
addPedestal(scene, kind === "separate");
const caseObject = {
kind,
stage,
scene,
camera,
annotations: [],
gametes: new Map(),
selected: null,
crossover: null,
assortmentMarker: null
};
if (kind === "linked") {
buildLinkedCase(caseObject);
} else {
buildSeparateCase(caseObject);
}
cases.push(caseObject);
return caseObject;
}
function buildLinkedCase(caseObject) {
const { scene } = caseObject;
// Parent homologs: both loci are on the same chromosome.
addRod(scene, {
x: -0.33,
y: 0.92,
z: 0,
length: 2.35,
radius: 0.13,
lowerColor: BODY.linkedA,
upperColor: BODY.linkedA,
tilt: -0.035,
beads: [
{ offset: -0.67, color: ALLELE.A, radius: 0.18 },
{ offset: 0.67, color: ALLELE.B, radius: 0.18 }
]
});
addRod(scene, {
x: 0.33,
y: 0.92,
z: 0.08,
length: 2.35,
radius: 0.13,
lowerColor: BODY.linkedB,
upperColor: BODY.linkedB,
tilt: 0.035,
beads: [
{ offset: -0.67, color: ALLELE.a, radius: 0.18 },
{ offset: 0.67, color: ALLELE.b, radius: 0.18 }
]
});
const parentMarkerA = new THREE.Object3D();
parentMarkerA.position.set(-0.33, 2.28, 0);
scene.add(parentMarkerA);
const parentMarkerB = new THREE.Object3D();
parentMarkerB.position.set(0.33, 2.28, 0.08);
scene.add(parentMarkerB);
annotation(
caseObject,
'<i class="dot A"></i><i class="dot B"></i>A · B',
"gene-label",
parentMarkerA
);
annotation(
caseObject,
'<i class="dot a"></i><i class="dot b"></i>a · b',
"gene-label",
parentMarkerB
);
// Crossover symbol between the linked loci.
const crossover = new THREE.Group();
crossover.position.set(0, 0.92, 0.18);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.18, 0.035, 12, 36),
material(0xf4a514, {
roughness: 0.35,
emissive: 0xb36e00,
emissiveIntensity: 0.42
})
);
crossover.add(ring);
const center = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 18, 12),
material(0xffd367, {
roughness: 0.3,
emissive: 0xc47b00,
emissiveIntensity: 0.48
})
);
crossover.add(center);
scene.add(crossover);
caseObject.crossover = crossover;
const crossoverMarker = new THREE.Object3D();
crossoverMarker.position.set(0.42, 0.94, 0.22);
scene.add(crossoverMarker);
annotation(
caseObject,
"crossover interval",
"cue-label",
crossoverMarker
);
const data = [
{
id: "AB",
p: 35,
lower: BODY.linkedA,
upper: BODY.linkedA,
alleles: ["A", "B"]
},
{
id: "Ab",
p: 15,
lower: BODY.linkedA,
upper: BODY.linkedB,
alleles: ["A", "b"]
},
{
id: "aB",
p: 15,
lower: BODY.linkedB,
upper: BODY.linkedA,
alleles: ["a", "B"]
},
{
id: "ab",
p: 35,
lower: BODY.linkedB,
upper: BODY.linkedB,
alleles: ["a", "b"]
}
];
data.forEach((item, index) => {
const group = new THREE.Group();
const x = [-1.25, -0.42, 0.42, 1.25][index];
const y = -1.27;
group.position.set(x, y, 0);
group.userData.gamete = item.id;
group.userData.baseY = y;
group.userData.phase = index * 1.7;
group.userData.baseScale = item.p === 35 ? 1.08 : 0.86;
addRod(group, {
length: 1.02,
radius: 0.105,
lowerColor: item.lower,
upperColor: item.upper,
beads: [
{
offset: -0.27,
color: ALLELE[item.alleles[0]],
radius: 0.15
},
{
offset: 0.27,
color: ALLELE[item.alleles[1]],
radius: 0.15
}
]
});
scene.add(group);
caseObject.gametes.set(item.id, group);
const marker = new THREE.Object3D();
marker.position.set(0, 0.72, 0);
group.add(marker);
const dots = item.alleles
.map(
(allele) =>
`<i class="dot ${allele}"></i>`
)
.join("");
const label = annotation(
caseObject,
`${dots}${item.id}<b>${item.p}%</b>`,
"gamete-label",
marker
);
label.dataset.gamete = item.id;
label.tabIndex = 0;
label.setAttribute("role", "button");
label.setAttribute("aria-label", `${item.id}, ${item.p} percent`);
});
}
function buildSeparateCase(caseObject) {
const { scene } = caseObject;
// Pair 1 carries A/a.
addRod(scene, {
x: -0.95,
y: 0.92,
z: -0.17,
length: 2.2,
radius: 0.12,
lowerColor: BODY.pairA,
upperColor: BODY.pairA,
beads: [{ offset: 0, color: ALLELE.A, radius: 0.17 }]
});
addRod(scene, {
x: -0.95,
y: 0.92,
z: 0.17,
length: 2.2,
radius: 0.12,
lowerColor: BODY.pairA,
upperColor: BODY.pairA,
beads: [{ offset: 0, color: ALLELE.a, radius: 0.17 }]
});
// Pair 2 carries B/b on a different chromosome pair.
addRod(scene, {
x: 0.95,
y: 0.92,
z: -0.17,
length: 2.2,
radius: 0.12,
lowerColor: BODY.pairB,
upperColor: BODY.pairB,
beads: [{ offset: 0, color: ALLELE.B, radius: 0.17 }]
});
addRod(scene, {
x: 0.95,
y: 0.92,
z: 0.17,
length: 2.2,
radius: 0.12,
lowerColor: BODY.pairB,
upperColor: BODY.pairB,
beads: [{ offset: 0, color: ALLELE.b, radius: 0.17 }]
});
const markerA = new THREE.Object3D();
markerA.position.set(-0.95, 2.25, 0);
scene.add(markerA);
const markerB = new THREE.Object3D();
markerB.position.set(0.95, 2.25, 0);
scene.add(markerB);
annotation(
caseObject,
'<i class="dot A"></i> / <i class="dot a"></i>',
"gene-label",
markerA
);
annotation(
caseObject,
'<i class="dot B"></i> / <i class="dot b"></i>',
"gene-label",
markerB
);
const assortmentMarker = new THREE.Group();
assortmentMarker.position.set(0, 0.91, 0.2);
[-0.08, 0, 0.08].forEach((offset, index) => {
const dot = new THREE.Mesh(
new THREE.SphereGeometry(index === 1 ? 0.07 : 0.045, 16, 12),
material(index === 1 ? 0xf4a514 : 0xb8bebd, {
roughness: 0.45
})
);
dot.position.x = offset;
assortmentMarker.add(dot);
});
scene.add(assortmentMarker);
caseObject.assortmentMarker = assortmentMarker;
const labelMarker = new THREE.Object3D();
labelMarker.position.set(0, 0.68, 0.28);
scene.add(labelMarker);
annotation(
caseObject,
"independent assortment",
"cue-label",
labelMarker
);
const data = [
{ id: "AB", p: 25, alleles: ["A", "B"] },
{ id: "Ab", p: 25, alleles: ["A", "b"] },
{ id: "aB", p: 25, alleles: ["a", "B"] },
{ id: "ab", p: 25, alleles: ["a", "b"] }
];
data.forEach((item, index) => {
const group = new THREE.Group();
const x = [-1.25, -0.42, 0.42, 1.25][index];
const y = -1.3;
group.position.set(x, y, 0);
group.userData.gamete = item.id;
group.userData.baseY = y;
group.userData.phase = index * 1.7;
group.userData.baseScale = 1;
// A gamete contains two physically separate chromosomes.
addRod(group, {
x: -0.13,
y: 0,
z: 0.02,
length: 0.9,
radius: 0.082,
lowerColor: BODY.pairA,
upperColor: BODY.pairA,
tilt: -0.055,
beads: [
{
offset: 0,
color: ALLELE[item.alleles[0]],
radius: 0.125
}
]
});
addRod(group, {
x: 0.13,
y: 0,
z: -0.02,
length: 0.9,
radius: 0.082,
lowerColor: BODY.pairB,
upperColor: BODY.pairB,
tilt: 0.055,
beads: [
{
offset: 0,
color: ALLELE[item.alleles[1]],
radius: 0.125
}
]
});
scene.add(group);
caseObject.gametes.set(item.id, group);
const marker = new THREE.Object3D();
marker.position.set(0, 0.66, 0);
group.add(marker);
const dots = item.alleles
.map((allele) => `<i class="dot ${allele}"></i>`)
.join("");
const label = annotation(
caseObject,
`${dots}${item.id}<b>${item.p}%</b>`,
"gamete-label",
marker
);
label.dataset.gamete = item.id;
label.tabIndex = 0;
label.setAttribute("role", "button");
label.setAttribute("aria-label", `${item.id}, ${item.p} percent`);
});
}
const linkedCase = createCase("linked");
const separateCase = createCase("separate");
function selectGamete(caseObject, id) {
caseObject.selected = caseObject.selected === id ? null : id;
caseObject.stage.querySelectorAll(".gamete-label").forEach((element) => {
element.classList.toggle(
"active",
element.dataset.gamete === caseObject.selected
);
});
document
.querySelectorAll(`.gamete-row[data-case="${caseObject.kind}"]`)
.forEach((row) => {
const active = row.dataset.gamete === caseObject.selected;
row.classList.toggle("active", active);
row.setAttribute("aria-pressed", String(active));
});
}
document.querySelectorAll(".gamete-row").forEach((row) => {
row.addEventListener("click", () => {
const caseObject = cases.find(
(entry) => entry.kind === row.dataset.case
);
if (caseObject) {
selectGamete(caseObject, row.dataset.gamete);
}
});
});
cases.forEach((caseObject) => {
caseObject.stage.querySelectorAll(".gamete-label").forEach((label) => {
label.addEventListener("click", () => {
selectGamete(caseObject, label.dataset.gamete);
});
label.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
selectGamete(caseObject, label.dataset.gamete);
}
});
});
});
function updateCamera(caseObject) {
const width = Math.max(1, caseObject.stage.clientWidth);
const height = Math.max(1, caseObject.stage.clientHeight);
const aspect = width / height;
caseObject.camera.aspect = aspect;
const aspectCompensation = Math.max(1, 0.95 / aspect);
const radius = orbit.radius * aspectCompensation;
const sinPhi = Math.sin(orbit.phi);
caseObject.camera.position.set(
target.x + radius * sinPhi * Math.sin(orbit.theta),
target.y + radius * Math.cos(orbit.phi),
target.z + radius * sinPhi * Math.cos(orbit.theta)
);
caseObject.camera.lookAt(target);
caseObject.camera.updateProjectionMatrix();
caseObject.camera.updateMatrixWorld();
}
function updateAnnotations(caseObject) {
const width = caseObject.stage.clientWidth;
const height = caseObject.stage.clientHeight;
caseObject.annotations.forEach(({ element, anchor }) => {
anchor.getWorldPosition(projected);
projected.project(caseObject.camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible = projected.z > -1 && projected.z < 1;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.visibility = visible ? "visible" : "hidden";
});
}
function updateAnimations(caseObject) {
caseObject.gametes.forEach((group, id) => {
const selected = caseObject.selected === id;
const targetScale =
group.userData.baseScale * (selected ? 1.14 : 1);
const currentScale = group.scale.x || group.userData.baseScale;
const nextScale = THREE.MathUtils.lerp(
currentScale,
targetScale,
0.12
);
group.scale.setScalar(nextScale);
if (motionEnabled) {
group.position.y =
group.userData.baseY +
Math.sin(elapsed * 1.15 + group.userData.phase) * 0.025;
group.rotation.y =
Math.sin(elapsed * 0.72 + group.userData.phase) * 0.035;
}
});
if (caseObject.crossover && motionEnabled) {
const pulse = 1 + Math.sin(elapsed * 2.4) * 0.09;
caseObject.crossover.scale.setScalar(pulse);
caseObject.crossover.rotation.z = elapsed * 0.25;
}
if (caseObject.assortmentMarker && motionEnabled) {
caseObject.assortmentMarker.rotation.y =
Math.sin(elapsed * 0.9) * 0.12;
}
}
function renderCase(caseObject) {
const rect = caseObject.stage.getBoundingClientRect();
if (
rect.bottom < 0 ||
rect.top > window.innerHeight ||
rect.width <= 0 ||
rect.height <= 0
) {
return;
}
const x = rect.left;
const y = window.innerHeight - rect.bottom;
renderer.setViewport(x, y, rect.width, rect.height);
renderer.setScissor(x, y, rect.width, rect.height);
renderer.setScissorTest(true);
renderer.clear(true, true, true);
updateCamera(caseObject);
updateAnimations(caseObject);
updateAnnotations(caseObject);
renderer.render(caseObject.scene, caseObject.camera);
}
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
}
window.addEventListener("resize", resize);
// Camera controls are implemented directly, without an addon.
const pointers = new Map();
let dragDistance = 0;
let pinchDistance = 0;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
dragDistance = 0;
canvas.classList.add("dragging");
if (pointers.size === 2) {
const values = [...pointers.values()];
pinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
dragDistance += Math.hypot(dx, dy);
if (pointers.size >= 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (pinchDistance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (pinchDistance / Math.max(distance, 1)),
6.2,
10.8
);
}
pinchDistance = distance;
} else {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.0045,
0.92,
1.48
);
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
pinchDistance = 0;
}
if (pointers.size === 0) {
canvas.classList.remove("dragging");
if (dragDistance < 5) {
pickGamete(event.clientX, event.clientY);
}
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.006,
6.2,
10.8
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
Object.assign(orbit, DEFAULT_ORBIT);
});
function pickGamete(clientX, clientY) {
for (const caseObject of cases) {
const rect = caseObject.stage.getBoundingClientRect();
if (
clientX < rect.left ||
clientX > rect.right ||
clientY < rect.top ||
clientY > rect.bottom
) {
continue;
}
pointerNDC.x =
((clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y =
-((clientY - rect.top) / rect.height) * 2 + 1;
updateCamera(caseObject);
raycaster.setFromCamera(pointerNDC, caseObject.camera);
const hits = raycaster.intersectObjects(
caseObject.scene.children,
true
);
if (hits.length) {
let object = hits[0].object;
while (object && !object.userData.gamete) {
object = object.parent;
}
if (object?.userData.gamete) {
selectGamete(caseObject, object.userData.gamete);
}
}
break;
}
}
const motionButton = document.querySelector("#motion-toggle");
function updateMotionButton() {
motionButton.textContent = motionEnabled
? "Pause motion"
: "Resume motion";
motionButton.setAttribute("aria-pressed", String(!motionEnabled));
}
motionButton.addEventListener("click", () => {
motionEnabled = !motionEnabled;
updateMotionButton();
});
updateMotionButton();
document.querySelector("#reset-view").addEventListener("click", () => {
Object.assign(orbit, DEFAULT_ORBIT);
});
let lastTime = performance.now();
function frame(now) {
const delta = Math.min(0.05, (now - lastTime) / 1000);
lastTime = now;
if (motionEnabled) {
elapsed += delta;
}
renderer.setScissorTest(false);
renderer.setViewport(0, 0, window.innerWidth, window.innerHeight);
renderer.clear(true, true, true);
cases.forEach(renderCase);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
```