Prompt specification
Build or customize the Three.js 3D scene "Observatory instrument teaching platform" 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: Observatory instrument teaching platform
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/observatory-instrument-teaching-platform-x1595
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a scene with a small telescope mount, distant-star direction markers, and enlarged optical path; keep the pointing geometry and lens-axis alignment coherent throughout rotation. Voxel style.
Working Three.js Starter (observatory-instrument-teaching-platform-x1595):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Pointing Laboratory</title>
<style>
:root {
color-scheme: dark;
font-family: "Courier New", monospace;
--cyan: #79ddff;
--gold: #ffd166;
--panel: rgba(5, 14, 28, 0.82);
--border: rgba(121, 221, 255, 0.32);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #050b16;
}
canvas {
display: block;
touch-action: none;
}
#app {
position: fixed;
inset: 0;
}
.hud {
position: fixed;
z-index: 10;
pointer-events: none;
color: #e9f7ff;
text-shadow: 0 2px 8px #000;
}
#title {
top: 18px;
left: 20px;
max-width: 430px;
}
#title h1 {
margin: 0;
font-size: clamp(18px, 2.5vw, 29px);
line-height: 1;
letter-spacing: 0.08em;
}
#title p {
margin: 9px 0 0;
color: var(--cyan);
font-size: 11px;
line-height: 1.5;
letter-spacing: 0.08em;
}
#readout {
top: 18px;
right: 18px;
width: 218px;
padding: 13px 15px;
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
font-size: 12px;
line-height: 1.75;
}
#readout .row {
display: flex;
justify-content: space-between;
gap: 14px;
}
#readout .label {
color: #829cb5;
}
#readout .value {
color: var(--cyan);
text-align: right;
}
#readout #mode {
color: var(--gold);
font-weight: bold;
letter-spacing: 0.12em;
}
#controls {
position: fixed;
z-index: 20;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 8px;
width: min(920px, calc(100% - 28px));
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid rgba(121, 221, 255, 0.28);
background: rgba(7, 18, 34, 0.9);
color: #b9d9e9;
padding: 10px 13px;
font: 700 11px/1.2 "Courier New", monospace;
letter-spacing: 0.06em;
cursor: pointer;
transition: background 120ms, border-color 120ms, color 120ms;
}
button:hover,
button:focus-visible {
border-color: var(--cyan);
color: white;
background: rgba(17, 48, 72, 0.95);
outline: none;
}
button.active {
color: #08131f;
border-color: var(--gold);
background: var(--gold);
}
#sweep.active {
color: #08131f;
border-color: var(--gold);
background: var(--gold);
}
#help {
right: 18px;
bottom: 18px;
max-width: 260px;
padding: 10px 12px;
background: rgba(5, 14, 28, 0.72);
border-left: 2px solid var(--cyan);
color: #9eb8c9;
font-size: 10px;
line-height: 1.55;
}
@media (max-width: 760px) {
#readout {
top: auto;
right: 12px;
bottom: 82px;
width: 190px;
font-size: 10px;
}
#title {
top: 12px;
left: 12px;
}
#title p {
display: none;
}
#help {
display: none;
}
#controls {
bottom: 10px;
}
button {
padding: 9px 10px;
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<section id="title" class="hud">
<h1>ASTRA / VOXEL POINTING LAB</h1>
<p>
AZ–ALT MOUNT · COHERENT BORESIGHT · MAGNIFIED OPTICAL TRAIN
</p>
</section>
<section id="readout" class="hud" aria-live="polite">
<div class="row">
<span class="label">STATUS</span>
<span class="value" id="mode">LOCKED</span>
</div>
<div class="row">
<span class="label">AZIMUTH</span>
<span class="value" id="azimuth">+000.0°</span>
</div>
<div class="row">
<span class="label">ALTITUDE</span>
<span class="value" id="altitude">+00.0°</span>
</div>
<div class="row">
<span class="label">TARGET</span>
<span class="value" id="targetName">LYRA</span>
</div>
<div class="row">
<span class="label">TARGET ERROR</span>
<span class="value" id="targetError">0.00°</span>
</div>
</section>
<nav id="controls" aria-label="Telescope controls">
<button class="target active" data-target="0">1 · LYRA</button>
<button class="target" data-target="1">2 · ORION</button>
<button class="target" data-target="2">3 · POLARIS</button>
<button id="sweep">AUTO SWEEP</button>
<button id="resetCamera">RESET VIEW</button>
</nav>
<aside id="help" class="hud">
DRAG: ORBIT CAMERA<br>
WHEEL: ZOOM<br>
SHIFT / RIGHT-DRAG: PAN<br>
CLICK A STAR: SLEW TO TARGET<br>
KEYS 1–3: TARGET · SPACE: SWEEP
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene and camera
// -------------------------------------------------------------------------
const app = document.querySelector("#app");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050b16);
scene.fog = new THREE.FogExp2(0x07111f, 0.008);
const camera = new THREE.PerspectiveCamera(
46,
window.innerWidth / window.innerHeight,
0.05,
120
);
const Z_AXIS = new THREE.Vector3(0, 0, 1);
const Y_AXIS = new THREE.Vector3(0, 1, 0);
// -------------------------------------------------------------------------
// Shared materials and voxel helpers
// -------------------------------------------------------------------------
const materials = {
body: new THREE.MeshStandardMaterial({
color: 0x173451,
roughness: 0.72,
metalness: 0.22
}),
dark: new THREE.MeshStandardMaterial({
color: 0x0c1828,
roughness: 0.78,
metalness: 0.18
}),
metal: new THREE.MeshStandardMaterial({
color: 0x70879a,
roughness: 0.48,
metalness: 0.68
}),
orange: new THREE.MeshStandardMaterial({
color: 0xf3a712,
roughness: 0.5,
metalness: 0.2
}),
cyan: new THREE.MeshBasicMaterial({
color: 0x65ddff,
toneMapped: false
}),
gold: new THREE.MeshBasicMaterial({
color: 0xffd166,
toneMapped: false
}),
ray: new THREE.MeshBasicMaterial({
color: 0xffc857,
transparent: true,
opacity: 0.86,
toneMapped: false,
depthWrite: false
}),
guide: new THREE.MeshBasicMaterial({
color: 0x65ddff,
transparent: true,
opacity: 0.35,
toneMapped: false,
depthWrite: false
})
};
function addBox(
parent,
size,
position,
material,
castShadow = true,
receiveShadow = false
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(
position[0],
position[1],
position[2]
);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function addSquareFrame(
parent,
z,
outer,
bar,
depth,
material
) {
addBox(
parent,
[outer, bar, depth],
[0, (outer - bar) / 2, z],
material
);
addBox(
parent,
[outer, bar, depth],
[0, -(outer - bar) / 2, z],
material
);
addBox(
parent,
[bar, outer - 2 * bar, depth],
[-(outer - bar) / 2, 0, z],
material
);
addBox(
parent,
[bar, outer - 2 * bar, depth],
[(outer - bar) / 2, 0, z],
material
);
}
function beamBetween(parent, a, b, width, material) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const delta = end.clone().sub(start);
const length = delta.length();
if (length < 0.0001) return null;
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, width, length),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(Z_AXIS, delta.normalize());
mesh.renderOrder = 4;
parent.add(mesh);
return mesh;
}
function makeLabel(text, color = "#79ddff", width = 3, height = 0.62) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(4, 12, 25, 0.78)";
ctx.fillRect(0, 8, canvas.width, 112);
ctx.strokeStyle = color;
ctx.lineWidth = 4;
ctx.strokeRect(2, 10, canvas.width - 4, 108);
ctx.fillStyle = color;
ctx.font = "bold 40px monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 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,
fog: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 30;
return sprite;
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa9d8ff, 0x101522, 1.75));
const keyLight = new THREE.DirectionalLight(0xffe6b0, 3.1);
keyLight.position.set(-7, 12, -5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -13;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x45bbff, 20, 18, 2);
rimLight.position.set(4, 5, 4);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Voxel ground
// -------------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
metalness: 0.02
});
const tileGeometry = new THREE.BoxGeometry(1.42, 0.12, 1.42);
const tileCount = 17 * 17;
const tiles = new THREE.InstancedMesh(
tileGeometry,
floorMaterial,
tileCount
);
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let x = -8; x <= 8; x++) {
for (let z = -8; z <= 8; z++) {
dummy.position.set(x * 1.5, -0.075, z * 1.5);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const checker = (x + z) & 1;
tileColor.setHex(
checker
? 0x14283a
: 0x102131
);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
addBox(
scene,
[28, 0.22, 28],
[0, -0.23, 0],
materials.dark,
false,
true
);
// Low voxel terrain blocks near the horizon.
let seed = 9137;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0x182d3d,
roughness: 1,
metalness: 0
});
for (let i = 0; i < 42; i++) {
const angle = random() * Math.PI * 2;
const radius = 8 + random() * 5;
const height = 0.2 + random() * 1.2;
const width = 0.35 + random() * 1.1;
addBox(
scene,
[width, height, width],
[
Math.sin(angle) * radius,
height / 2 - 0.03,
Math.cos(angle) * radius
],
terrainMaterial
);
}
// -------------------------------------------------------------------------
// Mount
//
// The complete rotating assembly is parented in this order:
// yawPivot (Y rotation) -> altPivot (X rotation) -> telescope.
//
// Therefore the telescope's local +Z axis is the authoritative boresight.
// The distant direction markers and all ray geometry are derived from that
// same transformed axis.
// -------------------------------------------------------------------------
const YAW_Y = 1.18;
const ALT_OFFSET = 0.43;
const SKY_ORIGIN = new THREE.Vector3(0, YAW_Y + ALT_OFFSET, 0);
// Fixed base and column.
addBox(scene, [1.85, 0.18, 1.85], [0, 0.08, 0], materials.dark);
addBox(scene, [1.30, 0.18, 1.30], [0, 0.26, 0], materials.body);
addBox(scene, [0.52, 0.74, 0.52], [0, 0.70, 0], materials.metal);
addBox(scene, [0.72, 0.15, 0.72], [0, 1.03, 0], materials.dark);
// Azimuth compass blocks.
for (let i = 0; i < 16; i++) {
const a = (i / 16) * Math.PI * 2;
const marker = addBox(
scene,
[0.11, 0.055, i % 4 === 0 ? 0.42 : 0.25],
[
Math.sin(a) * 1.18,
0.20,
Math.cos(a) * 1.18
],
i % 4 === 0 ? materials.orange : materials.metal,
false
);
marker.rotation.y = a;
}
const cardinalNames = ["N", "E", "S", "W"];
for (let i = 0; i < 4; i++) {
const a = (i / 4) * Math.PI * 2;
const label = makeLabel(
cardinalNames[i],
i === 0 ? "#ffd166" : "#79ddff",
0.48,
0.38
);
label.position.set(
Math.sin(a) * 1.72,
0.18,
Math.cos(a) * 1.72
);
scene.add(label);
}
const yawPivot = new THREE.Group();
yawPivot.position.y = YAW_Y;
scene.add(yawPivot);
addBox(yawPivot, [0.96, 0.18, 0.96], [0, 0, 0], materials.body);
addBox(yawPivot, [0.64, 0.46, 0.56], [0, 0.25, 0], materials.dark);
addBox(yawPivot, [0.86, 0.25, 0.62], [0, ALT_OFFSET, 0], materials.body);
// Visible altitude axle.
addBox(
yawPivot,
[1.25, 0.12, 0.14],
[0, ALT_OFFSET, 0],
materials.orange
);
// Azimuth motor.
addBox(
yawPivot,
[0.27, 0.32, 0.42],
[0.69, 0.08, 0],
materials.orange
);
const altPivot = new THREE.Group();
altPivot.position.y = ALT_OFFSET;
yawPivot.add(altPivot);
// Trunnions and altitude hardware.
addBox(altPivot, [0.25, 0.28, 0.30], [-0.48, 0, 0], materials.metal);
addBox(altPivot, [0.25, 0.28, 0.30], [0.48, 0, 0], materials.metal);
addBox(altPivot, [0.25, 0.25, 0.25], [0.61, 0.12, 0], materials.orange);
// A child with no rotation is used to read the true world-space +Z axis.
const scopeForward = new THREE.Object3D();
altPivot.add(scopeForward);
// -------------------------------------------------------------------------
// Telescope: an open voxel tube so the miniature optical train is visible.
// All lens surfaces are thin boxes in local XY planes, perpendicular to +Z.
// -------------------------------------------------------------------------
const scope = new THREE.Group();
altPivot.add(scope);
const tubeLength = 2.18;
// Four rails form an open square tube.
addBox(
scope,
[0.60, 0.12, tubeLength],
[0, 0.24, 0.04],
materials.body
);
addBox(
scope,
[0.60, 0.12, tubeLength],
[0, -0.24, 0.04],
materials.body
);
addBox(
scope,
[0.12, 0.36, tubeLength],
[-0.24, 0, 0.04],
materials.body
);
addBox(
scope,
[0.12, 0.36, tubeLength],
[0.24, 0, 0.04],
materials.body
);
// Front objective assembly.
addSquareFrame(
scope,
1.18,
0.68,
0.10,
0.13,
materials.metal
);
const lensMaterial = new THREE.MeshStandardMaterial({
color: 0x67d8ff,
transparent: true,
opacity: 0.52,
roughness: 0.14,
metalness: 0.05,
depthWrite: false
});
addBox(
scope,
[0.48, 0.48, 0.028],
[0, 0, 1.18],
lensMaterial,
false
);
// Pixel-like subdivisions in the objective glass.
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
addBox(
scope,
[0.135, 0.135, 0.012],
[x * 0.15, y * 0.15, 1.198],
materials.cyan,
false
);
}
}
// Rear focuser and eyepiece.
addSquareFrame(
scope,
-1.12,
0.58,
0.09,
0.12,
materials.metal
);
addBox(
scope,
[0.38, 0.38, 0.40],
[0, 0, -1.29],
materials.dark
);
addBox(
scope,
[0.26, 0.26, 0.30],
[0, 0, -1.60],
materials.body
);
addBox(
scope,
[0.31, 0.31, 0.035],
[0, 0, -1.765],
materials.cyan,
false
);
// Finder scope.
addBox(
scope,
[0.20, 0.18, 0.76],
[0, 0.43, 0.22],
materials.dark
);
addBox(
scope,
[0.13, 0.12, 0.025],
[0, 0.43, 0.615],
materials.cyan,
false
);
// Miniature internal ray trace, parallel to and coaxial with the scope.
const smallRayPoints = [
[-0.14, -0.14],
[-0.14, 0.14],
[0.14, -0.14],
[0.14, 0.14]
];
for (const [x, y] of smallRayPoints) {
beamBetween(
scope,
[x, y, 1.17],
[0, 0, -0.79],
0.016,
materials.ray
);
beamBetween(
scope,
[0, 0, -0.79],
[-x * 0.55, -y * 0.55, -1.34],
0.016,
materials.ray
);
}
// Exact optical-axis centerline.
addBox(
scope,
[0.018, 0.018, 2.58],
[0, 0, -0.12],
materials.cyan,
false
);
// -------------------------------------------------------------------------
// Magnified optical path
//
// This inset is a child of the exact same altPivot. Its local +Z axis is
// therefore always parallel to the telescope's physical lens axis.
// -------------------------------------------------------------------------
const inset = new THREE.Group();
inset.position.set(2.55, 0.12, 0);
altPivot.add(inset);
// Magnified objective.
addSquareFrame(
inset,
1.18,
1.22,
0.11,
0.14,
materials.metal
);
addBox(
inset,
[0.98, 0.98, 0.035],
[0, 0, 1.18],
lensMaterial,
false
);
// Magnified eyepiece.
addSquareFrame(
inset,
-1.39,
0.48,
0.08,
0.14,
materials.metal
);
addBox(
inset,
[0.29, 0.29, 0.18],
[0, 0, -1.39],
materials.dark
);
// Incoming parallel rays, refracted rays, focal crossing and diverging rays.
const largeRayPoints = [
[-0.35, -0.35],
[-0.35, 0.35],
[0, -0.43],
[0, 0.43],
[0.35, -0.35],
[0.35, 0.35]
];
for (const [x, y] of largeRayPoints) {
// Travel direction is local -Z, from the sky into the objective.
beamBetween(
inset,
[x, y, 2.08],
[x, y, 1.18],
0.024,
materials.ray
);
beamBetween(
inset,
[x, y, 1.18],
[0, 0, -0.78],
0.026,
materials.ray
);
beamBetween(
inset,
[0, 0, -0.78],
[-x * 0.54, -y * 0.54, -1.38],
0.024,
materials.ray
);
}
// Enlarged optical-axis centerline.
addBox(
inset,
[0.024, 0.024, 3.78],
[0, 0, 0.25],
materials.cyan,
false
);
// Dashed/graphic correspondences between the real lens and magnified lens.
const connectorCorners = [
[-0.24, -0.24, 1.18],
[-0.24, 0.24, 1.18],
[0.24, -0.24, 1.18],
[0.24, 0.24, 1.18]
];
for (const [x, y, z] of connectorCorners) {
beamBetween(
altPivot,
[x, y, z],
[2.55 + Math.sign(x) * 0.48, Math.sign(y) * 0.48, z],
0.014,
materials.guide
);
}
const insetLabel = makeLabel(
"3× OPTICAL PATH",
"#ffd166",
2.55,
0.48
);
insetLabel.position.set(0, 0.98, 1.10);
inset.add(insetLabel);
const axisLabel = makeLabel(
"COAXIAL LENS AXIS",
"#79ddff",
2.35,
0.40
);
axisLabel.position.set(0, -0.92, 1.08);
inset.add(axisLabel);
// -------------------------------------------------------------------------
// Targets and direction markers
// -------------------------------------------------------------------------
const targets = [
{
name: "LYRA",
azimuth: 0,
altitude: 28,
color: 0xffd166
},
{
name: "ORION",
azimuth: -26,
altitude: 17,
color: 0x79ddff
},
{
name: "POLARIS",
azimuth: 15,
altitude: 52,
color: 0xc5a8ff
}
];
function directionFromAngles(azimuth, altitude) {
return new THREE.Vector3(
Math.sin(azimuth) * Math.cos(altitude),
Math.sin(altitude),
Math.cos(azimuth) * Math.cos(altitude)
).normalize();
}
function signedDegrees(radians) {
let degrees = radians * 180 / Math.PI;
degrees = ((degrees + 540) % 360) - 180;
return degrees;
}
const markerHitMeshes = [];
const fixedMarkers = [];
const fixedGuides = [];
function createVoxelMarker(color, index = null, labelText = null) {
const group = new THREE.Group();
const starMaterial = new THREE.MeshBasicMaterial({
color,
toneMapped: false
});
const frameMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.65,
toneMapped: false
});
const starMeshes = [];
starMeshes.push(
addBox(group, [0.28, 0.28, 0.13], [0, 0, 0], starMaterial, false)
);
starMeshes.push(
addBox(group, [0.25, 0.10, 0.10], [-0.19, 0, 0], starMaterial, false)
);
starMeshes.push(
addBox(group, [0.25, 0.10, 0.10], [0.19, 0, 0], starMaterial, false)
);
starMeshes.push(
addBox(group, [0.10, 0.25, 0.10], [0, 0.19, 0], starMaterial, false)
);
starMeshes.push(
addBox(group, [0.10, 0.25, 0.10], [0, -0.19, 0], starMaterial, false)
);
// Square targeting bracket.
const bracketSize = 0.78;
const bracketOffset = bracketSize / 2;
starMeshes.push(
addBox(
group,
[bracketSize, 0.045, 0.055],
[0, bracketOffset, 0],
frameMaterial,
false
)
);
starMeshes.push(
addBox(
group,
[bracketSize, 0.045, 0.055],
[0, -bracketOffset, 0],
frameMaterial,
false
)
);
starMeshes.push(
addBox(
group,
[0.045, bracketSize, 0.055],
[bracketOffset, 0, 0],
frameMaterial,
false
)
);
starMeshes.push(
addBox(
group,
[0.045, bracketSize, 0.055],
[-bracketOffset, 0, 0],
frameMaterial,
false
)
);
for (const mesh of starMeshes) {
if (index !== null) {
mesh.userData.targetIndex = index;
markerHitMeshes.push(mesh);
}
}
let label = null;
if (labelText) {
label = makeLabel(labelText, "#bdeeff", 3.35, 0.62);
label.position.set(0, 0.73, 0);
group.add(label);
}
return {
group,
starMaterial,
frameMaterial,
label
};
}
targets.forEach((target, index) => {
const azimuth = THREE.MathUtils.degToRad(target.azimuth);
const altitude = THREE.MathUtils.degToRad(target.altitude);
const direction = directionFromAngles(azimuth, altitude);
const position = SKY_ORIGIN.clone().addScaledVector(direction, 26);
const marker = createVoxelMarker(
target.color,
index,
`${target.name} ${signedDegrees(azimuth) >= 0 ? "+" : ""}` +
`${signedDegrees(azimuth).toFixed(0)}° / ` +
`${target.altitude.toFixed(0)}°`
);
marker.group.position.copy(position);
scene.add(marker.group);
fixedMarkers.push(marker);
// Dim voxel dots show each fixed celestial direction.
const guideMaterial = new THREE.MeshBasicMaterial({
color: target.color,
transparent: true,
opacity: 0.24,
toneMapped: false,
depthWrite: false
});
const guideDots = [];
for (let i = 0; i < 13; i++) {
const distance = 2 + i * 1.85;
const dot = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 0.045, 0.12),
guideMaterial
);
dot.position.copy(SKY_ORIGIN).addScaledVector(direction, distance);
dot.quaternion.setFromUnitVectors(Z_AXIS, direction);
scene.add(dot);
guideDots.push(dot);
}
fixedGuides.push({
material: guideMaterial,
dots: guideDots
});
});
// A separate boresight marker follows the actual telescope axis at all times.
const boresight = createVoxelMarker(
0xffd166,
null,
"BORESIGHT"
);
scene.add(boresight.group);
// -------------------------------------------------------------------------
// Dynamic pointing geometry
// -------------------------------------------------------------------------
const aimMaterial = new THREE.MeshBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.76,
toneMapped: false,
depthWrite: false
});
const aimDots = [];
for (let i = 0; i < 34; i++) {
const dot = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 0.045, 0.14),
aimMaterial
);
dot.renderOrder = 5;
scene.add(dot);
aimDots.push(dot);
}
const projectionMaterial = new THREE.MeshBasicMaterial({
color: 0x48c9ef,
transparent: true,
opacity: 0.52,
toneMapped: false,
depthWrite: false
});
const projectionDots = [];
for (let i = 0; i < 13; i++) {
const dot = new THREE.Mesh(
new THREE.BoxGeometry(0.065, 0.035, 0.13),
projectionMaterial
);
scene.add(dot);
projectionDots.push(dot);
}
const altitudeArcDots = [];
for (let i = 0; i < 15; i++) {
const dot = new THREE.Mesh(
new THREE.BoxGeometry(0.06, 0.06, 0.06),
projectionMaterial
);
scene.add(dot);
altitudeArcDots.push(dot);
}
// -------------------------------------------------------------------------
// Background voxel stars
// -------------------------------------------------------------------------
const backgroundStarCount = 150;
const backgroundStars = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.065, 0.065, 0.065),
new THREE.MeshBasicMaterial({
color: 0xb9ddff,
transparent: true,
opacity: 0.68,
toneMapped: false
}),
backgroundStarCount
);
for (let i = 0; i < backgroundStarCount; i++) {
const azimuth = random() * Math.PI * 2;
const altitude = 0.08 + random() * 1.08;
const direction = directionFromAngles(azimuth, altitude);
const distance = 34 + random() * 8;
const size = 0.6 + random() * 1.5;
dummy.position.copy(SKY_ORIGIN).addScaledVector(direction, distance);
dummy.scale.setScalar(size);
dummy.rotation.set(0, azimuth, 0);
dummy.updateMatrix();
backgroundStars.setMatrixAt(i, dummy.matrix);
}
backgroundStars.instanceMatrix.needsUpdate = true;
scene.add(backgroundStars);
// -------------------------------------------------------------------------
// Pointing state and controls
// -------------------------------------------------------------------------
let selectedTarget = 0;
let sweeping = false;
let yaw = THREE.MathUtils.degToRad(targets[0].azimuth);
let altitude = THREE.MathUtils.degToRad(targets[0].altitude);
let goalYaw = yaw;
let goalAltitude = altitude;
const modeElement = document.querySelector("#mode");
const azimuthElement = document.querySelector("#azimuth");
const altitudeElement = document.querySelector("#altitude");
const targetNameElement = document.querySelector("#targetName");
const targetErrorElement = document.querySelector("#targetError");
const targetButtons = [
...document.querySelectorAll(".target")
];
const sweepButton = document.querySelector("#sweep");
function updateTargetButtons() {
targetButtons.forEach((button, index) => {
button.classList.toggle("active", index === selectedTarget);
});
sweepButton.classList.toggle("active", sweeping);
sweepButton.textContent = sweeping ? "STOP SWEEP" : "AUTO SWEEP";
}
function selectTarget(index) {
selectedTarget = index;
sweeping = false;
const target = targets[index];
goalYaw = THREE.MathUtils.degToRad(target.azimuth);
goalAltitude = THREE.MathUtils.degToRad(target.altitude);
targetNameElement.textContent = target.name;
updateTargetButtons();
}
targetButtons.forEach((button) => {
button.addEventListener("click", () => {
selectTarget(Number(button.dataset.target));
});
});
sweepButton.addEventListener("click", () => {
sweeping = !sweeping;
updateTargetButtons();
});
// -------------------------------------------------------------------------
// Custom camera controls — no external controls addon
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 2.15, 0);
const defaultCameraTarget = cameraTarget.clone();
let cameraRadius = 16.5;
let cameraTheta = 2.35;
let cameraPhi = 1.03;
function updateCamera() {
const horizontal = Math.sin(cameraPhi) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraTheta),
cameraTarget.y + Math.cos(cameraPhi) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraTarget.copy(defaultCameraTarget);
cameraRadius = 16.5;
cameraTheta = 2.35;
cameraPhi = 1.03;
updateCamera();
}
document.querySelector("#resetCamera").addEventListener(
"click",
resetCamera
);
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let pointerDown = false;
let pointerButton = 0;
let pointerMoved = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerButton = event.button;
pointerMoved = false;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
if (Math.abs(dx) + Math.abs(dy) > 4) {
pointerMoved = true;
}
pointerX = event.clientX;
pointerY = event.clientY;
if (pointerButton === 2 || event.shiftKey) {
camera.updateMatrixWorld();
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const scale = cameraRadius * 0.0015;
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(up, dy * scale);
cameraTarget.y = THREE.MathUtils.clamp(
cameraTarget.y,
0.5,
7
);
} else {
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.24,
1.48
);
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
if (
pointerButton === 0 &&
!pointerMoved &&
event.target === renderer.domElement
) {
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 hit = raycaster.intersectObjects(
markerHitMeshes,
false
)[0];
if (hit && Number.isInteger(hit.object.userData.targetIndex)) {
selectTarget(hit.object.userData.targetIndex);
}
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
7.5,
31
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Digit1") selectTarget(0);
if (event.code === "Digit2") selectTarget(1);
if (event.code === "Digit3") selectTarget(2);
if (event.code === "Space") {
event.preventDefault();
sweeping = !sweeping;
updateTargetButtons();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const worldForward = new THREE.Vector3();
const worldPivot = new THREE.Vector3();
const horizontalForward = new THREE.Vector3();
function shortestAngleDifference(from, to) {
return Math.atan2(
Math.sin(to - from),
Math.cos(to - from)
);
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
if (sweeping) {
yaw = -0.10 + Math.sin(time * 0.31) * 0.62;
altitude =
0.38 + Math.sin(time * 0.21 + 0.8) * 0.22;
} else {
const response = 1 - Math.exp(-delta * 3.2);
yaw += shortestAngleDifference(yaw, goalYaw) * response;
altitude += (goalAltitude - altitude) * response;
if (
Math.abs(shortestAngleDifference(yaw, goalYaw)) < 0.0001 &&
Math.abs(goalAltitude - altitude) < 0.0001
) {
yaw = goalYaw;
altitude = goalAltitude;
}
}
// The actual telescope transform is the single source of truth.
yawPivot.rotation.y = yaw;
altPivot.rotation.x = -altitude;
scene.updateMatrixWorld(true);
// Read the true transformed local +Z axis.
scopeForward.getWorldDirection(worldForward);
altPivot.getWorldPosition(worldPivot);
horizontalForward.set(
worldForward.x,
0,
worldForward.z
);
if (horizontalForward.lengthSq() < 0.00001) {
horizontalForward.set(0, 0, 1);
} else {
horizontalForward.normalize();
}
const actualYaw = Math.atan2(
worldForward.x,
worldForward.z
);
const actualAltitude = Math.asin(
THREE.MathUtils.clamp(worldForward.y, -1, 1)
);
// Dynamic voxel boresight ray.
aimDots.forEach((dot, index) => {
const distance = 1.48 + index * 0.62;
dot.position
.copy(worldPivot)
.addScaledVector(worldForward, distance);
dot.quaternion.setFromUnitVectors(
Z_AXIS,
worldForward
);
});
// Boresight target bracket, always on the same axis as the scope.
boresight.group.position
.copy(worldPivot)
.addScaledVector(worldForward, 22.7);
boresight.group.quaternion.copy(camera.quaternion);
// Ground-plane projection of the current pointing direction.
projectionDots.forEach((dot, index) => {
const distance = 1.55 + index * 0.34;
dot.position.set(
worldPivot.x + horizontalForward.x * distance,
0.025,
worldPivot.z + horizontalForward.z * distance
);
dot.quaternion.setFromUnitVectors(
Z_AXIS,
horizontalForward
);
});
// Vertical altitude arc in the current pointing plane.
altitudeArcDots.forEach((dot, index) => {
const fraction = index / (altitudeArcDots.length - 1);
const angle = actualAltitude * fraction;
const radius = 2.25;
dot.position.copy(worldPivot)
.addScaledVector(
horizontalForward,
Math.cos(angle) * radius
);
dot.position.y += Math.sin(angle) * radius;
});
// Keep the fixed marker brackets facing the camera.
fixedMarkers.forEach((marker) => {
marker.group.quaternion.copy(camera.quaternion);
});
// Highlight the selected fixed star.
fixedMarkers.forEach((marker, index) => {
const selected = index === selectedTarget;
const color = targets[index].color;
marker.starMaterial.color.setHex(
selected ? 0xffd166 : color
);
marker.frameMaterial.color.setHex(
selected ? 0xffd166 : color
);
marker.frameMaterial.opacity = selected ? 0.95 : 0.48;
fixedGuides[index].material.opacity = selected ? 0.50 : 0.20;
});
// Readout and lock state.
const selectedDirection = directionFromAngles(
goalYaw,
goalAltitude
);
const targetError = Math.acos(
THREE.MathUtils.clamp(
worldForward.dot(selectedDirection),
-1,
1
)
);
const mode = sweeping
? "AUTO SWEEP"
: targetError < 0.15 * Math.PI / 180
? "LOCKED"
: "SLEWING";
modeElement.textContent = mode;
azimuthElement.textContent =
`${signedDegrees(actualYaw) >= 0 ? "+" : ""}` +
`${signedDegrees(actualYaw).toFixed(1)}°`;
altitudeElement.textContent =
`${actualAltitude >= 0 ? "+" : ""}` +
`${THREE.MathUtils.radToDeg(actualAltitude).toFixed(1)}°`;
targetErrorElement.textContent =
`${THREE.MathUtils.radToDeg(targetError).toFixed(2)}°`;
// Gentle voxel-light pulse.
materials.ray.opacity = 0.76 + Math.sin(time * 3.0) * 0.12;
aimMaterial.opacity = 0.65 + Math.sin(time * 3.6) * 0.16;
updateCamera();
renderer.render(scene, camera);
}
updateTargetButtons();
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```