Prompt specification
Build or customize the Three.js 3D scene "Similarity-versus-ancestry sorting board" 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: Similarity-versus-ancestry sorting board
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/similarity-versus-ancestry-sorting-board-x0896
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing convergent evolution. Let users compare feature similarity and tree relationships for fictional organisms; highlight convergent traits independently from clade membership and explain why the two classifications differ. Low-poly.
Working Three.js Starter (similarity-versus-ancestry-sorting-board-x0896):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mosaic of Origins — Convergent Evolution</title>
<style>
:root {
color-scheme: dark;
--bg: #08110f;
--panel: #101c18;
--panel-2: #15251f;
--line: #294138;
--text: #edf7f1;
--muted: #9eb5aa;
--verdant: #55d98a;
--cinder: #ff9961;
--tide: #59c9ff;
--trait: #f4d35e;
--trait-soft: rgba(244, 211, 94, 0.14);
--radius: 16px;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
overflow: hidden;
background:
radial-gradient(circle at 20% 0%, rgba(42, 97, 72, 0.2), transparent 34rem),
radial-gradient(circle at 100% 100%, rgba(22, 65, 82, 0.16), transparent 35rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
button,
select {
color: var(--text);
}
button:focus-visible,
select:focus-visible,
input:focus-visible {
outline: 2px solid var(--trait);
outline-offset: 2px;
}
header {
height: 74px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 0 22px;
border-bottom: 1px solid var(--line);
background: rgba(8, 17, 15, 0.82);
backdrop-filter: blur(14px);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
flex: 0 0 auto;
display: grid;
place-items: center;
border: 1px solid #4a6b5c;
border-radius: 11px;
background:
linear-gradient(135deg, rgba(85, 217, 138, 0.18), rgba(89, 201, 255, 0.1)),
#12231d;
color: var(--trait);
font-size: 20px;
}
.brand h1 {
margin: 0;
font-size: clamp(17px, 2vw, 22px);
line-height: 1.1;
letter-spacing: -0.02em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
}
.header-note {
color: var(--muted);
font-size: 12px;
text-align: right;
}
.workspace {
height: calc(100vh - 74px);
display: grid;
grid-template-columns: minmax(0, 1fr) 405px;
gap: 14px;
padding: 14px;
}
.stage {
min-width: 0;
min-height: 0;
display: flex;
flex-direction: column;
gap: 12px;
}
#viewport {
position: relative;
min-height: 440px;
flex: 1;
overflow: hidden;
border: 1px solid var(--line);
border-radius: var(--radius);
background: #0b1713;
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
}
#scene {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene.dragging {
cursor: grabbing;
}
.stage-top {
position: absolute;
z-index: 4;
top: 15px;
left: 15px;
right: 15px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.stage-title,
.trait-badge {
border: 1px solid rgba(72, 103, 90, 0.65);
border-radius: 11px;
background: rgba(8, 20, 16, 0.8);
backdrop-filter: blur(10px);
}
.stage-title {
padding: 10px 12px;
}
.stage-title strong {
display: block;
font-size: 12px;
letter-spacing: 0.02em;
}
.stage-title span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
}
.trait-badge {
max-width: 280px;
padding: 9px 11px;
color: var(--trait);
font-size: 11px;
text-align: right;
}
.legend {
position: absolute;
z-index: 4;
left: 15px;
bottom: 15px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100% - 30px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(72, 103, 90, 0.55);
border-radius: 9px;
background: rgba(8, 20, 16, 0.78);
color: #c7d8d0;
font-size: 10px;
backdrop-filter: blur(9px);
}
.dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 8px currentColor;
}
.legend-item.trait-dot {
color: var(--trait);
}
.camera-help {
position: absolute;
z-index: 4;
right: 15px;
bottom: 15px;
padding: 7px 9px;
border: 1px solid rgba(72, 103, 90, 0.5);
border-radius: 9px;
background: rgba(8, 20, 16, 0.78);
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
#labels {
position: absolute;
inset: 0;
z-index: 3;
overflow: hidden;
pointer-events: none;
}
.species-label {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid rgba(102, 132, 118, 0.7);
border-radius: 8px;
background: rgba(8, 18, 15, 0.86);
color: #dceae3;
font-size: 10px;
white-space: nowrap;
pointer-events: auto;
cursor: pointer;
transform-origin: center;
transition: border-color 150ms, background 150ms;
}
.species-label:hover,
.species-label.selected {
border-color: var(--trait);
background: rgba(27, 39, 28, 0.95);
}
.species-label .dot {
width: 7px;
height: 7px;
}
.insight-strip {
display: grid;
grid-template-columns: auto 1fr;
gap: 12px;
align-items: center;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: var(--radius);
background: linear-gradient(90deg, rgba(244, 211, 94, 0.07), transparent 48%), var(--panel);
}
.insight-icon {
width: 38px;
height: 38px;
display: grid;
place-items: center;
border-radius: 11px;
background: var(--trait-soft);
color: var(--trait);
font-size: 20px;
}
.insight-strip strong {
display: block;
margin-bottom: 3px;
font-size: 12px;
}
.insight-strip p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
aside {
min-height: 0;
overflow-y: auto;
padding-right: 3px;
scrollbar-width: thin;
scrollbar-color: #365346 transparent;
}
.card {
margin-bottom: 12px;
padding: 15px;
border: 1px solid var(--line);
border-radius: var(--radius);
background: rgba(16, 28, 24, 0.94);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.12);
}
.card-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
}
.card h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.01em;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.12em;
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding: 4px;
border-radius: 10px;
background: #0b1713;
}
.segmented button {
padding: 7px 5px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
font-size: 10px;
cursor: pointer;
}
.segmented button[aria-pressed="true"] {
background: #2a4136;
color: var(--text);
box-shadow: inset 0 0 0 1px #3c5b4c;
}
.control-label {
display: block;
margin: 13px 0 6px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
text-transform: uppercase;
letter-spacing: 0.08em;
}
select {
width: 100%;
padding: 9px 30px 9px 10px;
border: 1px solid #30483d;
border-radius: 9px;
background: #0b1713;
font-size: 11px;
cursor: pointer;
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 12px;
color: var(--muted);
font-size: 11px;
}
.toggle-row input {
accent-color: var(--trait);
}
.text-button {
padding: 5px 8px;
border: 1px solid #30483d;
border-radius: 8px;
background: transparent;
color: #c8dbd1;
font-size: 10px;
cursor: pointer;
}
.text-button:hover {
border-color: #638271;
background: #1b3027;
}
.organism-grid {
display: grid;
grid-template-columns: 1fr 24px 1fr;
gap: 7px;
align-items: end;
}
.versus {
align-self: center;
color: #668075;
font-size: 10px;
text-align: center;
}
.pair-summary {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin: 12px 0;
padding: 10px;
border-radius: 10px;
background: #0b1713;
}
.pair-summary strong {
display: block;
font-size: 11px;
}
.pair-summary span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.score {
text-align: right;
}
.score b {
color: var(--trait);
font-size: 20px;
line-height: 1;
}
.score small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
}
.metric {
margin-top: 10px;
padding: 11px;
border: 1px solid #263d33;
border-radius: 10px;
background: rgba(9, 22, 17, 0.64);
}
.metric-top {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
}
.metric-top span {
color: var(--muted);
font-size: 10px;
}
.metric-top b {
font-size: 12px;
}
.metric p {
margin: 6px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.bar {
height: 5px;
margin-top: 8px;
overflow: hidden;
border-radius: 999px;
background: #263b32;
}
.bar > span {
display: block;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #b9a63e, var(--trait));
transition: width 250ms ease;
}
.feature-chips {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 9px;
}
.feature-chip {
padding: 5px 7px;
border: 1px solid #3b4c44;
border-radius: 999px;
color: #b8cac1;
font-size: 9px;
}
.feature-chip.convergent {
border-color: rgba(244, 211, 94, 0.55);
background: var(--trait-soft);
color: var(--trait);
}
.feature-chip.inherited {
border-color: rgba(85, 217, 138, 0.45);
background: rgba(85, 217, 138, 0.09);
color: var(--verdant);
}
.explanation {
margin-top: 10px;
padding: 11px;
border-left: 3px solid var(--trait);
border-radius: 0 9px 9px 0;
background: rgba(244, 211, 94, 0.07);
color: #d7dfd8;
font-size: 10px;
line-height: 1.55;
}
.explanation strong {
color: var(--trait);
}
.trait-origins {
display: grid;
gap: 7px;
}
.origin-row {
display: grid;
grid-template-columns: 8px 1fr auto;
gap: 8px;
align-items: center;
padding: 7px 8px;
border-radius: 8px;
background: #0b1713;
font-size: 10px;
}
.origin-row .dot {
color: var(--trait);
}
.origin-row small {
color: var(--muted);
font-size: 9px;
}
.tree-wrap {
overflow-x: auto;
margin-top: 4px;
}
#tree {
display: block;
width: 100%;
min-width: 350px;
height: auto;
}
.tree-note {
margin: 8px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.matrix-wrap {
overflow-x: auto;
}
table {
width: 100%;
min-width: 360px;
border-collapse: collapse;
font-size: 9px;
}
th,
td {
padding: 8px 5px;
border-bottom: 1px solid #263b32;
text-align: center;
}
th {
color: var(--muted);
font-weight: 600;
}
th:first-child,
td:first-child {
text-align: left;
}
tbody tr.selected-a td:first-child {
color: var(--trait);
font-weight: 700;
}
tbody tr.selected-b td:first-child {
color: #9be8ff;
font-weight: 700;
}
.matrix-dot {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
background: #385247;
}
.matrix-dot.yes {
background: var(--trait);
box-shadow: 0 0 7px rgba(244, 211, 94, 0.65);
}
.readout {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.readout strong {
color: var(--text);
}
@media (max-width: 980px) {
body {
overflow: auto;
}
.workspace {
height: auto;
grid-template-columns: 1fr;
}
#viewport {
height: 65vh;
min-height: 500px;
flex: none;
}
aside {
overflow: visible;
}
.header-note {
display: none;
}
}
@media (max-width: 560px) {
header {
height: 68px;
padding: 0 13px;
}
.brand p {
display: none;
}
.workspace {
padding: 8px;
gap: 9px;
}
#viewport {
height: 62vh;
min-height: 460px;
}
.trait-badge {
display: none;
}
.camera-help {
display: none;
}
.insight-strip {
grid-template-columns: 1fr;
}
.insight-icon {
display: none;
}
}
</style>
</head>
<body>
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">⌁</div>
<div>
<h1>Mosaic of Origins</h1>
<p>Convergent evolution in six fictional organisms</p>
</div>
</div>
<div class="header-note">
Body color = clade ancestry · Gold anatomy = selected trait
</div>
</header>
<main class="workspace">
<section class="stage" aria-label="Interactive evolutionary diorama">
<div id="viewport">
<canvas
id="scene"
tabindex="0"
aria-label="Interactive 3D diorama of six fictional organisms. Drag to orbit and scroll to zoom."
></canvas>
<div id="labels"></div>
<div class="stage-top">
<div class="stage-title">
<strong>Interactive low-poly diorama</strong>
<span>Click a specimen to set A · Shift-click to set B</span>
</div>
<div class="trait-badge" id="stageTraitBadge">
Gold = gliding membrane
</div>
</div>
<div class="legend" aria-label="Legend">
<div class="legend-item" style="color: var(--verdant)">
<span class="dot"></span> Verdant clade
</div>
<div class="legend-item" style="color: var(--cinder)">
<span class="dot"></span> Cinder clade
</div>
<div class="legend-item" style="color: var(--tide)">
<span class="dot"></span> Tide clade
</div>
<div class="legend-item trait-dot">
<span class="dot"></span> Independent trait
</div>
</div>
<div class="camera-help">Drag: orbit · Wheel: zoom · Click: compare</div>
</div>
<div class="insight-strip">
<div class="insight-icon" aria-hidden="true">↗</div>
<div>
<strong id="insightTitle">One solution, three separate origins</strong>
<p id="insightText"></p>
</div>
</div>
</section>
<aside aria-label="Evolution comparison controls">
<section class="card">
<div class="card-heading">
<h2>Visualization lens</h2>
<span class="eyebrow">Independent overlays</span>
</div>
<div class="segmented" id="lensControls" aria-label="Visualization mode">
<button type="button" data-lens="clade" aria-pressed="false">Clade</button>
<button type="button" data-lens="trait" aria-pressed="false">Trait</button>
<button type="button" data-lens="both" aria-pressed="true">Both</button>
</div>
<label class="control-label" for="traitSelect">Highlight a trait</label>
<select id="traitSelect">
<option value="gliding">Gliding membrane — 3 origins</option>
<option value="digging">Digging claws — 3 origins</option>
<option value="grip">Gripping paws — 2 origins</option>
<option value="stocky">Stocky torso — 2 origins</option>
<option value="shortEars">Short ears — 2 origins</option>
<option value="all">All convergent traits</option>
</select>
<div class="toggle-row">
<label>
<input type="checkbox" id="autoRotate" checked>
Slow camera orbit
</label>
<button class="text-button" id="resetCamera" type="button">Reset view</button>
</div>
</section>
<section class="card" aria-live="polite">
<div class="card-heading">
<h2>Compare two organisms</h2>
<span class="eyebrow">Form vs ancestry</span>
</div>
<div class="organism-grid">
<div>
<label class="control-label" for="organismA">Organism A</label>
<select id="organismA"></select>
</div>
<div class="versus">VS</div>
<div>
<label class="control-label" for="organismB">Organism B</label>
<select id="organismB"></select>
</div>
</div>
<div class="pair-summary">
<div>
<strong id="pairType">Convergent similarity</strong>
<span id="pairClades">Verdant lineage × Cinder lineage</span>
</div>
<div class="score">
<b id="similarityScore">50%</b>
<small>feature overlap</small>
</div>
</div>
<div class="metric">
<div class="metric-top">
<span>Feature similarity</span>
<b id="featureCount">2 / 4 traits</b>
</div>
<div class="bar"><span id="featureBar" style="width: 50%"></span></div>
<p id="featureText"></p>
<div class="feature-chips" id="featureChips"></div>
</div>
<div class="metric">
<div class="metric-top">
<span>Tree relationship</span>
<b id="treeRelationship">Different clades</b>
</div>
<p id="treeText"></p>
</div>
<div class="explanation" id="explanation"></div>
</section>
<section class="card">
<div class="card-heading">
<h2 id="traitInfoTitle">Gliding membrane</h2>
<span class="eyebrow" id="traitInfoCount">3 origins</span>
</div>
<div class="trait-origins" id="traitOrigins"></div>
</section>
<section class="card">
<div class="card-heading">
<h2>Phylogenetic relationships</h2>
<span class="eyebrow">Clade tree</span>
</div>
<div class="tree-wrap">
<svg
id="tree"
viewBox="0 0 410 390"
role="img"
aria-label="Tree showing the ancestry of the six fictional organisms"
></svg>
</div>
<p class="tree-note">
Tree position represents common ancestry. Colored leaves retain their clade even when a gold trait crosses between clades.
</p>
</section>
<section class="card">
<div class="card-heading">
<h2>Feature matrix</h2>
<span class="eyebrow">Selected traits</span>
</div>
<div class="matrix-wrap">
<table>
<thead>
<tr>
<th>Organism</th>
<th>Glide</th>
<th>Dig</th>
<th>Grip</th>
<th>Stocky</th>
<th>Stream</th>
</tr>
</thead>
<tbody id="matrixBody"></tbody>
</table>
</div>
<p class="tree-note">
A filled gold cell means the trait is present; it does not identify the clade.
</p>
</section>
<section class="card">
<div class="card-heading">
<h2>Why the classifications differ</h2>
<span class="eyebrow">Key idea</span>
</div>
<p class="readout">
<strong>Clades classify descent.</strong> Members inherit traits from a common ancestor.
<strong>Feature groups classify solutions.</strong> Similar bodies or functions can arise on separate branches.
Therefore, one convergent trait can connect organisms that the tree still places in distant clades.
</p>
</section>
</aside>
</main>
<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 viewport = $("#viewport");
const canvas = $("#scene");
const COLORS = {
Verdant: "#55d98a",
Cinder: "#ff9961",
Tide: "#59c9ff",
trait: "#f4d35e",
neutralTrait: "#52645c"
};
const speciesData = [
{
id: "leafFox",
name: "Canopy Fox",
clade: "Verdant",
color: COLORS.Verdant,
habitat: "Emerald canopy",
architecture: "fox",
position: [-5.1, 0, -1.5],
labelHeight: 2.75,
features: [
{ key: "gliding", label: "Gliding membrane", origin: "Verdant" },
{ key: "grip", label: "Gripping paws", origin: "Verdant" },
{ key: "longTail", label: "Long balancing tail", origin: "Verdant" }
]
},
{
id: "cinderGlider",
name: "Cinder Glider",
clade: "Cinder",
color: COLORS.Cinder,
habitat: "Ash cliffs",
architecture: "glider",
position: [0, 0, -4.7],
labelHeight: 2.8,
features: [
{ key: "gliding", label: "Gliding membrane", origin: "Cinder" },
{ key: "grip", label: "Gripping paws", origin: "Cinder" },
{ key: "scaledTail", label: "Heat-scaled tail", origin: "Cinder" }
]
},
{
id: "reefSkater",
name: "Reef Skater",
clade: "Tide",
color: COLORS.Tide,
habitat: "Coral current",
architecture: "skater",
position: [5.0, 0, -1.4],
labelHeight: 2.65,
features: [
{ key: "gliding", label: "Gliding membrane", origin: "Tide" },
{ key: "sideFins", label: "Lateral fins", origin: "Tide" },
{ key: "streamlined", label: "Streamlined body", origin: "Tide" }
]
},
{
id: "mossBadger",
name: "Moss Badger",
clade: "Verdant",
color: COLORS.Verdant,
habitat: "Root wetlands",
architecture: "badger",
position: [-4.6, 0, 3.8],
labelHeight: 2.6,
features: [
{ key: "digging", label: "Digging claws", origin: "Verdant" },
{ key: "stocky", label: "Stocky torso", origin: "Verdant" },
{ key: "shortEars", label: "Short ears", origin: "Verdant" }
]
},
{
id: "ashBadger",
name: "Ash Badger",
clade: "Cinder",
color: COLORS.Cinder,
habitat: "Cinder plain",
architecture: "badger",
position: [0.2, 0, 4.6],
labelHeight: 2.6,
features: [
{ key: "digging", label: "Digging claws", origin: "Cinder" },
{ key: "stocky", label: "Stocky torso", origin: "Cinder" },
{ key: "shortEars", label: "Short ears", origin: "Cinder" },
{ key: "heatVents", label: "Heat vents", origin: "Cinder" }
]
},
{
id: "siltNewt",
name: "Silt Newt",
clade: "Tide",
color: COLORS.Tide,
habitat: "Silt shallows",
architecture: "newt",
position: [5.0, 0, 3.6],
labelHeight: 2.45,
features: [
{ key: "digging", label: "Digging claws", origin: "Tide" },
{ key: "streamlined", label: "Streamlined body", origin: "Tide" },
{ key: "gillFrill", label: "Gill frill", origin: "Tide" }
]
}
];
const speciesById = Object.fromEntries(speciesData.map((s) => [s.id, s]));
const traitCatalog = {
gliding: {
label: "Gliding membrane",
description: "A broad surface that slows a controlled descent between trees, cliffs, or currents."
},
digging: {
label: "Digging claws",
description: "Enlarged claws that independently evolved for excavating soil or sediment."
},
grip: {
label: "Gripping paws",
description: "Curved, strong digits that evolved separately for gripping uneven surfaces."
},
stocky: {
label: "Stocky torso",
description: "A broad, low body form that evolved independently for pushing through resistant material."
},
shortEars: {
label: "Short ears",
description: "Reduced external ears that arose separately in subterranean specialists."
},
streamlined: {
label: "Streamlined body",
description: "A single-origin Tide-clade feature inherited within that lineage."
}
};
const convergentTraits = ["gliding", "digging", "grip", "stocky", "shortEars"];
const state = {
lens: "both",
trait: "gliding",
organismA: "leafFox",
organismB: "cinderGlider",
autoRotate: true
};
/* ---------- Three.js scene ---------- */
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#0b1713");
scene.fog = new THREE.Fog("#0b1713", 17, 34);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const ambient = new THREE.HemisphereLight("#d9fff0", "#26372e", 2.0);
scene.add(ambient);
const sun = new THREE.DirectionalLight("#fff0cc", 3.2);
sun.position.set(7, 13, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
scene.add(sun);
const rim = new THREE.DirectionalLight("#5aa7ff", 1.4);
rim.position.set(-8, 6, -7);
scene.add(rim);
const groundMaterial = new THREE.MeshStandardMaterial({
color: "#10251c",
roughness: 0.96,
flatShading: true
});
const ground = new THREE.Mesh(
new THREE.CylinderGeometry(9.4, 9.8, 0.35, 12),
groundMaterial
);
ground.position.y = -0.2;
ground.receiveShadow = true;
scene.add(ground);
const underlay = new THREE.Mesh(
new THREE.CylinderGeometry(9.9, 10.3, 0.18, 12),
new THREE.MeshStandardMaterial({
color: "#07110e",
roughness: 1,
flatShading: true
})
);
underlay.position.y = -0.44;
scene.add(underlay);
const groundGrid = new THREE.GridHelper(18, 18, "#29483a", "#1c342a");
groundGrid.position.y = -0.015;
groundGrid.material.transparent = true;
groundGrid.material.opacity = 0.2;
scene.add(groundGrid);
const creatureRefs = {};
const pickables = [];
const traitMaterials = new Map();
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
flatShading: true,
...extra
});
}
function addMesh(parent, geometry, mat, position = [0, 0, 0], scale = [1, 1, 1], rotation = [0, 0, 0]) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(...position);
mesh.scale.set(...scale);
mesh.rotation.set(...rotation);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function triangleGeometry(points) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points.flat(), 3)
);
geometry.computeVertexNormals();
return geometry;
}
function getTraitMaterial(speciesId, key, wireframe = false) {
const mapKey = `${speciesId}:${key}`;
if (!traitMaterials.has(mapKey)) {
const mat = wireframe
? new THREE.MeshBasicMaterial({
color: COLORS.neutralTrait,
wireframe: true,
transparent: true,
opacity: 0.42
})
: new THREE.MeshStandardMaterial({
color: COLORS.neutralTrait,
roughness: 0.72,
flatShading: true,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.62
});
traitMaterials.set(mapKey, mat);
}
return traitMaterials.get(mapKey);
}
function tagTrait(mesh, speciesId, key) {
mesh.userData.traitKey = key;
mesh.userData.speciesId = speciesId;
if (!creatureRefs[speciesId].traitMeshes.some((item) => item.mesh === mesh)) {
creatureRefs[speciesId].traitMeshes.push({ mesh, key });
}
}
function addEyes(parent, y, z, x = 0.25, size = 0.085) {
const eyeMat = material("#07100d", { roughness: 0.42 });
for (const side of [-1, 1]) {
addMesh(
parent,
new THREE.SphereGeometry(size, 7, 5),
eyeMat,
[side * x, y, z]
);
}
}
function addLegs(parent, bodyY, spreadX, frontZ, backZ, legHeight, pawY = 0.16) {
const legMat = material("#30483c", { roughness: 0.9 });
const pawMat = material("#24362f", { roughness: 0.95 });
for (const x of [-spreadX, spreadX]) {
for (const z of [frontZ, backZ]) {
addMesh(
parent,
new THREE.CylinderGeometry(0.12, 0.16, legHeight, 5),
legMat,
[x, bodyY - legHeight * 0.55, z]
);
addMesh(
parent,
new THREE.SphereGeometry(0.19, 6, 4),
pawMat,
[x, pawY, z + 0.05],
[1.1, 0.65, 1.25]
);
}
}
}
function addMembrane(parent, speciesId, options = {}) {
const mat = getTraitMaterial(speciesId, "gliding");
const outer = options.outer ?? 1.4;
const front = options.front ?? 0.5;
const back = options.back ?? -0.9;
const y = options.y ?? 1.12;
for (const side of [-1, 1]) {
const points = [
[side * 0.38, y + 0.08, front],
[side * outer, y - 0.05, -0.28],
[side * 0.4, y - 0.22, back]
];
const mesh = addMesh(
parent,
triangleGeometry(points),
mat
);
tagTrait(mesh, speciesId, "gliding");
}
}
function addClaws(parent, speciesId, xPositions = [-0.48, 0.48], z = 0.56) {
const mat = getTraitMaterial(speciesId, "digging");
for (const x of xPositions) {
for (let i = -1; i <= 1; i++) {
const claw = addMesh(
parent,
new THREE.ConeGeometry(0.075, 0.31, 4),
mat,
[x + i * 0.08, 0.13, z + 0.16],
[1, 1, 1],
[Math.PI / 2, 0, 0]
);
tagTrait(claw, speciesId, "digging");
}
}
}
function addBodyHalo(parent, speciesId, key, scale, position) {
const mat = getTraitMaterial(speciesId, key, true);
const halo = addMesh(
parent,
new THREE.IcosahedronGeometry(1, 1),
mat,
position,
scale.map((v) => v * 1.075)
);
tagTrait(halo, speciesId, key);
}
function addEar(parent, speciesId, x, y, z, tagged = false) {
const mat = tagged
? getTraitMaterial(speciesId, "shortEars")
: material("#3b5748");
const ear = addMesh(
parent,
new THREE.ConeGeometry(0.19, 0.42, 5),
mat,
[x, y, z]
);
if (tagged) tagTrait(ear, speciesId, "shortEars");
}
function addGripPaws(parent, speciesId) {
const mat = getTraitMaterial(speciesId, "grip");
for (const x of [-0.48, 0.48]) {
for (let i = -1; i <= 1; i++) {
const toe = addMesh(
parent,
new THREE.ConeGeometry(0.075, 0.22, 5),
mat,
[x + i * 0.075, 0.16, 0.72],
[1, 1, 1],
[Math.PI / 2, 0, 0]
);
tagTrait(toe, speciesId, "grip");
}
}
}
function buildCreature(species) {
const root = new THREE.Group();
root.position.set(...species.position);
root.rotation.y = Math.atan2(-species.position[0], -species.position[2]) + Math.PI;
scene.add(root);
const visual = new THREE.Group();
visual.position.y = 0.16;
root.add(visual);
const bodyMaterial = material(species.color);
const darkMaterial = material(new THREE.Color(species.color).multiplyScalar(0.58));
const bellyMaterial = material("#d9d6b6", { roughness: 0.9 });
creatureRefs[species.id] = {
root,
visual,
bodyMaterial,
baseColor: new THREE.Color(species.color),
traitMeshes: [],
selectionRing: null
};
if (species.architecture === "fox" || species.architecture === "glider") {
addMesh(
visual,
new THREE.IcosahedronGeometry(0.84, 1),
bodyMaterial,
[0, 1.0, 0],
[0.92, 0.64, 1.18]
);
addMesh(
visual,
new THREE.IcosahedronGeometry(0.56, 1),
bodyMaterial,
[0, 1.4, 0.88],
[0.95, 0.88, 0.92]
);
addMesh(
visual,
new THREE.ConeGeometry(0.29, 0.55, 6),
bellyMaterial,
[0, 1.28, 1.33],
[1, 0.85, 1],
[Math.PI / 2, 0, 0]
);
addEyes(visual, 1.53, 1.19, 0.26, 0.075);
addMesh(
visual,
new THREE.SphereGeometry(0.09, 6, 4),
material("#101512"),
[0, 1.36, 1.56],
[1.15, 0.8, 0.8]
);
addEar(visual, species.id, -0.3, 1.86, 0.78);
addEar(visual, species.id, 0.3, 1.86, 0.78);
addLegs(visual, 1.0, 0.48, 0.55, -0.57, 0.72);
if (species.architecture === "fox") {
const tailMat = material(new THREE.Color(species.color).multiplyScalar(0.86));
addMesh(
visual,
new THREE.ConeGeometry(0.33, 1.4, 7),
tailMat,
[0, 0.93, -1.18],
[1, 1, 1],
[-Math.PI / 2, 0, 0]
);
addMesh(
visual,
new THREE.IcosahedronGeometry(0.38, 1),
tailMat,
[0, 1.0, -1.68],
[0.9, 0.9, 1.15]
);
} else {
addMesh(
visual,
new THREE.ConeGeometry(0.23, 0.9, 6),
darkMaterial,
[0, 0.94, -1.18],
[1, 1, 1],
[-Math.PI / 2, 0, 0]
);
for (let i = 0; i < 4; i++) {
addMesh(
visual,
new THREE.ConeGeometry(0.13, 0.25, 4),
darkMaterial,
[0, 1.55 - i * 0.03, -0.35 + i * 0.34],
[1, 1, 1],
[0, 0, Math.PI]
);
}
}
addMembrane(visual, species.id, {
outer: species.architecture === "glider" ? 1.7 : 1.45,
y: 1.15
});
addGripPaws(visual, species.id);
}
if (species.architecture === "skater") {
addMesh(
visual,
new THREE.IcosahedronGeometry(0.85, 1),
bodyMaterial,
[0, 1.02, 0],
[0.62, 0.43, 1.35]
);
addMesh(
visual,
new THREE.IcosahedronGeometry(0.43, 1),
bodyMaterial,
[0, 1.05, 1.02],
[0.9, 0.78, 0.9]
);
addEyes(visual, 1.18, 1.21, 0.34, 0.07);
const tailMat = getTraitMaterial(species.id, "gliding");
for (const side of [-1, 1]) {
const tail = addMesh(
visual,
triangleGeometry([
[side * 0.12, 1.08, -1.02],
[side * 0.77, 1.32, -1.4],
[side * 0.1, 0.72, -1.38]
]),
tailMat
);
tagTrait(tail, species.id, "gliding");
}
addMembrane(visual, species.id, {
outer: 1.62,
front: 0.55,
back: -0.85,
y: 1.03
});
addBodyHalo(
visual,
species.id,
"streamlined",
[0.64, 0.46, 1.38],
[0, 1.02, 0]
);
}
if (species.architecture === "badger") {
addMesh(
visual,
new THREE.IcosahedronGeometry(0.9, 1),
bodyMaterial,
[0, 0.91, 0],
[1.08, 0.78, 1.25]
);
addMesh(
visual,
new THREE.IcosahedronGeometry(0.55, 1),
bodyMaterial,
[0, 1.26, 0.98],
[1.0, 0.9, 0.9]
);
addMesh(
visual,
new THREE.ConeGeometry(0.27, 0.42, 6),
bellyMaterial,
[0, 1.12, 1.37],
[1, 0.78, 1],
[Math.PI / 2, 0, 0]
);
addEyes(visual, 1.4, 1.25, 0.27, 0.07);
addEar(visual, species.id, -0.27, 1.72, 0.86, true);
addEar(visual, species.id, 0.27, 1.72, 0.86, true);
addLegs(visual, 0.88, 0.56, 0.58, -0.62, 0.58, 0.14);
addClaws(visual, species.id);
addBodyHalo(
visual,
species.id,
"stocky",
[1.1, 0.81, 1.27],
[0, 0.91, 0]
);
if (species.id === "ashBadger") {
for (const x of [-0.38, 0, 0.38]) {
addMesh(
visual,
new THREE.ConeGeometry(0.12, 0.35, 4),
darkMaterial,
[x, 1.75, -0.12]
);
}
}
}
if (species.architecture === "newt") {
addMesh(
visual,
new THREE.IcosahedronGeometry(0.85, 1),
bodyMaterial,
[0, 0.78, 0],
[0.55, 0.42, 1.45]
);
addMesh(
visual,
new THREE.IcosahedronGeometry(0.43, 1),
bodyMaterial,
[0, 0.87, 1.12],
[0.9, 0.78, 0.9]
);
addEyes(visual, 1.01, 1.34, 0.27, 0.065);
addMesh(
visual,
new THREE.ConeGeometry(0.31, 1.65, 7),
bodyMaterial,
[0, 0.68, -1.48],
[1, 1, 1],
[-Math.PI / 2, 0, 0]
);
const finMat = darkMaterial;
for (const side of [-1, 1]) {
addMesh(
visual,
triangleGeometry([
[side * 0.2, 0.97, 0.35],
[side * 0.2, 1.48, -0.25],
[side * 0.2, 0.88, -0.58]
]),
finMat
);
}
addLegs(visual, 0.73, 0.39, 0.55, -0.62, 0.45, 0.13);
addClaws(visual, species.id, [-0.39, 0.39], 0.6);
addBodyHalo(
visual,
species.id,
"streamlined",
[0.58, 0.45, 1.48],
[0, 0.78, 0]
);
}
const plinthMat = material(new THREE.Color(species.color).multiplyScalar(0.22));
const plinth = addMesh(
root,
new THREE.CylinderGeometry(1.22, 1.36, 0.12, 10),
plinthMat,
[0, 0.06, 0]
);
plinth.receiveShadow = true;
const ringMat = new THREE.MeshBasicMaterial({
color: species.color,
transparent: true,
opacity: 0.68,
side: THREE.DoubleSide
});
const ring = addMesh(
root,
new THREE.RingGeometry(1.34, 1.42, 40),
ringMat,
[0, 0.13, 0],
[1, 1, 1],
[-Math.PI / 2, 0, 0]
);
ring.castShadow = false;
ring.receiveShadow = false;
const selectionRing = addMesh(
root,
new THREE.TorusGeometry(1.53, 0.022, 6, 48),
new THREE.MeshBasicMaterial({
color: COLORS.trait,
transparent: true,
opacity: 0.9
}),
[0, 0.16, 0],
[1, 1, 1],
[-Math.PI / 2, 0, 0]
);
selectionRing.visible = false;
creatureRefs[species.id].selectionRing = selectionRing;
creatureRefs[species.id].plinthMaterial = plinthMat;
creatureRefs[species.id].ringMaterial = ringMat;
root.traverse((object) => {
if (object.isMesh) {
object.userData.speciesId = species.id;
pickables.push(object);
}
});
}
speciesData.forEach(buildCreature);
/* ---------- Camera controls, implemented without addons ---------- */
const cameraState = {
theta: 0.68,
phi: 0.93,
radius: 15.8,
targetTheta: 0.68,
targetPhi: 0.93,
targetRadius: 15.8,
target: new THREE.Vector3(0, 0.45, 0)
};
function updateCamera(dt = 1) {
if (state.autoRotate && !dragging) {
cameraState.targetTheta += dt * 0.055;
}
const smoothing = 1 - Math.pow(0.001, dt);
cameraState.theta += (cameraState.targetTheta - cameraState.theta) * smoothing;
cameraState.phi += (cameraState.targetPhi - cameraState.phi) * smoothing;
cameraState.radius += (cameraState.targetRadius - cameraState.radius) * smoothing;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.targetTheta = 0.68;
cameraState.targetPhi = 0.93;
cameraState.targetRadius = 15.8;
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let pointerStart = { x: 0, y: 0 };
let previousPointer = { x: 0, y: 0 };
let pointerTravel = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerStart = { x: event.clientX, y: event.clientY };
previousPointer = { x: event.clientX, y: event.clientY };
pointerTravel = 0;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
pointerTravel += Math.abs(dx) + Math.abs(dy);
cameraState.targetTheta -= dx * 0.007;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.006,
0.38,
1.36
);
previousPointer.x = event.clientX;
previousPointer.y = event.clientY;
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (pointerTravel < 7) {
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 hit = raycaster.intersectObjects(pickables, false)[0];
if (hit?.object.userData.speciesId) {
setComparedOrganism(hit.object.userData.speciesId, event.shiftKey);
}
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
canvas.classList.remove("dragging");
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius + event.deltaY * 0.012,
9.5,
23
);
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
/* ---------- HTML labels projected from Three.js ---------- */
const labels = [];
for (const species of speciesData) {
const label = document.createElement("button");
label.type = "button";
label.className = "species-label";
label.innerHTML = `
<span class="dot" style="color:${species.color}"></span>
<span>${species.name}</span>
`;
label.title = `${species.name} — ${species.clade} clade. Click to compare as A; Shift-click as B.`;
label.addEventListener("click", (event) => {
setComparedOrganism(species.id, event.shiftKey);
});
$("#labels").appendChild(label);
labels.push({ species, element: label });
}
const projected = new THREE.Vector3();
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
for (const item of labels) {
const ref = creatureRefs[item.species.id];
projected.set(
ref.root.position.x,
ref.root.position.y + item.species.labelHeight,
ref.root.position.z
);
projected.project(camera);
const visible = projected.z > -1 && projected.z < 1;
item.element.style.display = visible ? "flex" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -100%)`;
}
}
/* ---------- Comparison and educational logic ---------- */
function getSharedFeatures(a, b) {
return a.features
.map((feature) => {
const other = b.features.find((item) => item.label === feature.label);
if (!other) return null;
return {
key: feature.key,
label: feature.label,
originA: feature.origin,
originB: other.origin,
convergent: feature.origin !== other.origin
};
})
.filter(Boolean);
}
function mRcaFor(a, b) {
if (a === b) return `leaf-${a}`;
const tide = ["reefSkater", "siltNewt"];
const verdant = ["leafFox", "mossBadger"];
const cinder = ["cinderGlider", "ashBadger"];
const terrestrial = [...verdant, ...cinder];
if (tide.includes(a) && tide.includes(b)) return "tide";
if (verdant.includes(a) && verdant.includes(b)) return "verdant";
if (cinder.includes(a) && cinder.includes(b)) return "cinder";
if (terrestrial.includes(a) && terrestrial.includes(b)) return "land";
return "root";
}
const ancestorNames = {
root: "the root ancestor",
tide: "the Tide clade ancestor",
land: "the terrestrial ancestor",
verdant: "the Verdant clade ancestor",
cinder: "the Cinder clade ancestor"
};
function relationshipFor(a, b) {
if (a.id === b.id) return "Same organism";
const mrca = mRcaFor(a.id, b.id);
if (mrca === "tide" || mrca === "verdant" || mrca === "cinder") {
return `Sister taxa in the ${a.clade} clade`;
}
if (mrca === "land") {
return "Remote terrestrial relatives";
}
return "Distant lineages";
}
function populateSelects() {
const options = speciesData
.map((s) => `<option value="${s.id}">${s.name}</option>`)
.join("");
$("#organismA").innerHTML = options;
$("#organismB").innerHTML = options;
$("#organismA").value = state.organismA;
$("#organismB").value = state.organismB;
}
function renderComparison() {
const a = speciesById[state.organismA];
const b = speciesById[state.organismB];
const shared = getSharedFeatures(a, b);
const union = new Set([
...a.features.map((f) => f.label),
...b.features.map((f) => f.label)
]);
const percentage = a.id === b.id
? 100
: Math.round((shared.length / union.size) * 100);
const convergent = shared.filter((feature) => feature.convergent);
const inherited = shared.filter((feature) => !feature.convergent);
const mrca = mRcaFor(a.id, b.id);
const relation = relationshipFor(a, b);
let pairType = "Different solutions";
if (convergent.length) pairType = "Convergent similarity";
else if (inherited.length) pairType = "Ancestral similarity";
$("#pairType").textContent = pairType;
$("#pairClades").textContent = `${a.clade} lineage × ${b.clade} lineage`;
$("#similarityScore").textContent = `${percentage}%`;
$("#featureCount").textContent = `${shared.length} / ${union.size} traits`;
$("#featureBar").style.width = `${percentage}%`;
$("#featureText").textContent = a.id === b.id
? "The same profile is being compared with itself."
: `${shared.length} matching feature${shared.length === 1 ? "" : "s"} across a ${union.size}-trait union.`;
$("#treeRelationship").textContent = a.clade === b.clade
? "Same clade"
: "Different clades";
$("#treeText").textContent = a.id === b.id
? "This specimen occupies one terminal branch in the tree."
: `${relation}. Their most recent common ancestor is ${ancestorNames[mrca]}.`;
const chipContainer = $("#featureChips");
chipContainer.innerHTML = "";
if (!shared.length) {
chipContainer.innerHTML = '<span class="feature-chip">No shared profile feature</span>';
} else {
for (const feature of shared) {
const chip = document.createElement("span");
chip.className = `feature-chip ${feature.convergent ? "convergent" : "inherited"}`;
chip.textContent = `${feature.label} · ${feature.convergent ? "independent" : "inherited"}`;
chipContainer.appendChild(chip);
}
}
let explanation;
if (convergent.length) {
const labels = convergent.map((f) => f.label.toLowerCase()).join(", ");
explanation = `<strong>${a.name} and ${b.name} share ${labels}.</strong> ` +
`These forms arose independently in the ${a.clade} and ${b.clade} lineages. ` +
`The similarity reflects a shared functional solution, not a close tree relationship.`;
} else if (inherited.length) {
explanation = `<strong>${a.name} and ${b.name} share ${inherited.map((f) => f.label.toLowerCase()).join(", ")}.</strong> ` +
`Because both are in the ${a.clade} clade, the similarity can be inherited from their shared ancestor. ` +
`Here, feature similarity and ancestry point in the same direction.`;
} else {
explanation = `<strong>${a.name} and ${b.name} do not share a feature in this profile.</strong> ` +
`Their tree relationship is ${relation.toLowerCase()}. ` +
`A lack of matching features does not by itself change either organism's clade.`;
}
explanation += " Clades describe descent; feature groups describe form or function.";
$("#explanation").innerHTML = explanation;
}
function renderTraitInfo() {
const selected = state.trait;
const title = $("#traitInfoTitle");
const count = $("#traitInfoCount");
const container = $("#traitOrigins");
container.innerHTML = "";
if (selected === "all") {
title.textContent = "Convergent solutions";
count.textContent = "5 traits";
for (const key of convergentTraits) {
const occurrences = speciesData.filter((s) =>
s.features.some((feature) => feature.key === key)
);
const origins = new Set(occurrences.map((s) => s.clade));
const row = document.createElement("div");
row.className = "origin-row";
row.innerHTML = `
<span class="dot"></span>
<span>${traitCatalog[key].label}</span>
<small>${origins.size} origins</small>
`;
container.appendChild(row);
}
return;
}
const occurrences = speciesData.filter((s) =>
s.features.some((feature) => feature.key === selected)
);
const origins = new Set(occurrences.map((s) => s.clade));
title.textContent = traitCatalog[selected].label;
count.textContent = `${origins.size} independent ${origins.size === 1 ? "origin" : "origins"}`;
for (const species of occurrences) {
const row = document.createElement("div");
row.className = "origin-row";
row.innerHTML = `
<span class="dot" style="color:${species.color}"></span>
<span>${species.name}</span>
<small>${species.clade} lineage</small>
`;
container.appendChild(row);
}
}
const treeLeaves = [
{ id: "reefSkater", name: "Reef Skater", clade: "Tide", x: 278, y: 52 },
{ id: "siltNewt", name: "Silt Newt", clade: "Tide", x: 278, y: 126 },
{ id: "leafFox", name: "Canopy Fox", clade: "Verdant", x: 278, y: 193 },
{ id: "mossBadger", name: "Moss Badger", clade: "Verdant", x: 278, y: 253 },
{ id: "cinderGlider", name: "Cinder Glider", clade: "Cinder", x: 278, y: 300 },
{ id: "ashBadger", name: "Ash Badger", clade: "Cinder", x: 278, y: 355 }
];
const treeEdges = [
"M24 205 C58 205 58 89 101 89",
"M24 205 C58 205 58 315 101 315",
"M101 89 C175 89 175 52 270 52",
"M101 89 C175 89 175 126 270 126",
"M101 315 C145 315 145 223 181 223",
"M101 315 C145 315 145 315 181 315",
"M181 223 C226 223 226 193 270 193",
"M181 223 C226 223 226 253 270 253",
"M181 315 C226 315 226 300 270 300",
"M181 315 C226 315 226 355 270 355"
];
function renderTree() {
const a = state.organismA;
const b = state.organismB;
const mrca = mRcaFor(a, b);
let svg = `
<g fill="none" stroke="#365247" stroke-width="2" stroke-linecap="round">
${treeEdges.map((d) => `<path d="${d}"/>`).join("")}
</g>
<g font-family="Inter, system-ui, sans-serif" font-size="10">
<circle cx="24" cy="205" r="5" fill="#72877d" stroke="${mrca === "root" ? "#f4d35e" : "#72877d"}" stroke-width="2"/>
<text x="10" y="190" fill="#90a69b">root</text>
<circle cx="101" cy="89" r="6" fill="${COLORS.Tide}" stroke="${mrca === "tide" ? "#f4d35e" : "#365247"}" stroke-width="2"/>
<text x="108" y="72" fill="${COLORS.Tide}" font-weight="700">Tide</text>
<circle cx="101" cy="315" r="6" fill="#92a49b" stroke="${mrca === "land" ? "#f4d35e" : "#365247"}" stroke-width="2"/>
<text x="108" y="337" fill="#92a49b" font-weight="700">Land</text>
<circle cx="181" cy="223" r="6" fill="${COLORS.Verdant}" stroke="${mrca === "verdant" ? "#f4d35e" : "#365247"}" stroke-width="2"/>
<text x="188" y="207" fill="${COLORS.Verdant}" font-weight="700">Verdant</text>
<circle cx="181" cy="315" r="6" fill="${COLORS.Cinder}" stroke="${mrca === "cinder" ? "#f4d35e" : "#365247"}" stroke-width="2"/>
<text x="188" y="299" fill="${COLORS.Cinder}" font-weight="700">Cinder</text>
</g>
`;
for (const leaf of treeLeaves) {
const selected = leaf.id === a || leaf.id === b;
const fill = leaf.id === a ? COLORS.trait : leaf.id === b ? "#bdeeff" : COLORS[leaf.clade];
const textFill = selected ? "#ffffff" : "#c4d5cd";
svg += `
<g>
<circle cx="276" cy="${leaf.y}" r="${selected ? 6 : 4.5}" fill="${fill}" stroke="${selected ? "#f4d35e" : "#0b1713"}" stroke-width="${selected ? 2 : 1}"/>
<text x="287" y="${leaf.y + 3.5}" fill="${textFill}" font-family="Inter, system-ui, sans-serif" font-size="10" font-weight="${selected ? "700" : "500"}">
${leaf.name}
</text>
</g>
`;
}
$("#tree").innerHTML = svg;
}
function renderMatrix() {
const columns = ["gliding", "digging", "grip", "stocky", "streamlined"];
const body = $("#matrixBody");
body.innerHTML = speciesData.map((species) => {
const aClass = species.id === state.organismA ? "selected-a" : "";
const bClass = species.id === state.organismB ? "selected-b" : "";
const cells = columns.map((key) => {
const present = species.features.some((feature) => feature.key === key);
return `<td><span class="matrix-dot ${present ? "yes" : ""}"></span></td>`;
}).join("");
return `
<tr class="${aClass} ${bClass}">
<td style="color:${species.color}">${species.name}</td>
${cells}
</tr>
`;
}).join("");
}
function renderInsight() {
const selected = state.trait;
if (selected === "all") {
$("#insightTitle").textContent = "Similar solutions can occupy separate branches";
$("#insightText").textContent =
"Gold marks independently evolved traits. Each body still keeps its clade color, so similarity and ancestry remain visible at the same time.";
$("#stageTraitBadge").textContent = "Gold = all convergent traits";
return;
}
const occurrences = speciesData.filter((species) =>
species.features.some((feature) => feature.key === selected)
);
const origins = new Set(occurrences.map((species) => species.clade));
$("#insightTitle").textContent =
`${traitCatalog[selected].label}: ${origins.size} separate ${origins.size === 1 ? "origin" : "origins"}`;
$("#insightText").textContent =
`${traitCatalog[selected].description} The gold anatomy is a trait overlay; body color and the tree still identify clade ancestry.`;
$("#stageTraitBadge").textContent = `Gold = ${traitCatalog[selected].label.toLowerCase()}`;
}
function applyVisualState() {
const selectedKeys = state.trait === "all"
? convergentTraits
: [state.trait];
for (const species of speciesData) {
const ref = creatureRefs[species.id];
const matchingTrait = ref.traitMeshes.some((item) =>
selectedKeys.includes(item.key)
);
const dimBody = state.lens === "trait" && !matchingTrait;
ref.bodyMaterial.color.copy(ref.baseColor);
if (dimBody) {
ref.bodyMaterial.color.lerp(new THREE.Color("#15241e"), 0.68);
}
const isSelected = species.id === state.organismA || species.id === state.organismB;
ref.bodyMaterial.emissive.set(isSelected ? species.color : "#000000");
ref.bodyMaterial.emissiveIntensity = isSelected ? 0.055 : 0;
for (const item of ref.traitMeshes) {
const active = state.lens !== "clade" && selectedKeys.includes(item.key);
const mat = item.mesh.material;
if (mat.isMeshBasicMaterial) {
mat.color.set(active ? COLORS.trait : COLORS.neutralTrait);
mat.opacity = active ? 0.9 : 0.34;
} else {
mat.color.set(active ? COLORS.trait : COLORS.neutralTrait);
mat.emissive.set(active ? "#6b5312" : "#000000");
mat.emissiveIntensity = active ? 0.45 : 0;
mat.opacity = active ? 0.96 : 0.55;
}
}
ref.plinthMaterial.color
.copy(new THREE.Color(species.color).multiplyScalar(0.22))
.lerp(new THREE.Color("#102019"), dimBody ? 0.65 : 0);
ref.ringMaterial.opacity = state.lens === "trait" ? 0.95 : 0.68;
ref.selectionRing.visible = isSelected;
}
}
function renderAll() {
renderComparison();
renderTraitInfo();
renderTree();
renderMatrix();
renderInsight();
applyVisualState();
for (const item of labels) {
item.element.classList.toggle(
"selected",
item.species.id === state.organismA ||
item.species.id === state.organismB
);
}
document.querySelectorAll("#lensControls button").forEach((button) => {
button.setAttribute("aria-pressed", String(button.dataset.lens === state.lens));
});
}
function setComparedOrganism(id, assignB = false) {
if (!speciesById[id]) return;
if (assignB) {
state.organismB = id;
} else {
state.organismA = id;
if (state.organismA === state.organismB) {
const other = speciesData.find((species) => species.id !== id);
state.organismB = other.id;
}
}
$("#organismA").value = state.organismA;
$("#organismB").value = state.organismB;
renderAll();
}
/* ---------- Controls ---------- */
$("#lensControls").addEventListener("click", (event) => {
const button = event.target.closest("button[data-lens]");
if (!button) return;
state.lens = button.dataset.lens;
renderAll();
});
$("#traitSelect").addEventListener("change", (event) => {
state.trait = event.target.value;
renderAll();
});
$("#organismA").addEventListener("change", (event) => {
state.organismA = event.target.value;
renderAll();
});
$("#organismB").addEventListener("change", (event) => {
state.organismB = event.target.value;
renderAll();
});
$("#autoRotate").addEventListener("change", (event) => {
state.autoRotate = event.target.checked;
});
$("#resetCamera").addEventListener("click", resetCamera);
populateSelects();
renderAll();
updateCamera();
/* ---------- Resize and render loop ---------- */
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
updateCamera(dt);
const pulse = 1 + Math.sin(performance.now() * 0.004) * 0.025;
for (const species of speciesData) {
const ref = creatureRefs[species.id];
if (ref.selectionRing.visible) {
ref.selectionRing.scale.setScalar(pulse);
}
}
camera.updateMatrixWorld();
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```