Prompt specification
Build or customize the Three.js 3D scene "Rotate to Point an Antenna" 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: Rotate to Point an Antenna
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/rotate-to-point-an-antenna-x3822
Scene Description Prompt:
A stationary voxel spacecraft rotates smoothly to aim its antenna at a fixed target. Use quaternion interpolation. Three.js from cdnjs, one HTML file.
Working Three.js Starter (rotate-to-point-an-antenna-x3822):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Spacecraft — Quaternion Antenna Tracking</title>
<style>
:root {
color-scheme: dark;
--cyan: #65e8ff;
--cyan-dim: rgba(101, 232, 255, 0.45);
--orange: #ffad5b;
--panel: rgba(5, 12, 25, 0.72);
--border: rgba(116, 216, 255, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #020611;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#panel {
position: fixed;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 19px 20px 17px;
color: #eaf9ff;
background:
linear-gradient(135deg, rgba(14, 31, 53, 0.9), var(--panel));
border: 1px solid var(--border);
border-radius: 14px;
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.38),
inset 0 1px 0 rgba(255, 255, 255, 0.05);
backdrop-filter: blur(16px);
pointer-events: none;
user-select: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 13px var(--cyan);
}
h1 {
margin: 0 0 5px;
font-size: 20px;
line-height: 1.15;
letter-spacing: -0.025em;
}
.subtitle {
margin: 0 0 17px;
color: #93aabd;
font-size: 12px;
line-height: 1.5;
}
.status {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
padding: 10px 11px;
background: rgba(0, 0, 0, 0.2);
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 9px;
}
.status-label {
color: #8ea6ba;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.15em;
}
#statusText {
color: var(--orange);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
}
#statusText.locked {
color: var(--cyan);
text-shadow: 0 0 12px rgba(101, 232, 255, 0.45);
}
.telemetry {
display: grid;
gap: 8px;
}
.telemetry-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 18px;
font-size: 11px;
}
.telemetry-row span {
color: #7890a5;
letter-spacing: 0.06em;
}
.telemetry-row strong {
color: #e8f8ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-weight: 600;
}
.buttons {
display: flex;
gap: 8px;
margin-top: 16px;
pointer-events: auto;
}
button {
flex: 1;
padding: 9px 10px;
color: #bfefff;
background: rgba(37, 102, 133, 0.16);
border: 1px solid rgba(101, 232, 255, 0.22);
border-radius: 8px;
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease;
}
button:hover {
background: rgba(54, 153, 191, 0.24);
border-color: rgba(101, 232, 255, 0.45);
}
button:active {
transform: translateY(1px);
}
#hint {
position: fixed;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
padding: 8px 13px;
color: rgba(203, 231, 244, 0.67);
background: rgba(3, 8, 17, 0.55);
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
font-size: 10px;
letter-spacing: 0.08em;
white-space: nowrap;
backdrop-filter: blur(10px);
pointer-events: none;
user-select: none;
}
#targetLabel {
position: fixed;
z-index: 3;
padding: 6px 9px;
color: #c7f8ff;
background: rgba(2, 12, 24, 0.75);
border: 1px solid rgba(101, 232, 255, 0.35);
border-radius: 6px;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
white-space: nowrap;
transform: translate(15px, -50%);
pointer-events: none;
user-select: none;
}
#targetLabel::before {
content: "";
display: inline-block;
width: 5px;
height: 5px;
margin-right: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 8px var(--cyan);
vertical-align: 1px;
}
@media (max-width: 620px) {
#panel {
top: 12px;
left: 12px;
width: min(310px, calc(100vw - 24px));
padding: 15px;
}
.subtitle {
margin-bottom: 12px;
}
#hint {
bottom: 12px;
font-size: 9px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D voxel spacecraft"></canvas>
<section id="panel">
<div class="eyebrow">Attitude Control / Live</div>
<h1>VX-9 Relay Probe</h1>
<p class="subtitle">
Station-keeping enabled. Quaternion-slerped bus rotation with an
independently stabilized antenna gimbal.
</p>
<div class="status">
<span class="status-label">TARGET LOCK</span>
<strong id="statusText">ACQUIRING</strong>
</div>
<div class="telemetry">
<div class="telemetry-row">
<span>POINTING ERROR</span>
<strong id="errorValue">0.00°</strong>
</div>
<div class="telemetry-row">
<span>TARGET RANGE</span>
<strong id="rangeValue">0.0 m</strong>
</div>
<div class="telemetry-row">
<span>BUS YAW</span>
<strong id="yawValue">0.0°</strong>
</div>
<div class="telemetry-row">
<span>TARGET</span>
<strong>T-07 / FIXED</strong>
</div>
</div>
<div class="buttons">
<button id="resetView" type="button">RESET VIEW</button>
<button id="pauseButton" type="button">PAUSE</button>
</div>
</section>
<div id="targetLabel">FIXED TARGET / T-07</div>
<div id="hint">DRAG TO ORBIT · WHEEL TO ZOOM · R RESET · SPACE PAUSE</div>
<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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x020611);
scene.fog = new THREE.FogExp2(0x020611, 0.009);
const camera = new THREE.PerspectiveCamera(
48,
window.innerWidth / window.innerHeight,
0.1,
180
);
// -------------------------------------------------------------------------
// Custom orbit controls. No addons are used.
// -------------------------------------------------------------------------
const orbit = {
theta: 0.82,
phi: 1.02,
radius: 16,
targetTheta: 0.82,
targetPhi: 1.02,
targetRadius: 16,
dragging: false,
pointerX: 0,
pointerY: 0
};
const DEFAULT_ORBIT = {
theta: 0.82,
phi: 1.02,
radius: 16
};
function updateCamera(dt) {
const damping = 1 - Math.exp(-9 * dt);
orbit.theta += (orbit.targetTheta - orbit.theta) * damping;
orbit.phi += (orbit.targetPhi - orbit.phi) * damping;
orbit.radius += (orbit.targetRadius - orbit.radius) * damping;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.radius * Math.cos(orbit.phi),
orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(0, 0, 0);
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
orbit.dragging = true;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.pointerX;
const dy = event.clientY - orbit.pointerY;
orbit.pointerX = event.clientX;
orbit.pointerY = event.clientY;
orbit.targetTheta -= dx * 0.006;
orbit.targetPhi = THREE.MathUtils.clamp(
orbit.targetPhi - dy * 0.006,
0.2,
Math.PI - 0.2
);
});
function endDrag(event) {
orbit.dragging = false;
if (
event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius + event.deltaY * 0.012,
7,
31
);
},
{ passive: false }
);
function resetView() {
orbit.targetTheta = DEFAULT_ORBIT.theta;
orbit.targetPhi = DEFAULT_ORBIT.phi;
orbit.targetRadius = DEFAULT_ORBIT.radius;
}
// -------------------------------------------------------------------------
// Lighting and environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x7fcfff, 0x10111d, 1.35));
const keyLight = new THREE.DirectionalLight(0xb9eaff, 3.1);
keyLight.position.set(7, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xff8a55, 1.6);
rimLight.position.set(-8, 2, -7);
scene.add(rimLight);
const grid = new THREE.GridHelper(38, 38, 0x17415b, 0x102438);
grid.position.y = -3.1;
grid.material.transparent = true;
grid.material.opacity = 0.22;
grid.material.depthWrite = false;
scene.add(grid);
// Deterministic star field.
let randomSeed = 71423;
function seededRandom() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const starPositions = [];
const starColors = [];
for (let i = 0; i < 1800; i++) {
const radius = 28 + seededRandom() * 80;
const theta = seededRandom() * Math.PI * 2;
const phi = Math.acos(2 * seededRandom() - 1);
starPositions.push(
radius * Math.sin(phi) * Math.cos(theta),
radius * Math.cos(phi),
radius * Math.sin(phi) * Math.sin(theta)
);
const brightness = 0.35 + seededRandom() * 0.65;
starColors.push(
brightness * 0.72,
brightness * 0.88,
brightness
);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(starPositions, 3)
);
starGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(starColors, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
size: 0.075,
vertexColors: true,
transparent: true,
opacity: 0.92,
depthWrite: false,
fog: false
})
);
scene.add(stars);
// -------------------------------------------------------------------------
// Voxel spacecraft
// -------------------------------------------------------------------------
const ship = new THREE.Group();
ship.position.set(0, 0, 0);
scene.add(ship);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const materials = {
hull: new THREE.MeshStandardMaterial({
color: 0x8293a4,
metalness: 0.55,
roughness: 0.67
}),
hullLight: new THREE.MeshStandardMaterial({
color: 0xb4c2cc,
metalness: 0.48,
roughness: 0.55
}),
hullDark: new THREE.MeshStandardMaterial({
color: 0x263441,
metalness: 0.72,
roughness: 0.62
}),
panel: new THREE.MeshStandardMaterial({
color: 0xd27638,
metalness: 0.38,
roughness: 0.58
}),
glass: new THREE.MeshStandardMaterial({
color: 0x1cc8ef,
emissive: 0x087995,
emissiveIntensity: 1.5,
metalness: 0.25,
roughness: 0.25
}),
engine: new THREE.MeshStandardMaterial({
color: 0x1d2731,
metalness: 0.85,
roughness: 0.35
}),
dish: new THREE.MeshStandardMaterial({
color: 0x9eabb4,
metalness: 0.68,
roughness: 0.47
}),
dishDark: new THREE.MeshStandardMaterial({
color: 0x43515d,
metalness: 0.72,
roughness: 0.48
}),
feed: new THREE.MeshStandardMaterial({
color: 0xffae58,
emissive: 0x8b3510,
emissiveIntensity: 0.8,
metalness: 0.55,
roughness: 0.35
}),
flame: new THREE.MeshBasicMaterial({
color: 0x65dfff,
transparent: true,
opacity: 0.82,
blending: THREE.AdditiveBlending,
depthWrite: false
})
};
function addBlock(
parent,
position,
scale,
material,
options = {}
) {
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.position.set(...position);
mesh.scale.set(...scale);
if (options.rotationY) {
mesh.rotation.y = options.rotationY;
}
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Central voxel hull.
for (let zIndex = 0; zIndex < 7; zIndex++) {
const z = -1.2 + zIndex * 0.4;
const halfWidth = zIndex > 4 ? 1 : zIndex < 1 ? 2 : 1;
for (let xIndex = -halfWidth; xIndex <= halfWidth; xIndex++) {
const x = xIndex * 0.38;
for (let yIndex = -1; yIndex <= 1; yIndex++) {
const y = yIndex * 0.32;
const isTop = yIndex === 1;
const isNose = zIndex > 4 && Math.abs(xIndex) === 1;
const material =
isTop && xIndex % 2 === 0
? materials.hullLight
: isNose
? materials.panel
: materials.hull;
addBlock(
ship,
[x, y, z],
[0.36, 0.29, 0.36],
material
);
}
}
}
// Lower keel and structural blocks.
addBlock(ship, [0, -0.48, -0.15], [1.45, 0.28, 2.45], materials.hullDark);
addBlock(ship, [0, -0.58, 1.15], [0.72, 0.22, 0.95], materials.panel);
addBlock(ship, [0, -0.58, -1.25], [0.95, 0.25, 0.55], materials.hullDark);
// Voxel wings.
for (const side of [-1, 1]) {
addBlock(ship, [side * 1.55, -0.12, 0.05], [1.25, 0.22, 1.2], materials.hullDark);
addBlock(ship, [side * 2.25, -0.08, -0.05], [0.62, 0.18, 1.05], materials.panel);
addBlock(ship, [side * 2.68, -0.03, -0.05], [0.28, 0.15, 0.72], materials.hullLight);
addBlock(ship, [side * 1.68, 0.02, 0.72], [0.72, 0.16, 0.28], materials.hullLight);
// Engine pod.
addBlock(ship, [side * 1.28, -0.08, -1.45], [0.55, 0.52, 1.05], materials.engine);
addBlock(ship, [side * 1.28, -0.08, -2.02], [0.42, 0.4, 0.18], materials.hullDark);
// Voxel thruster flame.
addBlock(
ship,
[side * 1.28, -0.08, -2.32],
[0.23, 0.23, 0.62],
materials.flame,
{ castShadow: false }
);
// Navigation light.
addBlock(
ship,
[side * 2.52, 0.06, 0.38],
[0.12, 0.12, 0.12],
side < 0 ? materials.flame : materials.feed,
{ castShadow: false }
);
}
// Forward nose and cockpit.
addBlock(ship, [0, 0.02, 1.58], [0.72, 0.3, 0.32], materials.panel);
addBlock(ship, [0, 0.28, 1.35], [0.72, 0.24, 0.56], materials.glass);
addBlock(ship, [0, 0.46, 0.68], [0.82, 0.18, 0.7], materials.hullLight);
// Antenna mast and mount.
addBlock(ship, [0, 0.94, 0.38], [0.22, 0.85, 0.22], materials.hullDark);
addBlock(ship, [0, 1.35, 0.38], [0.64, 0.22, 0.64], materials.panel);
const dishMount = new THREE.Group();
dishMount.position.set(0, 1.62, 0.55);
ship.add(dishMount);
addBlock(dishMount, [0, -0.12, 0], [0.48, 0.25, 0.48], materials.hullDark);
const dishRotator = new THREE.Group();
dishMount.add(dishRotator);
// A parabolic dish made from individual voxel blocks.
// Local +Z is the antenna boresight.
const dishRadius = 1.02;
const cell = 0.19;
for (let ix = -5; ix <= 5; ix++) {
for (let iy = -5; iy <= 5; iy++) {
const x = ix * cell;
const y = iy * cell;
const radius = Math.hypot(x, y);
if (radius > dishRadius) continue;
const normalizedRadius = radius / dishRadius;
const z = -0.62 * (1 - normalizedRadius * normalizedRadius);
const material =
radius > dishRadius - cell * 1.15
? materials.dishDark
: (ix + iy) % 2 === 0
? materials.dish
: materials.dishDark;
addBlock(
dishRotator,
[x, y, z],
[0.175, 0.175, 0.17],
material,
{ castShadow: false }
);
}
}
// Feed horn and support along the boresight.
addBlock(
dishRotator,
[0, 0, 0.84],
[0.055, 0.055, 1.35],
materials.hullDark,
{ castShadow: false }
);
addBlock(
dishRotator,
[0, 0, 1.51],
[0.26, 0.26, 0.27],
materials.feed,
{ castShadow: false }
);
addBlock(
dishRotator,
[0, 0, 1.69],
[0.12, 0.12, 0.1],
materials.glass,
{ castShadow: false }
);
// Faint attitude reference ring.
const referenceRing = new THREE.Mesh(
new THREE.TorusGeometry(3.75, 0.012, 4, 160),
new THREE.MeshBasicMaterial({
color: 0x3e9dbb,
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
referenceRing.rotation.x = Math.PI / 2;
referenceRing.position.y = -0.18;
scene.add(referenceRing);
// -------------------------------------------------------------------------
// Fixed target
// -------------------------------------------------------------------------
const targetPosition = new THREE.Vector3(-6.5, 3.25, 7.8);
const targetGroup = new THREE.Group();
targetGroup.position.copy(targetPosition);
scene.add(targetGroup);
const targetMaterial = new THREE.MeshStandardMaterial({
color: 0x8cecff,
emissive: 0x1599b8,
emissiveIntensity: 2.2,
metalness: 0.45,
roughness: 0.3
});
addBlock(targetGroup, [0, 0, 0], [0.72, 0.72, 0.72], targetMaterial);
addBlock(targetGroup, [0.55, 0.1, 0], [0.22, 0.32, 0.25], materials.hullLight);
addBlock(targetGroup, [-0.48, -0.12, 0.05], [0.22, 0.25, 0.32], materials.panel);
addBlock(targetGroup, [0.06, 0.52, -0.08], [0.24, 0.22, 0.25], materials.glass);
const targetEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(2.1, 2.1, 2.1)),
new THREE.LineBasicMaterial({
color: 0x65e8ff,
transparent: true,
opacity: 0.32
})
);
targetGroup.add(targetEdges);
const targetRings = [];
for (let i = 0; i < 2; i++) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.45 + i * 0.33, 0.018, 6, 96),
new THREE.MeshBasicMaterial({
color: 0x65e8ff,
transparent: true,
opacity: 0.42 - i * 0.14,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
targetRings.push(ring);
targetGroup.add(ring);
}
const targetLight = new THREE.PointLight(0x49dfff, 28, 9, 2);
targetGroup.add(targetLight);
// -------------------------------------------------------------------------
// Aiming beam
// -------------------------------------------------------------------------
const beamGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
targetPosition.clone()
]);
const beam = new THREE.Line(
beamGeometry,
new THREE.LineDashedMaterial({
color: 0x65e8ff,
transparent: true,
opacity: 0.45,
dashSize: 0.22,
gapSize: 0.15,
depthWrite: false
})
);
beam.computeLineDistances();
scene.add(beam);
// -------------------------------------------------------------------------
// Quaternion tracking
// -------------------------------------------------------------------------
const bodyTargetQuaternion = new THREE.Quaternion();
const desiredBodyEuler = new THREE.Euler(0, 0, 0, "YXZ");
const pivotWorld = new THREE.Vector3();
const targetDirection = new THREE.Vector3();
const desiredWorldQuaternion = new THREE.Quaternion();
const desiredLocalQuaternion = new THREE.Quaternion();
const inverseBodyQuaternion = new THREE.Quaternion();
const actualAntennaQuaternion = new THREE.Quaternion();
const actualDirection = new THREE.Vector3();
const feedWorld = new THREE.Vector3();
const lookMatrix = new THREE.Matrix4();
const basisRight = new THREE.Vector3();
const basisUp = new THREE.Vector3();
const basisForward = new THREE.Vector3();
/**
* Builds a quaternion whose local +Z axis points along `forward`.
* A stable up vector prevents arbitrary roll around the boresight.
*/
function quaternionLookingAlong(forward, upHint = new THREE.Vector3(0, 1, 0)) {
basisForward.copy(forward).normalize();
basisRight.crossVectors(upHint, basisForward);
if (basisRight.lengthSq() < 0.000001) {
basisRight.crossVectors(new THREE.Vector3(1, 0, 0), basisForward);
}
basisRight.normalize();
basisUp.crossVectors(basisForward, basisRight).normalize();
lookMatrix.makeBasis(basisRight, basisUp, basisForward);
return new THREE.Quaternion().setFromRotationMatrix(lookMatrix);
}
let elapsed = 0;
let paused = false;
function updateSpacecraft(dt) {
// The spacecraft translates nowhere, but its body slowly rotates.
desiredBodyEuler.set(
Math.sin(elapsed * 0.43) * 0.055,
elapsed * 0.16,
Math.sin(elapsed * 0.31) * 0.045,
"YXZ"
);
bodyTargetQuaternion.setFromEuler(desiredBodyEuler);
// Quaternion interpolation for the spacecraft attitude.
const bodyBlend = 1 - Math.exp(-2.8 * dt);
ship.quaternion.slerp(bodyTargetQuaternion, bodyBlend);
ship.updateMatrixWorld(true);
dishMount.getWorldPosition(pivotWorld);
targetDirection
.copy(targetPosition)
.sub(pivotWorld)
.normalize();
// Desired antenna orientation in world space.
desiredWorldQuaternion.copy(
quaternionLookingAlong(
targetDirection,
new THREE.Vector3(0, 1, 0)
)
);
// Convert the world-space orientation into the rotating spacecraft's
// local frame. The dish is a child of the spacecraft.
inverseBodyQuaternion.copy(ship.quaternion).invert();
desiredLocalQuaternion
.copy(inverseBodyQuaternion)
.multiply(desiredWorldQuaternion);
// Quaternion interpolation for the antenna gimbal.
const antennaBlend = 1 - Math.exp(-6.5 * dt);
dishRotator.quaternion.slerp(
desiredLocalQuaternion,
antennaBlend
);
ship.updateMatrixWorld(true);
// Pointing error.
dishRotator.getWorldQuaternion(actualAntennaQuaternion);
actualDirection
.set(0, 0, 1)
.applyQuaternion(actualAntennaQuaternion)
.normalize();
const errorRadians = actualDirection.angleTo(targetDirection);
const errorDegrees = THREE.MathUtils.radToDeg(errorRadians);
// Update the dashed boresight beam.
dishRotator.localToWorld(feedWorld.set(0, 0, 1.72));
const beamPositions = beam.geometry.attributes.position;
beamPositions.setXYZ(0, feedWorld.x, feedWorld.y, feedWorld.z);
beamPositions.setXYZ(
1,
targetPosition.x,
targetPosition.y,
targetPosition.z
);
beamPositions.needsUpdate = true;
beam.computeLineDistances();
return {
errorDegrees,
range: pivotWorld.distanceTo(targetPosition)
};
}
// -------------------------------------------------------------------------
// UI and input
// -------------------------------------------------------------------------
const statusText = document.querySelector("#statusText");
const errorValue = document.querySelector("#errorValue");
const rangeValue = document.querySelector("#rangeValue");
const yawValue = document.querySelector("#yawValue");
const pauseButton = document.querySelector("#pauseButton");
const targetLabel = document.querySelector("#targetLabel");
const shipEuler = new THREE.Euler();
const projectedTarget = new THREE.Vector3();
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "RESUME" : "PAUSE";
});
document.querySelector("#resetView").addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.code === "KeyR") {
resetView();
}
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
});
function updateHUD(errorDegrees, range) {
errorValue.textContent = `${errorDegrees.toFixed(2)}°`;
rangeValue.textContent = `${range.toFixed(1)} m`;
shipEuler.setFromQuaternion(ship.quaternion, "YXZ");
yawValue.textContent = `${THREE.MathUtils.radToDeg(shipEuler.y).toFixed(1)}°`;
const locked = errorDegrees < 0.75;
statusText.textContent = locked ? "LOCKED" : "TRACKING";
statusText.classList.toggle("locked", locked);
}
function updateTargetLabel() {
projectedTarget.copy(targetPosition).project(camera);
const visible =
projectedTarget.z > -1 &&
projectedTarget.z < 1 &&
Math.abs(projectedTarget.x) < 1.15 &&
Math.abs(projectedTarget.y) < 1.15;
targetLabel.style.display = visible ? "block" : "none";
if (!visible) return;
targetLabel.style.left =
`${(projectedTarget.x * 0.5 + 0.5) * window.innerWidth}px`;
targetLabel.style.top =
`${(-projectedTarget.y * 0.5 + 0.5) * window.innerHeight}px`;
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
elapsed += dt;
}
updateCamera(dt);
const telemetry = updateSpacecraft(paused ? 0 : dt);
stars.rotation.y = elapsed * 0.003;
stars.rotation.x = Math.sin(elapsed * 0.04) * 0.018;
referenceRing.rotation.z = elapsed * 0.06;
const pulse = 1 + Math.sin(elapsed * 2.7) * 0.055;
targetEdges.rotation.y = elapsed * 0.28;
targetEdges.rotation.x = elapsed * 0.17;
targetRings.forEach((ring, index) => {
ring.quaternion.copy(camera.quaternion);
ring.scale.setScalar(pulse + index * 0.13);
});
targetLight.intensity = 25 + Math.sin(elapsed * 3.4) * 5;
beam.material.opacity = 0.38 + Math.sin(elapsed * 2.2) * 0.07;
updateHUD(telemetry.errorDegrees, telemetry.range);
updateTargetLabel();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```