Prompt specification
Build or customize the Three.js 3D scene "Independent assortment combination carousel" 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: Independent assortment combination carousel
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/independent-assortment-combination-carousel-x0954
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows sexual reproduction and genetic variation. Animate alternative orientations of two homologous chromosome pairs before separation; show resulting gamete combinations under a simplified model with explicitly stated chromosome count. Stylized solid forms.
Working Three.js Starter (independent-assortment-combination-carousel-x0954):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Meiosis, Fertilization & Genetic Variation</title>
<style>
:root {
--bg: #07111f;
--panel: rgba(11, 24, 42, 0.86);
--panel-solid: #0b182a;
--border: rgba(149, 184, 222, 0.17);
--text: #edf6ff;
--muted: #9bb0c9;
--cyan: #48dfcf;
--coral: #ff806f;
--gold: #ffd166;
--violet: #a78bfa;
--blue: #7cc8ff;
--radius: 16px;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button {
font: inherit;
}
#app {
height: 100dvh;
display: grid;
grid-template-rows: 78px minmax(350px, 1fr) 236px;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 14px 24px;
border-bottom: 1px solid var(--border);
background: linear-gradient(90deg, #091525, #0b1a2d 55%, #091525);
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.brand-icon {
width: 42px;
height: 42px;
flex: 0 0 auto;
border-radius: 13px;
display: grid;
place-items: center;
background: linear-gradient(135deg, rgba(72, 223, 207, 0.2), rgba(255, 128, 111, 0.2));
border: 1px solid rgba(124, 200, 255, 0.26);
box-shadow: inset 0 0 18px rgba(124, 200, 255, 0.08);
}
.brand-icon svg {
width: 27px;
height: 27px;
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 25px);
line-height: 1.12;
letter-spacing: -0.025em;
}
.subtitle {
margin-top: 4px;
color: var(--muted);
font-size: 12px;
}
.model-badges {
display: flex;
justify-content: flex-end;
flex-wrap: wrap;
gap: 8px;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 32px;
padding: 7px 11px;
border-radius: 999px;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.035);
color: #c8d8e9;
font-size: 12px;
white-space: nowrap;
}
.badge strong {
color: white;
}
.badge-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
#stage {
position: relative;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, rgba(31, 66, 104, 0.28), transparent 47%),
#07111f;
}
#webgl {
position: absolute;
inset: 0;
}
#webgl canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#webgl canvas:active {
cursor: grabbing;
}
.glass-card {
background: var(--panel);
border: 1px solid var(--border);
border-radius: var(--radius);
box-shadow: 0 16px 44px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(13px);
}
.status-card {
position: absolute;
top: 18px;
left: 18px;
width: min(350px, calc(100% - 36px));
padding: 16px 17px;
pointer-events: none;
z-index: 3;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.status-card h2 {
margin: 7px 0 6px;
font-size: 19px;
letter-spacing: -0.02em;
}
.status-card p {
margin: 0;
color: #b8c8da;
font-size: 13px;
line-height: 1.48;
}
.control-card {
position: absolute;
top: 18px;
right: 18px;
width: min(335px, calc(100% - 36px));
padding: 14px;
z-index: 4;
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
color: #c5d5e6;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.control-title span:last-child {
color: var(--muted);
font-size: 10px;
font-weight: 600;
letter-spacing: 0;
text-transform: none;
}
.orientation-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin-bottom: 11px;
}
.orientation-button {
min-height: 47px;
padding: 7px 4px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
color: #b8c9dc;
cursor: pointer;
transition: 160ms ease;
}
.orientation-button:hover {
border-color: rgba(124, 200, 255, 0.48);
background: rgba(124, 200, 255, 0.08);
}
.orientation-button.active {
color: white;
border-color: rgba(72, 223, 207, 0.72);
background: rgba(72, 223, 207, 0.13);
box-shadow: inset 0 0 18px rgba(72, 223, 207, 0.06);
}
.orientation-button strong,
.orientation-button small {
display: block;
}
.orientation-button strong {
font-size: 14px;
}
.orientation-button small {
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.button-row {
display: flex;
gap: 7px;
}
.action-button {
flex: 1;
min-height: 39px;
padding: 8px 11px;
border-radius: 10px;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.045);
color: #dce9f6;
cursor: pointer;
font-size: 12px;
font-weight: 700;
transition: 160ms ease;
}
.action-button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(124, 200, 255, 0.5);
background: rgba(124, 200, 255, 0.09);
}
.action-button.primary {
border-color: rgba(72, 223, 207, 0.5);
color: #071a1b;
background: linear-gradient(135deg, #64ead9, #38bfb2);
}
.action-button:disabled {
opacity: 0.4;
cursor: default;
}
.legend-card {
position: absolute;
left: 18px;
bottom: 17px;
padding: 11px 13px;
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 10px 15px;
max-width: calc(100% - 36px);
color: #b9c9da;
font-size: 11px;
pointer-events: none;
z-index: 3;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.legend-shape {
display: inline-block;
width: 11px;
height: 11px;
border-radius: 50%;
}
.legend-shape.coral {
background: var(--coral);
}
.legend-shape.cyan {
background: var(--cyan);
}
.legend-shape.gold {
background: var(--gold);
}
.legend-shape.violet {
background: var(--violet);
border-radius: 2px;
}
.camera-hint {
position: absolute;
right: 18px;
bottom: 17px;
padding: 8px 11px;
color: #849bb4;
font-size: 10px;
pointer-events: none;
z-index: 3;
}
#lower {
display: grid;
grid-template-columns: 1fr 0.9fr 1.4fr;
gap: 1px;
border-top: 1px solid var(--border);
background: var(--border);
overflow: hidden;
z-index: 5;
}
.lower-section {
padding: 14px 18px;
background: var(--panel-solid);
min-width: 0;
}
.lower-section h3 {
margin: 0 0 8px;
font-size: 12px;
letter-spacing: 0.045em;
text-transform: uppercase;
color: #dce9f7;
}
.lower-section p {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.chip-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.result-chip,
.possible-chip {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 34px;
padding: 6px 9px;
border-radius: 9px;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.035);
}
.result-chip {
min-width: 76px;
}
.result-chip b {
color: var(--gold);
font-size: 15px;
}
.result-chip small {
color: #8299b1;
font-size: 9px;
}
.possible-chip {
color: #cbd9e8;
font-size: 12px;
font-weight: 800;
}
.possible-chip:nth-child(1),
.possible-chip:nth-child(4) {
border-color: rgba(255, 209, 102, 0.26);
}
.possible-chip:nth-child(2),
.possible-chip:nth-child(3) {
border-color: rgba(167, 139, 250, 0.27);
}
.parent-choice-row {
display: grid;
grid-template-columns: 66px 1fr;
align-items: center;
gap: 9px;
margin-bottom: 7px;
}
.parent-choice-row > span {
color: #a9bbcf;
font-size: 11px;
font-weight: 700;
}
.gamete-choice {
min-width: 47px;
min-height: 30px;
padding: 5px 7px;
border-radius: 8px;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.03);
color: #aebfd1;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 140ms ease;
}
.gamete-choice:hover {
border-color: rgba(124, 200, 255, 0.46);
}
.gamete-choice.active {
color: white;
border-color: rgba(124, 200, 255, 0.72);
background: rgba(124, 200, 255, 0.13);
}
.fertilize-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 8px;
}
.fertilize-row .action-button {
flex: 0 0 auto;
min-width: 170px;
}
#zygoteResult {
color: #aebfd1;
font-size: 11px;
line-height: 1.35;
}
#zygoteResult strong {
display: block;
margin-bottom: 3px;
color: white;
font-size: 12px;
}
@media (max-width: 980px) {
body {
overflow: auto;
}
#app {
height: auto;
grid-template-rows: auto 650px auto;
}
header {
align-items: flex-start;
flex-direction: column;
}
.model-badges {
justify-content: flex-start;
}
#lower {
grid-template-columns: 1fr;
overflow: visible;
}
.lower-section {
min-height: 150px;
}
}
@media (max-width: 620px) {
#app {
grid-template-rows: auto 720px auto;
}
header {
padding: 14px;
}
.status-card {
top: 12px;
left: 12px;
width: calc(100% - 24px);
}
.control-card {
top: auto;
bottom: 58px;
left: 12px;
width: calc(100% - 24px);
}
.legend-card {
left: 12px;
bottom: 12px;
}
.camera-hint {
display: none;
}
.orientation-grid {
grid-template-columns: repeat(2, 1fr);
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-icon" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M8 4c7 0 8 8 8 12s-1 12-8 12" stroke="#48dfcf" stroke-width="3" stroke-linecap="round"/>
<path d="M24 4c-7 0-8 8-8 12s1 12 8 12" stroke="#ff806f" stroke-width="3" stroke-linecap="round"/>
<path d="M10 16h12" stroke="#ffd166" stroke-width="2.4" stroke-linecap="round"/>
</svg>
</div>
<div>
<h1>Meiosis, Fertilization & Genetic Variation</h1>
<div class="subtitle">A manipulable 3D model of sexual reproduction</div>
</div>
</div>
<div class="model-badges">
<div class="badge"><span class="badge-dot"></span><strong>2n = 4</strong> · 2 homologous pairs</div>
<div class="badge">Gamete <strong>n = 2</strong></div>
<div class="badge">Crossing over <strong>omitted</strong></div>
</div>
</header>
<main id="stage">
<div id="webgl"></div>
<section class="status-card glass-card" aria-live="polite">
<div class="eyebrow" id="stageKicker">PROPHASE I</div>
<h2 id="stageTitle">A diploid parent cell</h2>
<p id="stageCopy">
Each homolog has been replicated into two sister chromatids.
</p>
</section>
<section class="control-card glass-card">
<div class="control-title">
<span>Metaphase orientation</span>
<span>alleles facing the left pole</span>
</div>
<div class="orientation-grid" id="orientationButtons"></div>
<div class="button-row">
<button class="action-button primary" id="runButton" type="button">Run meiosis</button>
<button class="action-button" id="pauseButton" type="button" disabled>Pause</button>
<button class="action-button" id="resetButton" type="button">Reset</button>
</div>
</section>
<section class="legend-card glass-card">
<span class="legend-item"><span class="legend-shape coral"></span>Long pair</span>
<span class="legend-item"><span class="legend-shape cyan"></span>Short pair</span>
<span class="legend-item"><span class="legend-shape gold"></span>A / B allele</span>
<span class="legend-item"><span class="legend-shape violet"></span>a / b allele</span>
</section>
<div class="camera-hint glass-card">Drag to orbit · Wheel to zoom · Double-click to reset view</div>
</main>
<section id="lower">
<article class="lower-section">
<h3>Products from this meiosis</h3>
<div class="chip-row" id="runResults"></div>
<p>
One meiosis creates four haploid cells. Without crossing over, each
reciprocal gamete type normally appears twice.
</p>
</article>
<article class="lower-section">
<h3>Possible gametes across many meioses</h3>
<div class="chip-row" id="possibleGametes"></div>
<p>
Independent assortment of the two chromosome pairs can produce all
four combinations: AB, Ab, aB and ab.
</p>
</article>
<article class="lower-section">
<h3>Fertilization between two AaBb parents</h3>
<div class="parent-choice-row">
<span>Parent 1</span>
<div class="chip-row" id="parent1Choices"></div>
</div>
<div class="parent-choice-row">
<span>Parent 2</span>
<div class="chip-row" id="parent2Choices"></div>
</div>
<div class="fertilize-row">
<button class="action-button primary" id="fertilizeButton" type="button">
Combine selected gametes
</button>
<div id="zygoteResult">Select one gamete from each parent.</div>
</div>
</article>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Application state
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const webgl = document.getElementById("webgl");
const stageKicker = document.getElementById("stageKicker");
const stageTitle = document.getElementById("stageTitle");
const stageCopy = document.getElementById("stageCopy");
const orientationButtons = document.getElementById("orientationButtons");
const runButton = document.getElementById("runButton");
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const fertilizeButton = document.getElementById("fertilizeButton");
const runResults = document.getElementById("runResults");
const possibleGametes = document.getElementById("possibleGametes");
const parent1Choices = document.getElementById("parent1Choices");
const parent2Choices = document.getElementById("parent2Choices");
const zygoteResult = document.getElementById("zygoteResult");
const combinations = ["AB", "Ab", "aB", "ab"];
// Each orientation names the allele of pair 1 and pair 2 that faces left.
const orientations = [
{ id: "AB", pair1: "A", pair2: "B", label: "A + B left" },
{ id: "Ab", pair1: "A", pair2: "b", label: "A + b left" },
{ id: "aB", pair1: "a", pair2: "B", label: "a + B left" },
{ id: "ab", pair1: "a", pair2: "b", label: "a + b left" }
];
let selectedOrientation = 0;
let selectedGametes = { p1: "AB", p2: "Ab" };
let mode = "meiosis";
let running = false;
let elapsed = 0;
const duration = 15;
let lastFrame = performance.now();
let lastStageKey = "";
let fusionStart = 0;
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 15, 27);
const camera = new THREE.PerspectiveCamera(
42,
stage.clientWidth / stage.clientHeight,
0.1,
60
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(stage.clientWidth, stage.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
webgl.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xbfe7ff, 0x182033, 2.1));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x48dfcf, 26, 18, 2);
rimLight.position.set(-5, 2, 4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff806f, 22, 18, 2);
warmLight.position.set(5, -1, -4);
scene.add(warmLight);
// A low, solid platform gives the chromosomes spatial context.
const floor = new THREE.Mesh(
new THREE.CircleGeometry(8.5, 80),
new THREE.MeshStandardMaterial({
color: 0x0a1727,
roughness: 0.95,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.55;
floor.receiveShadow = true;
scene.add(floor);
const chromosomeRoot = new THREE.Group();
const spindleGroup = new THREE.Group();
const gameteGroup = new THREE.Group();
const fusionGroup = new THREE.Group();
scene.add(chromosomeRoot, spindleGroup, gameteGroup, fusionGroup);
const parentMembrane = new THREE.Mesh(
new THREE.SphereGeometry(2.55, 48, 32),
new THREE.MeshStandardMaterial({
color: 0x3d8fff,
transparent: true,
opacity: 0.10,
roughness: 0.45,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
})
);
parentMembrane.position.y = 0.15;
parentMembrane.scale.set(1.16, 1.04, 0.96);
scene.add(parentMembrane);
const parentLabel = makeTextSprite("Diploid parent • 2n = 4", 2.7, 0.56, "#7cc8ff");
parentLabel.position.set(0, -2.25, 0.3);
scene.add(parentLabel);
const chromosomeRecords = [];
// -------------------------------------------------------------------------
// Text sprites
// -------------------------------------------------------------------------
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.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function drawLabel(sprite, text, accent = "#7cc8ff") {
const { canvas, ctx, texture } = sprite.userData.label;
const w = canvas.width;
const h = canvas.height;
ctx.clearRect(0, 0, w, h);
roundedRect(ctx, 12, 20, w - 24, h - 40, 34);
ctx.fillStyle = "rgba(7, 17, 31, 0.90)";
ctx.fill();
ctx.strokeStyle = accent;
ctx.globalAlpha = 0.7;
ctx.lineWidth = 5;
ctx.stroke();
ctx.globalAlpha = 1;
let fontSize = 88;
if (text.length > 17) fontSize = 48;
else if (text.length > 11) fontSize = 60;
else if (text.length > 7) fontSize = 70;
ctx.font = `800 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f3f8ff";
ctx.fillText(text, w / 2, h / 2 + 2);
texture.needsUpdate = true;
}
function makeTextSprite(text, width = 1.4, height = 0.55, accent = "#7cc8ff") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 256;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
sprite.userData.label = { canvas, ctx, texture };
drawLabel(sprite, text, accent);
return sprite;
}
// -------------------------------------------------------------------------
// Stylized solid chromosomes
// -------------------------------------------------------------------------
const pairColors = [0xff806f, 0x48dfcf];
const uppercaseColor = 0xffd166;
const lowercaseColor = 0xa78bfa;
function makeChromatid(pair, allele, sourceColor = null) {
const group = new THREE.Group();
const isLong = pair === 1;
const shaftLength = isLong ? 1.62 : 1.02;
const radius = isLong ? 0.17 : 0.145;
const rod = new THREE.Mesh(
new THREE.CapsuleGeometry(radius, shaftLength, 8, 18),
new THREE.MeshStandardMaterial({
color: pairColors[pair - 1],
roughness: 0.38,
metalness: 0.04
})
);
rod.castShadow = true;
rod.receiveShadow = true;
group.add(rod);
// A raised centromere band makes each chromatid easy to read in 3D.
const centromere = new THREE.Mesh(
new THREE.TorusGeometry(radius * 1.08, 0.038, 8, 20),
new THREE.MeshStandardMaterial({
color: 0x102238,
roughness: 0.5,
metalness: 0.08
})
);
centromere.rotation.x = Math.PI / 2;
group.add(centromere);
const markerY = isLong ? 0.43 : 0.28;
const isUppercase = allele === allele.toUpperCase();
const markerMaterial = new THREE.MeshStandardMaterial({
color: isUppercase ? uppercaseColor : lowercaseColor,
roughness: 0.3,
metalness: 0.12
});
// Spheres encode uppercase alleles; small cubes encode lowercase alleles.
const marker = isUppercase
? new THREE.Mesh(new THREE.SphereGeometry(0.115, 16, 12), markerMaterial)
: new THREE.Mesh(new THREE.BoxGeometry(0.19, 0.19, 0.19), markerMaterial);
marker.position.set(0, markerY, radius + 0.025);
marker.castShadow = true;
group.add(marker);
if (sourceColor !== null) {
const sourceFoot = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 14, 10),
new THREE.MeshStandardMaterial({
color: sourceColor,
roughness: 0.35,
metalness: 0.1
})
);
sourceFoot.position.set(0, -shaftLength / 2 - 0.13, 0.02);
group.add(sourceFoot);
}
return group;
}
function addInitialChromosomes() {
const specifications = [
{
pair: 1,
allele: "A",
center: new THREE.Vector3(-1.03, 0.33, -0.57),
rotation: 0.16
},
{
pair: 1,
allele: "a",
center: new THREE.Vector3(0.97, -0.22, -0.57),
rotation: -0.13
},
{
pair: 2,
allele: "B",
center: new THREE.Vector3(-0.84, -0.79, 0.60),
rotation: 0.22
},
{
pair: 2,
allele: "b",
center: new THREE.Vector3(0.83, 0.66, 0.60),
rotation: -0.19
}
];
for (const specification of specifications) {
for (let sister = 0; sister < 2; sister++) {
const group = makeChromatid(specification.pair, specification.allele);
chromosomeRoot.add(group);
const initial = specification.center.clone();
initial.z += sister === 0 ? -0.135 : 0.135;
chromosomeRecords.push({
group,
pair: specification.pair,
allele: specification.allele,
sister,
initial,
initialRotation: specification.rotation
});
}
}
}
addInitialChromosomes();
// -------------------------------------------------------------------------
// Spindle and gamete membranes
// -------------------------------------------------------------------------
function cylinderBetween(start, end, radius, material) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const cylinder = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 8),
material
);
cylinder.position.copy(start).add(end).multiplyScalar(0.5);
cylinder.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return cylinder;
}
function buildSpindle() {
const fiberMaterial = new THREE.MeshStandardMaterial({
color: 0x8fa7ff,
transparent: true,
opacity: 0.24,
roughness: 0.8,
depthWrite: false
});
const poleMaterial = new THREE.MeshStandardMaterial({
color: 0xb7c9ff,
roughness: 0.3,
metalness: 0.15
});
const leftPole = new THREE.Vector3(-3.48, 0, 0);
const rightPole = new THREE.Vector3(3.48, 0, 0);
const leftSphere = new THREE.Mesh(
new THREE.SphereGeometry(0.14, 20, 14),
poleMaterial
);
leftSphere.position.copy(leftPole);
const rightSphere = leftSphere.clone();
rightSphere.position.copy(rightPole);
spindleGroup.add(leftSphere, rightSphere);
for (const z of [-0.62, 0.62]) {
for (const side of [-1, 1]) {
const target = new THREE.Vector3(side * 0.53, 0, z);
spindleGroup.add(
cylinderBetween(leftPole, target, 0.017, fiberMaterial),
cylinderBetween(rightPole, target, 0.017, fiberMaterial)
);
}
}
spindleGroup.add(
cylinderBetween(leftPole, rightPole, 0.014, fiberMaterial)
);
spindleGroup.visible = false;
}
buildSpindle();
const gameteSlots = [-3.6, -1.2, 1.2, 3.6];
const gameteBubbles = [];
const gameteLabels = [];
for (let i = 0; i < gameteSlots.length; i++) {
const material = new THREE.MeshStandardMaterial({
color: 0x4b9dff,
transparent: true,
opacity: 0,
roughness: 0.42,
depthWrite: false,
side: THREE.DoubleSide
});
const bubble = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 24),
material
);
bubble.position.set(gameteSlots[i], 0.18, 0);
bubble.scale.set(0.82, 1.55, 0.72);
gameteGroup.add(bubble);
gameteBubbles.push(bubble);
const label = makeTextSprite("AB", 1.05, 0.42, "#ffd166");
label.position.set(gameteSlots[i], -1.83, 0.25);
gameteGroup.add(label);
gameteLabels.push(label);
}
gameteGroup.visible = false;
// -------------------------------------------------------------------------
// Direct camera controls — no Three.js add-ons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
let cameraYaw = 0.70;
let cameraPitch = 0.94;
let cameraRadius = 12.4;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updateCamera() {
const horizontal = Math.sin(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + Math.cos(cameraPitch) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0.70;
cameraPitch = 0.94;
cameraRadius = 12.4;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(cameraPitch + dy * 0.005, 0.53, 1.38);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
7.5,
18
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
updateCamera();
// -------------------------------------------------------------------------
// Meiosis positioning
// -------------------------------------------------------------------------
function smoothstep(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function sideFor(record, orientation) {
const leftAllele = record.pair === 1
? orientation.pair1
: orientation.pair2;
return record.allele === leftAllele ? -1 : 1;
}
function metaphasePosition(record, orientation) {
const side = sideFor(record, orientation);
const pairZ = record.pair === 1 ? -0.62 : 0.62;
const sisterOffset = record.sister === 0 ? -0.135 : 0.135;
return new THREE.Vector3(
side * 0.53,
0,
pairZ + sisterOffset
);
}
function meiosisIPosition(record, orientation) {
const side = sideFor(record, orientation);
const pairZ = record.pair === 1 ? -0.62 : 0.62;
const sisterOffset = record.sister === 0 ? -0.135 : 0.135;
return new THREE.Vector3(
side * 3.25,
0,
pairZ + sisterOffset
);
}
function finalPosition(record, orientation) {
const side = sideFor(record, orientation);
const slot = side < 0
? record.sister
: 2 + record.sister;
return new THREE.Vector3(
gameteSlots[slot],
record.pair === 1 ? 0.72 : -0.63,
0
);
}
function resultsFor(orientation) {
const otherPair1 = orientation.pair1 === "A" ? "a" : "A";
const otherPair2 = orientation.pair2 === "B" ? "b" : "B";
const left = orientation.pair1 + orientation.pair2;
const right = otherPair1 + otherPair2;
return [left, left, right, right];
}
function updateMeiosis() {
const t = elapsed / duration;
const selected = orientations[selectedOrientation];
for (const record of chromosomeRecords) {
const meta0 = metaphasePosition(record, orientations[0]);
const meta1 = metaphasePosition(record, orientations[1]);
const meta2 = metaphasePosition(record, orientations[2]);
const meta3 = metaphasePosition(record, orientations[3]);
const selectedMeta = metaphasePosition(record, selected);
const meiosisI = meiosisIPosition(record, selected);
const final = finalPosition(record, selected);
let position;
if (t < 0.08) {
position = record.initial;
} else if (t < 0.16) {
position = new THREE.Vector3().lerpVectors(
record.initial,
meta0,
smoothstep((t - 0.08) / 0.08)
);
} else if (t < 0.24) {
position = new THREE.Vector3().lerpVectors(
meta0,
meta1,
smoothstep((t - 0.16) / 0.08)
);
} else if (t < 0.32) {
position = new THREE.Vector3().lerpVectors(
meta1,
meta2,
smoothstep((t - 0.24) / 0.08)
);
} else if (t < 0.40) {
position = new THREE.Vector3().lerpVectors(
meta2,
meta3,
smoothstep((t - 0.32) / 0.08)
);
} else if (t < 0.46) {
position = new THREE.Vector3().lerpVectors(
meta3,
selectedMeta,
smoothstep((t - 0.40) / 0.06)
);
} else if (t < 0.64) {
position = new THREE.Vector3().lerpVectors(
selectedMeta,
meiosisI,
smoothstep((t - 0.46) / 0.18)
);
} else if (t < 0.85) {
position = new THREE.Vector3().lerpVectors(
meiosisI,
final,
smoothstep((t - 0.64) / 0.21)
);
} else {
position = final;
}
record.group.position.copy(position);
const straighten = smoothstep((t - 0.08) / 0.38);
record.group.rotation.z = record.initialRotation * (1 - straighten);
record.group.rotation.y = record.initialRotation * 0.45 * (1 - straighten);
}
// Parent-cell membrane fades as the single cell divides.
let membraneOpacity = 0.10;
if (t >= 0.46) {
membraneOpacity = THREE.MathUtils.lerp(
0.10,
0,
smoothstep((t - 0.46) / 0.22)
);
}
parentMembrane.material.opacity = membraneOpacity;
parentMembrane.visible = membraneOpacity > 0.005;
parentLabel.material.opacity = t < 0.35 ? 1 : 0;
// Spindle is most visible during orientation and homolog separation.
spindleGroup.visible = t > 0.075 && t < 0.66;
spindleGroup.traverse(object => {
if (object.material && object.material.transparent) {
object.material.opacity =
0.24 * (1 - smoothstep((t - 0.56) / 0.10));
}
});
// Four separate haploid cell membranes appear during meiosis II.
const bubbleReveal = smoothstep((t - 0.72) / 0.13);
const labelReveal = smoothstep((t - 0.83) / 0.07);
gameteGroup.visible = t > 0.72;
for (const bubble of gameteBubbles) {
bubble.material.opacity = 0.115 * bubbleReveal;
}
for (const label of gameteLabels) {
label.material.opacity = labelReveal;
}
updateStageStatus(t);
}
// -------------------------------------------------------------------------
// Fertilization scene
// -------------------------------------------------------------------------
function clearGroup(group) {
group.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
for (const material of materials) {
if (material.map) material.map.dispose();
material.dispose();
}
}
});
group.clear();
}
function buildFusionScene() {
clearGroup(fusionGroup);
const membrane = new THREE.Mesh(
new THREE.SphereGeometry(2.45, 48, 32),
new THREE.MeshStandardMaterial({
color: 0x54a8ff,
transparent: true,
opacity: 0.12,
roughness: 0.45,
depthWrite: false,
side: THREE.DoubleSide
})
);
membrane.position.y = 0.12;
membrane.scale.set(1.15, 1.02, 0.96);
fusionGroup.add(membrane);
const parentColors = [0x7cc8ff, 0xffc857];
for (let pair = 1; pair <= 2; pair++) {
const alleles = [
selectedGametes.p1[pair - 1],
selectedGametes.p2[pair - 1]
];
for (let source = 0; source < 2; source++) {
const chromatid = makeChromatid(
pair,
alleles[source],
parentColors[source]
);
const x = source === 0 ? -0.64 : 0.64;
const y = pair === 1 ? 0.68 : -0.62;
chromatid.position.set(x, y, 0);
fusionGroup.add(chromatid);
const alleleLabel = makeTextSprite(
alleles[source],
0.45,
0.29,
source === 0 ? "#7cc8ff" : "#ffc857"
);
alleleLabel.position.set(
x + 0.43,
pair === 1 ? 1.28 : -1.02,
0.25
);
fusionGroup.add(alleleLabel);
}
}
const zygoteLabel = makeTextSprite("Zygote • 2n = 4", 2.15, 0.52, "#7cc8ff");
zygoteLabel.position.set(0, 2.16, 0.2);
fusionGroup.add(zygoteLabel);
const equationLabel = makeTextSprite("n + n → 2n", 1.55, 0.42, "#48dfcf");
equationLabel.position.set(0, -2.12, 0.2);
fusionGroup.add(equationLabel);
}
function updateFusion(now) {
const raw = THREE.MathUtils.clamp((now - fusionStart) / 1150, 0, 1);
const eased = 1 - Math.pow(1 - raw, 3);
fusionGroup.scale.setScalar(0.08 + eased * 0.92);
fusionGroup.rotation.y = (1 - eased) * 0.55;
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
function renderOrientationButtons() {
orientationButtons.innerHTML = "";
orientations.forEach((orientation, index) => {
const button = document.createElement("button");
button.type = "button";
button.className =
"orientation-button" +
(index === selectedOrientation ? " active" : "");
button.innerHTML = `
<strong>${orientation.id}</strong>
<small>${orientation.label}</small>
`;
button.addEventListener("click", () => {
if (running) return;
selectedOrientation = index;
resetScene();
renderOrientationButtons();
renderRunResults();
});
orientationButtons.appendChild(button);
});
}
function renderPossibleGametes() {
possibleGametes.innerHTML = combinations
.map(
combination =>
`<span class="possible-chip">${combination}</span>`
)
.join("");
}
function renderRunResults() {
const results = resultsFor(orientations[selectedOrientation]);
runResults.innerHTML = results
.map(
(result, index) => `
<span class="result-chip">
<b>${result}</b>
<small>gamete ${index + 1}</small>
</span>
`
)
.join("");
}
function renderParentChoices() {
for (const parent of ["p1", "p2"]) {
const container = parent === "p1" ? parent1Choices : parent2Choices;
container.innerHTML = "";
for (const combination of combinations) {
const button = document.createElement("button");
button.type = "button";
button.className =
"gamete-choice" +
(selectedGametes[parent] === combination ? " active" : "");
button.textContent = combination;
button.addEventListener("click", () => {
selectedGametes[parent] = combination;
renderParentChoices();
updateZygoteText();
});
container.appendChild(button);
}
}
}
function updateZygoteText() {
const pair1 =
selectedGametes.p1[0] + selectedGametes.p2[0];
const pair2 =
selectedGametes.p1[1] + selectedGametes.p2[1];
zygoteResult.innerHTML = `
<strong>Zygote genotype: ${pair1} / ${pair2} · 2n = 4</strong>
<span>One haploid gamete from each parent restores the diploid number.</span>
`;
}
function setStageInfo(kicker, title, copy) {
stageKicker.textContent = kicker;
stageTitle.textContent = title;
stageCopy.textContent = copy;
}
function updateStageStatus(t) {
let key = "";
let kicker = "";
let title = "";
let copy = "";
if (t < 0.08) {
key = "prophase";
kicker = "PROPHASE I";
title = "A diploid parent cell";
copy =
"The model has 2n = 4: two homologous pairs. DNA replication gives each homolog two sister chromatids.";
} else if (t < 0.16) {
key = "preview0";
kicker = "ALTERNATIVE ORIENTATION 1 OF 4";
title = "Pair 1: A left · Pair 2: B left";
copy =
"Each replicated homologous pair can face either pole independently.";
} else if (t < 0.24) {
key = "preview1";
kicker = "ALTERNATIVE ORIENTATION 2 OF 4";
title = "Pair 1: A left · Pair 2: b left";
copy =
"The short pair has flipped, while the long pair keeps its orientation.";
} else if (t < 0.32) {
key = "preview2";
kicker = "ALTERNATIVE ORIENTATION 3 OF 4";
title = "Pair 1: a left · Pair 2: B left";
copy =
"Now the long pair flips instead. These orientation changes create independent assortment.";
} else if (t < 0.40) {
key = "preview3";
kicker = "ALTERNATIVE ORIENTATION 4 OF 4";
title = "Pair 1: a left · Pair 2: b left";
copy =
"All four displayed orientations are possible because each pair assorts independently.";
} else if (t < 0.46) {
key = "selected";
const selected = orientations[selectedOrientation];
kicker = "SELECTED METAPHASE ORIENTATION";
title = `${selected.pair1} + ${selected.pair2} face the left pole`;
copy =
"The selected orientation is locked in before homologous chromosomes separate.";
} else if (t < 0.64) {
key = "meiosis1";
kicker = "MEIOSIS I";
title = "Homologous chromosomes separate";
copy =
"Each homolog moves toward an opposite pole, but its two sister chromatids remain joined.";
} else if (t < 0.85) {
key = "meiosis2";
kicker = "MEIOSIS II";
title = "Sister chromatids separate";
copy =
"The sister chromatids now separate, producing four haploid gametes with n = 2.";
} else {
key = "gametes";
const results = resultsFor(orientations[selectedOrientation]);
kicker = "GAMETES";
title = "Four haploid products";
copy =
`This run produced ${results[0]}, ${results[0]}, ${results[2]} and ${results[2]}. Across repeated meioses, all four combinations can occur.`;
}
if (key !== lastStageKey) {
lastStageKey = key;
setStageInfo(kicker, title, copy);
}
}
function updateButtons() {
pauseButton.disabled = !running;
pauseButton.textContent = running ? "Pause" : "Paused";
}
function resetScene() {
mode = "meiosis";
running = false;
elapsed = 0;
lastStageKey = "";
chromosomeRoot.visible = true;
parentMembrane.visible = true;
spindleGroup.visible = false;
gameteGroup.visible = false;
fusionGroup.visible = false;
updateMeiosis();
updateButtons();
updateZygoteText();
}
function startMeiosis() {
if (mode === "fusion") resetScene();
mode = "meiosis";
elapsed = 0;
running = true;
lastStageKey = "";
chromosomeRoot.visible = true;
parentMembrane.visible = true;
spindleGroup.visible = false;
gameteGroup.visible = false;
fusionGroup.visible = false;
renderRunResults();
updateButtons();
}
runButton.addEventListener("click", startMeiosis);
pauseButton.addEventListener("click", () => {
running = !running;
updateButtons();
});
resetButton.addEventListener("click", resetScene);
fertilizeButton.addEventListener("click", () => {
running = false;
mode = "fusion";
fusionStart = performance.now();
chromosomeRoot.visible = false;
parentMembrane.visible = false;
spindleGroup.visible = false;
gameteGroup.visible = false;
fusionGroup.visible = true;
buildFusionScene();
updateButtons();
setStageInfo(
"FERTILIZATION",
"Two haploid gametes form a diploid zygote",
"Gametes from two AaBb parents fuse. Each gamete has n = 2; the restored zygote has 2n = 4."
);
});
// -------------------------------------------------------------------------
// Initialization and animation loop
// -------------------------------------------------------------------------
renderOrientationButtons();
renderPossibleGametes();
renderRunResults();
renderParentChoices();
updateZygoteText();
resetScene();
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(stage);
resize();
function animate(now) {
const delta = Math.min(0.05, (now - lastFrame) / 1000);
lastFrame = now;
if (mode === "meiosis") {
if (running) {
elapsed = Math.min(duration, elapsed + delta);
if (elapsed >= duration) {
running = false;
updateButtons();
const results = resultsFor(orientations[selectedOrientation]);
selectedGametes.p1 = results[0];
renderParentChoices();
updateZygoteText();
}
}
updateMeiosis();
} else {
updateFusion(now);
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```