Prompt specification
Build or customize the Three.js 3D scene "Urban Habitat Corridor Planning Diorama" 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: Urban Habitat Corridor Planning Diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/urban-habitat-corridor-planning-diorama-x3943
Scene Description Prompt:
A voxel neighborhood habitat puzzle comparing green-link connectivity for a fictional species movement model. Three.js from cdnjs, one HTML file.
Working Three.js Starter (urban-habitat-corridor-planning-diorama-x3943):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Verdant Relay — Voxel Connectivity Puzzle</title>
<style>
:root {
color-scheme: dark;
--bg: #07110d;
--panel: #0c1813;
--panel-2: #10231a;
--line: rgba(160, 255, 192, 0.13);
--line-strong: rgba(160, 255, 192, 0.28);
--text: #e7f7ec;
--muted: #88a796;
--green: #61ef9a;
--lime: #c7ff70;
--amber: #ffc06c;
--danger: #ff806f;
--blue: #70d7ff;
--radius: 16px;
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
--sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
background: var(--bg);
color: var(--text);
font-family: var(--sans);
}
body {
overflow: hidden;
}
button {
font: inherit;
}
.app {
width: 100vw;
height: 100vh;
display: grid;
grid-template-rows: 74px minmax(0, 1fr);
background:
radial-gradient(circle at 75% 15%, rgba(69, 191, 111, 0.08), transparent 30%),
var(--bg);
}
.topbar {
position: relative;
z-index: 20;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 22px;
border-bottom: 1px solid var(--line);
background: rgba(7, 17, 13, 0.92);
backdrop-filter: blur(16px);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 3px;
flex: 0 0 auto;
transform: rotate(-2deg);
}
.brand-mark i {
display: block;
border-radius: 2px;
background: #193b28;
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(4),
.brand-mark i:nth-child(6),
.brand-mark i:nth-child(9) {
background: var(--green);
box-shadow: 0 0 9px rgba(97, 239, 154, 0.25);
}
.brand-mark i:nth-child(8) {
background: var(--lime);
}
.brand-copy {
min-width: 0;
}
.brand-title {
margin: 0;
font-size: 15px;
font-weight: 800;
letter-spacing: 0.15em;
white-space: nowrap;
}
.brand-subtitle {
margin-top: 4px;
color: var(--muted);
font: 10px/1 var(--mono);
letter-spacing: 0.08em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.top-actions {
display: flex;
align-items: center;
gap: 12px;
}
.status-pill {
display: flex;
align-items: center;
gap: 8px;
min-height: 36px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
background: rgba(255, 255, 255, 0.025);
font: 11px/1 var(--mono);
white-space: nowrap;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 9px rgba(255, 192, 108, 0.45);
}
.status-pill.complete {
color: var(--green);
border-color: rgba(97, 239, 154, 0.35);
background: rgba(97, 239, 154, 0.07);
}
.status-pill.complete .status-dot {
background: var(--green);
box-shadow: 0 0 11px rgba(97, 239, 154, 0.75);
}
.primary-button,
.secondary-button,
.text-button {
border: 0;
cursor: pointer;
transition:
transform 140ms ease,
border-color 140ms ease,
background 140ms ease,
color 140ms ease;
}
.primary-button,
.secondary-button {
min-height: 39px;
padding: 0 14px;
border-radius: 10px;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.02em;
}
.primary-button {
color: #082013;
background: var(--green);
box-shadow: 0 7px 22px rgba(49, 214, 114, 0.12);
}
.primary-button:hover,
.secondary-button:hover {
transform: translateY(-1px);
}
.primary-button:active,
.secondary-button:active {
transform: translateY(0);
}
.secondary-button {
color: var(--text);
border: 1px solid var(--line-strong);
background: rgba(255, 255, 255, 0.035);
}
.secondary-button.active {
color: var(--lime);
border-color: rgba(199, 255, 112, 0.5);
background: rgba(199, 255, 112, 0.08);
}
.layout {
min-height: 0;
display: grid;
grid-template-columns: 356px minmax(0, 1fr);
}
.sidebar {
position: relative;
z-index: 10;
min-height: 0;
overflow: auto;
padding: 24px 22px 28px;
border-right: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(15, 34, 25, 0.76), rgba(8, 20, 14, 0.96)),
var(--panel);
scrollbar-width: thin;
scrollbar-color: #264535 transparent;
}
.eyebrow {
margin: 0 0 9px;
color: var(--green);
font: 10px/1 var(--mono);
font-weight: 700;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 290px;
font-size: clamp(27px, 3vw, 38px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.intro {
margin: 13px 0 19px;
color: #a4b9ac;
font-size: 13px;
line-height: 1.55;
}
.intro strong {
color: var(--text);
font-weight: 650;
}
.section {
margin-top: 20px;
padding-top: 19px;
border-top: 1px solid var(--line);
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.section-heading h2 {
margin: 0;
color: #b9cdbf;
font: 10px/1 var(--mono);
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-heading span {
color: #658173;
font: 9px/1 var(--mono);
}
.model-card {
display: grid;
grid-template-columns: 42px 1fr;
gap: 12px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.025);
}
.model-icon {
width: 42px;
height: 42px;
position: relative;
border: 1px solid rgba(97, 239, 154, 0.25);
border-radius: 10px;
background: rgba(97, 239, 154, 0.06);
}
.model-icon::before,
.model-icon::after {
content: "";
position: absolute;
border-radius: 2px;
background: var(--green);
}
.model-icon::before {
width: 8px;
height: 8px;
left: 10px;
top: 10px;
box-shadow: 12px 0 0 #3a9360, 0 12px 0 #3a9360, 12px 12px 0 var(--lime);
}
.model-icon::after {
display: none;
}
.model-card h3 {
margin: 0 0 5px;
font-size: 13px;
}
.model-card p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.model-tags {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 9px;
}
.model-tags span {
padding: 4px 7px;
border: 1px solid var(--line);
border-radius: 5px;
color: #a9c0b1;
background: rgba(255, 255, 255, 0.025);
font: 8px/1 var(--mono);
letter-spacing: 0.06em;
text-transform: uppercase;
}
.grove-list {
display: grid;
gap: 7px;
}
.grove-row {
display: grid;
grid-template-columns: 30px minmax(0, 1fr) auto;
align-items: center;
gap: 9px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.018);
}
.grove-id {
width: 28px;
height: 28px;
display: grid;
place-items: center;
border-radius: 7px;
color: var(--amber);
background: rgba(255, 192, 108, 0.1);
font: 11px/1 var(--mono);
font-weight: 800;
}
.grove-row.linked .grove-id {
color: var(--green);
background: rgba(97, 239, 154, 0.12);
}
.grove-name {
overflow: hidden;
color: #c6d7cb;
font-size: 11px;
font-weight: 650;
text-overflow: ellipsis;
white-space: nowrap;
}
.grove-state {
margin-top: 3px;
color: #718d7c;
font: 8px/1 var(--mono);
letter-spacing: 0.04em;
text-transform: uppercase;
}
.grove-row.linked .grove-state {
color: var(--green);
}
.route-length {
color: #789487;
font: 9px/1 var(--mono);
white-space: nowrap;
}
.map-shell {
padding: 10px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(3, 12, 8, 0.32);
}
.map-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
color: #789487;
font: 9px/1 var(--mono);
}
.map-grid {
display: grid;
grid-template-columns: repeat(9, 1fr);
gap: 3px;
aspect-ratio: 1;
}
.map-cell {
position: relative;
min-width: 0;
padding: 0;
border: 1px solid rgba(130, 176, 145, 0.13);
border-radius: 3px;
background: #15271e;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease,
box-shadow 120ms ease;
}
.map-cell:hover:not(:disabled) {
z-index: 2;
transform: scale(1.16);
border-color: rgba(97, 239, 154, 0.55);
}
.map-cell.green {
background: #278451;
border-color: #4ac47a;
box-shadow: inset 0 0 7px rgba(125, 255, 168, 0.16);
}
.map-cell.blocked {
background:
repeating-linear-gradient(
135deg,
#28302c,
#28302c 3px,
#202824 3px,
#202824 6px
);
border-color: rgba(176, 193, 182, 0.12);
cursor: not-allowed;
}
.map-cell.source {
border-color: var(--amber);
box-shadow: 0 0 0 1px rgba(255, 192, 108, 0.14);
}
.map-cell.source.linked {
border-color: var(--green);
box-shadow: 0 0 8px rgba(97, 239, 154, 0.22);
}
.map-cell.nest {
border-color: var(--lime);
background: #3a5f2e;
box-shadow: 0 0 9px rgba(199, 255, 112, 0.2);
}
.map-cell .cell-label {
position: absolute;
inset: 0;
display: grid;
place-items: center;
color: #fff6d6;
font: 700 8px/1 var(--mono);
text-shadow: 0 1px 2px #000;
}
.map-cell.nest .cell-label {
color: var(--lime);
}
.map-legend {
display: flex;
flex-wrap: wrap;
gap: 11px;
margin-top: 10px;
color: #718d7c;
font: 8px/1 var(--mono);
}
.legend-item {
display: flex;
align-items: center;
gap: 5px;
}
.legend-swatch {
width: 8px;
height: 8px;
border-radius: 2px;
background: #278451;
}
.legend-swatch.source {
background: var(--amber);
}
.legend-swatch.rock {
background: #38423c;
}
.budget-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 10px;
margin-bottom: 8px;
color: #91aa9b;
font: 9px/1 var(--mono);
}
.budget-row strong {
color: var(--text);
font-weight: 700;
}
.budget-track {
height: 6px;
overflow: hidden;
border-radius: 999px;
background: #172a20;
}
.budget-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #2faa65, var(--green));
box-shadow: 0 0 10px rgba(97, 239, 154, 0.3);
transition: width 240ms ease;
}
.comparison-card {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.018);
}
.comparison-head {
display: grid;
grid-template-columns: 1fr 58px 58px;
gap: 6px;
padding: 10px 11px;
border-bottom: 1px solid var(--line);
color: #678274;
font: 8px/1 var(--mono);
letter-spacing: 0.08em;
text-transform: uppercase;
}
.comparison-head span:not(:first-child) {
text-align: right;
}
.metric-row {
display: grid;
grid-template-columns: 1fr 58px 58px;
gap: 6px;
align-items: center;
padding: 10px 11px;
border-bottom: 1px solid rgba(160, 255, 192, 0.07);
font: 9px/1 var(--mono);
}
.metric-row:last-child {
border-bottom: 0;
}
.metric-label {
color: #9eb3a5;
font-family: var(--sans);
font-size: 10px;
}
.metric-before,
.metric-after {
text-align: right;
}
.metric-before {
color: #718d7c;
}
.metric-after {
color: var(--green);
font-weight: 800;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.actions .secondary-button {
width: 100%;
}
.text-button {
width: 100%;
margin-top: 9px;
padding: 7px;
color: #789487;
background: transparent;
font: 9px/1 var(--mono);
}
.text-button:hover {
color: var(--text);
}
.viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #07130e;
}
#canvas-host {
position: absolute;
inset: 0;
}
#canvas-host canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.viewport::after {
content: "";
position: absolute;
inset: 0;
z-index: 2;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 45%, rgba(0, 8, 4, 0.28) 100%),
linear-gradient(180deg, rgba(5, 15, 10, 0.08), transparent 30%, rgba(5, 15, 10, 0.22));
}
.scene-top {
position: absolute;
top: 18px;
left: 18px;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 10px;
color: #b9d0c0;
background: rgba(6, 18, 12, 0.72);
backdrop-filter: blur(10px);
font: 9px/1 var(--mono);
letter-spacing: 0.08em;
pointer-events: none;
}
.scene-top i {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 8px rgba(97, 239, 154, 0.5);
}
.compare-label {
position: absolute;
top: 20px;
z-index: 6;
display: none;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 8px;
color: #90a99a;
background: rgba(5, 15, 10, 0.76);
backdrop-filter: blur(8px);
font: 9px/1 var(--mono);
letter-spacing: 0.08em;
pointer-events: none;
}
#baseline-label {
left: 18px;
}
#plan-label {
left: calc(50% + 18px);
color: var(--green);
}
.compare-divider {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
z-index: 5;
display: none;
width: 1px;
background: linear-gradient(
transparent,
rgba(160, 255, 192, 0.2) 18%,
rgba(160, 255, 192, 0.2) 82%,
transparent
);
pointer-events: none;
}
.viewport.comparing .compare-label,
.viewport.comparing .compare-divider {
display: block;
}
.viewport.comparing .scene-top {
display: none;
}
.scene-help {
position: absolute;
right: 18px;
bottom: 18px;
z-index: 5;
display: flex;
align-items: center;
gap: 15px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 10px;
color: #789487;
background: rgba(6, 18, 12, 0.7);
backdrop-filter: blur(10px);
font: 8px/1 var(--mono);
pointer-events: none;
}
.scene-help b {
color: #b9d0c0;
font-weight: 600;
}
.toast {
position: absolute;
left: 50%;
bottom: 22px;
z-index: 12;
max-width: min(90%, 420px);
padding: 11px 15px;
border: 1px solid rgba(97, 239, 154, 0.24);
border-radius: 10px;
color: #c9ddcf;
background: rgba(10, 29, 19, 0.94);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.3);
font-size: 11px;
text-align: center;
opacity: 0;
transform: translate(-50%, 10px);
transition:
opacity 180ms ease,
transform 180ms ease;
pointer-events: none;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.win-banner {
position: absolute;
top: 50%;
left: 50%;
z-index: 10;
width: min(360px, calc(100% - 40px));
padding: 24px;
border: 1px solid rgba(97, 239, 154, 0.42);
border-radius: 18px;
text-align: center;
background:
radial-gradient(circle at 50% 0%, rgba(97, 239, 154, 0.14), transparent 55%),
rgba(8, 25, 16, 0.94);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.45);
opacity: 0;
transform: translate(-50%, -42%) scale(0.96);
transition:
opacity 250ms ease,
transform 250ms ease;
pointer-events: none;
}
.win-banner.show {
opacity: 1;
transform: translate(-50%, -50%) scale(1);
}
.win-icon {
width: 46px;
height: 46px;
display: grid;
place-items: center;
margin: 0 auto 13px;
border-radius: 13px;
color: #092013;
background: var(--green);
font-size: 22px;
font-weight: 900;
box-shadow: 0 0 30px rgba(97, 239, 154, 0.28);
}
.win-banner h2 {
margin: 0 0 7px;
font-size: 22px;
letter-spacing: -0.025em;
}
.win-banner p {
margin: 0;
color: #96b19f;
font-size: 12px;
line-height: 1.5;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
grid-template-rows: 68px auto;
}
.brand-subtitle {
display: none;
}
.status-pill {
display: none;
}
.layout {
display: flex;
flex-direction: column;
}
.sidebar {
order: 2;
overflow: visible;
border-right: 0;
border-top: 1px solid var(--line);
}
.viewport {
order: 1;
height: 65vh;
min-height: 470px;
}
.scene-help {
display: none;
}
}
@media (max-width: 540px) {
.topbar {
padding: 0 12px;
}
.brand-title {
font-size: 12px;
}
.brand-mark {
width: 32px;
height: 32px;
}
#compare-button {
padding: 0 10px;
font-size: 10px;
}
.sidebar {
padding: 22px 15px;
}
.viewport {
min-height: 430px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
<i></i><i></i><i></i>
<i></i><i></i><i></i>
</div>
<div class="brand-copy">
<p class="brand-title">VERDANT RELAY</p>
<div class="brand-subtitle">VOXEL CONNECTIVITY STUDY // MOSS WEAVER</div>
</div>
</div>
<div class="top-actions">
<div id="status-pill" class="status-pill">
<span class="status-dot"></span>
<span id="status-text">0 / 3 GROVES LINKED</span>
</div>
<button id="compare-button" class="secondary-button" type="button">
Compare baseline
</button>
</div>
</header>
<main class="layout">
<aside class="sidebar">
<p class="eyebrow">Field objective 07</p>
<h1>Link the three groves.</h1>
<p class="intro">
The fictional <strong>Mossweaver</strong> can cross only edge-touching
verdant voxels. Build a continuous green corridor from every feeding
grove to the central nesting beacon.
</p>
<section class="section">
<div class="section-heading">
<h2>Movement model</h2>
<span>SPECIES: M-07</span>
</div>
<div class="model-card">
<div class="model-icon" aria-hidden="true"></div>
<div>
<h3>Edge-adjacent traversal</h3>
<p>Green links must touch horizontally or vertically. Diagonal contact does not count.</p>
<div class="model-tags">
<span>4-way</span>
<span>No elevation jump</span>
<span>Living links only</span>
</div>
</div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Feeding groves</h2>
<span>LIVE LINK STATUS</span>
</div>
<div id="grove-list" class="grove-list"></div>
</section>
<section class="section">
<div class="section-heading">
<h2>Habitat map</h2>
<span>CLICK A CELL TO LINK</span>
</div>
<div class="map-shell">
<div class="map-header">
<span>VOXEL FIELD / 9 × 9</span>
<span id="budget-label">0 / 9 LINKS</span>
</div>
<div id="map-grid" class="map-grid" aria-label="Interactive habitat map"></div>
<div class="map-legend">
<span class="legend-item"><i class="legend-swatch"></i>Verdant link</span>
<span class="legend-item"><i class="legend-swatch source"></i>Feeding grove</span>
<span class="legend-item"><i class="legend-swatch rock"></i>Rock</span>
</div>
</div>
<div style="margin-top: 13px">
<div class="budget-row">
<span>LINK BUDGET</span>
<strong id="budget-text">0 / 9 used</strong>
</div>
<div class="budget-track">
<div id="budget-fill" class="budget-fill"></div>
</div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Connectivity comparison</h2>
<span>BASELINE → PLAN</span>
</div>
<div class="comparison-card">
<div class="comparison-head">
<span>Metric</span>
<span>Before</span>
<span>After</span>
</div>
<div class="metric-row">
<span class="metric-label">Linked groves</span>
<span id="metric-linked-before" class="metric-before">0 / 3</span>
<span id="metric-linked-after" class="metric-after">0 / 3</span>
</div>
<div class="metric-row">
<span class="metric-label">Network islands</span>
<span id="metric-components-before" class="metric-before">4</span>
<span id="metric-components-after" class="metric-after">4</span>
</div>
<div class="metric-row">
<span class="metric-label">Longest relay</span>
<span id="metric-route-before" class="metric-before">—</span>
<span id="metric-route-after" class="metric-after">—</span>
</div>
<div class="metric-row">
<span class="metric-label">Added links</span>
<span class="metric-before">0</span>
<span id="metric-added" class="metric-after">0</span>
</div>
</div>
<div class="actions">
<button id="hint-button" class="secondary-button" type="button">Suggest a link</button>
<button id="undo-button" class="secondary-button" type="button">Undo link</button>
</div>
<button id="reset-button" class="text-button" type="button">RESET FIELD PLAN</button>
</section>
</aside>
<section id="viewport" class="viewport">
<div id="canvas-host"></div>
<div class="scene-top"><i></i><span id="scene-top-text">YOUR PLAN / LIVE MODEL</span></div>
<div id="baseline-label" class="compare-label">BASELINE / SURVEY 07</div>
<div id="plan-label" class="compare-label">YOUR PLAN / LIVE MODEL</div>
<div class="compare-divider"></div>
<div class="scene-help">
<span><b>DRAG</b> orbit</span>
<span><b>SCROLL</b> zoom</span>
<span><b>CLICK</b> place link</span>
</div>
<div id="toast" class="toast"></div>
<div id="win-banner" class="win-banner">
<div class="win-icon">✓</div>
<h2>Network viable</h2>
<p>All three feeding groves now share one continuous, edge-connected habitat.</p>
</div>
</section>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Puzzle data
// ---------------------------------------------------------------------
const COLS = 9;
const ROWS = 9;
const BUDGET = 9;
const SOURCES = [
{ id: "A", x: 1, z: 1, name: "West Canopy" },
{ id: "B", x: 7, z: 1, name: "East Canopy" },
{ id: "C", x: 4, z: 8, name: "South Fern" }
];
const NEST = { x: 4, z: 4 };
const BLOCKED = new Set(
[
[0, 3], [1, 4], [2, 5], [3, 6],
[6, 6], [7, 5], [8, 3], [2, 2],
[6, 2], [0, 7], [8, 7]
].map(([x, z]) => indexOf(x, z))
);
const INITIAL_GREEN = new Set(
[
[1, 1], [2, 1], [3, 1],
[7, 1], [7, 2], [7, 3],
[4, 7], [4, 8],
[4, 4]
].map(([x, z]) => indexOf(x, z))
);
// A valid optimal seven-link route, used only by the hint button.
const SOLUTION_HINT = [
[4, 3], [4, 2], [4, 1],
[5, 3], [6, 3],
[4, 5], [4, 6]
].map(([x, z]) => indexOf(x, z));
let planGreen = new Set(INITIAL_GREEN);
let addedCells = [];
let comparing = false;
let won = false;
let toastTimer = 0;
const $ = (selector) => document.querySelector(selector);
function indexOf(x, z) {
return z * COLS + x;
}
function coordinatesOf(index) {
return {
x: index % COLS,
z: Math.floor(index / COLS)
};
}
function neighbors(index) {
const { x, z } = coordinatesOf(index);
const result = [];
if (x > 0) result.push(indexOf(x - 1, z));
if (x < COLS - 1) result.push(indexOf(x + 1, z));
if (z > 0) result.push(indexOf(x, z - 1));
if (z < ROWS - 1) result.push(indexOf(x, z + 1));
return result;
}
function isAdjacentToGreen(index) {
return neighbors(index).some((n) => planGreen.has(n));
}
// ---------------------------------------------------------------------
// DOM construction and interface state
// ---------------------------------------------------------------------
const mapGrid = $("#map-grid");
const mapButtons = [];
for (let z = 0; z < ROWS; z++) {
for (let x = 0; x < COLS; x++) {
const index = indexOf(x, z);
const button = document.createElement("button");
button.type = "button";
button.className = "map-cell";
button.dataset.index = String(index);
button.setAttribute(
"aria-label",
`Habitat cell ${x + 1}, ${z + 1}${BLOCKED.has(index) ? ", blocked" : ""}`
);
const label = document.createElement("span");
label.className = "cell-label";
button.appendChild(label);
button.addEventListener("click", () => attemptToAdd(index));
mapGrid.appendChild(button);
mapButtons.push(button);
}
}
const groveList = $("#grove-list");
const groveRows = {};
for (const source of SOURCES) {
const row = document.createElement("div");
row.className = "grove-row";
const id = document.createElement("div");
id.className = "grove-id";
id.textContent = source.id;
const copy = document.createElement("div");
const name = document.createElement("div");
name.className = "grove-name";
name.textContent = source.name;
const state = document.createElement("div");
state.className = "grove-state";
state.textContent = "Not connected";
copy.append(name, state);
const route = document.createElement("div");
route.className = "route-length";
route.textContent = "—";
row.append(id, copy, route);
groveList.appendChild(row);
groveRows[source.id] = { row, state, route };
}
function showToast(message) {
const toast = $("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
// ---------------------------------------------------------------------
// Connectivity calculations
// ---------------------------------------------------------------------
function distanceField(startIndex, greenSet) {
const distances = new Map();
const queue = [startIndex];
distances.set(startIndex, 0);
for (let cursor = 0; cursor < queue.length; cursor++) {
const current = queue[cursor];
const distance = distances.get(current);
for (const next of neighbors(current)) {
if (
greenSet.has(next) &&
!BLOCKED.has(next) &&
!distances.has(next)
) {
distances.set(next, distance + 1);
queue.push(next);
}
}
}
return distances;
}
function countComponents(greenSet) {
const visited = new Set();
let components = 0;
for (const index of greenSet) {
if (visited.has(index)) continue;
components++;
const queue = [index];
visited.add(index);
for (let cursor = 0; cursor < queue.length; cursor++) {
const current = queue[cursor];
for (const next of neighbors(current)) {
if (greenSet.has(next) && !visited.has(next)) {
visited.add(next);
queue.push(next);
}
}
}
}
return components;
}
function analyze(greenSet) {
const nestIndex = indexOf(NEST.x, NEST.z);
const distances = distanceField(nestIndex, greenSet);
const sourceDistances = {};
let connected = 0;
let longest = null;
for (const source of SOURCES) {
const distance = distances.get(indexOf(source.x, source.z)) ?? null;
sourceDistances[source.id] = distance;
if (distance !== null) {
connected++;
longest = longest === null ? distance : Math.max(longest, distance);
}
}
return {
connected,
components: countComponents(greenSet),
longest,
sourceDistances
};
}
function updateInterface() {
const baseline = analyze(INITIAL_GREEN);
const current = analyze(planGreen);
const wasWon = won;
won = current.connected === SOURCES.length;
$("#status-text").textContent =
`${current.connected} / ${SOURCES.length} GROVES LINKED`;
$("#status-pill").classList.toggle("complete", won);
$("#win-banner").classList.toggle("show", won);
$("#metric-linked-before").textContent = `${baseline.connected} / 3`;
$("#metric-linked-after").textContent = `${current.connected} / 3`;
$("#metric-components-before").textContent = String(baseline.components);
$("#metric-components-after").textContent = String(current.components);
$("#metric-route-before").textContent = "—";
$("#metric-route-after").textContent =
current.longest === null ? "—" : `${current.longest} steps`;
$("#metric-added").textContent = String(addedCells.length);
$("#budget-label").textContent = `${addedCells.length} / ${BUDGET} LINKS`;
$("#budget-text").textContent = `${addedCells.length} / ${BUDGET} used`;
$("#budget-fill").style.width =
`${Math.min(100, (addedCells.length / BUDGET) * 100)}%`;
for (const source of SOURCES) {
const index = indexOf(source.x, source.z);
const distance = current.sourceDistances[source.id];
const connected = distance !== null;
const row = groveRows[source.id];
row.row.classList.toggle("linked", connected);
row.state.textContent = connected ? "Connected to beacon" : "Not connected";
row.route.textContent = connected ? `${distance} steps` : "—";
mapButtons[index].classList.toggle("linked", connected);
}
for (let index = 0; index < mapButtons.length; index++) {
const button = mapButtons[index];
const blocked = BLOCKED.has(index);
const source = SOURCES.find((s) => indexOf(s.x, s.z) === index);
const isNest = index === indexOf(NEST.x, NEST.z);
button.classList.toggle("green", planGreen.has(index));
button.classList.toggle("blocked", blocked);
button.classList.toggle("source", Boolean(source));
button.classList.toggle("nest", isNest);
button.disabled = blocked;
const label = button.querySelector(".cell-label");
label.textContent = source ? source.id : isNest ? "H" : "";
button.title = blocked
? "Blocked terrain"
: source
? `${source.name}: ${rowForSource(source.id).state.textContent}`
: isNest
? "Mossweaver nesting beacon"
: `Place a green link at ${coordinatesOf(index).x + 1}, ${coordinatesOf(index).z + 1}`;
}
if (won && !wasWon) {
spawnSuccessParticles();
showToast("Habitat network complete. All three groves are linked.");
}
if (!won && wasWon) {
clearParticles();
}
updatePlanMarkerColors(current);
}
function rowForSource(id) {
return groveRows[id];
}
// ---------------------------------------------------------------------
// Three.js setup
// ---------------------------------------------------------------------
const host = $("#canvas-host");
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;
host.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-7, 7, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, -0.05, 0);
let cameraYaw = 0.68;
let cameraPitch = 0.83;
let cameraZoom = 1;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const shared = {
tileGeometry: new THREE.BoxGeometry(0.93, 1, 0.93),
slabGeometry: new THREE.BoxGeometry(9.7, 0.3, 9.7),
greenGeometry: new THREE.BoxGeometry(0.8, 0.1, 0.8),
trunkGeometry: new THREE.BoxGeometry(0.14, 0.55, 0.14),
leafGeometry: new THREE.BoxGeometry(0.52, 0.45, 0.52),
rockGeometry: new THREE.BoxGeometry(0.58, 0.42, 0.58),
markerBaseGeometry: new THREE.BoxGeometry(0.58, 0.08, 0.58),
markerGeometry: new THREE.BoxGeometry(0.25, 0.25, 0.25)
};
const materials = {
soil: new THREE.MeshStandardMaterial({
color: 0x263b30,
roughness: 0.95,
metalness: 0
}),
soilDark: new THREE.MeshStandardMaterial({
color: 0x1b2d24,
roughness: 1
}),
grass: new THREE.MeshStandardMaterial({
color: 0x496647,
roughness: 0.92
}),
dry: new THREE.MeshStandardMaterial({
color: 0x5b6245,
roughness: 1
}),
moss: new THREE.MeshStandardMaterial({
color: 0x365b42,
roughness: 0.96
}),
stone: new THREE.MeshStandardMaterial({
color: 0x4c5b53,
roughness: 0.92
}),
slab: new THREE.MeshStandardMaterial({
color: 0x13251c,
roughness: 0.85
}),
treeTrunk: new THREE.MeshStandardMaterial({
color: 0x4b3b2c,
roughness: 1
}),
treeLeaf: new THREE.MeshStandardMaterial({
color: 0x2e6b45,
roughness: 0.9
}),
treeLeafLight: new THREE.MeshStandardMaterial({
color: 0x3f8754,
roughness: 0.88
}),
rock: new THREE.MeshStandardMaterial({
color: 0x5a655e,
roughness: 1
}),
greenBaseline: new THREE.MeshStandardMaterial({
color: 0x347f52,
emissive: 0x0d3e25,
emissiveIntensity: 0.38,
roughness: 0.62
}),
greenPlan: new THREE.MeshStandardMaterial({
color: 0x55e68a,
emissive: 0x116d39,
emissiveIntensity: 0.52,
roughness: 0.48
}),
amber: new THREE.MeshStandardMaterial({
color: 0xffbd69,
emissive: 0x8a3d0c,
emissiveIntensity: 0.6,
roughness: 0.4
}),
green: new THREE.MeshStandardMaterial({
color: 0x74ffb5,
emissive: 0x167946,
emissiveIntensity: 0.8,
roughness: 0.35
}),
nest: new THREE.MeshStandardMaterial({
color: 0xc9ff70,
emissive: 0x4d7e1e,
emissiveIntensity: 0.72,
roughness: 0.4
}),
markerBase: new THREE.MeshStandardMaterial({
color: 0x13291d,
emissive: 0x07150e,
emissiveIntensity: 0.4,
roughness: 0.7
})
};
const TOP_MATERIALS = [
materials.grass,
materials.dry,
materials.moss,
materials.stone
];
function deterministicNoise(index) {
const value = Math.sin(index * 12.9898 + 78.233) * 43758.5453;
return value - Math.floor(value);
}
function makeLabelSprite(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.fillStyle = "rgba(6, 18, 12, 0.88)";
context.strokeStyle = color;
context.lineWidth = 5;
context.beginPath();
context.arc(64, 64, 39, 0, Math.PI * 2);
context.fill();
context.stroke();
context.fillStyle = color;
context.font = "bold 48px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 64, 67);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(0.52, 0.52, 0.52);
sprite.renderOrder = 10;
return sprite;
}
function buildHabitat(greenSet, isBaseline) {
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07130e);
scene.fog = new THREE.FogExp2(0x07130e, 0.027);
const hemisphere = new THREE.HemisphereLight(0xbce7d0, 0x182219, 1.7);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xd6ffe3, 2.25);
sun.position.set(-7, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x5ea6ff, 0.65);
rim.position.set(8, 5, -9);
scene.add(rim);
const slab = new THREE.Mesh(shared.slabGeometry, materials.slab);
slab.position.y = -1.12;
slab.receiveShadow = true;
scene.add(slab);
const terrainMeshes = [];
const topHeights = new Map();
for (let z = 0; z < ROWS; z++) {
for (let x = 0; x < COLS; x++) {
const index = indexOf(x, z);
const noise = deterministicNoise(index);
const height = 0.95 + noise * 0.78;
const bottom = -0.96;
topHeights.set(index, bottom + height);
const topMaterial = BLOCKED.has(index)
? materials.stone
: TOP_MATERIALS[Math.floor(noise * TOP_MATERIALS.length)];
const sideMaterial = noise > 0.58 ? materials.soil : materials.soilDark;
const terrain = new THREE.Mesh(shared.tileGeometry, [
sideMaterial,
sideMaterial,
topMaterial,
sideMaterial,
sideMaterial,
sideMaterial
]);
terrain.scale.y = height;
terrain.position.set(
x - (COLS - 1) / 2,
bottom + height / 2,
z - (ROWS - 1) / 2
);
terrain.castShadow = true;
terrain.receiveShadow = true;
terrain.userData.cellIndex = index;
scene.add(terrain);
terrainMeshes.push(terrain);
if (BLOCKED.has(index)) {
const rock = new THREE.Mesh(shared.rockGeometry, materials.rock);
rock.position.set(
x - (COLS - 1) / 2 + (noise - 0.5) * 0.16,
bottom + height + 0.18,
z - (ROWS - 1) / 2 + (deterministicNoise(index + 90) - 0.5) * 0.16
);
rock.rotation.y = noise * Math.PI;
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
if (index % 3 === 0) {
const crown = new THREE.Mesh(
new THREE.BoxGeometry(0.36, 0.32, 0.36),
materials.treeLeaf
);
crown.position.set(
rock.position.x + 0.05,
rock.position.y + 0.29,
rock.position.z - 0.04
);
crown.rotation.y = 0.4;
crown.castShadow = true;
scene.add(crown);
}
}
}
}
const greenGroup = new THREE.Group();
scene.add(greenGroup);
const habitat = {
scene,
terrainMeshes,
topHeights,
greenGroup,
nodeRecords: {},
particleGroup: new THREE.Group(),
particles: [],
hintMesh: null
};
scene.add(habitat.particleGroup);
for (const source of SOURCES) {
habitat.nodeRecords[source.id] = createNodeMarker(
habitat,
source.x,
source.z,
source.id,
isBaseline ? materials.amber : materials.amber,
false
);
}
habitat.nodeRecords.H = createNodeMarker(
habitat,
NEST.x,
NEST.z,
"H",
materials.nest,
true
);
const hintMaterial = new THREE.MeshBasicMaterial({
color: 0xc7ff70,
transparent: true,
opacity: 0.42,
depthWrite: false
});
habitat.hintMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.86, 0.13, 0.86),
hintMaterial
);
habitat.hintMesh.visible = false;
scene.add(habitat.hintMesh);
rebuildGreen(habitat, greenSet, isBaseline);
return habitat;
}
function createNodeMarker(habitat, x, z, text, material, isNest) {
const group = new THREE.Group();
const worldX = x - (COLS - 1) / 2;
const worldZ = z - (ROWS - 1) / 2;
const top = habitat.topHeights.get(indexOf(x, z));
group.position.set(worldX, top + 0.08, worldZ);
const base = new THREE.Mesh(shared.markerBaseGeometry, materials.markerBase);
base.position.y = 0.07;
base.receiveShadow = true;
group.add(base);
const blocks = [];
if (isNest) {
const positions = [
[0, 0.28, 0, 0.34],
[0, 0.55, 0, 0.28],
[0, 0.79, 0, 0.22],
[0.18, 0.42, 0, 0.18],
[-0.18, 0.42, 0, 0.18]
];
for (const [px, py, pz, scale] of positions) {
const block = new THREE.Mesh(shared.markerGeometry, material);
block.scale.setScalar(scale / 0.25);
block.position.set(px, py, pz);
block.castShadow = true;
group.add(block);
blocks.push(block);
}
} else {
const positions = [
[0, 0.26, 0, 0.25],
[0, 0.5, 0, 0.2]
];
for (const [px, py, pz, scale] of positions) {
const block = new THREE.Mesh(shared.markerGeometry, material);
block.scale.setScalar(scale / 0.25);
block.position.set(px, py, pz);
block.castShadow = true;
group.add(block);
blocks.push(block);
}
}
const label = makeLabelSprite(text, isNest ? "#c7ff70" : "#ffc06c");
label.position.set(0, isNest ? 1.2 : 0.92, 0);
group.add(label);
habitat.scene.add(group);
return {
group,
blocks,
material,
baseY: group.position.y,
isNest
};
}
function rebuildGreen(habitat, greenSet, isBaseline = false) {
habitat.greenGroup.clear();
for (const index of greenSet) {
const { x, z } = coordinatesOf(index);
const top = habitat.topHeights.get(index);
const tile = new THREE.Mesh(
shared.greenGeometry,
isBaseline ? materials.greenBaseline : materials.greenPlan
);
tile.position.set(
x - (COLS - 1) / 2,
top + 0.1,
z - (ROWS - 1) / 2
);
tile.castShadow = true;
tile.receiveShadow = true;
habitat.greenGroup.add(tile);
}
}
function updatePlanMarkerColors(stats) {
for (const source of SOURCES) {
const record = planHabitat.nodeRecords[source.id];
const linked = stats.sourceDistances[source.id] !== null;
record.material.color.setHex(linked ? 0x74ffb5 : 0xffbd69);
record.material.emissive.setHex(linked ? 0x167946 : 0x8a3d0c);
record.material.emissiveIntensity = linked ? 0.9 : 0.6;
}
}
const baselineHabitat = buildHabitat(INITIAL_GREEN, true);
const planHabitat = buildHabitat(planGreen, false);
// ---------------------------------------------------------------------
// Puzzle actions
// ---------------------------------------------------------------------
function attemptToAdd(index, forced = false) {
const { x, z } = coordinatesOf(index);
if (BLOCKED.has(index)) {
showToast("That voxel is blocked by rock and cannot hold a living link.");
return false;
}
if (planGreen.has(index)) {
showToast("This voxel is already part of the verdant network.");
return false;
}
if (addedCells.length >= BUDGET) {
showToast("The nine-link field budget is exhausted. Undo a link or reset.");
return false;
}
if (!forced && !isAdjacentToGreen(index)) {
showToast("A link must touch the existing green network along an edge.");
return false;
}
planGreen.add(index);
addedCells.push(index);
rebuildGreen(planHabitat, planGreen);
updateInterface();
if (won) {
showToast("Habitat network complete. All three groves are linked.");
} else {
showToast("Verdant link placed.");
}
return true;
}
function undoLink() {
if (addedCells.length === 0) {
showToast("There are no player-added links to undo.");
return;
}
const index = addedCells.pop();
planGreen.delete(index);
rebuildGreen(planHabitat, planGreen);
updateInterface();
showToast("Last added link removed.");
}
function resetPlan() {
planGreen = new Set(INITIAL_GREEN);
addedCells = [];
won = false;
rebuildGreen(planHabitat, planGreen);
clearParticles();
updateInterface();
showToast("Field plan reset to the baseline survey.");
}
function suggestLink() {
if (won) {
showToast("The habitat network is already complete.");
return;
}
if (addedCells.length >= BUDGET) {
showToast("Undo a link before requesting another suggestion.");
return;
}
for (const index of SOLUTION_HINT) {
if (!planGreen.has(index) && isAdjacentToGreen(index)) {
attemptToAdd(index, true);
return;
}
}
showToast("The suggested spine is no longer adjacent. Undo or reset for an optimal route.");
}
$("#hint-button").addEventListener("click", suggestLink);
$("#undo-button").addEventListener("click", undoLink);
$("#reset-button").addEventListener("click", resetPlan);
// ---------------------------------------------------------------------
// Baseline / plan comparison
// ---------------------------------------------------------------------
function toggleComparison() {
comparing = !comparing;
$("#viewport").classList.toggle("comparing", comparing);
$("#compare-button").classList.toggle("active", comparing);
$("#compare-button").textContent = comparing
? "Exit comparison"
: "Compare baseline";
if (comparing) {
showToast("Baseline survey on the left; your live plan on the right.");
}
}
$("#compare-button").addEventListener("click", toggleComparison);
// ---------------------------------------------------------------------
// Direct camera controls
// ---------------------------------------------------------------------
let pointerState = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerState = {
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerState) return;
const totalDistance = Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY
);
if (totalDistance > 5) pointerState.moved = true;
if (!pointerState.moved) return;
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.48,
1.25
);
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerState) return;
const wasClick = !pointerState.moved;
pointerState = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (wasClick) {
handleSceneClick(event.clientX, event.clientY);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState = null;
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.85
);
},
{ passive: false }
);
function handleSceneClick(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
const localX = clientX - rect.left;
const localY = clientY - rect.top;
const width = rect.width;
const height = rect.height;
if (comparing && localX < width / 2) return;
const viewportX = comparing ? width / 2 : 0;
const viewportWidth = comparing ? width / 2 : width;
setCameraProjection(viewportWidth, height);
pointer.x = ((localX - viewportX) / viewportWidth) * 2 - 1;
pointer.y = -(localY / height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
planHabitat.terrainMeshes,
false
);
if (intersections.length > 0) {
const index = intersections[0].object.userData.cellIndex;
attemptToAdd(index);
}
}
function setCameraProjection(width, height) {
const aspect = Math.max(0.1, width / height);
const halfHeight = Math.max(6.2, 5.8 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
function updateCamera() {
const radius = 16;
const horizontal = Math.cos(cameraPitch) * radius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * radius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
// ---------------------------------------------------------------------
// Success particles
// ---------------------------------------------------------------------
function spawnSuccessParticles() {
clearParticles();
const nestIndex = indexOf(NEST.x, NEST.z);
const top = planHabitat.topHeights.get(nestIndex);
const colors = [0x61ef9a, 0xc7ff70, 0xffc06c, 0x70d7ff];
for (let i = 0; i < 42; i++) {
const geometry = new THREE.BoxGeometry(0.09, 0.09, 0.09);
const material = new THREE.MeshBasicMaterial({
color: colors[i % colors.length]
});
const particle = new THREE.Mesh(geometry, material);
particle.position.set(
NEST.x - (COLS - 1) / 2 + (Math.random() - 0.5) * 0.4,
top + 0.7,
NEST.z - (ROWS - 1) / 2 + (Math.random() - 0.5) * 0.4
);
const velocity = new THREE.Vector3(
(Math.random() - 0.5) * 2.2,
1.6 + Math.random() * 2.2,
(Math.random() - 0.5) * 2.2
);
planHabitat.particleGroup.add(particle);
planHabitat.particles.push({ mesh: particle, velocity, age: 0 });
}
}
function clearParticles() {
for (const particle of planHabitat.particles) {
planHabitat.particleGroup.remove(particle.mesh);
particle.mesh.geometry.dispose();
particle.mesh.material.dispose();
}
planHabitat.particles.length = 0;
}
function updateParticles(delta) {
for (const particle of planHabitat.particles) {
particle.age += delta;
particle.velocity.y -= 3.4 * delta;
particle.mesh.position.addScaledVector(particle.velocity, delta);
particle.mesh.rotation.x += delta * 3;
particle.mesh.rotation.y += delta * 2;
if (particle.age > 3.2) {
particle.mesh.visible = false;
}
}
}
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
let viewWidth = 1;
let viewHeight = 1;
function resizeRenderer() {
viewWidth = Math.max(1, host.clientWidth);
viewHeight = Math.max(1, host.clientHeight);
renderer.setSize(viewWidth, viewHeight, false);
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(host);
resizeRenderer();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera();
for (const source of SOURCES) {
const record = planHabitat.nodeRecords[source.id];
record.group.position.y =
record.baseY + Math.sin(elapsed * 2.1 + source.x) * 0.035;
}
const nest = planHabitat.nodeRecords.H;
nest.group.position.y =
nest.baseY + Math.sin(elapsed * 1.7) * 0.025;
updateParticles(delta);
renderer.setScissorTest(false);
renderer.setViewport(0, 0, viewWidth, viewHeight);
if (comparing) {
const halfWidth = Math.floor(viewWidth / 2);
renderer.setScissorTest(true);
renderer.setViewport(0, 0, halfWidth, viewHeight);
renderer.setScissor(0, 0, halfWidth, viewHeight);
setCameraProjection(halfWidth, viewHeight);
renderer.render(baselineHabitat.scene, camera);
renderer.setViewport(halfWidth, 0, viewWidth - halfWidth, viewHeight);
renderer.setScissor(halfWidth, 0, viewWidth - halfWidth, viewHeight);
setCameraProjection(viewWidth - halfWidth, viewHeight);
renderer.render(planHabitat.scene, camera);
renderer.setScissorTest(false);
} else {
setCameraProjection(viewWidth, viewHeight);
renderer.render(planHabitat.scene, camera);
}
});
// Keyboard shortcuts: C = compare, Z = undo, H = hint, R = reset.
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
switch (event.key.toLowerCase()) {
case "c":
toggleComparison();
break;
case "z":
undoLink();
break;
case "h":
suggestLink();
break;
case "r":
resetPlan();
break;
}
});
updateInterface();
</script>
</body>
</html>
```