Prompt specification
Build or customize the Three.js 3D scene "Resolve ambiguous landmark instructions" 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: Resolve ambiguous landmark instructions
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/resolve-ambiguous-landmark-instructions-x1340
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows relative positions defined by observers and landmarks. Let users select an observer and destination before following relative directions. Demonstrate how an identical left-turn instruction reaches a different place when the starting orientation changes. Voxel style.
Working Three.js Starter (resolve-ambiguous-landmark-instructions-x1340):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Relative — Voxel Landmark Lab</title>
<style>
:root {
--ink: #173139;
--ink-soft: #49636a;
--cream: #f5f1e4;
--paper: #fffdf6;
--teal: #25c9a7;
--teal-dark: #138a77;
--gold: #ffbd4a;
--violet: #9f7aea;
--coral: #ff715b;
--line: #d8d3c3;
--shadow: 5px 5px 0 rgba(23, 49, 57, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
min-height: 100%;
background: var(--cream);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
select,
input {
font: inherit;
}
button {
cursor: pointer;
}
#app {
height: 100dvh;
display: flex;
flex-direction: column;
}
header {
height: 72px;
flex: 0 0 72px;
display: flex;
align-items: center;
gap: 24px;
padding: 0 24px;
background: var(--ink);
color: var(--paper);
border-bottom: 4px solid var(--gold);
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 240px;
}
.brand-mark {
position: relative;
width: 30px;
height: 30px;
background: var(--gold);
box-shadow: 7px -7px 0 var(--teal);
}
.brand-mark::after {
content: "";
position: absolute;
width: 9px;
height: 9px;
right: -12px;
top: -7px;
background: var(--gold);
}
.brand h1 {
margin: 0;
font-size: 17px;
line-height: 1;
letter-spacing: 0.18em;
font-weight: 900;
}
.brand p {
margin: 5px 0 0;
color: #adc1c2;
font-size: 10px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.header-note {
display: flex;
align-items: center;
gap: 9px;
color: #c2d0cf;
font-size: 12px;
letter-spacing: 0.04em;
}
.live-dot {
width: 8px;
height: 8px;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(37, 201, 167, 0.14);
}
.header-spacer {
flex: 1;
}
.header-button {
border: 1px solid #49636a;
background: transparent;
color: var(--paper);
padding: 9px 12px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.header-button:hover {
border-color: var(--gold);
color: var(--gold);
}
main {
min-height: 0;
flex: 1;
display: grid;
grid-template-columns: 344px minmax(0, 1fr);
}
aside {
min-height: 0;
overflow-y: auto;
padding: 23px 22px 28px;
background: var(--cream);
border-right: 3px solid var(--ink);
z-index: 5;
scrollbar-width: thin;
scrollbar-color: #9caeaa transparent;
}
.panel-kicker {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: var(--teal-dark);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.panel-kicker::before {
content: "";
width: 18px;
height: 4px;
background: var(--teal);
}
.panel-title {
margin: 0 0 7px;
font-size: 25px;
line-height: 1.05;
letter-spacing: -0.04em;
}
.panel-intro {
margin: 0 0 22px;
color: var(--ink-soft);
font-size: 12px;
line-height: 1.55;
}
.control-section {
padding: 18px 0;
border-top: 1px solid var(--line);
}
.section-heading {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 13px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-heading .number {
color: #829391;
}
.field {
margin-bottom: 14px;
}
.field:last-child {
margin-bottom: 0;
}
.field-label {
display: block;
margin-bottom: 6px;
color: var(--ink-soft);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
select {
width: 100%;
height: 43px;
padding: 0 38px 0 12px;
border: 2px solid var(--ink);
border-radius: 0;
background-color: var(--paper);
color: var(--ink);
font-size: 13px;
font-weight: 750;
outline: none;
box-shadow: 3px 3px 0 rgba(23, 49, 57, 0.12);
}
select:focus {
border-color: var(--teal-dark);
}
.pair-summary {
margin-top: 11px;
padding: 10px 11px;
display: flex;
align-items: center;
gap: 8px;
background: #e3e7dc;
border-left: 4px solid var(--gold);
color: var(--ink-soft);
font-size: 11px;
}
.pair-summary strong {
color: var(--ink);
}
.orientation-grid {
display: grid;
grid-template-columns: 122px 1fr;
align-items: center;
gap: 16px;
}
.compass {
position: relative;
width: 116px;
height: 116px;
border: 3px solid var(--ink);
border-radius: 50%;
background:
linear-gradient(90deg, transparent 48%, rgba(23, 49, 57, 0.13) 49%, rgba(23, 49, 57, 0.13) 51%, transparent 52%),
linear-gradient(transparent 48%, rgba(23, 49, 57, 0.13) 49%, rgba(23, 49, 57, 0.13) 51%, transparent 52%),
var(--paper);
box-shadow: var(--shadow);
}
.compass::after {
content: "";
position: absolute;
width: 12px;
height: 12px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: var(--ink);
z-index: 3;
}
.compass-letter {
position: absolute;
z-index: 4;
font-size: 9px;
font-weight: 900;
color: var(--ink-soft);
}
.compass-letter.north {
left: 50%;
top: 7px;
transform: translateX(-50%);
}
.compass-letter.east {
right: 8px;
top: 50%;
transform: translateY(-50%);
}
.compass-letter.south {
left: 50%;
bottom: 7px;
transform: translateX(-50%);
}
.compass-letter.west {
left: 8px;
top: 50%;
transform: translateY(-50%);
}
.needle-arm {
position: absolute;
z-index: 2;
left: 50%;
bottom: 50%;
width: 5px;
height: 43px;
transform-origin: 50% 100%;
transform: translateX(-50%) rotate(0deg);
background: var(--teal);
transition: transform 180ms ease;
}
.needle-arm::before {
content: "";
position: absolute;
top: -8px;
left: 50%;
width: 0;
height: 0;
transform: translateX(-50%);
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-bottom: 11px solid var(--teal);
}
.orientation-copy strong {
display: block;
margin-bottom: 4px;
font-size: 17px;
}
.orientation-copy span {
display: block;
margin-bottom: 12px;
color: var(--ink-soft);
font-size: 10px;
line-height: 1.4;
}
.rotate-buttons {
display: flex;
gap: 6px;
}
.rotate-buttons button {
flex: 1;
border: 1px solid #aebbb6;
background: transparent;
color: var(--ink);
padding: 7px 5px;
font-size: 10px;
font-weight: 800;
}
.rotate-buttons button:hover {
border-color: var(--teal-dark);
color: var(--teal-dark);
}
input[type="range"] {
width: 100%;
margin: 12px 0 4px;
accent-color: var(--teal-dark);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
color: #829391;
font-size: 8px;
font-weight: 900;
}
.instruction-box {
padding: 12px;
background: #e7e9df;
border: 1px solid #cbd0c4;
}
#instructionList {
max-height: 208px;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 6px;
padding-right: 3px;
scrollbar-width: thin;
}
.instruction-step {
display: grid;
grid-template-columns: 29px 1fr;
min-height: 43px;
align-items: center;
border: 1px solid #c7cec3;
background: var(--paper);
transition: transform 120ms ease, border-color 120ms ease;
}
.instruction-step.active {
transform: translateX(4px);
border-color: var(--teal-dark);
box-shadow: 3px 3px 0 rgba(19, 138, 119, 0.16);
}
.instruction-step.done {
opacity: 0.62;
}
.step-index {
align-self: stretch;
display: grid;
place-items: center;
color: #78908c;
border-right: 1px solid #d8ddd3;
font-size: 9px;
font-weight: 900;
}
.step-content {
padding: 7px 9px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.turn-badge {
display: inline-block;
padding: 4px 6px;
font-size: 9px;
font-weight: 950;
letter-spacing: 0.06em;
}
.turn-badge.left {
color: #086c60;
background: #bcebdc;
}
.turn-badge.right {
color: #a64030;
background: #ffd7ce;
}
.turn-badge.around {
color: #5e4199;
background: #dfd3fa;
}
.turn-badge.straight {
color: #53646a;
background: #e4e8e4;
}
.walk-cue {
color: var(--ink-soft);
font-size: 10px;
white-space: nowrap;
}
.walk-cue b {
color: var(--ink);
font-size: 13px;
}
.micro-note {
margin: 9px 0 0;
color: #6d807d;
font-size: 9px;
line-height: 1.45;
}
.action-stack {
display: grid;
gap: 9px;
margin-top: 18px;
}
.primary-button,
.secondary-button {
min-height: 46px;
border: 2px solid var(--ink);
border-radius: 0;
font-size: 11px;
font-weight: 950;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: transform 100ms ease, box-shadow 100ms ease;
}
.primary-button {
background: var(--teal);
color: #0c3b35;
box-shadow: 4px 4px 0 var(--ink);
}
.secondary-button {
background: var(--paper);
color: var(--ink);
box-shadow: 4px 4px 0 rgba(23, 49, 57, 0.18);
}
.primary-button:hover,
.secondary-button:hover {
transform: translate(-2px, -2px);
box-shadow: 6px 6px 0 var(--ink);
}
.primary-button:active,
.secondary-button:active {
transform: translate(2px, 2px);
box-shadow: 1px 1px 0 var(--ink);
}
.primary-button:disabled,
.secondary-button:disabled {
cursor: not-allowed;
opacity: 0.5;
transform: none;
}
.progress-track {
height: 5px;
margin-top: 13px;
overflow: hidden;
background: #d7d9ce;
}
#progressFill {
width: 0;
height: 100%;
background: var(--teal-dark);
transition: width 80ms linear;
}
#statusLine {
min-height: 16px;
margin-top: 7px;
color: var(--ink-soft);
font-size: 10px;
text-align: center;
}
#scene {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #b9cbc4;
}
#canvasHost {
position: absolute;
inset: 0;
}
#canvasHost canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#canvasHost canvas:active {
cursor: grabbing;
}
.scene-top {
position: absolute;
top: 18px;
left: 18px;
z-index: 3;
padding: 9px 11px;
border: 2px solid var(--ink);
background: rgba(255, 253, 246, 0.91);
box-shadow: 4px 4px 0 rgba(23, 49, 57, 0.15);
pointer-events: none;
}
.scene-top strong {
display: block;
font-size: 11px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.scene-top span {
display: block;
margin-top: 4px;
color: var(--ink-soft);
font-size: 9px;
}
.scene-key {
position: absolute;
top: 18px;
right: 18px;
z-index: 3;
padding: 10px 12px;
background: rgba(23, 49, 57, 0.91);
color: var(--paper);
box-shadow: 4px 4px 0 rgba(23, 49, 57, 0.16);
pointer-events: none;
font-size: 9px;
}
.key-row {
display: flex;
align-items: center;
gap: 7px;
margin: 4px 0;
}
.key-swatch {
width: 10px;
height: 10px;
background: var(--gold);
}
.key-swatch.violet {
background: var(--violet);
}
.key-swatch.landmark {
background: var(--coral);
}
.camera-hint {
position: absolute;
right: 18px;
bottom: 17px;
z-index: 3;
color: rgba(23, 49, 57, 0.72);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
pointer-events: none;
}
.compare-card {
position: absolute;
left: 18px;
bottom: 18px;
z-index: 4;
width: min(560px, calc(100% - 36px));
padding: 15px 16px 13px;
border: 3px solid var(--ink);
background: rgba(255, 253, 246, 0.95);
box-shadow: 6px 6px 0 rgba(23, 49, 57, 0.2);
pointer-events: none;
}
.compare-kicker {
display: flex;
align-items: center;
gap: 7px;
color: var(--teal-dark);
font-size: 9px;
font-weight: 950;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.compare-kicker::before {
content: "";
width: 8px;
height: 8px;
background: var(--teal);
}
.compare-card h2 {
margin: 7px 0 12px;
font-size: 20px;
letter-spacing: -0.035em;
}
.compare-grid {
display: grid;
grid-template-columns: 1fr 70px 1fr;
gap: 8px;
align-items: stretch;
}
.trial {
min-width: 0;
padding: 9px 10px;
border-left: 5px solid var(--gold);
background: #f0ead9;
}
.trial.violet {
border-left-color: var(--violet);
background: #e9e3f4;
}
.trial span {
display: block;
margin-bottom: 5px;
color: var(--ink-soft);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
}
.trial b {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 12px;
}
.versus {
display: grid;
place-items: center;
text-align: center;
}
.locked-cue {
display: grid;
place-items: center;
width: 43px;
height: 34px;
color: #086c60;
background: #bcebdc;
border: 1px solid #70c6ae;
font-size: 11px;
font-weight: 950;
transform: rotate(-3deg);
}
.versus small {
margin-top: 5px;
color: var(--ink-soft);
font-size: 8px;
font-weight: 800;
}
#compareResult {
margin: 11px 0 0;
color: var(--ink-soft);
font-size: 10px;
line-height: 1.45;
}
.compare-card.complete {
box-shadow: 6px 6px 0 var(--teal);
}
.toast {
position: absolute;
z-index: 8;
left: 50%;
top: 18px;
max-width: min(520px, calc(100% - 40px));
padding: 10px 14px;
color: var(--paper);
background: var(--ink);
border-left: 5px solid var(--gold);
box-shadow: 4px 4px 0 rgba(23, 49, 57, 0.2);
font-size: 11px;
font-weight: 700;
opacity: 0;
transform: translate(-50%, -12px);
transition: opacity 160ms ease, transform 160ms ease;
pointer-events: none;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 860px) {
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100dvh;
}
header {
height: 64px;
flex-basis: 64px;
padding: 0 15px;
}
.header-note {
display: none;
}
main {
display: flex;
flex-direction: column;
}
#scene {
order: 1;
height: 58dvh;
min-height: 430px;
}
aside {
order: 2;
overflow: visible;
border-right: 0;
border-top: 3px solid var(--ink);
}
.scene-key {
display: none;
}
.compare-card {
right: 14px;
left: 14px;
bottom: 14px;
width: auto;
}
.compare-grid {
grid-template-columns: 1fr 56px 1fr;
}
.camera-hint {
display: none;
}
}
@media (max-width: 520px) {
.brand {
min-width: 0;
}
.brand p {
display: none;
}
.header-button {
font-size: 9px;
}
.compare-card {
padding: 12px;
}
.compare-card h2 {
font-size: 17px;
}
.trial b {
font-size: 10px;
}
.orientation-grid {
grid-template-columns: 105px 1fr;
gap: 11px;
}
.compass {
width: 100px;
height: 100px;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>RELATIVE</h1>
<p>Voxel landmark lab</p>
</div>
</div>
<div class="header-note">
<span class="live-dot"></span>
Spatial reasoning with a movable frame of reference
</div>
<div class="header-spacer"></div>
<button class="header-button" id="resetViewBtn" type="button">
Reset camera
</button>
</header>
<main>
<aside aria-label="Route controls">
<div class="panel-kicker">Direction laboratory</div>
<h2 class="panel-title">Where does “left” point?</h2>
<p class="panel-intro">
Choose an observer and a landmark. The same instruction script is then
replayed from two different starting orientations.
</p>
<section class="control-section">
<div class="section-heading">
<span>Reference points</span>
<span class="number">01</span>
</div>
<div class="field">
<label class="field-label" for="observerSelect">Observer</label>
<select id="observerSelect"></select>
</div>
<div class="field">
<label class="field-label" for="destinationSelect">Destination landmark</label>
<select id="destinationSelect"></select>
</div>
<div class="pair-summary">
<strong id="pairSummary">Mira → Sun Tower</strong>
</div>
</section>
<section class="control-section">
<div class="section-heading">
<span>Starting orientation</span>
<span class="number">02</span>
</div>
<div class="orientation-grid">
<div class="compass" aria-hidden="true">
<span class="compass-letter north">N</span>
<span class="compass-letter east">E</span>
<span class="compass-letter south">S</span>
<span class="compass-letter west">W</span>
<div class="needle-arm" id="compassNeedle"></div>
</div>
<div class="orientation-copy">
<strong id="headingValue">NORTH · 0°</strong>
<span>Direction words depend on this initial facing.</span>
<div class="rotate-buttons">
<button id="rotateLeftBtn" type="button">↶ Left</button>
<button id="rotateRightBtn" type="button">Right ↷</button>
</div>
</div>
</div>
<input
id="headingRange"
type="range"
min="0"
max="270"
step="90"
value="0"
aria-label="Starting orientation"
/>
<div class="range-labels">
<span>N</span><span>E</span><span>S</span><span>W</span>
</div>
</section>
<section class="control-section">
<div class="section-heading">
<span>Relative script</span>
<span id="routeMeta">Preparing route…</span>
</div>
<div class="instruction-box">
<div id="instructionList" aria-live="polite"></div>
</div>
<p class="micro-note">
A one-block calibration step guarantees that the script begins with
an explicit LEFT cue. The destination remains fixed.
</p>
</section>
<div class="action-stack">
<button class="primary-button" id="followBtn" type="button">
Follow relative directions
</button>
<button class="secondary-button" id="compareBtn" type="button">
Run both orientations
</button>
</div>
<div class="progress-track" aria-hidden="true">
<div id="progressFill"></div>
</div>
<div id="statusLine" aria-live="polite">Choose a destination to begin.</div>
</aside>
<section id="scene" aria-label="Interactive voxel district">
<div id="canvasHost"></div>
<div class="scene-top">
<strong>Voxel district</strong>
<span>Drag to orbit · Scroll to zoom · Click a figure or landmark</span>
</div>
<div class="scene-key">
<div class="key-row">
<span class="key-swatch"></span>
Selected route
</div>
<div class="key-row">
<span class="key-swatch violet"></span>
Same script, +90° start
</div>
<div class="key-row">
<span class="key-swatch landmark"></span>
Destination
</div>
</div>
<div class="compare-card" id="compareCard">
<div class="compare-kicker">Contrast run</div>
<h2>Same words. Different places.</h2>
<div class="compare-grid">
<div class="trial">
<span>A · SELECTED FACING</span>
<b id="endpointA">Sun Tower</b>
</div>
<div class="versus">
<div class="locked-cue">↰ LEFT</div>
<small>same cue</small>
</div>
<div class="trial violet">
<span>B · FACING +90°</span>
<b id="endpointB">Different block</b>
</div>
</div>
<p id="compareResult">
Both observers receive the same script. Only the initial frame of
reference changes.
</p>
</div>
<div class="camera-hint">Drag to orbit · Scroll to zoom</div>
<div class="toast" id="toast" role="status"></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";
// -------------------------------------------------------------------------
// Data and DOM
// -------------------------------------------------------------------------
const DIRS = [
{ name: "North", short: "N", x: 0, z: -1, yaw: 0 },
{ name: "East", short: "E", x: 1, z: 0, yaw: -Math.PI / 2 },
{ name: "South", short: "S", x: 0, z: 1, yaw: Math.PI },
{ name: "West", short: "W", x: -1, z: 0, yaw: Math.PI / 2 }
];
const observers = [
{
id: "mira",
name: "Mira",
role: "West promenade",
x: -3,
z: 1,
color: 0x25c9a7,
defaultHeading: 0
},
{
id: "rook",
name: "Rook",
role: "South switchyard",
x: 3,
z: -2,
color: 0x4d91e8,
defaultHeading: 1
},
{
id: "pip",
name: "Pip",
role: "Garden steps",
x: -1,
z: 5,
color: 0xff8468,
defaultHeading: 2
}
];
const landmarks = [
{
id: "sun",
name: "Sun Tower",
type: "tower",
target: { x: 8, z: 6 },
build: { x: 8, z: 8 },
color: 0xff715b,
accent: 0xffd15c
},
{
id: "archive",
name: "Blue Archive",
type: "library",
target: { x: -8, z: 6 },
build: { x: -8, z: 8 },
color: 0x4d91e8,
accent: 0xf6f0d5
},
{
id: "foundry",
name: "Coral Foundry",
type: "foundry",
target: { x: 8, z: -6 },
build: { x: 8, z: -8 },
color: 0xf09a38,
accent: 0x53656a
},
{
id: "garden",
name: "Mint Garden",
type: "garden",
target: { x: -8, z: -6 },
build: { x: -8, z: -8 },
color: 0x4dbb83,
accent: 0xffd15c
}
];
const $ = (id) => document.getElementById(id);
const observerSelect = $("observerSelect");
const destinationSelect = $("destinationSelect");
const headingRange = $("headingRange");
const headingValue = $("headingValue");
const compassNeedle = $("compassNeedle");
const instructionList = $("instructionList");
const routeMeta = $("routeMeta");
const progressFill = $("progressFill");
const statusLine = $("statusLine");
const compareCard = $("compareCard");
const compareResult = $("compareResult");
const endpointA = $("endpointA");
const endpointB = $("endpointB");
const toast = $("toast");
const state = {
observerId: observers[0].id,
destinationId: landmarks[0].id,
heading: observers[0].defaultHeading,
route: null,
animation: null
};
const getObserver = () => observers.find((o) => o.id === state.observerId);
const getLandmark = () => landmarks.find((l) => l.id === state.destinationId);
const keyFor = (x, z) => `${x},${z}`;
const parseKey = (key) => {
const [x, z] = key.split(",").map(Number);
return { x, z };
};
observers.forEach((observer) => {
const option = document.createElement("option");
option.value = observer.id;
option.textContent = `${observer.name} — ${observer.role}`;
observerSelect.appendChild(option);
});
landmarks.forEach((landmark) => {
const option = document.createElement("option");
option.value = landmark.id;
option.textContent = landmark.name;
destinationSelect.appendChild(option);
});
observerSelect.value = state.observerId;
destinationSelect.value = state.destinationId;
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const host = $("canvasHost");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9cbc4);
scene.fog = new THREE.Fog(0xb9cbc4, 46, 86);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
host.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-20, 20, 15, -15, 0.1, 130);
const cameraTarget = new THREE.Vector3(0, 0, 0);
const cameraState = {
theta: 0.68,
phi: 0.77,
radius: 39
};
function updateCamera() {
const horizontal = cameraState.radius * Math.cos(cameraState.phi);
camera.position.set(
cameraTarget.x + Math.sin(cameraState.theta) * horizontal,
cameraTarget.y + Math.sin(cameraState.phi) * cameraState.radius,
cameraTarget.z + Math.cos(cameraState.theta) * horizontal
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resizeRenderer() {
const width = Math.max(1, host.clientWidth);
const height = Math.max(1, host.clientHeight);
const aspect = width / height;
const frustumHeight = Math.max(23, 40 / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
updateCamera();
}
new ResizeObserver(resizeRenderer).observe(host);
resizeRenderer();
const hemi = new THREE.HemisphereLight(0xf7fff0, 0x3d5357, 1.75);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4d7, 2.5);
sun.position.set(18, 28, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 75;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaed7e8, 0.65);
fill.position.set(-18, 12, -16);
scene.add(fill);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02,
flatShading: true
})
);
}
return materialCache.get(color);
}
function cube(parent, width, height, depth, color, x = 0, y = 0, z = 0, material = null) {
const mesh = new THREE.Mesh(
unitBox,
material || getMaterial(color)
);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(title, subtitle = "", background = "#173139", foreground = "#fffdf6") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 7, 7, 498, 114, 16);
ctx.fillStyle = background;
ctx.fill();
ctx.fillStyle = foreground;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 34px system-ui, sans-serif";
ctx.fillText(title, 256, subtitle ? 47 : 65, 465);
if (subtitle) {
ctx.globalAlpha = 0.72;
ctx.font = "800 19px system-ui, sans-serif";
ctx.fillText(subtitle, 256, 87, 455);
ctx.globalAlpha = 1;
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(4.2, 1.05, 1);
sprite.renderOrder = 30;
return sprite;
}
function clearGroup(group) {
while (group.children.length) {
group.remove(group.children[group.children.length - 1]);
}
}
function disposeLabel(label) {
if (label?.material?.map) {
label.material.map.dispose();
label.material.dispose();
}
}
// -------------------------------------------------------------------------
// Voxel environment
// -------------------------------------------------------------------------
const world = new THREE.Group();
scene.add(world);
cube(world, 34, 1, 32, 0x536c61, 0, -0.56, 0);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const tileGeometry = new THREE.BoxGeometry(1, 1, 1);
const tileCount = 33 * 31;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let x = -16; x <= 16; x++) {
for (let z = -15; z <= 15; z++) {
dummy.position.set(x, 0, z);
dummy.scale.set(0.94, 0.08, 0.94);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const plaza = Math.abs(x) <= 2 && Math.abs(z) <= 2;
const variation = (x * 17 + z * 31 + 100) % 3;
if (plaza) {
tileColor.setHex(variation === 0 ? 0xaab7a5 : 0x9eafa1);
} else {
tileColor.setHex(
variation === 0
? 0x8da88c
: variation === 1
? 0x95af93
: 0x87a188
);
}
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
world.add(tiles);
// Low perimeter walls.
cube(world, 34, 0.55, 0.3, 0x536c61, 0, 0.28, -15.65);
cube(world, 34, 0.55, 0.3, 0x536c61, 0, 0.28, 15.65);
cube(world, 0.3, 0.55, 32, 0x536c61, -16.65, 0.28, 0);
cube(world, 0.3, 0.55, 32, 0x536c61, 16.65, 0.28, 0);
// Central voxel fountain.
const fountain = new THREE.Group();
fountain.position.set(0, 0, 0);
world.add(fountain);
cube(fountain, 2.3, 0.3, 2.3, 0x52646b, 0, 0.19, 0);
cube(fountain, 1.75, 0.16, 1.75, 0x4fc6bd, 0, 0.4, 0);
cube(fountain, 0.45, 0.9, 0.45, 0xd5d3c2, 0, 0.85, 0);
cube(fountain, 0.9, 0.22, 0.9, 0x4fc6bd, 0, 1.28, 0);
cube(fountain, 0.35, 0.28, 0.35, 0xf3c84c, 0, 1.5, 0);
function addTree(x, z, scale = 1, hue = 0x3d8d63) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
world.add(tree);
cube(tree, 0.38, 1.1, 0.38, 0x705b43, 0, 0.55, 0);
cube(tree, 1.35, 1.25, 1.35, hue, 0, 1.55, 0);
cube(tree, 0.95, 0.85, 0.95, 0x55a970, 0.25, 2.3, -0.15);
cube(tree, 0.72, 0.62, 0.72, 0x6fbd7f, -0.35, 2.15, 0.2);
}
[
[-14, -11, 1.05], [-11, -12, 0.85], [-5, -12, 1.0],
[3, -12, 0.9], [11, -12, 1.1], [14, -9, 0.9],
[14, 1, 1.0], [14, 10, 0.85], [10, 12, 1.05],
[3, 12, 0.9], [-4, 12, 1.0], [-12, 12, 0.85],
[-14, 8, 0.95], [-14, 2, 1.05], [11, 4, 0.85],
[-5, 9, 0.8], [5, 9, 0.9]
].forEach(([x, z, s], index) => {
addTree(x, z, s, index % 3 === 0 ? 0x3e9566 : 0x4b9d69);
});
function addLamp(x, z) {
const lamp = new THREE.Group();
lamp.position.set(x, 0, z);
world.add(lamp);
cube(lamp, 0.15, 2.2, 0.15, 0x43575a, 0, 1.1, 0);
cube(lamp, 0.75, 0.16, 0.28, 0x43575a, 0.25, 2.15, 0);
cube(lamp, 0.38, 0.32, 0.38, 0xffdc78, 0.5, 2.02, 0);
}
addLamp(-5, -4);
addLamp(5, 4);
addLamp(-5, 7);
addLamp(5, -7);
// Small voxel utility huts.
[
[-13, -2, 0x7a8790, 0xf0b650],
[13, 7, 0x6d7780, 0xe98b64],
[11, -11, 0x7d8d78, 0x71c49a]
].forEach(([x, z, body, roof]) => {
const hut = new THREE.Group();
hut.position.set(x, 0, z);
world.add(hut);
cube(hut, 1.8, 1.3, 1.8, body, 0, 0.65, 0);
cube(hut, 2.1, 0.28, 2.1, roof, 0, 1.42, 0);
cube(hut, 0.55, 0.75, 0.08, 0x263f46, 0, 0.55, -0.94);
});
// -------------------------------------------------------------------------
// Landmarks and observers
// -------------------------------------------------------------------------
const landmarkGroups = [];
const observerGroups = [];
const destinationBeacons = new Map();
function makeLandmark(landmark) {
const group = new THREE.Group();
group.position.set(landmark.build.x, 0, landmark.build.z);
group.userData = { kind: "landmark", id: landmark.id };
world.add(group);
cube(group, 3.1, 0.3, 3.1, 0xd5cfb9, 0, 0.18, 0);
if (landmark.type === "tower") {
cube(group, 1.35, 2.15, 1.35, landmark.color, 0, 1.4, 0);
cube(group, 1.65, 0.3, 1.65, landmark.accent, 0, 2.62, 0);
cube(group, 0.85, 0.8, 0.85, landmark.color, 0, 3.15, 0);
cube(group, 0.35, 0.45, 0.35, landmark.accent, 0, 3.75, 0);
} else if (landmark.type === "library") {
cube(group, 2.55, 1.35, 2.15, landmark.color, 0, 1.02, 0);
cube(group, 2.85, 0.25, 2.4, landmark.accent, 0, 1.82, 0);
cube(group, 0.48, 1.0, 0.48, landmark.accent, -0.72, 0.83, -1.18);
cube(group, 0.48, 1.0, 0.48, landmark.accent, 0, 0.83, -1.18);
cube(group, 0.48, 1.0, 0.48, landmark.accent, 0.72, 0.83, -1.18);
cube(group, 1.5, 0.35, 0.1, 0x284961, 0, 1.42, -1.1);
} else if (landmark.type === "foundry") {
cube(group, 2.45, 1.15, 2.1, landmark.color, 0, 0.93, 0);
cube(group, 2.7, 0.3, 2.3, landmark.accent, 0, 1.65, 0);
cube(group, 0.65, 1.9, 0.65, 0x5e6868, 0.6, 2.2, 0.3);
cube(group, 0.9, 0.25, 0.9, landmark.accent, 0.6, 3.22, 0.3);
cube(group, 1.15, 0.75, 0.12, 0x34464d, 0, 0.95, -1.08);
} else {
cube(group, 2.55, 0.35, 2.25, 0x8e7658, 0, 0.42, 0);
cube(group, 1.65, 0.16, 1.45, 0x56bca4, -0.35, 0.66, 0.25);
cube(group, 0.48, 1.2, 0.48, 0x705b43, 0.75, 1.05, 0.5);
cube(group, 1.2, 1.1, 1.2, landmark.color, 0.75, 1.9, 0.5);
cube(group, 0.78, 0.72, 0.78, 0x62c58a, 0.5, 2.75, 0.4);
}
const label = makeLabel(
landmark.name.toUpperCase(),
"LANDMARK",
"#173139",
"#fffdf6"
);
label.position.set(0, landmark.type === "garden" ? 3.55 : 4.45, 0);
group.add(label);
landmarkGroups.push(group);
return group;
}
landmarks.forEach(makeLandmark);
for (const landmark of landmarks) {
const beacon = new THREE.Group();
beacon.position.set(landmark.target.x, 0, landmark.target.z);
world.add(beacon);
const material = new THREE.MeshStandardMaterial({
color: 0xffbd4a,
emissive: 0x8f5510,
emissiveIntensity: 0.35,
roughness: 0.65
});
cube(beacon, 0.18, 0.13, 0.18, 0xffbd4a, -0.48, 0.13, -0.48, material);
cube(beacon, 0.18, 0.13, 0.18, 0xffbd4a, 0.48, 0.13, -0.48, material);
cube(beacon, 0.18, 0.13, 0.18, 0xffbd4a, -0.48, 0.13, 0.48, material);
cube(beacon, 0.18, 0.13, 0.18, 0xffbd4a, 0.48, 0.13, 0.48, material);
cube(beacon, 0.16, 0.65, 0.16, 0xffbd4a, 0, 0.4, 0, material);
beacon.visible = false;
destinationBeacons.set(landmark.id, beacon);
}
function makeObserver(observer) {
const group = new THREE.Group();
group.userData = { kind: "observer", id: observer.id };
group.position.set(observer.x, 0.05, observer.z);
// Legs, body, head, visor, and backpack.
cube(group, 0.24, 0.46, 0.28, 0x273f47, -0.17, 0.23, 0);
cube(group, 0.24, 0.46, 0.28, 0x273f47, 0.17, 0.23, 0);
cube(group, 0.62, 0.68, 0.42, observer.color, 0, 0.78, 0);
cube(group, 0.56, 0.54, 0.52, 0xf2c79f, 0, 1.36, -0.01);
cube(group, 0.43, 0.18, 0.08, 0x233c45, 0, 1.39, -0.29);
cube(group, 0.36, 0.42, 0.2, 0x526a70, 0, 0.82, 0.3);
// Blocky forward chevron.
cube(group, 0.14, 0.07, 0.4, 0xffd05b, -0.18, 0.08, -0.62);
cube(group, 0.14, 0.07, 0.4, 0xffd05b, 0.18, 0.08, -0.62);
cube(group, 0.14, 0.07, 0.33, 0xffd05b, 0, 0.08, -0.82);
const label = makeLabel(
observer.name.toUpperCase(),
observer.role.toUpperCase(),
observer.color === 0x25c9a7 ? "#126c61" : "#173139",
"#fffdf6"
);
label.position.set(0, 2.25, 0);
group.add(label);
world.add(group);
observerGroups.push(group);
return group;
}
observers.forEach(makeObserver);
const selectedObserverGroup = new Map(
observerGroups.map((group, index) => [observers[index].id, group])
);
const alternateObserver = new THREE.Group();
alternateObserver.visible = false;
world.add(alternateObserver);
cube(alternateObserver, 0.24, 0.46, 0.28, 0x503c75, -0.17, 0.23, 0);
cube(alternateObserver, 0.24, 0.46, 0.28, 0x503c75, 0.17, 0.23, 0);
cube(alternateObserver, 0.62, 0.68, 0.42, 0x9f7aea, 0, 0.78, 0);
cube(alternateObserver, 0.56, 0.54, 0.52, 0xf2c79f, 0, 1.36, -0.01);
cube(alternateObserver, 0.43, 0.18, 0.08, 0x302345, 0, 1.39, -0.29);
cube(alternateObserver, 0.36, 0.42, 0.2, 0x503c75, 0, 0.82, 0.3);
cube(alternateObserver, 0.14, 0.07, 0.4, 0xd8c7ff, -0.18, 0.08, -0.62);
cube(alternateObserver, 0.14, 0.07, 0.4, 0xd8c7ff, 0.18, 0.08, -0.62);
cube(alternateObserver, 0.14, 0.07, 0.33, 0xd8c7ff, 0, 0.08, -0.82);
const startMarker = new THREE.Group();
world.add(startMarker);
const startMaterial = new THREE.MeshStandardMaterial({
color: 0x25c9a7,
emissive: 0x0b6d5e,
emissiveIntensity: 0.35
});
cube(startMarker, 0.2, 0.12, 0.2, 0x25c9a7, -0.55, 0.12, -0.55, startMaterial);
cube(startMarker, 0.2, 0.12, 0.2, 0x25c9a7, 0.55, 0.12, -0.55, startMaterial);
cube(startMarker, 0.2, 0.12, 0.2, 0x25c9a7, -0.55, 0.12, 0.55, startMaterial);
cube(startMarker, 0.2, 0.12, 0.2, 0x25c9a7, 0.55, 0.12, 0.55, startMaterial);
const routeGroup = new THREE.Group();
const alternateRouteGroup = new THREE.Group();
const alternateEndpointGroup = new THREE.Group();
world.add(routeGroup, alternateRouteGroup, alternateEndpointGroup);
// -------------------------------------------------------------------------
// Grid route construction
// -------------------------------------------------------------------------
const blockedCells = new Set();
// Central fountain footprint.
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
blockedCells.add(keyFor(x, z));
}
}
// Landmark building footprints.
for (const landmark of landmarks) {
for (let dx = -1; dx <= 1; dx++) {
for (let dz = -1; dz <= 1; dz++) {
blockedCells.add(keyFor(landmark.build.x + dx, landmark.build.z + dz));
}
}
}
function manhattan(a, b) {
return Math.abs(a.x - b.x) + Math.abs(a.z - b.z);
}
function findGridPath(start, goal, extraBlocked = null) {
const bounds = { minX: -15, maxX: 15, minZ: -14, maxZ: 14 };
const startKey = keyFor(start.x, start.z);
const goalKey = keyFor(goal.x, goal.z);
const isBlocked = (key) =>
blockedCells.has(key) || (extraBlocked && extraBlocked.has(key));
if (isBlocked(startKey) || isBlocked(goalKey)) return null;
const open = [{ key: startKey, x: start.x, z: start.z, f: manhattan(start, goal) }];
const cameFrom = new Map();
const gScore = new Map([[startKey, 0]]);
const closed = new Set();
const neighborDirections = [DIRS[0], DIRS[1], DIRS[2], DIRS[3]];
while (open.length) {
open.sort((a, b) => a.f - b.f);
const current = open.shift();
if (closed.has(current.key)) continue;
closed.add(current.key);
if (current.key === goalKey) {
const path = [];
let cursor = goalKey;
while (cursor) {
path.unshift(parseKey(cursor));
if (cursor === startKey) break;
cursor = cameFrom.get(cursor);
}
return path;
}
for (const direction of neighborDirections) {
const nx = current.x + direction.x;
const nz = current.z + direction.z;
if (
nx < bounds.minX ||
nx > bounds.maxX ||
nz < bounds.minZ ||
nz > bounds.maxZ
) {
continue;
}
const neighborKey = keyFor(nx, nz);
if (closed.has(neighborKey) || isBlocked(neighborKey)) continue;
const tentative = (gScore.get(current.key) ?? Infinity) + 1;
if (tentative >= (gScore.get(neighborKey) ?? Infinity)) continue;
cameFrom.set(neighborKey, current.key);
gScore.set(neighborKey, tentative);
open.push({
key: neighborKey,
x: nx,
z: nz,
f: tentative + Math.abs(nx - goal.x) + Math.abs(nz - goal.z)
});
}
}
return null;
}
function fallbackManhattanPath(start, goal) {
const path = [start];
let current = { ...start };
while (current.z !== goal.z) {
current = {
x: current.x,
z: current.z + Math.sign(goal.z - current.z)
};
path.push({ ...current });
}
while (current.x !== goal.x) {
current = {
x: current.x + Math.sign(goal.x - current.x),
z: current.z
};
path.push({ ...current });
}
return path;
}
function directionBetween(a, b) {
const dx = b.x - a.x;
const dz = b.z - a.z;
if (dz === -1 && dx === 0) return 0;
if (dx === 1 && dz === 0) return 1;
if (dz === 1 && dx === 0) return 2;
if (dx === -1 && dz === 0) return 3;
return null;
}
function pathToSegments(path) {
const segments = [];
for (let i = 0; i < path.length - 1; i++) {
const direction = directionBetween(path[i], path[i + 1]);
if (direction === null) continue;
const previous = segments[segments.length - 1];
if (previous && previous.direction === direction) {
previous.length++;
} else {
segments.push({
direction,
length: 1,
startX: path[i].x,
startZ: path[i].z
});
}
}
let distance = 0;
segments.forEach((segment) => {
segment.startDistance = distance;
distance += segment.length;
segment.endDistance = distance;
});
return { segments, distance };
}
function rotatePathClockwise(path) {
const start = path[0];
return path.map((point, index) => {
if (index === 0) return { ...point };
const dx = point.x - start.x;
const dz = point.z - start.z;
return {
x: start.x - dz,
z: start.z + dx
};
});
}
function turnForDelta(delta) {
if (delta === 1) return { id: "right", label: "RIGHT" };
if (delta === 2) return { id: "around", label: "AROUND" };
if (delta === 3) return { id: "left", label: "LEFT" };
return { id: "straight", label: "STRAIGHT" };
}
function buildScript(segments, startingHeading) {
let currentHeading = startingHeading;
return segments.map((segment) => {
const delta = (segment.direction - currentHeading + 4) % 4;
const turn = turnForDelta(delta);
currentHeading = segment.direction;
return {
...segment,
turn
};
});
}
function drawRoute(path, group, color, dashed = false) {
clearGroup(group);
const material = dashed
? new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.18,
roughness: 0.8,
transparent: true,
opacity: 0.85
})
: getMaterial(color);
for (let i = 0; i < path.length - 1; i++) {
const a = path[i];
const b = path[i + 1];
const dx = Math.sign(b.x - a.x);
const dz = Math.sign(b.z - a.z);
const length = Math.abs(b.x - a.x) + Math.abs(b.z - a.z);
const spacing = dashed ? 0.55 : 0.46;
const size = dashed ? 0.16 : 0.24;
for (let step = 0; step < length; step += spacing) {
cube(
group,
size,
0.1,
size,
color,
a.x + dx * (step + 0.5),
0.12,
a.z + dz * (step + 0.5),
material
);
}
}
}
function makeEndpointMarker(point) {
clearGroup(alternateEndpointGroup);
const material = new THREE.MeshStandardMaterial({
color: 0x9f7aea,
emissive: 0x4c277d,
emissiveIntensity: 0.45
});
cube(alternateEndpointGroup, 0.22, 0.14, 0.22, 0x9f7aea, -0.55, 0.13, -0.55, material);
cube(alternateEndpointGroup, 0.22, 0.14, 0.22, 0x9f7aea, 0.55, 0.13, -0.55, material);
cube(alternateEndpointGroup, 0.22, 0.14, 0.22, 0x9f7aea, -0.55, 0.13, 0.55, material);
cube(alternateEndpointGroup, 0.22, 0.14, 0.22, 0x9f7aea, 0.55, 0.13, 0.55, material);
cube(alternateEndpointGroup, 0.18, 0.8, 0.18, 0x9f7aea, 0, 0.45, 0, material);
const label = makeLabel(
"NEW FACING ENDS HERE",
`GRID ${point.x}, ${point.z}`,
"#594079",
"#fffdf6"
);
label.position.set(0, 1.55, 0);
alternateEndpointGroup.add(label);
alternateEndpointGroup.position.set(point.x, 0, point.z);
}
function placeName(point) {
let zone;
if (point.z <= -8) zone = "Lower terrace";
else if (point.z >= 8) zone = "Upper terrace";
else if (point.x <= -8) zone = "West promenade";
else if (point.x >= 8) zone = "East arcade";
else zone = "Central gardens";
return `${zone} · ${point.x}, ${point.z}`;
}
// -------------------------------------------------------------------------
// UI and route generation
// -------------------------------------------------------------------------
function updateHeadingUI() {
headingRange.value = String(state.heading * 90);
headingValue.textContent = `${DIRS[state.heading].name.toUpperCase()} · ${state.heading * 90}°`;
compassNeedle.style.transform = `translateX(-50%) rotate(${state.heading * 90}deg)`;
}
function cancelAnimation() {
state.animation = null;
alternateObserver.visible = false;
progressFill.style.width = "0%";
}
function buildRoute() {
cancelAnimation();
const observer = getObserver();
const landmark = getLandmark();
const start = { x: observer.x, z: observer.z };
const target = { x: landmark.target.x, z: landmark.target.z };
// First physical move is left of the selected initial heading.
const firstDirection = (state.heading + 3) % 4;
const calibration = {
x: start.x + DIRS[firstDirection].x,
z: start.z + DIRS[firstDirection].z
};
// Prevent an immediate reversal through the starting cell.
let gridPath = findGridPath(
calibration,
target,
new Set([keyFor(start.x, start.z)])
);
if (!gridPath) {
gridPath = fallbackManhattanPath(calibration, target);
}
const mainPath = [start, ...gridPath.slice(1)];
const alternatePath = rotatePathClockwise(mainPath);
const main = pathToSegments(mainPath);
const alternate = pathToSegments(alternatePath);
const script = buildScript(main.segments, state.heading);
state.route = {
observer,
landmark,
mainPath,
alternatePath,
mainSegments: main.segments,
alternateSegments: alternate.segments,
totalDistance: main.distance,
script
};
drawRoute(mainPath, routeGroup, 0xffbd4a, false);
drawRoute(alternatePath, alternateRouteGroup, 0x9f7aea, true);
const alternateEnd = alternatePath[alternatePath.length - 1];
makeEndpointMarker(alternateEnd);
destinationBeacons.forEach((beacon, id) => {
beacon.visible = id === landmark.id;
});
startMarker.position.set(start.x, 0, start.z);
const observerGroup = selectedObserverGroup.get(observer.id);
observerGroup.position.set(start.x, 0.05, start.z);
observerGroup.rotation.y = DIRS[state.heading].yaw;
alternateObserver.position.set(start.x, 0.05, start.z);
alternateObserver.rotation.y = DIRS[(state.heading + 1) % 4].yaw;
alternateObserver.visible = false;
$("pairSummary").textContent = `${observer.name} → ${landmark.name}`;
routeMeta.textContent = `${main.distance} blocks · ${script.length} moves`;
instructionList.innerHTML = "";
script.forEach((step, index) => {
const article = document.createElement("article");
article.className = "instruction-step";
article.innerHTML = `
<div class="step-index">${String(index + 1).padStart(2, "0")}</div>
<div class="step-content">
<span class="turn-badge ${step.turn.id}">
${step.turn.id === "left" ? "↰" : step.turn.id === "right" ? "↱" : "→"}
${step.turn.label}
</span>
<span class="walk-cue"><b>${step.length}</b> ${step.length === 1 ? "block" : "blocks"}</span>
</div>
`;
instructionList.appendChild(article);
});
endpointA.textContent = landmark.name;
endpointB.textContent = placeName(alternateEnd);
const endpointDistance = manhattan(alternateEnd, target);
compareResult.textContent =
`Both scripts begin with LEFT. With the initial frame rotated 90°, ` +
`the route ends ${endpointDistance} blocks from the chosen landmark.`;
compareCard.classList.remove("complete");
progressFill.style.width = "0%";
statusLine.textContent = `Ready · ${main.distance} blocks to ${landmark.name}.`;
updateHeadingUI();
}
let toastTimer;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2800);
}
observerSelect.addEventListener("change", () => {
state.observerId = observerSelect.value;
state.heading = getObserver().defaultHeading;
buildRoute();
});
destinationSelect.addEventListener("change", () => {
state.destinationId = destinationSelect.value;
buildRoute();
});
headingRange.addEventListener("input", () => {
state.heading = Number(headingRange.value) / 90;
buildRoute();
});
$("rotateLeftBtn").addEventListener("click", () => {
state.heading = (state.heading + 3) % 4;
buildRoute();
});
$("rotateRightBtn").addEventListener("click", () => {
state.heading = (state.heading + 1) % 4;
buildRoute();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function sampleSegments(segments, distance) {
let remaining = distance;
for (let index = 0; index < segments.length; index++) {
const segment = segments[index];
if (remaining <= segment.length || index === segments.length - 1) {
const amount = Math.min(1, remaining / segment.length);
const direction = DIRS[segment.direction];
return {
x: segment.startX + direction.x * segment.length * amount,
z: segment.startZ + direction.z * segment.length * amount,
direction: segment.direction,
index
};
}
remaining -= segment.length;
}
const last = segments[segments.length - 1];
return {
x: last.startX + DIRS[last.direction].x * last.length,
z: last.startZ + DIRS[last.direction].z * last.length,
direction: last.direction,
index: segments.length - 1
};
}
function highlightInstruction(index, done = false) {
const steps = [...instructionList.children];
steps.forEach((step, stepIndex) => {
step.classList.toggle("active", stepIndex === index);
step.classList.toggle("done", done || stepIndex < index);
});
}
function beginAnimation(compare) {
if (!state.route) return;
cancelAnimation();
const observerGroup = selectedObserverGroup.get(state.observerId);
const start = state.route.mainPath[0];
observerGroup.position.set(start.x, 0.05, start.z);
observerGroup.rotation.y = DIRS[state.heading].yaw;
alternateObserver.position.set(start.x, 0.05, start.z);
alternateObserver.rotation.y = DIRS[(state.heading + 1) % 4].yaw;
alternateObserver.visible = compare;
const duration = Math.max(
2300,
Math.min(8200, state.route.totalDistance * 165)
);
state.animation = {
startTime: performance.now(),
duration,
compare,
completed: false
};
statusLine.textContent = compare
? "Running the same script from both orientations…"
: "Following the selected orientation…";
}
function updateAnimation(now) {
const animation = state.animation;
if (!animation) return;
const progress = Math.min(1, (now - animation.startTime) / animation.duration);
const distance = state.route.totalDistance * progress;
const observerGroup = selectedObserverGroup.get(state.observerId);
const mainSample = sampleSegments(state.route.mainSegments, distance);
observerGroup.position.set(mainSample.x, 0.05, mainSample.z);
observerGroup.rotation.y = DIRS[mainSample.direction].yaw;
if (animation.compare) {
const alternateSample = sampleSegments(state.route.alternateSegments, distance);
alternateObserver.position.set(alternateSample.x, 0.05, alternateSample.z);
alternateObserver.rotation.y = DIRS[alternateSample.direction].yaw;
}
progressFill.style.width = `${progress * 100}%`;
highlightInstruction(mainSample.index, progress >= 1);
if (progress >= 1) {
state.animation = null;
if (animation.compare) {
compareCard.classList.add("complete");
statusLine.textContent = "Same LEFT instruction. Two different world locations.";
showToast(
`Same script. Trial B ends at ${placeName(
state.route.alternatePath[state.route.alternatePath.length - 1]
)}.`
);
} else {
statusLine.textContent = `Arrived at ${state.route.landmark.name}.`;
showToast(`Arrived at ${state.route.landmark.name}.`);
}
}
}
$("followBtn").addEventListener("click", () => beginAnimation(false));
$("compareBtn").addEventListener("click", () => beginAnimation(true));
// -------------------------------------------------------------------------
// Direct camera controls and scene picking
// -------------------------------------------------------------------------
let dragState = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragState = {
pointerId: event.pointerId,
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 (!dragState || dragState.pointerId !== event.pointerId) return;
const dx = event.clientX - dragState.lastX;
const dy = event.clientY - dragState.lastY;
if (
Math.abs(event.clientX - dragState.startX) +
Math.abs(event.clientY - dragState.startY) >
5
) {
dragState.moved = true;
}
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.004,
0.42,
1.18
);
dragState.lastX = event.clientX;
dragState.lastY = event.clientY;
updateCamera();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickScene(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;
camera.updateMatrixWorld();
raycaster.setFromCamera(pointer, camera);
const roots = [...landmarkGroups, ...observerGroups];
const hits = raycaster.intersectObjects(roots, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.kind) object = object.parent;
if (!object || !object.visible) continue;
if (object.userData.kind === "landmark") {
state.destinationId = object.userData.id;
destinationSelect.value = state.destinationId;
buildRoute();
showToast(`Destination changed to ${getLandmark().name}.`);
return;
}
if (object.userData.kind === "observer") {
state.observerId = object.userData.id;
observerSelect.value = state.observerId;
state.heading = getObserver().defaultHeading;
buildRoute();
showToast(`Observer changed to ${getObserver().name}.`);
return;
}
}
}
renderer.domElement.addEventListener("pointerup", (event) => {
if (dragState && !dragState.moved) {
pickScene(event);
}
dragState = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
dragState = null;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.018,
20,
54
);
updateCamera();
},
{ passive: false }
);
$("resetViewBtn").addEventListener("click", () => {
cameraState.theta = 0.68;
cameraState.phi = 0.77;
cameraState.radius = 39;
updateCamera();
});
// -------------------------------------------------------------------------
// Main loop and initial state
// -------------------------------------------------------------------------
function render(now) {
updateAnimation(now);
renderer.render(scene, camera);
requestAnimationFrame(render);
}
buildRoute();
requestAnimationFrame(render);
</script>
</body>
</html>
```