Prompt specification
Build or customize the Three.js 3D scene "Terrain Changes an Observation Window" 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: Terrain Changes an Observation Window
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/terrain-changes-an-observation-window-x3796
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing telescope targets above terrain and the horizon. Create a toy observing planner with a fixed synthetic target path and adjustable terrain profile. Compute visible intervals from occlusion, avoiding real astronomical scheduling claims. Low-poly.
Working Three.js Starter (terrain-changes-an-observation-window-x3796):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Telescope Window — Synthetic Occlusion Planner</title>
<style>
:root {
color-scheme: dark;
--bg: #071319;
--panel: rgba(9, 24, 30, 0.88);
--panel-border: rgba(151, 224, 216, 0.18);
--text: #e9f5ef;
--muted: #91aaa8;
--mint: #74e6c4;
--amber: #ffba68;
--coral: #ff7b72;
--cyan: #72cfe2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport,
#viewport canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
#viewport canvas {
display: block;
touch-action: none;
}
.panel {
position: absolute;
z-index: 10;
border: 1px solid var(--panel-border);
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.planner {
top: 18px;
bottom: 18px;
left: 18px;
width: 370px;
padding: 20px;
overflow-y: auto;
border-radius: 20px;
scrollbar-width: thin;
scrollbar-color: rgba(116, 230, 196, 0.28) transparent;
}
.planner::-webkit-scrollbar {
width: 6px;
}
.planner::-webkit-scrollbar-thumb {
border-radius: 10px;
background: rgba(116, 230, 196, 0.25);
}
.kicker {
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 7px;
font-size: 28px;
line-height: 1.05;
letter-spacing: -0.04em;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.synthetic-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 6px 9px;
border: 1px solid rgba(255, 186, 104, 0.25);
border-radius: 999px;
background: rgba(255, 186, 104, 0.07);
color: var(--amber);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.synthetic-badge::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
content: "";
box-shadow: 0 0 12px currentColor;
}
section {
margin-top: 20px;
padding-top: 17px;
border-top: 1px solid rgba(151, 224, 216, 0.11);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
color: #c7d9d4;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.section-title span:last-child {
color: var(--muted);
font-weight: 600;
letter-spacing: 0.04em;
text-transform: none;
}
.presets {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 7px;
}
.preset,
.small-button {
border: 1px solid rgba(151, 224, 216, 0.14);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
color: #b9cbc7;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
color 150ms ease,
transform 150ms ease;
}
.preset {
padding: 9px 8px;
font-size: 11px;
}
.preset:hover,
.small-button:hover {
border-color: rgba(116, 230, 196, 0.42);
background: rgba(116, 230, 196, 0.08);
color: white;
}
.preset:active,
.small-button:active {
transform: translateY(1px);
}
.preset.active {
border-color: rgba(116, 230, 196, 0.52);
background: rgba(116, 230, 196, 0.13);
color: var(--mint);
}
.control {
margin-top: 12px;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
font-size: 11px;
}
.control-row label {
color: #b9cbc7;
}
output {
min-width: 60px;
color: var(--mint);
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 10px;
outline: none;
background:
linear-gradient(
90deg,
rgba(116, 230, 196, 0.7),
rgba(116, 230, 196, 0.7)
)
no-repeat,
rgba(255, 255, 255, 0.1);
appearance: none;
-webkit-appearance: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
border: 2px solid #092024;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 0 1px rgba(116, 230, 196, 0.35);
appearance: none;
-webkit-appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #092024;
border-radius: 50%;
background: var(--mint);
}
.chart-card {
overflow: hidden;
border: 1px solid rgba(151, 224, 216, 0.1);
border-radius: 11px;
background: rgba(0, 0, 0, 0.13);
}
.chart-card canvas {
display: block;
width: 100%;
height: 92px;
}
.chart-caption {
padding: 0 10px 8px;
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.time-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
margin: 3px 0 11px;
}
.time-value {
color: var(--amber);
font-size: 11px;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
#timeline {
height: 116px !important;
}
.interval-list {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 10px;
}
.interval {
padding: 5px 7px;
border: 1px solid rgba(116, 230, 196, 0.19);
border-radius: 6px;
background: rgba(116, 230, 196, 0.07);
color: #b9ebdc;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.interval.none {
border-color: rgba(255, 123, 114, 0.22);
background: rgba(255, 123, 114, 0.07);
color: #ffc0ba;
}
.disclaimer {
margin: 18px 0 0;
padding: 11px;
border-left: 2px solid var(--amber);
background: rgba(255, 186, 104, 0.055);
color: #b8aaa0;
font-size: 10px;
line-height: 1.55;
}
.hud {
top: 18px;
right: 18px;
width: 252px;
padding: 16px;
border-radius: 16px;
}
.hud-label {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 9px;
color: var(--mint);
font-size: 12px;
font-weight: 800;
}
.status-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.hud.blocked .status-row {
color: var(--coral);
}
.hud-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 14px;
}
.metric {
padding: 9px;
border: 1px solid rgba(151, 224, 216, 0.09);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
}
.metric strong {
display: block;
margin-top: 4px;
color: #e8f4ef;
font-size: 12px;
font-variant-numeric: tabular-nums;
}
.hud-path {
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid rgba(151, 224, 216, 0.09);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.view-tools {
position: absolute;
z-index: 8;
right: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 9px;
}
.small-button {
padding: 8px 11px;
font-size: 10px;
}
.hint {
padding: 8px 11px;
border: 1px solid rgba(151, 224, 216, 0.12);
border-radius: 9px;
background: rgba(7, 19, 25, 0.72);
color: var(--muted);
font-size: 10px;
backdrop-filter: blur(10px);
}
#labels {
position: absolute;
inset: 0;
z-index: 5;
overflow: hidden;
pointer-events: none;
}
.target-label {
position: absolute;
min-width: 86px;
padding: 6px 8px;
transform: translate(-50%, -125%);
border: 1px solid rgba(116, 230, 196, 0.38);
border-radius: 7px;
background: rgba(7, 19, 25, 0.78);
box-shadow: 0 7px 20px rgba(0, 0, 0, 0.25);
color: var(--mint);
text-align: center;
backdrop-filter: blur(7px);
will-change: left, top;
}
.target-label b,
.target-label span {
display: block;
}
.target-label b {
font-size: 9px;
letter-spacing: 0.09em;
}
.target-label span {
margin-top: 2px;
color: #91aaa8;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.08em;
}
.target-label.blocked {
border-color: rgba(255, 123, 114, 0.43);
color: var(--coral);
opacity: 0.82;
}
@media (max-width: 780px) {
.planner {
top: 10px;
bottom: auto;
left: 10px;
width: min(350px, calc(100vw - 20px));
max-height: 56vh;
padding: 15px;
border-radius: 15px;
}
h1 {
font-size: 23px;
}
.hud {
top: auto;
right: 10px;
bottom: 10px;
width: 220px;
padding: 12px;
}
.view-tools {
display: none;
}
.target-label {
min-width: 72px;
padding: 4px 6px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="labels"></div>
<aside class="panel planner">
<div class="kicker">Telescope Window</div>
<h1>Synthetic occlusion planner</h1>
<p class="intro">
Explore a fixed fictional target path against a low-poly terrain profile.
Clear intervals are calculated from terrain occlusion and the local horizon.
</p>
<div class="synthetic-badge">Geometry toy · no sky data</div>
<section>
<div class="section-title">
<span>Terrain profile</span>
<span>adjustable crest</span>
</div>
<div class="presets">
<button class="preset" data-preset="clear">Open plain</button>
<button class="preset" data-preset="north">North horn</button>
<button class="preset" data-preset="west">West knoll</button>
<button class="preset" data-preset="broad">Broad rise</button>
</div>
<div class="control">
<div class="control-row">
<label for="crestHeight">Crest height</label>
<output id="crestHeightValue"></output>
</div>
<input id="crestHeight" type="range" min="0" max="9" step="0.1">
</div>
<div class="control">
<div class="control-row">
<label for="crestDistance">Crest distance</label>
<output id="crestDistanceValue"></output>
</div>
<input id="crestDistance" type="range" min="4" max="20" step="0.5">
</div>
<div class="control">
<div class="control-row">
<label for="crestWidth">Crest width</label>
<output id="crestWidthValue"></output>
</div>
<input id="crestWidth" type="range" min="1" max="6" step="0.2">
</div>
<div class="control">
<div class="control-row">
<label for="crestBearing">Crest bearing</label>
<output id="crestBearingValue"></output>
</div>
<input id="crestBearing" type="range" min="-80" max="80" step="1">
</div>
<div class="control">
<div class="control-row">
<label for="roughness">Surface roughness</label>
<output id="roughnessValue"></output>
</div>
<input id="roughness" type="range" min="0" max="1" step="0.05">
</div>
<div class="chart-card" style="margin-top:14px">
<canvas id="profile"></canvas>
<div class="chart-caption">
Terrain slice through the observer along the selected crest bearing.
Distances are arbitrary scene units.
</div>
</div>
</section>
<section>
<div class="section-title">
<span>Fixed synthetic path</span>
<span>0–120 toy minutes</span>
</div>
<div class="time-row">
<input id="time" type="range" min="0" max="120" step="0.1" value="60">
<span id="timeValue" class="time-value">60.0 toy min</span>
</div>
<div class="chart-card">
<canvas id="timeline"></canvas>
</div>
<div id="intervals" class="interval-list"></div>
</section>
<p class="disclaimer">
Synthetic demonstration only. The path, time scale, angles, and terrain are
fictional. No ephemerides, real coordinates, weather, astronomical
visibility, or actual observing schedules are represented.
</p>
</aside>
<aside id="hud" class="panel hud">
<div class="hud-label">Current synthetic sample</div>
<div class="status-row">
<span class="status-dot"></span>
<span id="statusText">Checking line of sight…</span>
</div>
<div class="hud-grid">
<div class="metric">
<span class="hud-label">Clear total</span>
<strong id="durationText">—</strong>
</div>
<div class="metric">
<span class="hud-label">Intervals</span>
<strong id="intervalCount">—</strong>
</div>
</div>
<div class="hud-path" id="pathText"></div>
</aside>
<div class="view-tools">
<div class="hint">Drag to orbit · wheel to zoom · click a target</div>
<button id="resetView" class="small-button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.querySelector("#viewport");
const labelLayer = document.querySelector("#labels");
const profileCanvas = document.querySelector("#profile");
const timelineCanvas = document.querySelector("#timeline");
const hud = document.querySelector("#hud");
const observer = new THREE.Vector3(0, 1.65, 0);
const pathRadius = 27;
const pathDuration = 120;
const sampleCount = 361;
const yAxis = new THREE.Vector3(0, 1, 0);
const zAxis = new THREE.Vector3(0, 0, 1);
const state = {
crestHeight: 8,
crestDistance: 8.5,
crestWidth: 2.4,
crestBearing: 0,
roughness: 0.35,
time: 60
};
const presets = {
clear: {
crestHeight: 0.5,
crestDistance: 11,
crestWidth: 4.2,
crestBearing: 0,
roughness: 0.22
},
north: {
crestHeight: 8,
crestDistance: 8.5,
crestWidth: 2.4,
crestBearing: 0,
roughness: 0.35
},
west: {
crestHeight: 6.6,
crestDistance: 10,
crestWidth: 2.2,
crestBearing: -48,
roughness: 0.32
},
broad: {
crestHeight: 5.4,
crestDistance: 13,
crestWidth: 5.8,
crestBearing: 0,
roughness: 0.45
}
};
// -----------------------------------------------------------------------
// Renderer, scene, and camera
// -----------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071319);
scene.fog = new THREE.Fog(0x071319, 43, 92);
const camera = new THREE.PerspectiveCamera(
46,
window.innerWidth / window.innerHeight,
0.1,
150
);
const orbit = {
yaw: 0.58,
pitch: 0.38,
distance: 40,
target: new THREE.Vector3(0, 4, -2)
};
function updateCamera() {
const horizontal = Math.cos(orbit.pitch) * orbit.distance;
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontal,
orbit.target.y + Math.sin(orbit.pitch) * orbit.distance,
orbit.target.z + Math.cos(orbit.yaw) * horizontal
);
camera.lookAt(orbit.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xa5dfd5, 0x17221e, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffd5a0, 2.7);
sun.position.set(-13, 24, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -28;
sun.shadow.camera.right = 28;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
scene.add(sun);
// -----------------------------------------------------------------------
// Procedural low-poly terrain
// -----------------------------------------------------------------------
function terrainHeight(x, z) {
const distance = Math.hypot(x, z);
const angle = Math.atan2(x, -z);
const bearing = THREE.MathUtils.degToRad(state.crestBearing);
const centerX = Math.sin(bearing) * state.crestDistance;
const centerZ = -Math.cos(bearing) * state.crestDistance;
const dx = x - centerX;
const dz = z - centerZ;
const gaussian = Math.exp(
-(dx * dx + dz * dz) / (state.crestWidth * state.crestWidth)
);
const rolling =
0.42 * Math.sin(x * 0.38) * Math.cos(z * 0.31) +
0.26 * Math.cos((x - z) * 0.23) +
0.12 * Math.sin((x + z) * 0.17);
const distantShelf =
0.62 *
Math.exp(-Math.pow((distance - 27) / 5.5, 2)) *
(0.55 + 0.45 * Math.sin(angle * 3 + 0.6));
return Math.max(
0.02,
0.08 +
state.roughness * rolling +
state.crestHeight * gaussian +
distantShelf
);
}
const terrainGeometry = new THREE.PlaneGeometry(72, 72, 64, 64);
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPositions = terrainGeometry.attributes.position;
const terrainColors = new Float32Array(terrainPositions.count * 3);
terrainGeometry.setAttribute(
"color",
new THREE.BufferAttribute(terrainColors, 3)
);
const terrainMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.96,
metalness: 0.02
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.receiveShadow = true;
scene.add(terrain);
const terrainBase = new THREE.Mesh(
new THREE.BoxGeometry(72, 1.1, 72),
new THREE.MeshStandardMaterial({
color: 0x102428,
roughness: 1
})
);
terrainBase.position.y = -0.61;
terrainBase.receiveShadow = true;
scene.add(terrainBase);
const lowColor = new THREE.Color(0x173c3c);
const midColor = new THREE.Color(0x315b4d);
const highColor = new THREE.Color(0x70704c);
const peakColor = new THREE.Color(0x9d8864);
const workingColor = new THREE.Color();
function updateTerrain() {
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
const height = terrainHeight(x, z);
terrainPositions.setY(i, height);
if (height < 1.4) {
workingColor.copy(lowColor).lerp(midColor, height / 1.4);
} else if (height < 4.8) {
workingColor.copy(midColor).lerp(highColor, (height - 1.4) / 3.4);
} else {
workingColor.copy(highColor).lerp(
peakColor,
THREE.MathUtils.clamp((height - 4.8) / 4.5, 0, 1)
);
}
terrainColors[i * 3] = workingColor.r;
terrainColors[i * 3 + 1] = workingColor.g;
terrainColors[i * 3 + 2] = workingColor.b;
}
terrainPositions.needsUpdate = true;
terrainGeometry.attributes.color.needsUpdate = true;
terrainGeometry.computeVertexNormals();
terrainGeometry.computeBoundingSphere();
}
// -----------------------------------------------------------------------
// Synthetic sky grid and fixed target path
// -----------------------------------------------------------------------
function makeSkyLine(points, color, opacity, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const options = {
color,
transparent: true,
opacity,
depthWrite: false
};
const material = dashed
? new THREE.LineDashedMaterial({
...options,
dashSize: 0.45,
gapSize: 0.3
})
: new THREE.LineBasicMaterial(options);
const line = new THREE.Line(geometry, material);
if (dashed) {
line.computeLineDistances();
}
return line;
}
function pathAngles(time) {
const normalized = THREE.MathUtils.clamp(time / pathDuration, 0, 1);
const azimuth = THREE.MathUtils.degToRad(-70 + normalized * 140);
const altitude = THREE.MathUtils.degToRad(
10 + Math.sin(Math.PI * normalized) * 22
);
return { azimuth, altitude };
}
function pathDirection(time) {
const { azimuth, altitude } = pathAngles(time);
return new THREE.Vector3(
Math.cos(altitude) * Math.sin(azimuth),
Math.sin(altitude),
-Math.cos(altitude) * Math.cos(azimuth)
).normalize();
}
function pathPosition(time) {
return observer.clone().addScaledVector(pathDirection(time), pathRadius);
}
for (const altitudeDegrees of [0, 10, 20, 30]) {
const points = [];
const altitude = THREE.MathUtils.degToRad(altitudeDegrees);
for (let i = 0; i <= 128; i++) {
const azimuth = THREE.MathUtils.degToRad(-180 + (i / 128) * 360);
points.push(
new THREE.Vector3(
observer.x + pathRadius * Math.cos(altitude) * Math.sin(azimuth),
observer.y + pathRadius * Math.sin(altitude),
observer.z - pathRadius * Math.cos(altitude) * Math.cos(azimuth)
)
);
}
scene.add(
makeSkyLine(
points,
altitudeDegrees === 0 ? 0x70d6d5 : 0x6d9f9d,
altitudeDegrees === 0 ? 0.38 : 0.11
)
);
}
for (const azimuthDegrees of [-90, -45, 0, 45, 90]) {
const points = [];
const azimuth = THREE.MathUtils.degToRad(azimuthDegrees);
for (let altitudeDegrees = 0; altitudeDegrees <= 55; altitudeDegrees += 2.5) {
const altitude = THREE.MathUtils.degToRad(altitudeDegrees);
points.push(
new THREE.Vector3(
observer.x + pathRadius * Math.cos(altitude) * Math.sin(azimuth),
observer.y + pathRadius * Math.sin(altitude),
observer.z - pathRadius * Math.cos(altitude) * Math.cos(azimuth)
)
);
}
scene.add(makeSkyLine(points, 0x6d9f9d, 0.09));
}
const pathPoints = [];
for (let i = 0; i <= 240; i++) {
pathPoints.push(pathPosition((i / 240) * pathDuration));
}
const pathLine = makeSkyLine(pathPoints, 0xffb45f, 0.74, true);
scene.add(pathLine);
// A very subtle local-horizon disk.
const horizonDisk = new THREE.Mesh(
new THREE.CircleGeometry(pathRadius, 64),
new THREE.MeshBasicMaterial({
color: 0x6bd3d0,
transparent: true,
opacity: 0.018,
side: THREE.DoubleSide,
depthWrite: false
})
);
horizonDisk.rotation.x = -Math.PI / 2;
horizonDisk.position.y = observer.y;
scene.add(horizonDisk);
// -----------------------------------------------------------------------
// Low-poly telescope and aiming reticle
// -----------------------------------------------------------------------
const mount = new THREE.Group();
scene.add(mount);
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x243d42,
roughness: 0.55,
metalness: 0.65,
flatShading: true
});
const lightMetal = new THREE.MeshStandardMaterial({
color: 0x8fb0a9,
roughness: 0.45,
metalness: 0.55,
flatShading: true
});
const lensMaterial = new THREE.MeshStandardMaterial({
color: 0x6ed8df,
emissive: 0x1b6970,
emissiveIntensity: 0.65,
roughness: 0.18,
metalness: 0.15,
flatShading: true
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.68, 0.9, 0.34, 8),
darkMetal
);
base.position.y = 0.17;
base.castShadow = true;
mount.add(base);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(0.22, 0.31, 1.2, 8),
lightMetal
);
pedestal.position.y = 0.86;
pedestal.castShadow = true;
mount.add(pedestal);
const barrel = new THREE.Group();
barrel.position.copy(observer);
scene.add(barrel);
const tube = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.3, 1.7, 8),
lightMetal
);
tube.position.y = 0.83;
tube.castShadow = true;
barrel.add(tube);
const hood = new THREE.Mesh(
new THREE.CylinderGeometry(0.36, 0.33, 0.26, 10),
darkMetal
);
hood.position.y = 1.72;
hood.castShadow = true;
barrel.add(hood);
const lens = new THREE.Mesh(
new THREE.CylinderGeometry(0.285, 0.285, 0.045, 10),
lensMaterial
);
lens.position.y = 1.865;
barrel.add(lens);
const eyepiece = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.17, 0.3, 8),
darkMetal
);
eyepiece.position.y = 0.11;
eyepiece.castShadow = true;
barrel.add(eyepiece);
const reticleMaterial = new THREE.MeshBasicMaterial({
color: 0x74e6c4,
transparent: true,
opacity: 0.95,
depthTest: false
});
const reticle = new THREE.Group();
const reticleRing = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.035, 5, 32),
reticleMaterial
);
reticle.add(reticleRing);
reticle.renderOrder = 20;
scene.add(reticle);
const losGeometry = new THREE.BufferGeometry();
losGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const losMaterial = new THREE.LineBasicMaterial({
color: 0x74e6c4,
transparent: true,
opacity: 0.95
});
const sightLine = new THREE.Line(losGeometry, losMaterial);
sightLine.renderOrder = 15;
scene.add(sightLine);
const impactMarker = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.22, 0),
new THREE.MeshBasicMaterial({
color: 0xff7b72,
depthTest: false
})
);
impactMarker.visible = false;
impactMarker.renderOrder = 21;
scene.add(impactMarker);
function setLineEndpoints(line, start, end) {
const position = line.geometry.attributes.position;
position.setXYZ(0, start.x, start.y, start.z);
position.setXYZ(1, end.x, end.y, end.z);
position.needsUpdate = true;
line.geometry.computeBoundingSphere();
}
// -----------------------------------------------------------------------
// Named fixed targets
// -----------------------------------------------------------------------
const namedTargets = [
{ name: "EMBER-01", time: 18, color: 0xff9d72 },
{ name: "EMBER-02", time: 56, color: 0x74e6c4 },
{ name: "EMBER-03", time: 96, color: 0xffcf72 }
];
const pickMeshes = [];
const targetLabels = [];
const targetMeshes = [];
for (let i = 0; i < namedTargets.length; i++) {
const target = namedTargets[i];
const position = pathPosition(target.time);
const material = new THREE.MeshStandardMaterial({
color: target.color,
emissive: target.color,
emissiveIntensity: 0.48,
roughness: 0.4,
flatShading: true
});
const mesh = new THREE.Mesh(
new THREE.OctahedronGeometry(0.5, 0),
material
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.userData.targetIndex = i;
scene.add(mesh);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.7, 0.035, 5, 28),
new THREE.MeshBasicMaterial({
color: target.color,
transparent: true,
opacity: 0.7
})
);
ring.position.copy(position);
ring.quaternion.setFromUnitVectors(
zAxis,
observer.clone().sub(position).normalize()
);
scene.add(ring);
const label = document.createElement("div");
label.className = "target-label";
label.innerHTML = `<b>${target.name}</b><span>CHECKING</span>`;
labelLayer.appendChild(label);
pickMeshes.push(mesh);
targetMeshes.push(mesh);
targetLabels.push(label);
}
// -----------------------------------------------------------------------
// Visibility calculation
// -----------------------------------------------------------------------
function evaluateAtTime(time) {
const direction = pathDirection(time);
const targetPosition = observer.clone().addScaledVector(direction, pathRadius);
const maxDistance = targetPosition.distanceTo(observer);
const { altitude } = pathAngles(time);
// Local horizon test, independent of terrain.
if (altitude <= 0) {
return {
visible: false,
reason: "horizon",
targetPosition,
hitPoint: null
};
}
// March the observer-to-target segment against the analytic terrain.
// The first blocked sample is the visible terrain occlusion point.
const steps = 360;
for (let i = 1; i <= steps; i++) {
const distance = (i / steps) * maxDistance;
if (distance < 0.35) {
continue;
}
const x = observer.x + direction.x * distance;
const y = observer.y + direction.y * distance;
const z = observer.z + direction.z * distance;
if (y <= terrainHeight(x, z) + 0.035) {
return {
visible: false,
reason: "terrain",
targetPosition,
hitPoint: new THREE.Vector3(x, y, z)
};
}
}
return {
visible: true,
reason: "clear",
targetPosition,
hitPoint: null
};
}
let visibilitySamples = [];
let visibleIntervals = [];
let visibleDuration = 0;
function computeVisibility() {
visibilitySamples = [];
visibleIntervals = [];
const step = pathDuration / (sampleCount - 1);
let intervalStart = null;
for (let i = 0; i < sampleCount; i++) {
const time = i * step;
const result = evaluateAtTime(time);
visibilitySamples.push({
time,
altitude: THREE.MathUtils.radToDeg(pathAngles(time).altitude),
visible: result.visible,
reason: result.reason
});
if (result.visible && intervalStart === null) {
intervalStart = i === 0 ? 0 : Math.max(0, (i - 0.5) * step);
}
if (!result.visible && intervalStart !== null) {
visibleIntervals.push({
start: intervalStart,
end: Math.min(pathDuration, (i - 0.5) * step)
});
intervalStart = null;
}
}
if (intervalStart !== null) {
visibleIntervals.push({
start: intervalStart,
end: pathDuration
});
}
visibleDuration = visibleIntervals.reduce(
(sum, interval) => sum + interval.end - interval.start,
0
);
}
function updateCurrentSight() {
const result = evaluateAtTime(state.time);
const direction = pathDirection(state.time);
reticle.position.copy(result.targetPosition);
reticle.quaternion.setFromUnitVectors(zAxis, direction);
barrel.quaternion.setFromUnitVectors(yAxis, direction);
if (result.visible) {
reticleMaterial.color.setHex(0x74e6c4);
losMaterial.color.setHex(0x74e6c4);
setLineEndpoints(sightLine, observer, result.targetPosition);
impactMarker.visible = false;
} else {
reticleMaterial.color.setHex(0xff7b72);
losMaterial.color.setHex(0xff7b72);
setLineEndpoints(sightLine, observer, result.hitPoint || result.targetPosition);
impactMarker.visible = Boolean(result.hitPoint);
if (result.hitPoint) {
impactMarker.position.copy(result.hitPoint);
}
}
hud.classList.toggle("blocked", !result.visible);
const statusText = document.querySelector("#statusText");
if (result.reason === "terrain") {
statusText.textContent = "Terrain occluded";
} else if (result.reason === "horizon") {
statusText.textContent = "Below local horizon";
} else {
statusText.textContent = "Clear line of sight";
}
const { azimuth, altitude } = pathAngles(state.time);
document.querySelector("#pathText").innerHTML =
`Toy path coordinates:<br>` +
`Azimuth ${THREE.MathUtils.radToDeg(azimuth).toFixed(1)}° · ` +
`Altitude ${THREE.MathUtils.radToDeg(altitude).toFixed(1)}°`;
}
function updateNamedTargetStates() {
namedTargets.forEach((target, index) => {
const result = evaluateAtTime(target.time);
const label = targetLabels[index];
label.classList.toggle("blocked", !result.visible);
label.querySelector("span").textContent = result.visible
? "CLEAR"
: result.reason === "horizon"
? "BELOW HORIZON"
: "BLOCKED";
});
}
// -----------------------------------------------------------------------
// UI charts and readouts
// -----------------------------------------------------------------------
function prepareCanvas(canvas, cssHeight) {
const width = Math.max(1, canvas.clientWidth);
const height = cssHeight;
const ratio = Math.min(window.devicePixelRatio || 1, 2);
canvas.width = Math.round(width * ratio);
canvas.height = Math.round(height * ratio);
const context = canvas.getContext("2d");
context.setTransform(ratio, 0, 0, ratio, 0, 0);
context.clearRect(0, 0, width, height);
return { context, width, height };
}
function drawProfile() {
const { context, width, height } = prepareCanvas(profileCanvas, 92);
const left = 9;
const right = width - 9;
const top = 20;
const bottom = height - 13;
const maxHeight = 9;
const bearing = THREE.MathUtils.degToRad(state.crestBearing);
context.fillStyle = "rgba(255,255,255,0.018)";
context.fillRect(left, top, right - left, bottom - top);
context.strokeStyle = "rgba(151,224,216,0.12)";
context.lineWidth = 1;
context.beginPath();
context.moveTo(left, bottom);
context.lineTo(right, bottom);
context.stroke();
const points = [];
for (let i = 0; i <= 100; i++) {
const distance = (i / 100) * 30;
const x = Math.sin(bearing) * distance;
const z = -Math.cos(bearing) * distance;
const terrainY = terrainHeight(x, z);
const px = left + (distance / 30) * (right - left);
const py = bottom - (terrainY / maxHeight) * (bottom - top);
points.push([px, py]);
}
context.beginPath();
context.moveTo(points[0][0], bottom);
for (const [x, y] of points) {
context.lineTo(x, y);
}
context.lineTo(points[points.length - 1][0], bottom);
context.closePath();
context.fillStyle = "rgba(116,230,196,0.10)";
context.fill();
context.beginPath();
points.forEach(([x, y], index) => {
if (index === 0) context.moveTo(x, y);
else context.lineTo(x, y);
});
context.strokeStyle = "#74e6c4";
context.lineWidth = 1.5;
context.stroke();
const crestX =
left + (state.crestDistance / 30) * (right - left);
context.setLineDash([3, 4]);
context.strokeStyle = "rgba(255,186,104,0.55)";
context.beginPath();
context.moveTo(crestX, top);
context.lineTo(crestX, bottom);
context.stroke();
context.setLineDash([]);
context.font = "9px system-ui";
context.fillStyle = "#91aaa8";
context.fillText("terrain slice", left, 12);
context.textAlign = "right";
context.fillText("30", right, height - 2);
context.textAlign = "left";
context.fillText("0", left, height - 2);
}
function drawTimeline() {
const { context, width, height } = prepareCanvas(timelineCanvas, 116);
const left = 9;
const right = width - 9;
const top = 19;
const bottom = height - 22;
const plotWidth = right - left;
const plotHeight = bottom - top;
context.fillStyle = "rgba(255,123,114,0.10)";
context.fillRect(left, top, plotWidth, plotHeight);
for (const interval of visibleIntervals) {
const startX = left + (interval.start / pathDuration) * plotWidth;
const endX = left + (interval.end / pathDuration) * plotWidth;
context.fillStyle = "rgba(116,230,196,0.18)";
context.fillRect(startX, top, endX - startX, plotHeight);
}
context.strokeStyle = "rgba(151,224,216,0.14)";
context.lineWidth = 1;
context.beginPath();
context.moveTo(left, bottom);
context.lineTo(right, bottom);
context.stroke();
const maxAltitude = 40;
context.beginPath();
visibilitySamples.forEach((sample, index) => {
const x = left + (sample.time / pathDuration) * plotWidth;
const y = bottom - (sample.altitude / maxAltitude) * plotHeight;
if (index === 0) context.moveTo(x, y);
else context.lineTo(x, y);
});
context.strokeStyle = "#ffba68";
context.lineWidth = 1.6;
context.stroke();
const currentX = left + (state.time / pathDuration) * plotWidth;
context.strokeStyle = "rgba(255,255,255,0.85)";
context.lineWidth = 1;
context.beginPath();
context.moveTo(currentX, top);
context.lineTo(currentX, bottom);
context.stroke();
context.font = "9px system-ui";
context.fillStyle = "#91aaa8";
context.fillText("synthetic altitude", left, 11);
context.textAlign = "center";
for (const tick of [0, 30, 60, 90, 120]) {
const x = left + (tick / pathDuration) * plotWidth;
context.fillText(String(tick), x, height - 6);
}
context.textAlign = "right";
context.fillText("toy min", right, 11);
context.textAlign = "left";
}
function updateIntervalList() {
const container = document.querySelector("#intervals");
container.replaceChildren();
if (!visibleIntervals.length) {
const item = document.createElement("div");
item.className = "interval none";
item.textContent = "No clear intervals";
container.appendChild(item);
} else {
for (const interval of visibleIntervals) {
const item = document.createElement("div");
item.className = "interval";
item.textContent =
`${interval.start.toFixed(1)}–${interval.end.toFixed(1)} toy min`;
container.appendChild(item);
}
}
document.querySelector("#durationText").textContent =
`${visibleDuration.toFixed(1)} toy min`;
document.querySelector("#intervalCount").textContent =
visibleIntervals.length;
}
const controlDefinitions = [
{
id: "crestHeight",
output: "crestHeightValue",
format: value => `${value.toFixed(1)} m`
},
{
id: "crestDistance",
output: "crestDistanceValue",
format: value => `${value.toFixed(1)} m`
},
{
id: "crestWidth",
output: "crestWidthValue",
format: value => `${value.toFixed(1)} m`
},
{
id: "crestBearing",
output: "crestBearingValue",
format: value =>
`${value > 0 ? "+" : ""}${value.toFixed(0)}°`
},
{
id: "roughness",
output: "roughnessValue",
format: value => value.toFixed(2)
}
];
function updateControlReadouts() {
for (const control of controlDefinitions) {
const input = document.querySelector(`#${control.id}`);
const output = document.querySelector(`#${control.output}`);
output.textContent = control.format(Number(input.value));
}
}
function markPreset(name) {
document.querySelectorAll("[data-preset]").forEach(button => {
button.classList.toggle("active", button.dataset.preset === name);
});
}
function applyTerrainChange() {
updateTerrain();
computeVisibility();
updateCurrentSight();
updateNamedTargetStates();
updateIntervalList();
drawProfile();
drawTimeline();
}
function setTime(time) {
state.time = THREE.MathUtils.clamp(time, 0, pathDuration);
document.querySelector("#time").value = state.time;
document.querySelector("#timeValue").textContent =
`${state.time.toFixed(1)} toy min`;
updateCurrentSight();
drawTimeline();
}
controlDefinitions.forEach(control => {
const input = document.querySelector(`#${control.id}`);
input.addEventListener("input", () => {
state[control.id] = Number(input.value);
markPreset(null);
updateControlReadouts();
applyTerrainChange();
});
});
document.querySelector("#time").addEventListener("input", event => {
setTime(Number(event.target.value));
});
document.querySelectorAll("[data-preset]").forEach(button => {
button.addEventListener("click", () => {
const preset = presets[button.dataset.preset];
for (const [key, value] of Object.entries(preset)) {
state[key] = value;
const input = document.querySelector(`#${key}`);
if (input) input.value = value;
}
markPreset(button.dataset.preset);
updateControlReadouts();
applyTerrainChange();
});
});
document.querySelector("#resetView").addEventListener("click", () => {
orbit.yaw = 0.58;
orbit.pitch = 0.38;
orbit.distance = 40;
orbit.target.set(0, 4, -2);
});
// -----------------------------------------------------------------------
// Direct camera controls and target picking
// -----------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragState = null;
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragState = {
id: event.pointerId,
x: event.clientX,
y: event.clientY,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragState || dragState.id !== event.pointerId) return;
const dx = event.clientX - dragState.x;
const dy = event.clientY - dragState.y;
if (Math.abs(dx) + Math.abs(dy) > 2) {
dragState.moved = true;
}
orbit.yaw -= dx * 0.005;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.004,
0.12,
1.25
);
dragState.x = event.clientX;
dragState.y = event.clientY;
});
renderer.domElement.addEventListener("pointerup", event => {
if (!dragState || dragState.id !== event.pointerId) return;
if (!dragState.moved) {
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;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickMeshes, false);
if (hits.length) {
const index = hits[0].object.userData.targetIndex;
setTime(namedTargets[index].time);
}
}
dragState = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
dragState = null;
});
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.distance = THREE.MathUtils.clamp(
orbit.distance * Math.exp(event.deltaY * 0.001),
21,
68
);
},
{ passive: false }
);
// -----------------------------------------------------------------------
// Labels, resize, and animation
// -----------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateTargetLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
targetMeshes.forEach((mesh, index) => {
mesh.getWorldPosition(projected);
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
const label = targetLabels[index];
label.style.display = visible ? "block" : "none";
if (visible) {
label.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
});
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
drawProfile();
drawTimeline();
}
window.addEventListener("resize", resize);
function animate() {
requestAnimationFrame(animate);
updateCamera();
renderer.render(scene, camera);
updateTargetLabels();
}
// Initial state.
for (const control of controlDefinitions) {
const input = document.querySelector(`#${control.id}`);
input.value = state[control.id];
}
updateControlReadouts();
markPreset("north");
applyTerrainChange();
setTime(state.time);
resize();
animate();
</script>
</body>
</html>
```