Prompt specification
Build or customize the Three.js 3D scene "Neural pathway routing explorer" 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: Neural pathway routing explorer
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/neural-pathway-routing-explorer-x1020
Scene Description Prompt:
Let viewers select sensory, interneuron, or motor components in a toy circuit; trace reachable outputs and explain that real reflex regulation is more complex than the diagram. Build this with Three.js from cdnjs as a low-poly scene in one HTML file.
Working Three.js Starter (neural-pathway-routing-explorer-x1020):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Reflex Circuit Lab</title>
<style>
:root {
--ink: #173033;
--muted: #64777a;
--paper: #f8fbfa;
--panel: #ffffff;
--line: #dce7e4;
--teal: #18b7a4;
--violet: #9b5de5;
--blue: #3f8cff;
--amber: #ffb547;
--coral: #ff7d63;
--shadow: 0 12px 34px rgba(31, 68, 67, 0.09);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
color: var(--ink);
background: var(--paper);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button {
font: inherit;
}
header {
height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 24px;
background: #173033;
color: white;
border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 40px;
height: 40px;
flex: 0 0 auto;
display: grid;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 12px;
background: rgba(255, 255, 255, 0.08);
}
.brand-mark svg {
width: 27px;
height: 27px;
}
.brand-copy {
min-width: 0;
}
.brand h1 {
margin: 0;
font-size: 18px;
letter-spacing: 0.01em;
}
.brand p {
margin: 3px 0 0;
color: #bdd0ce;
font-size: 12px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.teaching-badge {
flex: 0 0 auto;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 999px;
color: #d8e7e4;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
main {
height: calc(100vh - 72px);
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
}
.stage {
position: relative;
min-width: 0;
overflow: hidden;
background: #eaf2ef;
}
#viewport,
#labels {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#viewport canvas.dragging {
cursor: grabbing;
}
#viewport canvas:focus-visible {
outline: 3px solid rgba(63, 140, 255, 0.55);
outline-offset: -3px;
}
#labels {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
#labels.hidden {
display: none;
}
.stage-intro {
position: absolute;
z-index: 3;
top: 24px;
left: 26px;
max-width: 330px;
pointer-events: none;
}
.eyebrow {
display: block;
margin-bottom: 6px;
color: #47706d;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.stage-intro h2 {
margin: 0;
font-size: clamp(22px, 2.4vw, 34px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.stage-intro p {
max-width: 310px;
margin: 9px 0 0;
color: #607678;
font-size: 13px;
line-height: 1.5;
}
.stage-actions {
position: absolute;
z-index: 4;
top: 22px;
right: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
}
.stage-button {
min-height: 36px;
padding: 8px 12px;
border: 1px solid rgba(45, 79, 78, 0.14);
border-radius: 10px;
color: #294a4a;
background: rgba(255, 255, 255, 0.86);
box-shadow: 0 6px 18px rgba(32, 68, 66, 0.08);
cursor: pointer;
font-size: 12px;
font-weight: 700;
backdrop-filter: blur(8px);
}
.stage-button:hover {
background: white;
transform: translateY(-1px);
}
.stage-button[aria-pressed="true"] {
border-color: rgba(24, 183, 164, 0.45);
color: #087e71;
background: #effbf8;
}
.stage-legend {
position: absolute;
z-index: 3;
left: 24px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 14px;
max-width: calc(100% - 48px);
padding: 11px 13px;
border: 1px solid rgba(45, 79, 78, 0.12);
border-radius: 12px;
background: rgba(255, 255, 255, 0.82);
box-shadow: 0 8px 24px rgba(32, 68, 66, 0.07);
pointer-events: none;
backdrop-filter: blur(8px);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #53696b;
font-size: 11px;
font-weight: 650;
}
.legend-swatch {
width: 18px;
height: 5px;
border-radius: 999px;
background: var(--teal);
}
.legend-swatch.inhibitory {
background: var(--violet);
}
.legend-swatch.selected {
background: var(--amber);
}
.legend-swatch.dimmed {
background: #bdc9c7;
}
.node-label {
position: absolute;
min-width: 104px;
padding: 7px 9px;
border: 1px solid rgba(46, 77, 77, 0.14);
border-radius: 9px;
color: #264746;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 6px 14px rgba(39, 74, 72, 0.08);
text-align: center;
transform: translate(-50%, -50%);
transition: opacity 180ms ease, border-color 180ms ease,
background 180ms ease;
}
.node-label span,
.node-label small {
display: block;
}
.node-label span {
font-size: 11px;
font-weight: 800;
white-space: nowrap;
}
.node-label small {
margin-top: 2px;
color: #758687;
font-size: 9px;
white-space: nowrap;
}
.node-label.reached {
border-color: rgba(24, 183, 164, 0.42);
}
.node-label.selected {
border-color: var(--amber);
color: #80500b;
background: #fff8e9;
box-shadow: 0 8px 20px rgba(255, 181, 71, 0.18);
}
.node-label.unreached {
opacity: 0.42;
}
#loading {
position: absolute;
z-index: 8;
inset: 0;
display: grid;
place-items: center;
color: #4b6867;
background: #eaf2ef;
font-size: 14px;
font-weight: 700;
}
aside {
min-width: 0;
padding: 23px;
overflow-y: auto;
border-left: 1px solid var(--line);
background: var(--panel);
box-shadow: -10px 0 30px rgba(40, 72, 70, 0.04);
}
aside h2 {
margin: 0;
font-size: 17px;
letter-spacing: -0.02em;
}
.section-copy {
margin: 6px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.categories {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.category-button {
min-width: 0;
padding: 10px 5px 9px;
border: 1px solid var(--line);
border-radius: 12px;
color: #53696b;
background: #fbfdfc;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 160ms ease;
}
.category-button:hover {
border-color: #b9cbc7;
background: #f5faf8;
}
.category-button.active {
border-color: #173f3d;
color: white;
background: #173f3d;
box-shadow: 0 5px 12px rgba(23, 63, 61, 0.13);
}
.category-button small {
display: block;
margin-top: 3px;
opacity: 0.66;
font-size: 9px;
font-weight: 600;
}
.component-list {
display: grid;
gap: 7px;
margin-top: 10px;
}
.component-button {
display: flex;
align-items: center;
gap: 9px;
width: 100%;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 10px;
color: #355151;
background: white;
cursor: pointer;
text-align: left;
font-size: 12px;
font-weight: 700;
transition: 150ms ease;
}
.component-button:hover {
border-color: #abc2bd;
background: #f8fbfa;
}
.component-button.active {
border-color: rgba(255, 181, 71, 0.8);
background: #fff9ed;
}
.component-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--dot);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--dot) 13%, transparent);
}
.selected-card {
margin-top: 20px;
padding: 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: #f8fbfa;
}
.selected-card .kicker {
color: #4b7c76;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.selected-card h3 {
margin: 5px 0 7px;
font-size: 17px;
letter-spacing: -0.02em;
}
.selected-card p {
margin: 0;
color: #5d7072;
font-size: 12px;
line-height: 1.5;
}
.selected-card .micro-note {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid #dfe9e6;
color: #486765;
font-size: 11px;
font-style: italic;
}
.trace-card {
margin-top: 14px;
padding: 16px;
border: 1px solid #cde8e2;
border-radius: 15px;
background: linear-gradient(145deg, #f0fbf8, #f8fcfb);
}
.trace-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.trace-heading h3 {
margin: 0;
font-size: 15px;
}
.count-badge {
min-width: 25px;
padding: 4px 7px;
border-radius: 999px;
color: #087d70;
background: #d8f5ee;
text-align: center;
font-size: 10px;
font-weight: 850;
}
.terminal-output {
margin: 10px 0 12px;
padding: 10px 11px;
border: 1px solid #d7ebe6;
border-radius: 10px;
color: #315b57;
background: rgba(255, 255, 255, 0.8);
font-size: 11px;
line-height: 1.45;
}
.terminal-output strong {
color: #173f3d;
}
.output-list {
display: grid;
gap: 8px;
margin: 0;
padding: 0;
list-style: none;
}
.output-item {
display: flex;
align-items: flex-start;
gap: 9px;
padding: 9px;
border: 1px solid rgba(48, 91, 87, 0.09);
border-radius: 9px;
background: rgba(255, 255, 255, 0.75);
}
.output-item .component-dot {
margin-top: 3px;
}
.output-item strong,
.output-item small {
display: block;
}
.output-item strong {
color: #284846;
font-size: 11px;
}
.output-item small {
margin-top: 3px;
color: #748687;
font-size: 9px;
line-height: 1.4;
}
.empty-output {
padding: 12px;
border: 1px dashed #bfd5d0;
border-radius: 9px;
color: #6b7e7f;
font-size: 11px;
line-height: 1.5;
}
.model-note {
margin-top: 15px;
padding: 15px;
border: 1px solid #eadfca;
border-radius: 14px;
background: #fffaf1;
}
.model-note h3 {
display: flex;
align-items: center;
gap: 7px;
margin: 0 0 8px;
color: #73541f;
font-size: 13px;
}
.model-note p {
margin: 0;
color: #786748;
font-size: 11px;
line-height: 1.55;
}
.model-note strong {
color: #5f461a;
}
.keyboard-help {
margin: 13px 2px 0;
color: #8a999a;
font-size: 10px;
line-height: 1.45;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
header {
height: 66px;
padding: 0 14px;
}
.brand p,
.teaching-badge {
display: none;
}
main {
height: auto;
min-height: calc(100vh - 66px);
grid-template-columns: 1fr;
grid-template-rows: minmax(500px, 62vh) auto;
}
aside {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.stage-intro {
top: 18px;
left: 18px;
max-width: 230px;
}
.stage-intro h2 {
font-size: 24px;
}
.stage-intro p {
display: none;
}
.stage-actions {
top: 16px;
right: 16px;
max-width: 145px;
}
.stage-button {
min-height: 32px;
padding: 7px 9px;
font-size: 10px;
}
.stage-legend {
left: 14px;
bottom: 14px;
gap: 9px;
padding: 9px;
}
.legend-item {
font-size: 9px;
}
}
</style>
</head>
<body>
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 32 32" fill="none">
<path d="M5 16h6m10 0h6M11 16l4-6 7 2 5 4M11 16l4 6 7-2"
stroke="#72E0CF" stroke-width="2.2"
stroke-linecap="round" stroke-linejoin="round"/>
<circle cx="5" cy="16" r="3" fill="#FFB547"/>
<circle cx="16" cy="9" r="3" fill="#72E0CF"/>
<circle cx="16" cy="23" r="3" fill="#B68AF3"/>
<circle cx="27" cy="16" r="3" fill="#72E0CF"/>
</svg>
</div>
<div class="brand-copy">
<h1>Reflex Circuit Lab</h1>
<p>Explore how signals move through a simplified neural pathway</p>
</div>
</div>
<div class="teaching-badge">Teaching model</div>
</header>
<main>
<section class="stage" id="stage" aria-label="Interactive three-dimensional reflex circuit">
<div id="viewport"></div>
<div id="labels" aria-hidden="true"></div>
<div class="stage-intro">
<span class="eyebrow">3D pathway</span>
<h2>Trace a reflex pathway</h2>
<p>
Select a component, then follow the directed arrows to see every
downstream structure it can reach.
</p>
</div>
<div class="stage-actions">
<button class="stage-button" id="reset-view" type="button">Reset view</button>
<button class="stage-button" id="replay-pulse" type="button">Replay pulse</button>
<button class="stage-button" id="toggle-labels" type="button" aria-pressed="true">
Labels on
</button>
</div>
<div class="stage-legend" aria-label="Scene legend">
<div class="legend-item">
<span class="legend-swatch"></span>
Excitatory route
</div>
<div class="legend-item">
<span class="legend-swatch inhibitory"></span>
Inhibitory route
</div>
<div class="legend-item">
<span class="legend-swatch selected"></span>
Selected
</div>
<div class="legend-item">
<span class="legend-swatch dimmed"></span>
Not in selected trace
</div>
</div>
<div id="loading">Loading the 3D circuit…</div>
</section>
<aside aria-label="Circuit controls and explanation">
<h2>Choose a starting component</h2>
<p class="section-copy">
Use the categories or click a component directly in the scene.
</p>
<div class="categories" role="group" aria-label="Component categories">
<button class="category-button active" data-category="sensory" type="button">
Sensory
<small>2 components</small>
</button>
<button class="category-button" data-category="interneuron" type="button">
Interneuron
<small>2 components</small>
</button>
<button class="category-button" data-category="motor" type="button">
Motor
<small>2 components</small>
</button>
</div>
<div class="component-list" id="component-list"></div>
<section class="selected-card" aria-live="polite">
<div class="kicker" id="selected-category">Sensory component</div>
<h3 id="selected-name">Sensory neuron</h3>
<p id="selected-description"></p>
<p class="micro-note" id="selected-note"></p>
</section>
<section class="trace-card" aria-live="polite">
<div class="trace-heading">
<h3>Reachable outputs</h3>
<span class="count-badge" id="trace-count">4</span>
</div>
<div class="terminal-output" id="terminal-output"></div>
<ul class="output-list" id="output-list"></ul>
</section>
<section class="model-note">
<h3>⚠ This diagram is deliberately simplified</h3>
<p>
Real reflex regulation is more complex than this feed-forward toy
circuit. It can involve <strong>many parallel pathways</strong>,
reciprocal inhibition, descending commands, recurrent feedback,
sensory thresholds, fatigue, changing synapse strength, and
context-dependent modulation. The highlighted outputs describe
graph reachability—not a complete prediction of movement.
</p>
</section>
<p class="keyboard-help">
Camera: drag to orbit, mouse wheel to zoom. Keyboard focus: arrow keys
orbit, + / − zoom, and R resets the view.
</p>
</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 labelsLayer = $("#labels");
const palette = {
selected: new THREE.Color("#ffb547"),
muted: new THREE.Color("#b8c6c4"),
excitatory: new THREE.Color("#18b7a4"),
inhibitory: new THREE.Color("#9b5de5")
};
const nodes = {
receptor: {
category: "sensory",
label: "Stretch receptor",
role: "Stimulus detector",
position: [-5.0, 1.05, 0.0],
color: "#ff7d63",
shape: "receptor",
description:
"Detects a change in the environment and converts it into an electrical signal.",
note:
"Real receptors encode stimulus strength, duration, and location rather than acting as simple on/off switches."
},
sensory: {
category: "sensory",
label: "Sensory neuron",
role: "Afferent signal carrier",
position: [-2.65, 1.55, -0.2],
color: "#ffad57",
shape: "sensory",
description:
"Carries incoming sensory information toward the central processing network.",
note:
"In a real animal, sensory axons may branch widely and release their signals at many different targets."
},
excite: {
category: "interneuron",
label: "Excitatory interneuron",
role: "Excitatory relay",
position: [-0.2, 2.5, -0.65],
color: "#36c9b5",
shape: "interneuron",
description:
"Provides an excitatory relay that can increase the likelihood of motor-neuron activation.",
note:
"Real excitatory interneurons operate as part of large, varied populations with different timing and target preferences."
},
inhibit: {
category: "interneuron",
label: "Inhibitory interneuron",
role: "Inhibitory relay",
position: [-0.2, 0.45, 1.25],
color: "#a96bea",
shape: "interneuron",
description:
"Provides an inhibitory branch that can reduce or shape the motor command.",
note:
"Inhibition in real circuits is not merely a brake; it can shape timing, select competing pathways, and coordinate opposing muscles."
},
motor: {
category: "motor",
label: "Motor neuron",
role: "Efferent command carrier",
position: [2.45, 1.35, 0.05],
color: "#3f8cff",
shape: "motor",
description:
"Integrates incoming signals and carries an outgoing command toward the effector.",
note:
"A real motor neuron receives many excitatory and inhibitory inputs, each with different strengths and timing."
},
muscle: {
category: "motor",
label: "Muscle / effector",
role: "Terminal output",
position: [5.0, 1.0, 0.0],
color: "#ed6683",
shape: "muscle",
description:
"Receives the command and produces a visible response by contracting.",
note:
"Muscle activation also depends on neuromuscular transmission, muscle state, load, fatigue, and feedback from the body."
}
};
const nodeOrder = Object.keys(nodes);
const edges = [
{
id: "receptor-sensory",
from: "receptor",
to: "sensory",
type: "excitatory",
controls: [[-4.1, 1.25, 0.0], [-3.4, 1.45, 0.0]]
},
{
id: "sensory-excite",
from: "sensory",
to: "excite",
type: "excitatory",
controls: [[-1.7, 2.2, -0.58]]
},
{
id: "sensory-inhibit",
from: "sensory",
to: "inhibit",
type: "inhibitory",
controls: [[-1.6, 0.82, 0.78]]
},
{
id: "excite-motor",
from: "excite",
to: "motor",
type: "excitatory",
controls: [[0.95, 2.2, -0.4], [1.7, 1.85, 0.0]]
},
{
id: "inhibit-motor",
from: "inhibit",
to: "motor",
type: "inhibitory",
controls: [[0.85, 0.65, 1.08], [1.65, 0.95, 0.58]]
},
{
id: "motor-muscle",
from: "motor",
to: "muscle",
type: "excitatory",
controls: [[3.65, 1.25, 0.04]]
}
];
const adjacency = new Map(nodeOrder.map((id) => [id, []]));
edges.forEach((edge) => adjacency.get(edge.from).push(edge.to));
let selectedId = "sensory";
let currentCategory = "sensory";
let reachable = new Set();
let labelsVisible = true;
let elapsed = 0;
// Renderer and scene ---------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"3D reflex circuit. Drag to orbit, scroll to zoom, and click a component."
);
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eaf2ef");
scene.fog = new THREE.Fog("#eaf2ef", 16, 32);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
100
);
const orbit = {
theta: 0.08,
phi: 1.03,
radius: 14.4
};
const cameraTarget = new THREE.Vector3(0, 1.1, 0);
function updateCamera() {
orbit.phi = THREE.MathUtils.clamp(orbit.phi, 0.55, 1.45);
orbit.radius = THREE.MathUtils.clamp(orbit.radius, 8.5, 20);
camera.position.set(
cameraTarget.x +
orbit.radius * Math.sin(orbit.phi) * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z +
orbit.radius * Math.sin(orbit.phi) * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
function resetView() {
orbit.theta = 0.08;
orbit.phi = 1.03;
orbit.radius = 14.4;
updateCamera();
}
updateCamera();
const hemisphereLight = new THREE.HemisphereLight(
"#f7fffc",
"#78908c",
2.25
);
scene.add(hemisphereLight);
const keyLight = new THREE.DirectionalLight("#ffffff", 3.2);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight("#9ee7dd", 1.35);
fillLight.position.set(8, 4, -6);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(32, 22),
new THREE.MeshStandardMaterial({
color: "#e4efeb",
roughness: 0.93,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const diagramPad = new THREE.Mesh(
new THREE.CircleGeometry(9.4, 14),
new THREE.MeshStandardMaterial({
color: "#f2f8f5",
roughness: 0.98,
polygonOffset: true,
polygonOffsetFactor: 1
})
);
diagramPad.rotation.x = -Math.PI / 2;
diagramPad.position.y = 0.012;
diagramPad.receiveShadow = true;
scene.add(diagramPad);
const grid = new THREE.GridHelper(24, 24, "#c5d9d4", "#d4e3df");
grid.position.y = 0.018;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const nodeObjects = new Map();
const pickables = [];
function makeStandardMaterial(color) {
return new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.2,
roughness: 0.48,
metalness: 0.04,
flatShading: true,
transparent: true,
opacity: 1
});
}
function addPedestal(node) {
const color = new THREE.Color(node.color);
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.8,
transparent: true,
opacity: 0.19,
flatShading: true
});
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(0.66, 0.78, 0.16, 6),
material
);
pedestal.position.set(node.position[0], 0.09, node.position[2]);
pedestal.receiveShadow = true;
scene.add(pedestal);
}
function createNode(id, data) {
const group = new THREE.Group();
group.position.fromArray(data.position);
group.userData.nodeId = id;
const material = makeStandardMaterial(data.color);
const materials = [material];
let mainMesh;
if (data.shape === "receptor") {
mainMesh = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.53, 0),
material
);
} else if (data.shape === "sensory") {
mainMesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.5, 0),
material
);
} else if (data.shape === "interneuron") {
mainMesh = new THREE.Mesh(
new THREE.OctahedronGeometry(0.56, 0),
material
);
} else if (data.shape === "motor") {
mainMesh = new THREE.Mesh(
new THREE.CylinderGeometry(0.32, 0.48, 0.95, 6),
material
);
mainMesh.rotation.z = Math.PI / 2;
} else {
mainMesh = new THREE.Mesh(
new THREE.CylinderGeometry(0.37, 0.47, 1.15, 8),
material
);
mainMesh.rotation.z = Math.PI / 2;
const bandMaterial = makeStandardMaterial("#ffd0d9");
materials.push(bandMaterial);
[-0.43, 0.43].forEach((x) => {
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.055, 5, 12),
bandMaterial
);
band.rotation.y = Math.PI / 2;
band.position.x = x;
band.castShadow = true;
group.add(band);
});
}
mainMesh.castShadow = true;
mainMesh.receiveShadow = true;
mainMesh.userData.nodeId = id;
group.add(mainMesh);
// A larger invisible mesh makes the small low-poly targets easier to click.
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const hitMesh = new THREE.Mesh(
new THREE.SphereGeometry(0.78, 10, 7),
hitMaterial
);
hitMesh.userData.nodeId = id;
group.add(hitMesh);
group.traverse((child) => {
if (child.isMesh && child.userData.nodeId) {
pickables.push(child);
}
});
scene.add(group);
addPedestal(data);
const label = document.createElement("div");
label.className = "node-label";
label.innerHTML = `
<span>${data.label}</span>
<small>${data.role}</small>
`;
labelsLayer.appendChild(label);
nodeObjects.set(id, {
group,
mainMesh,
materials,
baseColor: new THREE.Color(data.color),
label,
labelOffset: new THREE.Vector3(0, 0.86, 0)
});
}
nodeOrder.forEach((id) => createNode(id, nodes[id]));
const edgeObjects = new Map();
function createEdge(edge) {
const start = new THREE.Vector3().fromArray(nodes[edge.from].position);
const end = new THREE.Vector3().fromArray(nodes[edge.to].position);
const controls = edge.controls.map(
(point) => new THREE.Vector3().fromArray(point)
);
const curve = new THREE.CatmullRomCurve3(
[start, ...controls, end],
false,
"catmullrom",
0.38
);
const color =
edge.type === "inhibitory"
? palette.inhibitory
: palette.excitatory;
const tubeMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.32,
roughness: 0.45,
metalness: 0.02,
transparent: true,
opacity: 0.85,
flatShading: true
});
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 42, 0.065, 6, false),
tubeMaterial
);
tube.castShadow = true;
scene.add(tube);
const arrowMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.3,
roughness: 0.45,
transparent: true,
opacity: 0.9,
flatShading: true
});
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.38, 6),
arrowMaterial
);
const arrowPoint = curve.getPointAt(0.79);
const tangent = curve.getTangentAt(0.79).normalize();
arrow.position.copy(arrowPoint);
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
arrow.castShadow = true;
scene.add(arrow);
const particleGeometry = new THREE.SphereGeometry(0.105, 7, 5);
const particles = [];
for (let i = 0; i < 2; i++) {
const particleMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.1,
roughness: 0.25,
transparent: true,
opacity: 0.95
});
const particle = new THREE.Mesh(
particleGeometry,
particleMaterial
);
particle.castShadow = true;
scene.add(particle);
particles.push(particle);
}
edgeObjects.set(edge.id, {
curve,
tube,
tubeMaterial,
arrow,
arrowMaterial,
particles,
baseColor: color.clone(),
type: edge.type
});
}
edges.forEach(createEdge);
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.78, 0.035, 6, 36),
new THREE.MeshBasicMaterial({
color: palette.selected,
transparent: true,
opacity: 0.85
})
);
selectionRing.rotation.x = Math.PI / 2;
scene.add(selectionRing);
// Reachability and UI --------------------------------------------------
function computeReachable(startId) {
const found = new Set([startId]);
const queue = [startId];
while (queue.length) {
const current = queue.shift();
adjacency.get(current).forEach((nextId) => {
if (!found.has(nextId)) {
found.add(nextId);
queue.push(nextId);
}
});
}
return found;
}
function findPath(targetId) {
const parents = new Map([[selectedId, null]]);
const queue = [selectedId];
while (queue.length) {
const current = queue.shift();
for (const nextId of adjacency.get(current)) {
if (!parents.has(nextId)) {
parents.set(nextId, current);
queue.push(nextId);
}
}
}
if (!parents.has(targetId)) return [];
const path = [];
let current = targetId;
while (current !== null) {
path.unshift(nodes[current].label);
current = parents.get(current);
}
return path;
}
function updateVisuals() {
const selected = nodes[selectedId];
for (const id of nodeOrder) {
const object = nodeObjects.get(id);
const isSelected = id === selectedId;
const isReachable = reachable.has(id);
let color;
if (isSelected) {
color = palette.selected.clone();
} else if (isReachable) {
color = object.baseColor.clone();
} else {
color = object.baseColor.clone().lerp(palette.muted, 0.72);
}
object.materials.forEach((material) => {
material.color.copy(color);
material.emissive.copy(color);
material.emissiveIntensity = isSelected
? 0.42
: isReachable
? 0.2
: 0.04;
material.opacity = isSelected || isReachable ? 1 : 0.3;
});
object.label.classList.toggle("selected", isSelected);
object.label.classList.toggle("reached", isReachable && !isSelected);
object.label.classList.toggle("unreached", !isReachable);
}
for (const edge of edges) {
const object = edgeObjects.get(edge.id);
const isReachable =
reachable.has(edge.from) && reachable.has(edge.to);
const color = isReachable
? object.baseColor.clone()
: palette.muted.clone();
object.tubeMaterial.color.copy(color);
object.tubeMaterial.emissive.copy(color);
object.tubeMaterial.emissiveIntensity = isReachable ? 0.48 : 0.02;
object.tubeMaterial.opacity = isReachable ? 0.95 : 0.16;
object.arrowMaterial.color.copy(color);
object.arrowMaterial.emissive.copy(color);
object.arrowMaterial.emissiveIntensity = isReachable ? 0.42 : 0.01;
object.arrowMaterial.opacity = isReachable ? 1 : 0.18;
object.arrow.visible = isReachable;
object.particles.forEach((particle) => {
particle.visible = isReachable;
particle.material.color.copy(color);
particle.material.emissive.copy(color);
particle.material.opacity = isReachable ? 1 : 0;
});
}
selectionRing.position.set(
selected.position[0],
0.22,
selected.position[2]
);
}
function renderCategories() {
document.querySelectorAll(".category-button").forEach((button) => {
const active = button.dataset.category === currentCategory;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
}
function renderComponentList() {
const list = $("#component-list");
list.replaceChildren();
nodeOrder
.filter((id) => nodes[id].category === currentCategory)
.forEach((id) => {
const data = nodes[id];
const button = document.createElement("button");
button.type = "button";
button.className = "component-button";
button.classList.toggle("active", id === selectedId);
button.setAttribute("aria-pressed", String(id === selectedId));
button.style.setProperty("--dot", data.color);
button.innerHTML = `
<span class="component-dot" aria-hidden="true"></span>
<span>${data.label}</span>
`;
button.addEventListener("click", () => selectNode(id));
list.appendChild(button);
});
}
function updatePanel() {
const data = nodes[selectedId];
const downstream = nodeOrder.filter(
(id) => id !== selectedId && reachable.has(id)
);
const terminals = downstream.filter(
(id) => adjacency.get(id).length === 0
);
$("#selected-category").textContent =
`${data.category[0].toUpperCase()}${data.category.slice(1)} component`;
$("#selected-name").textContent = data.label;
$("#selected-description").textContent = data.description;
$("#selected-note").textContent = data.note;
$("#trace-count").textContent = String(downstream.length);
if (terminals.length) {
$("#terminal-output").innerHTML =
`<strong>Terminal output:</strong> ` +
terminals.map((id) => nodes[id].label).join(", ");
} else {
$("#terminal-output").innerHTML =
`<strong>Terminal component:</strong> ` +
`${data.label}. No downstream structures are drawn.`;
}
const list = $("#output-list");
list.replaceChildren();
if (!downstream.length) {
const empty = document.createElement("li");
empty.className = "empty-output";
empty.textContent =
"This is the terminal output in the toy circuit.";
list.appendChild(empty);
return;
}
downstream.forEach((id) => {
const output = nodes[id];
const path = findPath(id);
const item = document.createElement("li");
item.className = "output-item";
item.style.setProperty("--dot", output.color);
item.innerHTML = `
<span class="component-dot" aria-hidden="true"></span>
<div>
<strong>${output.label}</strong>
<small>${path.join(" → ")}</small>
</div>
`;
list.appendChild(item);
});
}
function selectNode(id) {
selectedId = id;
currentCategory = nodes[id].category;
reachable = computeReachable(selectedId);
renderCategories();
renderComponentList();
updateVisuals();
updatePanel();
}
document.querySelectorAll(".category-button").forEach((button) => {
button.addEventListener("click", () => {
const category = button.dataset.category;
currentCategory = category;
if (nodes[selectedId].category !== category) {
const first = nodeOrder.find(
(id) => nodes[id].category === category
);
selectNode(first);
return;
}
renderCategories();
renderComponentList();
});
});
// Custom camera controls ------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
let dragged = false;
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickNode(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, false);
if (!intersections.length) return;
const nodeId = intersections[0].object.userData.nodeId;
if (nodeId) selectNode(nodeId);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = {
id: event.pointerId,
x: event.clientX,
y: event.clientY
};
dragged = false;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown || pointerDown.id !== event.pointerId) return;
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 4) {
dragged = true;
}
orbit.theta -= dx * 0.006;
orbit.phi -= dy * 0.005;
updateCamera();
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerDown || pointerDown.id !== event.pointerId) return;
if (!dragged) {
pickNode(event);
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
pointerDown = null;
renderer.domElement.classList.remove("dragging");
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
renderer.domElement.classList.remove("dragging");
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius += event.deltaY * 0.009;
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
orbit.theta -= 0.12;
break;
case "ArrowRight":
orbit.theta += 0.12;
break;
case "ArrowUp":
orbit.phi -= 0.08;
break;
case "ArrowDown":
orbit.phi += 0.08;
break;
case "+":
case "=":
orbit.radius -= 0.55;
break;
case "-":
case "_":
orbit.radius += 0.55;
break;
case "r":
case "R":
resetView();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
$("#reset-view").addEventListener("click", resetView);
$("#replay-pulse").addEventListener("click", () => {
elapsed = 0;
});
$("#toggle-labels").addEventListener("click", (event) => {
labelsVisible = !labelsVisible;
labelsLayer.classList.toggle("hidden", !labelsVisible);
event.currentTarget.textContent = labelsVisible ? "Labels on" : "Labels off";
event.currentTarget.setAttribute(
"aria-pressed",
String(labelsVisible)
);
});
// Resize and animation ---------------------------------------------------
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(viewport);
resize();
const clock = new THREE.Clock();
const projected = new THREE.Vector3();
const labelWorld = new THREE.Vector3();
function updateLabels() {
if (!labelsVisible) return;
for (const id of nodeOrder) {
const object = nodeObjects.get(id);
const node = nodes[id];
labelWorld
.copy(object.group.position)
.add(object.labelOffset)
.project(camera);
const x = (labelWorld.x * 0.5 + 0.5) * viewport.clientWidth;
const y = (-labelWorld.y * 0.5 + 0.5) * viewport.clientHeight;
const visible =
labelWorld.z > -1 &&
labelWorld.z < 1 &&
x > -80 &&
x < viewport.clientWidth + 80 &&
y > -50 &&
y < viewport.clientHeight + 50;
object.label.style.display = visible ? "block" : "none";
object.label.style.left = `${x}px`;
object.label.style.top = `${y}px`;
}
}
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
for (const edge of edges) {
const object = edgeObjects.get(edge.id);
if (!object.particles[0].visible) continue;
const speed = edge.type === "inhibitory" ? 0.19 : 0.27;
object.particles.forEach((particle, index) => {
const offset = index * 0.5;
const t = (elapsed * speed + offset) % 1;
particle.position.copy(object.curve.getPointAt(t));
});
}
const pulse = 1 + Math.sin(elapsed * 3.2) * 0.07;
selectionRing.scale.setScalar(pulse);
selectionRing.material.opacity = 0.66 + Math.sin(elapsed * 3.2) * 0.16;
updateLabels();
renderer.render(scene, camera);
});
// Initial state ----------------------------------------------------------
selectNode(selectedId);
$("#loading").remove();
</script>
</body>
</html>
```