Prompt specification
Build or customize the Three.js 3D scene "Sunlit and shaded globe halves" 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: Sunlit and shaded globe halves
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/sunlit-and-shaded-globe-halves-x1349
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts planetary rotation through daylight and darkness. Model a globe illuminated from one fixed direction. Add one surface marker rotating through light and shadow, with the rotation axis and illuminated boundary visible. Stylized solid forms.
Working Three.js Starter (sunlit-and-shaded-globe-halves-x1349):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<title>Planetary Day and Night</title>
<style>
:root {
--ink: #eef8ff;
--muted: #9db2c2;
--panel: rgba(7, 18, 31, 0.76);
--line: rgba(178, 220, 244, 0.18);
--gold: #ffd27a;
--cyan: #83e5f1;
--orange: #ff914d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #030914;
color: var(--ink);
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;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 2;
pointer-events: none;
border: 1px solid var(--line);
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.045), transparent 58%),
var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.intro {
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 22px 24px 20px;
border-radius: 18px;
}
.eyebrow {
margin-bottom: 10px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 38px);
font-weight: 650;
line-height: 1.03;
letter-spacing: -0.045em;
}
.intro p {
max-width: 32ch;
margin: 13px 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.phase-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.phase-label {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#phaseValue {
color: var(--gold);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.meter {
position: relative;
width: 100%;
height: 4px;
margin-top: 10px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
#meterFill {
width: 50%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #173047, var(--gold));
transition: width 120ms linear, background 250ms ease;
}
.legend {
right: 24px;
bottom: 24px;
display: grid;
gap: 10px;
min-width: 220px;
padding: 15px 17px;
border-radius: 14px;
font-size: 11px;
color: var(--muted);
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
}
.swatch {
width: 23px;
height: 3px;
border-radius: 10px;
flex: 0 0 auto;
}
.swatch.gold {
background: var(--gold);
box-shadow: 0 0 10px rgba(255, 210, 122, 0.7);
}
.swatch.cyan {
background: var(--cyan);
box-shadow: 0 0 10px rgba(131, 229, 241, 0.6);
}
.swatch.orange {
width: 8px;
height: 8px;
margin: 0 7px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 12px rgba(255, 145, 77, 0.8);
}
.toolbar {
position: fixed;
z-index: 3;
top: 24px;
right: 24px;
display: flex;
gap: 8px;
}
button {
border: 1px solid var(--line);
border-radius: 11px;
padding: 10px 13px;
color: var(--ink);
background: rgba(7, 18, 31, 0.76);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
font: inherit;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(255, 210, 122, 0.52);
background: rgba(17, 34, 49, 0.9);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--gold);
outline-offset: 3px;
}
.hint {
position: fixed;
z-index: 2;
left: 24px;
bottom: 24px;
pointer-events: none;
color: rgba(214, 233, 246, 0.58);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 700px) {
.intro {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 18px;
}
.toolbar {
top: auto;
right: 14px;
bottom: 14px;
}
.legend {
display: none;
}
.hint {
left: 14px;
bottom: 58px;
max-width: 55vw;
line-height: 1.5;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D planet rotating through daylight and darkness"></canvas>
<section class="panel intro" aria-live="polite">
<div class="eyebrow">Celestial mechanics / 01</div>
<h1>A world turning through the light.</h1>
<p>
Sunlight remains fixed in space while the tilted planet rotates beneath it.
Follow the orange marker as it crosses the illuminated boundary.
</p>
<div class="phase-row">
<span class="phase-label">Marker illumination</span>
<span id="phaseValue">Daylight</span>
</div>
<div class="meter"><div id="meterFill"></div></div>
</section>
<nav class="toolbar" aria-label="Scene controls">
<button id="pauseButton" type="button" aria-pressed="false">Pause rotation</button>
<button id="resetButton" type="button">Reset view</button>
</nav>
<aside class="panel legend" aria-label="Scene legend">
<div class="legend-item">
<span class="swatch gold"></span>
<span>Fixed sunlight / terminator</span>
</div>
<div class="legend-item">
<span class="swatch cyan"></span>
<span>Tilted rotation axis</span>
</div>
<div class="legend-item">
<span class="swatch orange"></span>
<span>Rotating surface marker</span>
</div>
</aside>
<div class="hint">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x030914);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const worldUp = new THREE.Vector3(0, 1, 0);
const planetRadius = 2.15;
// This vector points from the planet toward the fixed light source.
const sunDirection = new THREE.Vector3(-0.66, 0.38, 0.66).normalize();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0x5b8ba6, 0x02040a, 0.32);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xffe6b5, 4.0);
sunlight.position.copy(sunDirection).multiplyScalar(9);
sunlight.target.position.set(0, 0, 0);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -4;
sunlight.shadow.camera.right = 4;
sunlight.shadow.camera.top = 4;
sunlight.shadow.camera.bottom = -4;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 22;
sunlight.shadow.bias = -0.00035;
sunlight.shadow.normalBias = 0.025;
scene.add(sunlight, sunlight.target);
// -------------------------------------------------------------------------
// Background stars
// -------------------------------------------------------------------------
let randomSeed = 202503;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const starPositions = [];
for (let i = 0; i < 850; i++) {
const z = random() * 2 - 1;
const angle = random() * Math.PI * 2;
const radial = Math.sqrt(1 - z * z);
const distance = 24 + random() * 34;
starPositions.push(
distance * radial * Math.cos(angle),
distance * z,
distance * radial * Math.sin(angle)
);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0xc8e4ff,
size: 0.055,
sizeAttenuation: true,
transparent: true,
opacity: 0.72,
fog: false,
depthWrite: false
})
);
scene.add(stars);
// -------------------------------------------------------------------------
// Small stylized sun, aligned with the directional light
// -------------------------------------------------------------------------
const sunGroup = new THREE.Group();
sunGroup.position.copy(sunDirection).multiplyScalar(7.1);
const sunCore = new THREE.Mesh(
new THREE.SphereGeometry(0.22, 24, 16),
new THREE.MeshBasicMaterial({ color: 0xffd27a })
);
sunGroup.add(sunCore);
const sunGlow = new THREE.Mesh(
new THREE.SphereGeometry(0.39, 24, 16),
new THREE.MeshBasicMaterial({
color: 0xffb84f,
transparent: true,
opacity: 0.12,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
sunGroup.add(sunGlow);
const sunRayGeometry = new THREE.ConeGeometry(0.055, 0.24, 8);
const sunRayMaterial = new THREE.MeshBasicMaterial({ color: 0xffcf72 });
const rayBasisU = new THREE.Vector3()
.crossVectors(sunDirection, worldUp)
.normalize();
const rayBasisV = new THREE.Vector3()
.crossVectors(sunDirection, rayBasisU)
.normalize();
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2;
const radial = rayBasisU
.clone()
.multiplyScalar(Math.cos(angle))
.addScaledVector(rayBasisV, Math.sin(angle));
const ray = new THREE.Mesh(sunRayGeometry, sunRayMaterial);
ray.position.copy(radial).multiplyScalar(0.39);
ray.quaternion.setFromUnitVectors(worldUp, radial);
sunGroup.add(ray);
}
scene.add(sunGroup);
// -------------------------------------------------------------------------
// Planet hierarchy
//
// tiltRoot: fixed axial tilt
// spinGroup: actual planetary rotation
// -------------------------------------------------------------------------
const tiltRoot = new THREE.Group();
tiltRoot.rotation.z = 0.32;
scene.add(tiltRoot);
const spinGroup = new THREE.Group();
tiltRoot.add(spinGroup);
const oceanMaterial = new THREE.MeshStandardMaterial({
color: 0x07536b,
roughness: 0.82,
metalness: 0.02,
flatShading: true
});
const ocean = new THREE.Mesh(
new THREE.SphereGeometry(planetRadius, 64, 40),
oceanMaterial
);
ocean.receiveShadow = true;
spinGroup.add(ocean);
// -------------------------------------------------------------------------
// Spherical geographic helpers
// -------------------------------------------------------------------------
function latLonToVector(latitude, longitude, radius = 1) {
const lat = THREE.MathUtils.degToRad(latitude);
const lon = THREE.MathUtils.degToRad(longitude);
return new THREE.Vector3(
radius * Math.cos(lat) * Math.sin(lon),
radius * Math.sin(lat),
radius * Math.cos(lat) * Math.cos(lon)
);
}
function createLandPatch(
centerLatitude,
centerLongitude,
longitudeSpan,
latitudeSpan,
color,
seed
) {
const segments = 40;
const rings = 5;
const surfaceRadius = planetRadius + 0.024;
const positions = [];
positions.push(
...latLonToVector(
centerLatitude,
centerLongitude,
surfaceRadius
).toArray()
);
const boundary = [];
for (let i = 0; i < segments; i++) {
const angle = (i / segments) * Math.PI * 2;
const irregularity =
1 +
0.085 * Math.sin(angle * 3 + seed) +
0.045 * Math.cos(angle * 5 - seed * 0.7);
const latitude =
centerLatitude +
Math.sin(angle) * latitudeSpan * irregularity;
const longitude =
centerLongitude +
Math.cos(angle) * longitudeSpan * irregularity;
boundary.push({ latitude, longitude });
}
for (let ring = 1; ring <= rings; ring++) {
const t = ring / rings;
for (const point of boundary) {
const latitude = THREE.MathUtils.lerp(
centerLatitude,
point.latitude,
t
);
const longitude = THREE.MathUtils.lerp(
centerLongitude,
point.longitude,
t
);
positions.push(
...latLonToVector(latitude, longitude, surfaceRadius).toArray()
);
}
}
const indices = [];
for (let i = 0; i < segments; i++) {
const next = (i + 1) % segments;
indices.push(0, 1 + i, 1 + next);
}
for (let ring = 0; ring < rings - 1; ring++) {
const innerStart = 1 + ring * segments;
const outerStart = innerStart + segments;
for (let i = 0; i < segments; i++) {
const next = (i + 1) % segments;
const innerCurrent = innerStart + i;
const innerNext = innerStart + next;
const outerCurrent = outerStart + i;
const outerNext = outerStart + next;
indices.push(
innerCurrent,
outerCurrent,
outerNext,
innerCurrent,
outerNext,
innerNext
);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.94,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide,
polygonOffset: true,
polygonOffsetFactor: -1,
polygonOffsetUnits: -1
});
const patch = new THREE.Mesh(geometry, material);
patch.receiveShadow = true;
spinGroup.add(patch);
return patch;
}
function createPolarCap(sign, color) {
const segments = 64;
const rings = 7;
const capAngle = 17;
const surfaceRadius = planetRadius + 0.032;
const positions = [];
positions.push(
...latLonToVector(sign * 90, 0, surfaceRadius).toArray()
);
for (let ring = 1; ring <= rings; ring++) {
const polarAngle = (capAngle * ring) / rings;
const latitude = sign * (90 - polarAngle);
for (let i = 0; i < segments; i++) {
const longitude = (i / segments) * 360;
positions.push(
...latLonToVector(latitude, longitude, surfaceRadius).toArray()
);
}
}
const indices = [];
for (let i = 0; i < segments; i++) {
const next = (i + 1) % segments;
const triangle = [0, 1 + i, 1 + next];
if (sign < 0) triangle.reverse();
indices.push(...triangle);
}
for (let ring = 0; ring < rings - 1; ring++) {
const innerStart = 1 + ring * segments;
const outerStart = innerStart + segments;
for (let i = 0; i < segments; i++) {
const next = (i + 1) % segments;
const innerCurrent = innerStart + i;
const innerNext = innerStart + next;
const outerCurrent = outerStart + i;
const outerNext = outerStart + next;
const a = [innerCurrent, outerCurrent, outerNext];
const b = [innerCurrent, outerNext, innerNext];
if (sign < 0) {
a.reverse();
b.reverse();
}
indices.push(...a, ...b);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.98,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
const cap = new THREE.Mesh(geometry, material);
cap.receiveShadow = true;
spinGroup.add(cap);
}
// Simplified, solid land forms.
createLandPatch(43, -92, 27, 15, 0x68864c, 0.7);
createLandPatch(-22, -56, 14, 26, 0x73894d, 2.1);
createLandPatch(72, -43, 13, 11, 0xa6b29d, 3.4);
createLandPatch(48, 16, 18, 9, 0x6e824a, 1.3);
createLandPatch(10, 24, 20, 24, 0x9a8754, 4.2);
createLandPatch(43, 84, 43, 20, 0x71864d, 2.8);
createLandPatch(4, 116, 15, 10, 0x738b4d, 5.4);
createLandPatch(-25, 135, 18, 11, 0x9d8556, 0.2);
createPolarCap(1, 0xdce8e2);
createPolarCap(-1, 0xdce8e2);
// -------------------------------------------------------------------------
// Visible rotation axis and marker orbit
// -------------------------------------------------------------------------
const axisMaterial = new THREE.MeshBasicMaterial({
color: 0x83e5f1,
transparent: true,
opacity: 0.68,
depthTest: false,
depthWrite: false
});
const axis = new THREE.Mesh(
new THREE.CylinderGeometry(0.014, 0.014, 6.4, 12),
axisMaterial
);
axis.renderOrder = 12;
tiltRoot.add(axis);
const arrowGeometry = new THREE.ConeGeometry(0.075, 0.2, 16);
const northArrow = new THREE.Mesh(arrowGeometry, axisMaterial);
northArrow.position.y = 3.32;
northArrow.renderOrder = 12;
tiltRoot.add(northArrow);
const southArrow = new THREE.Mesh(arrowGeometry, axisMaterial);
southArrow.position.y = -3.32;
southArrow.rotation.z = Math.PI;
southArrow.renderOrder = 12;
tiltRoot.add(southArrow);
const markerLatitude = 20;
const markerLongitude = 35;
const markerLatitudeRadians = THREE.MathUtils.degToRad(markerLatitude);
const orbitRadius = planetRadius * Math.cos(markerLatitudeRadians);
const orbitHeight = planetRadius * Math.sin(markerLatitudeRadians);
const orbit = new THREE.Mesh(
new THREE.TorusGeometry(orbitRadius + 0.018, 0.009, 6, 160),
new THREE.MeshBasicMaterial({
color: 0x83e5f1,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
orbit.rotation.x = Math.PI / 2;
orbit.position.y = orbitHeight + 0.018;
tiltRoot.add(orbit);
// Four solid chevrons show the direction of rotation.
const directionArrowGeometry = new THREE.ConeGeometry(0.045, 0.15, 10);
const directionArrowMaterial = new THREE.MeshBasicMaterial({
color: 0x83e5f1,
transparent: true,
opacity: 0.68
});
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2 + 0.3;
const tangent = new THREE.Vector3(
Math.sin(angle),
0,
-Math.cos(angle)
);
const arrow = new THREE.Mesh(
directionArrowGeometry,
directionArrowMaterial
);
arrow.position.set(
(orbitRadius + 0.018) * Math.cos(angle),
orbitHeight + 0.018,
(orbitRadius + 0.018) * Math.sin(angle)
);
arrow.quaternion.setFromUnitVectors(worldUp, tangent);
tiltRoot.add(arrow);
}
// -------------------------------------------------------------------------
// One rotating surface marker
// -------------------------------------------------------------------------
const marker = new THREE.Group();
const markerNormal = latLonToVector(
markerLatitude,
markerLongitude,
1
).normalize();
marker.position.copy(markerNormal).multiplyScalar(planetRadius + 0.03);
marker.quaternion.setFromUnitVectors(worldUp, markerNormal);
spinGroup.add(marker);
const markerBaseMaterial = new THREE.MeshStandardMaterial({
color: 0xd66b2f,
emissive: 0x351000,
emissiveIntensity: 0.65,
roughness: 0.48,
metalness: 0.18
});
const markerCapMaterial = new THREE.MeshStandardMaterial({
color: 0xffb45d,
emissive: 0x3b1200,
emissiveIntensity: 0.5,
roughness: 0.35,
metalness: 0.15
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.15, 0.08, 16),
markerBaseMaterial
);
base.position.y = 0.04;
marker.add(base);
const baseRing = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.018, 8, 32),
markerCapMaterial
);
baseRing.rotation.x = Math.PI / 2;
baseRing.position.y = 0.075;
marker.add(baseRing);
const beacon = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.19, 16),
markerCapMaterial
);
beacon.position.y = 0.17;
marker.add(beacon);
const beaconTip = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 16, 12),
markerCapMaterial
);
beaconTip.position.y = 0.285;
marker.add(beaconTip);
marker.traverse((object) => {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
}
});
// -------------------------------------------------------------------------
// Terminator: the great circle fixed perpendicular to the sunlight
// -------------------------------------------------------------------------
const terminatorBasisU = new THREE.Vector3()
.crossVectors(sunDirection, worldUp)
.normalize();
const terminatorBasisV = new THREE.Vector3()
.crossVectors(sunDirection, terminatorBasisU)
.normalize();
const terminatorPoints = [];
for (let i = 0; i < 192; i++) {
const angle = (i / 192) * Math.PI * 2;
const point = terminatorBasisU
.clone()
.multiplyScalar(Math.cos(angle))
.addScaledVector(terminatorBasisV, Math.sin(angle))
.multiplyScalar(planetRadius + 0.035);
terminatorPoints.push(point);
}
const terminatorCurve = new THREE.CatmullRomCurve3(
terminatorPoints,
true,
"centripetal"
);
const terminatorGlow = new THREE.Mesh(
new THREE.TubeGeometry(terminatorCurve, 192, 0.045, 8, true),
new THREE.MeshBasicMaterial({
color: 0xffbd57,
transparent: true,
opacity: 0.12,
depthTest: false,
depthWrite: false
})
);
terminatorGlow.renderOrder = 10;
scene.add(terminatorGlow);
const terminator = new THREE.Mesh(
new THREE.TubeGeometry(terminatorCurve, 192, 0.014, 7, true),
new THREE.MeshBasicMaterial({
color: 0xffd27a,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
})
);
terminator.renderOrder = 11;
scene.add(terminator);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, and zoom
// -------------------------------------------------------------------------
const defaultView = {
radius: 8.8,
theta: 0.62,
phi: 1.18,
target: new THREE.Vector3(0, 0, 0)
};
const view = {
radius: defaultView.radius,
theta: defaultView.theta,
phi: defaultView.phi,
target: defaultView.target.clone()
};
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
view.target.x + view.radius * sinPhi * Math.sin(view.theta),
view.target.y + view.radius * Math.cos(view.phi),
view.target.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(view.target);
}
function resetCamera() {
view.radius = defaultView.radius;
view.theta = defaultView.theta;
view.phi = defaultView.phi;
view.target.copy(defaultView.target);
updateCamera();
}
let dragging = false;
let panning = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.shiftKey || event.button === 1;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (panning) {
camera.updateMatrixWorld();
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
1
);
const panScale = view.radius * 0.0015;
view.target
.addScaledVector(right, -dx * panScale)
.addScaledVector(up, dy * panScale);
} else {
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(
view.phi - dy * 0.006,
0.18,
Math.PI - 0.18
);
}
updateCamera();
});
function endPointer(event) {
dragging = false;
panning = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
5.1,
16
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.querySelector("#resetButton").addEventListener(
"click",
resetCamera
);
// -------------------------------------------------------------------------
// Rotation and illumination readout
// -------------------------------------------------------------------------
let paused = false;
const pauseButton = document.querySelector("#pauseButton");
const phaseValue = document.querySelector("#phaseValue");
const meterFill = document.querySelector("#meterFill");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume rotation" : "Pause rotation";
pauseButton.setAttribute("aria-pressed", String(paused));
});
const clock = new THREE.Clock();
const markerWorldPosition = new THREE.Vector3();
function updateIlluminationReadout() {
marker.getWorldPosition(markerWorldPosition);
markerWorldPosition.normalize();
const illumination = markerWorldPosition.dot(sunDirection);
const normalized = THREE.MathUtils.clamp((illumination + 1) * 0.5, 0, 1);
let phase;
let color;
if (illumination > 0.18) {
phase = "Daylight";
color = "#ffd27a";
} else if (illumination < -0.18) {
phase = "Night shadow";
color = "#7895b2";
} else {
phase = "Terminator";
color = "#ff914d";
}
phaseValue.textContent = phase;
phaseValue.style.color = color;
meterFill.style.width = `${normalized * 100}%`;
meterFill.style.background = `linear-gradient(90deg, #173047, ${color})`;
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
spinGroup.rotation.y =
(spinGroup.rotation.y + delta * 0.23) % (Math.PI * 2);
}
updateIlluminationReadout();
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
updateCamera();
updateIlluminationReadout();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```