The Challenge of 3D in Modern App Router
Integrating WebGL and Three.js into Next.js 15 App Router presents a collision between two paradigms: - Next.js 15 App Router defaults to React Server Components (RSC), executing on Node.js/Edge runtimes where DOM and WebGL contexts do not exist. - Three.js requires immediate access to the HTML `<canvas>` element and active GPU rendering loops.
A naive integration causes SSR crashes (`window is not defined`), client-side hydration mismatches, and severe layout shifts (CLS) as canvases mount asynchronously.
Preventing SSR Canvas Explosions
To guarantee zero layout shift (CLS = 0) while maintaining clean server rendering: 1. Wrap Three.js/R3F canvases in client boundaries (`"use client"`). 2. Use Next.js dynamic imports with `{ ssr: false }`. 3. Render an SVG or CSS cosmic atmosphere placeholder with identical dimensions during the initial server response.
const StarField = dynamic(() => import('@/components/galaxy/StarField'), {
ssr: false,
loading: () => <div className="fixed inset-0 bg-[#060713] -z-10" />
});Batching 100,000 Stars with InstancedMesh
The primary bottleneck in WebGL rendering is CPU-to-GPU draw calls. In naive setups, creating 5,000 individual `THREE.Mesh` objects results in 5,000 separate draw calls per frame: - **Individual Meshes (5,000 objects)**: ~18 FPS, 55ms frame time, 100% CPU core utilization. - **`THREE.InstancedMesh` (100,000 stars)**: **60 FPS**, 1.4ms frame time, **single draw call**.
With `InstancedMesh`, geometry and material data are uploaded to GPU VRAM once. The CPU only passes a transformation matrix buffer (`Float32Array`) describing position, scale, and rotation.
Custom GLSL Shaders for Galaxy Twinkle
Instead of updating star opacity via JavaScript `requestAnimationFrame` loops (which causes garbage collection stutter), we push the mathematical twinkle formula entirely into GPU fragment shaders:
// Fragment Shader
uniform float uTime;
varying vec3 vColor;
varying float vTwinkleSpeed;
void main() {
// Distance from center of point sprite to create soft star glow
float dist = distance(gl_PointCoord, vec2(0.5));
if (dist > 0.5) discard;
float alpha = smoothstep(0.5, 0.05, dist);
// Sinusoidal twinkle computed entirely in GPU hardware registers
float twinkle = 0.6 + 0.4 * sin(uTime * vTwinkleSpeed);
gl_FragColor = vec4(vColor, alpha * twinkle);
}Dynamic DPR & Mobile Thermal Budgeting
High-DPI mobile devices (e.g., iPhone 15 Pro with DPR = 3.0) will aggressively thermal-throttle and drop frames if forced to render a full-screen canvas at native 3x pixel density (over 8 million fragments per frame).
We implement dynamic DPR clamping: - Desktop viewports: `dpr = Math.min(window.devicePixelRatio, 2)` - Mobile / Battery saving: `dpr = 1.0` - Frameloop set to `demand` or throttled to 30fps when the tab is unfocused using `document.visibilityState`.
WCAG 2.1 AA Accessibility with WebGL Backgrounds
Cosmic luxury aesthetics must never compromise accessibility: 1. **Reduced Motion**: Respect `prefers-reduced-motion: reduce`. When detected, freeze all particle velocity vectors and disable cursor parallax:
@media (prefers-reduced-motion: reduce) {
canvas { pointer-events: none; }
}
- **Text Contrast Compliance**: Foreground prose text retains a contrast ratio $\ge 7.2:1$ against the deep-space `#060713` canvas background.
- **ARIA Canvas Attributes**: All WebGL canvas elements are explicitly marked with `aria-hidden="true"` so screen readers bypass decorative visual meshes.

