WebGL Moon Renderer Design
Date: 2026-01-15 Status: Superseded by Horizon Sphere Goal: Visual delight - a floating, tangible moon that feels magical
Overview
Replace the current 2D pre-rendered moon frames with a real-time WebGL 3D moon. The moon should appear to float subtly above the page, respond to pointer movement with parallax, and accurately display libration and phase for any date in 2026.
Design Decisions
| Decision | Choice | Rationale |
|---|---|---|
| Primary goal | Visual delight | Magic over strict accuracy |
| 2D/3D relationship | Replace entirely | 2D becomes fallback only |
| Float effect | Subtle lift + parallax | Understated, rewards attention |
| Lighting accuracy | Perceptually correct | Should feel right, not pass audit |
| WebGL approach | Raw WebGL | No Three.js - lean, full control |
| Texture loading | Progressive | 256px instant, 2048px quality |
| Ephemeris sampling | Every 6 hours | Day-level accuracy sufficient |
Architecture
moon-renderer.js (entry point)
├── Detects WebGL availability
├── If WebGL: delegates to moon-renderer-webgl.js
└── If no WebGL: uses current 2D canvas approach (existing code)
moon-renderer-webgl.js (new)
├── Creates WebGL context in a canvas
├── Loads textures progressively (256 → 2048)
├── Renders sphere with:
│ ├── Libration (rotate sphere per subearth lon/lat)
│ ├── Phase (directional light per subsolar lon/lat)
│ ├── Latitude rotation (same as current)
│ └── Parallax response (pointer offset)
└── Renders shadow (blurred disc beneath sphere)
WebGL Rendering
Geometry
UV sphere, ~32 latitude × 64 longitude segments, generated procedurally at init.
Vertex Shader
Applies rotations in order:
1. Libration: Y-axis rotation by subearth.lon, X-axis by subearth.lat
2. Position angle: Z-axis rotation by posangle
3. Latitude: Z-axis rotation by observer latitude
Optional displacement mapping pushes vertices along normals (±2% radius).
Fragment Shader
- Samples color texture (equirectangular projection)
- Diffuse lighting from directional “sun” at
subsolar.lon/lat - Soft terminator via smoothstep over ~10°
- No specular (matte regolith)
- Ambient term for earthshine approximation on dark side
Shadow
Separate draw call: dark ellipse on plane beneath sphere. Gaussian blur via multi-tap sampling. Shadow offset shifts opposite to parallax offset.
Parallax Interaction
- Track pointer position relative to viewport center
- Normalize to [-1, 1], apply small offset (±3-5% of moon diameter)
- Lerp toward target over ~100ms for organic feel
- Shadow shifts opposite direction
- On touch: use device orientation if available, else static
- Performance: render only when visible (IntersectionObserver), pause when idle
Data
Ephemeris
Trimmed mooninfo-2026.json:
- Source: NASA SVS 5587
- Sampled every 6 hours (1460 entries)
- Fields kept: subsolar.{lon,lat}, subearth.{lon,lat}, posangle
- Estimated size: ~30KB gzipped
Interpolation: linear lerp between bracketing entries for smooth scrubbing.
Textures
From NASA CGI Moon Kit (SVS 4720):
| File | Size | Purpose |
|---|---|---|
moon-color-256.jpg |
~10KB | Instant load placeholder |
moon-color-2048.jpg |
~1-2MB | Quality texture |
moon-displacement.png |
~500KB | Surface detail (optional) |
Build step: download NASA TIFFs, convert to optimized JPG/PNG.
Integration
Interface
Both 2D and WebGL renderers expose identical API:
{
render({ size, latitude, date }), // update appearance
getState(), // return current params
destroy() // clean up
}
Fallback Triggers
- No WebGL support
- Context creation fails
- Texture load fails
File Organization
assets/figures/
├── moon-renderer.js (modified: adds WebGL detection)
├── moon-renderer-webgl.js (new)
├── textures/
│ ├── moon-color-256.jpg (new)
│ ├── moon-color-2048.jpg (new)
│ └── moon-displacement.png (new, optional)
├── data/
│ ├── mooninfo-2026.json (new, trimmed ephemeris)
│ └── frame-index.json (existing, for fallback)
└── fallback/frames/ (existing, for 2D fallback)
Implementation Steps
- Prepare textures: Download NASA maps, convert to web formats, add to repo
- Prepare ephemeris: Script to trim mooninfo JSON to 6-hour intervals
- WebGL renderer core: Sphere geometry, shaders, basic rendering
- Libration + phase: Apply ephemeris data to rotations and lighting
- Shadow: Add floating shadow beneath moon
- Parallax: Mouse/touch interaction with smooth response
- Progressive loading: Texture swap from 256 to 2048
- Integration: Modify moon-renderer.js to detect and delegate
- Testing: Verify fallback works, check mobile, validate against NASA frames
Resources
- NASA SVS Moon Phase & Libration 2026: https://svs.gsfc.nasa.gov/5587/
- Ephemeris JSON: https://svs.gsfc.nasa.gov/vis/a000000/a005500/a005587/mooninfo_2026.json
- CGI Moon Kit (textures): https://svs.gsfc.nasa.gov/4720/