Prompt specification
Build or customize the Three.js 3D scene "Coordinate marker on a globe" 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: Coordinate marker on a globe
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/coordinate-marker-on-a-globe-x1314
Scene Description Prompt:
Coordinate marker on a globe. Add movable latitude and longitude arcs to a globe. Moving either slider changes the marker along the corresponding circle while the other coordinate remains fixed. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (coordinate-marker-on-a-globe-x1314):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Globe Coordinate Lab</title>
<style>
:root {
color-scheme: dark;
--background: #061019;
--panel: rgba(10, 24, 35, 0.82);
--panel-border: rgba(173, 221, 230, 0.14);
--text: #edf8f8;
--muted: #8fa8b0;
--latitude: #59e2d7;
--longitude: #ffc86a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 42%, #112d3b 0%, #081722 43%, #050d14 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#scene.grabbing {
cursor: grabbing;
}
.interface {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 30px;
left: 32px;
max-width: 430px;
}
.brand-mark {
display: flex;
align-items: center;
gap: 11px;
margin-bottom: 16px;
color: var(--latitude);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.brand-icon {
position: relative;
width: 28px;
height: 28px;
border: 1px solid currentColor;
border-radius: 50%;
}
.brand-icon::before,
.brand-icon::after {
content: "";
position: absolute;
border: 1px solid currentColor;
border-radius: 50%;
opacity: 0.75;
}
.brand-icon::before {
inset: 4px 8px;
}
.brand-icon::after {
inset: 8px 4px;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 48px);
line-height: 1;
letter-spacing: -0.045em;
font-weight: 650;
}
.brand p {
margin: 12px 0 0;
max-width: 370px;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.coordinates-hud {
position: absolute;
top: 30px;
right: 32px;
display: flex;
gap: 10px;
}
.hud-card {
min-width: 112px;
padding: 13px 15px 12px;
border: 1px solid var(--panel-border);
border-radius: 14px;
background: rgba(8, 20, 29, 0.66);
box-shadow: 0 14px 40px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(16px);
}
.hud-card span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.hud-card strong {
font-size: 19px;
font-weight: 650;
font-variant-numeric: tabular-nums;
}
.hud-card.latitude strong {
color: var(--latitude);
}
.hud-card.longitude strong {
color: var(--longitude);
}
.controls {
position: absolute;
left: 32px;
bottom: 28px;
width: 420px;
padding: 20px;
pointer-events: auto;
border: 1px solid var(--panel-border);
border-radius: 22px;
background: var(--panel);
box-shadow:
0 26px 70px rgba(0, 0, 0, 0.38),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(22px);
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 18px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.panel-head h2 {
margin: 0;
font-size: 20px;
letter-spacing: -0.025em;
font-weight: 650;
}
.reset-button {
width: 38px;
height: 38px;
padding: 0;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 11px;
color: var(--text);
background: rgba(255, 255, 255, 0.045);
cursor: pointer;
font-size: 20px;
transition:
background 160ms ease,
transform 160ms ease;
}
.reset-button:hover {
background: rgba(255, 255, 255, 0.09);
transform: rotate(-15deg);
}
.reset-button:focus-visible,
.center-button:focus-visible,
input[type="range"]:focus-visible {
outline: 2px solid var(--latitude);
outline-offset: 3px;
}
.control-row {
--accent: var(--latitude);
padding: 15px 0 14px;
border-top: 1px solid rgba(255, 255, 255, 0.065);
}
.control-row.longitude {
--accent: var(--longitude);
}
.control-meta {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 16px;
margin-bottom: 13px;
}
.control-meta label {
font-size: 14px;
font-weight: 650;
}
.control-meta output {
color: var(--accent);
font-size: 14px;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
--fill: 50%;
width: 100%;
height: 4px;
margin: 0;
border: 0;
border-radius: 999px;
appearance: none;
-webkit-appearance: none;
background:
linear-gradient(
90deg,
var(--accent) 0%,
var(--accent) var(--fill),
rgba(255, 255, 255, 0.12) var(--fill),
rgba(255, 255, 255, 0.12) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #10232d;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #10232d;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
}
.control-note {
display: flex;
align-items: center;
margin-top: 11px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.025em;
}
.control-note .dot {
width: 7px;
height: 7px;
margin-right: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 10px color-mix(in srgb, var(--accent) 65%, transparent);
}
.control-note .fixed {
margin-left: auto;
color: #b7c7cc;
font-weight: 650;
}
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
padding-top: 16px;
border-top: 1px solid rgba(255, 255, 255, 0.065);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 14px;
height: 2px;
border-radius: 2px;
background: var(--latitude);
}
.legend .longitude-key i {
background: var(--longitude);
}
.center-button {
flex: 0 0 auto;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 9px;
color: #dcebec;
background: rgba(255, 255, 255, 0.045);
cursor: pointer;
font-size: 10px;
font-weight: 700;
transition: background 160ms ease;
}
.center-button:hover {
background: rgba(255, 255, 255, 0.09);
}
.gesture-hint {
position: absolute;
right: 32px;
bottom: 32px;
color: rgba(191, 213, 218, 0.65);
font-size: 11px;
letter-spacing: 0.035em;
}
.gesture-hint kbd {
padding: 3px 6px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 5px;
color: #d7e5e7;
background: rgba(255, 255, 255, 0.045);
font-family: inherit;
font-size: 9px;
}
.marker-label {
position: fixed;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
min-width: 108px;
padding: 9px 11px;
border: 1px solid rgba(89, 226, 215, 0.34);
border-radius: 11px;
opacity: 0;
color: var(--text);
background: rgba(5, 17, 25, 0.88);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.36);
backdrop-filter: blur(10px);
transform: translate(-50%, -100%);
transition: opacity 140ms ease;
pointer-events: none;
}
.marker-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
width: 10px;
height: 10px;
border-right: 1px solid rgba(89, 226, 215, 0.34);
border-bottom: 1px solid rgba(89, 226, 215, 0.34);
background: rgba(5, 17, 25, 0.88);
transform: translateX(-50%) rotate(45deg);
}
.marker-label .pulse {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--latitude);
box-shadow: 0 0 12px var(--latitude);
}
.marker-label strong,
.marker-label small {
display: block;
line-height: 1.25;
}
.marker-label strong {
color: var(--latitude);
font-size: 12px;
font-variant-numeric: tabular-nums;
}
.marker-label small {
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
@media (max-width: 760px) {
.brand {
top: 18px;
left: 18px;
}
.brand-mark {
margin-bottom: 11px;
}
.brand p {
display: none;
}
.coordinates-hud {
top: 17px;
right: 16px;
gap: 6px;
}
.hud-card {
min-width: 82px;
padding: 10px;
}
.hud-card strong {
font-size: 15px;
}
.controls {
left: 12px;
right: 12px;
bottom: 12px;
width: auto;
padding: 16px;
border-radius: 18px;
}
.control-row {
padding: 12px 0;
}
.panel-footer {
padding-top: 13px;
}
.gesture-hint {
display: none;
}
}
@media (max-width: 520px) {
.brand p,
.coordinates-hud {
display: none;
}
h1 {
max-width: 270px;
font-size: 29px;
}
.brand-mark {
font-size: 9px;
}
.controls {
padding: 14px;
}
.panel-head {
margin-bottom: 12px;
}
.panel-head h2 {
font-size: 17px;
}
.control-row {
padding: 10px 0;
}
.control-meta {
margin-bottom: 10px;
}
.panel-footer {
align-items: flex-start;
flex-direction: column;
}
.center-button {
width: 100%;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive globe. Drag to orbit, scroll or pinch to zoom, and use the coordinate sliders to move the marker."
></canvas>
<div class="interface">
<header class="brand">
<div class="brand-mark">
<span class="brand-icon" aria-hidden="true"></span>
Orbital Navigation
</div>
<h1>Globe Coordinate Lab</h1>
<p>
Move the marker through intersecting latitude and longitude arcs.
One coordinate remains fixed while the other changes.
</p>
</header>
<aside class="coordinates-hud" aria-label="Current coordinates">
<div class="hud-card latitude">
<span>Latitude</span>
<strong id="hudLatitude">24° N</strong>
</div>
<div class="hud-card longitude">
<span>Longitude</span>
<strong id="hudLongitude">35° E</strong>
</div>
</aside>
<section class="controls" aria-label="Coordinate controls">
<div class="panel-head">
<div>
<p class="eyebrow">Manual navigation</p>
<h2>Coordinate controls</h2>
</div>
<button
id="resetAll"
class="reset-button"
type="button"
title="Reset coordinates and camera"
aria-label="Reset coordinates and camera"
>
↺
</button>
</div>
<div class="control-row latitude">
<div class="control-meta">
<label for="latitude">Latitude</label>
<output id="latitudeOutput" for="latitude">24° N</output>
</div>
<input
id="latitude"
type="range"
min="-80"
max="80"
step="1"
value="24"
aria-label="Latitude in degrees"
/>
<div class="control-note">
<span class="dot"></span>
Moves north / south
<span class="fixed">longitude fixed</span>
</div>
</div>
<div class="control-row longitude">
<div class="control-meta">
<label for="longitude">Longitude</label>
<output id="longitudeOutput" for="longitude">35° E</output>
</div>
<input
id="longitude"
type="range"
min="-180"
max="180"
step="1"
value="35"
aria-label="Longitude in degrees"
/>
<div class="control-note">
<span class="dot"></span>
Moves east / west
<span class="fixed">latitude fixed</span>
</div>
</div>
<div class="panel-footer">
<div class="legend" aria-label="Arc legend">
<span><i></i>Latitude arc</span>
<span class="longitude-key"><i></i>Longitude arc</span>
</div>
<button id="centerMarker" class="center-button" type="button">
Center marker
</button>
</div>
</section>
<div class="gesture-hint">
<kbd>Drag</kbd> orbit <kbd>Scroll</kbd> zoom
</div>
</div>
<div id="markerLabel" class="marker-label" aria-hidden="true">
<span class="pulse"></span>
<div>
<strong id="markerLatitude">24° N</strong>
<small id="markerLongitude">35° E</small>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and application state
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const latitudeInput = document.querySelector("#latitude");
const longitudeInput = document.querySelector("#longitude");
const latitudeOutput = document.querySelector("#latitudeOutput");
const longitudeOutput = document.querySelector("#longitudeOutput");
const hudLatitude = document.querySelector("#hudLatitude");
const hudLongitude = document.querySelector("#hudLongitude");
const markerLatitude = document.querySelector("#markerLatitude");
const markerLongitude = document.querySelector("#markerLongitude");
const markerLabel = document.querySelector("#markerLabel");
const DEFAULT_STATE = Object.freeze({
latitude: 24,
longitude: 35
});
const state = {
latitude: DEFAULT_STATE.latitude,
longitude: DEFAULT_STATE.longitude
};
const GLOBE_RADIUS = 2.18;
const SURFACE_RADIUS = 2.205;
const ARC_RADIUS = 2.235;
const DEG = Math.PI / 180;
function formatLatitude(value) {
const direction = value >= 0 ? "N" : "S";
return `${Math.round(Math.abs(value))}° ${direction}`;
}
function formatLongitude(value) {
const direction = value >= 0 ? "E" : "W";
return `${Math.round(Math.abs(value))}° ${direction}`;
}
function updateRangeFill(input) {
const minimum = Number(input.min);
const maximum = Number(input.max);
const percentage = ((Number(input.value) - minimum) / (maximum - minimum)) * 100;
input.style.setProperty("--fill", `${percentage}%`);
}
// -------------------------------------------------------------------------
// Renderer, scene, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0x061019, 0);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
yaw: DEFAULT_STATE.longitude * DEG,
pitch: DEFAULT_STATE.latitude * DEG,
distance: 7.8
};
function updateCamera() {
const horizontalDistance =
cameraState.distance * Math.cos(cameraState.pitch);
camera.position.set(
horizontalDistance * Math.cos(cameraState.yaw),
cameraState.distance * Math.sin(cameraState.pitch),
horizontalDistance * Math.sin(cameraState.yaw)
);
camera.lookAt(0, 0, 0);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xa4e8ff, 0x071016, 1.65));
const keyLight = new THREE.DirectionalLight(0xfff3d9, 3.2);
keyLight.position.set(-5, 7, 8);
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x68d7ef, 1.1);
fillLight.position.set(6, -2, 4);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0x43d9d1, 1.3, 16);
rimLight.position.set(-4, 1, -5);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Procedural low-resolution world texture
// -------------------------------------------------------------------------
function createWorldTexture() {
const textureCanvas = document.createElement("canvas");
textureCanvas.width = 1024;
textureCanvas.height = 512;
const context = textureCanvas.getContext("2d");
const oceanGradient = context.createLinearGradient(
0,
0,
0,
textureCanvas.height
);
oceanGradient.addColorStop(0, "#123b50");
oceanGradient.addColorStop(0.5, "#17465a");
oceanGradient.addColorStop(1, "#102f43");
context.fillStyle = oceanGradient;
context.fillRect(0, 0, textureCanvas.width, textureCanvas.height);
function drawPolygon(points, fill, stroke = "rgba(190, 225, 188, 0.28)") {
context.beginPath();
points.forEach(([longitude, latitude], index) => {
const x = ((longitude + 180) / 360) * textureCanvas.width;
const y = ((90 - latitude) / 180) * textureCanvas.height;
if (index === 0) {
context.moveTo(x, y);
} else {
context.lineTo(x, y);
}
});
context.closePath();
context.fillStyle = fill;
context.fill();
context.strokeStyle = stroke;
context.lineWidth = 1.25;
context.lineJoin = "round";
context.stroke();
}
const land = "#66866d";
const landLight = "#75947a";
drawPolygon([
[-168, 72], [-148, 70], [-132, 57], [-125, 48],
[-124, 40], [-117, 32], [-105, 25], [-97, 16],
[-84, 9], [-78, 22], [-81, 30], [-67, 45],
[-54, 50], [-62, 62], [-82, 67], [-108, 75],
[-135, 72]
], land);
drawPolygon([
[-62, 60], [-43, 59], [-21, 72], [-36, 82],
[-58, 78]
], landLight);
drawPolygon([
[-81, 12], [-70, 8], [-51, 1], [-35, -8],
[-42, -22], [-54, -55], [-70, -50], [-78, -20]
], land);
drawPolygon([
[-17, 35], [10, 37], [33, 31], [43, 12],
[50, -12], [35, -34], [18, -35], [8, -15],
[-8, 5], [-17, 16]
], landLight);
drawPolygon([
[-10, 36], [-10, 58], [5, 71], [30, 70],
[40, 58], [30, 45], [20, 36]
], land);
drawPolygon([
[30, 70], [60, 75], [100, 72], [145, 60],
[165, 52], [142, 40], [125, 30], [120, 18],
[105, 5], [95, 20], [78, 8], [68, 24],
[45, 30], [35, 42], [50, 50]
], land);
drawPolygon([
[112, -11], [130, -12], [142, -20], [153, -28],
[144, -39], [122, -35], [113, -24]
], landLight);
drawPolygon([
[47, -13], [51, -17], [49, -26], [44, -25]
], land);
drawPolygon([
[130, 32], [140, 42], [145, 45], [141, 35]
], land);
drawPolygon([
[166, -35], [178, -39], [174, -46], [166, -44]
], land);
drawPolygon([
[-8, 50], [1, 58], [-5, 59]
], land);
// Subtle horizontal scan lines give the map a technical atlas appearance.
context.globalAlpha = 0.035;
context.fillStyle = "#d4f4f1";
for (let y = 0; y < textureCanvas.height; y += 6) {
context.fillRect(0, y, textureCanvas.width, 1);
}
context.globalAlpha = 1;
const texture = new THREE.CanvasTexture(textureCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
texture.wrapS = THREE.RepeatWrapping;
return texture;
}
// -------------------------------------------------------------------------
// Low-poly globe and graticule
// -------------------------------------------------------------------------
const globeMaterial = new THREE.MeshStandardMaterial({
map: createWorldTexture(),
color: 0xffffff,
roughness: 0.88,
metalness: 0.02,
flatShading: true
});
const globe = new THREE.Mesh(
new THREE.SphereGeometry(GLOBE_RADIUS, 32, 16),
globeMaterial
);
scene.add(globe);
function createGraticule() {
const vertices = [];
function addSegment(pointA, pointB) {
vertices.push(...pointA, ...pointB);
}
function pointAt(latitude, longitude, radius) {
const lat = latitude * DEG;
const lon = longitude * DEG;
const cosLatitude = Math.cos(lat);
return [
radius * cosLatitude * Math.cos(lon),
radius * Math.sin(lat),
radius * cosLatitude * Math.sin(lon)
];
}
for (let latitude = -60; latitude <= 60; latitude += 20) {
for (let segment = 0; segment < 48; segment++) {
const longitudeA = (segment / 48) * 360;
const longitudeB = ((segment + 1) / 48) * 360;
addSegment(
pointAt(latitude, longitudeA, SURFACE_RADIUS),
pointAt(latitude, longitudeB, SURFACE_RADIUS)
);
}
}
for (let longitude = -180; longitude < 180; longitude += 20) {
for (let segment = 0; segment < 32; segment++) {
const latitudeA = -90 + (segment / 32) * 180;
const latitudeB = -90 + ((segment + 1) / 32) * 180;
addSegment(
pointAt(latitudeA, longitude, SURFACE_RADIUS),
pointAt(latitudeB, longitude, SURFACE_RADIUS)
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
const material = new THREE.LineBasicMaterial({
color: 0xb6e1e5,
transparent: true,
opacity: 0.16,
depthWrite: false
});
return new THREE.LineSegments(geometry, material);
}
scene.add(createGraticule());
// A subtle low-poly atmospheric shell.
const atmosphere = new THREE.Mesh(
new THREE.IcosahedronGeometry(2.29, 2),
new THREE.MeshBasicMaterial({
color: 0x60e3df,
transparent: true,
opacity: 0.035,
wireframe: true,
depthWrite: false
})
);
scene.add(atmosphere);
// -------------------------------------------------------------------------
// Background stars
// -------------------------------------------------------------------------
function createStars() {
const starCount = 700;
const positions = new Float32Array(starCount * 3);
for (let index = 0; index < starCount; index++) {
const azimuth = Math.random() * Math.PI * 2;
const vertical = Math.random() * 2 - 1;
const horizontal = Math.sqrt(1 - vertical * vertical);
const radius = 16 + Math.random() * 25;
positions[index * 3] =
radius * horizontal * Math.cos(azimuth);
positions[index * 3 + 1] = radius * vertical;
positions[index * 3 + 2] =
radius * horizontal * Math.sin(azimuth);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
const material = new THREE.PointsMaterial({
color: 0xc8e8ec,
size: 0.055,
transparent: true,
opacity: 0.72,
sizeAttenuation: true,
depthWrite: false
});
return new THREE.Points(geometry, material);
}
scene.add(createStars());
// -------------------------------------------------------------------------
// Latitude and longitude curve classes
// -------------------------------------------------------------------------
class LatitudeCurve extends THREE.Curve {
constructor(latitude, radius) {
super();
this.latitude = latitude;
this.radius = radius;
}
getPoint(t, target = new THREE.Vector3()) {
const latitude = this.latitude * DEG;
const longitude = t * Math.PI * 2;
const cosLatitude = Math.cos(latitude);
return target.set(
this.radius * cosLatitude * Math.cos(longitude),
this.radius * Math.sin(latitude),
this.radius * cosLatitude * Math.sin(longitude)
);
}
}
class LongitudeCurve extends THREE.Curve {
constructor(longitude, radius) {
super();
this.longitude = longitude;
this.radius = radius;
}
getPoint(t, target = new THREE.Vector3()) {
const latitude = (-90 + t * 180) * DEG;
const longitude = this.longitude * DEG;
const cosLatitude = Math.cos(latitude);
return target.set(
this.radius * cosLatitude * Math.cos(longitude),
this.radius * Math.sin(latitude),
this.radius * cosLatitude * Math.sin(longitude)
);
}
}
const latitudeMaterial = new THREE.MeshStandardMaterial({
color: 0x59e2d7,
emissive: 0x1b7774,
emissiveIntensity: 0.85,
roughness: 0.35,
metalness: 0.08
});
const longitudeMaterial = new THREE.MeshStandardMaterial({
color: 0xffc86a,
emissive: 0x805019,
emissiveIntensity: 0.8,
roughness: 0.35,
metalness: 0.08
});
const latitudeArc = new THREE.Mesh(
new THREE.BufferGeometry(),
latitudeMaterial
);
const longitudeArc = new THREE.Mesh(
new THREE.BufferGeometry(),
longitudeMaterial
);
latitudeArc.name = "Latitude arc";
longitudeArc.name = "Longitude arc";
scene.add(latitudeArc, longitudeArc);
let geometryLatitude = null;
let geometryLongitude = null;
function updateLatitudeArc() {
if (geometryLatitude === state.latitude) return;
geometryLatitude = state.latitude;
latitudeArc.geometry.dispose();
latitudeArc.geometry = new THREE.TubeGeometry(
new LatitudeCurve(state.latitude, ARC_RADIUS),
64,
0.014,
4,
true
);
}
function updateLongitudeArc() {
if (geometryLongitude === state.longitude) return;
geometryLongitude = state.longitude;
longitudeArc.geometry.dispose();
longitudeArc.geometry = new THREE.TubeGeometry(
new LongitudeCurve(state.longitude, ARC_RADIUS),
48,
0.016,
4,
false
);
}
// -------------------------------------------------------------------------
// Low-poly coordinate marker
// -------------------------------------------------------------------------
const markerGroup = new THREE.Group();
const markerStem = new THREE.Mesh(
new THREE.CylinderGeometry(0.014, 0.014, 0.16, 6),
new THREE.MeshBasicMaterial({ color: 0xdffffc })
);
markerStem.position.y = 0.08;
const markerHalo = new THREE.Mesh(
new THREE.OctahedronGeometry(0.13, 0),
new THREE.MeshBasicMaterial({
color: 0x7ffff2,
wireframe: true,
transparent: true,
opacity: 0.95
})
);
markerHalo.position.y = 0.2;
const markerCore = new THREE.Mesh(
new THREE.OctahedronGeometry(0.067, 0),
new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0x59e2d7,
emissiveIntensity: 1.3,
roughness: 0.25
})
);
markerCore.position.y = 0.2;
markerGroup.add(markerStem, markerHalo, markerCore);
scene.add(markerGroup);
const markerNormal = new THREE.Vector3();
const upAxis = new THREE.Vector3(0, 1, 0);
function updateMarker() {
const latitude = state.latitude * DEG;
const longitude = state.longitude * DEG;
const cosLatitude = Math.cos(latitude);
markerNormal.set(
cosLatitude * Math.cos(longitude),
Math.sin(latitude),
cosLatitude * Math.sin(longitude)
);
markerGroup.position
.copy(markerNormal)
.multiplyScalar(SURFACE_RADIUS);
markerGroup.quaternion.setFromUnitVectors(upAxis, markerNormal);
}
// -------------------------------------------------------------------------
// Interface synchronization
// -------------------------------------------------------------------------
function updateInterface() {
const latitudeText = formatLatitude(state.latitude);
const longitudeText = formatLongitude(state.longitude);
latitudeOutput.textContent = latitudeText;
longitudeOutput.textContent = longitudeText;
hudLatitude.textContent = latitudeText;
hudLongitude.textContent = longitudeText;
markerLatitude.textContent = latitudeText;
markerLongitude.textContent = longitudeText;
updateRangeFill(latitudeInput);
updateRangeFill(longitudeInput);
}
function refreshCoordinates() {
updateMarker();
updateLatitudeArc();
updateLongitudeArc();
updateInterface();
}
latitudeInput.addEventListener("input", () => {
state.latitude = Number(latitudeInput.value);
refreshCoordinates();
});
longitudeInput.addEventListener("input", () => {
state.longitude = Number(longitudeInput.value);
refreshCoordinates();
});
document.querySelector("#centerMarker").addEventListener("click", () => {
cameraState.yaw = state.longitude * DEG;
cameraState.pitch = state.latitude * DEG;
cameraState.distance = 7.8;
updateCamera();
});
document.querySelector("#resetAll").addEventListener("click", () => {
state.latitude = DEFAULT_STATE.latitude;
state.longitude = DEFAULT_STATE.longitude;
latitudeInput.value = String(state.latitude);
longitudeInput.value = String(state.longitude);
cameraState.yaw = state.longitude * DEG;
cameraState.pitch = state.latitude * DEG;
cameraState.distance = 7.8;
updateCamera();
refreshCoordinates();
});
refreshCoordinates();
// -------------------------------------------------------------------------
// Direct camera controls: mouse drag, touch drag/pinch, wheel zoom
// -------------------------------------------------------------------------
const pointers = new Map();
let activePointerId = null;
let previousX = 0;
let previousY = 0;
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
const deltaX = values[0].x - values[1].x;
const deltaY = values[0].y - values[1].y;
return Math.hypot(deltaX, deltaY);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
activePointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
canvas.classList.add("grabbing");
} else if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1 && activePointerId === event.pointerId) {
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
-1.3,
1.3
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance *
(previousPinchDistance / distance),
4.7,
12
);
updateCamera();
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (activePointerId === event.pointerId) {
activePointerId = null;
}
if (pointers.size === 1) {
const [remainingPointerId, remainingPointer] =
pointers.entries().next().value;
activePointerId = remainingPointerId;
previousX = remainingPointer.x;
previousY = remainingPointer.y;
}
previousPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
if (pointers.size === 0) {
canvas.classList.remove("grabbing");
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.006,
4.7,
12
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraState.yaw = state.longitude * DEG;
cameraState.pitch = state.latitude * DEG;
cameraState.distance = 7.8;
updateCamera();
});
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Projected HTML marker label
// -------------------------------------------------------------------------
const projectedPoint = new THREE.Vector3();
const cameraDirection = new THREE.Vector3();
const cameraToMarker = new THREE.Vector3();
function updateMarkerLabel() {
const worldPosition = markerGroup.getWorldPosition(projectedPoint);
camera.getWorldDirection(cameraDirection);
cameraToMarker.copy(worldPosition).sub(camera.position);
const inFrontOfCamera = cameraDirection.dot(cameraToMarker) > 0;
const onVisibleHemisphere =
markerNormal.dot(
camera.position.clone().sub(worldPosition)
) > 0;
projectedPoint.project(camera);
const visible =
inFrontOfCamera &&
onVisibleHemisphere &&
projectedPoint.z > -1 &&
projectedPoint.z < 1;
if (!visible) {
markerLabel.style.opacity = "0";
return;
}
const x = (projectedPoint.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projectedPoint.y * 0.5 + 0.5) * window.innerHeight;
markerLabel.style.left = `${x}px`;
markerLabel.style.top = `${y - 38}px`;
markerLabel.style.opacity = "1";
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
markerHalo.scale.setScalar(1 + Math.sin(elapsed * 3.4) * 0.07);
markerHalo.rotation.y = elapsed * 0.55;
markerHalo.rotation.x = elapsed * 0.22;
camera.updateMatrixWorld();
updateMarkerLabel();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```