No description https://snowbonk.com
  • TypeScript 94.2%
  • CSS 5%
  • HTML 0.4%
  • JavaScript 0.4%
Find a file
Andy Aylward 0daaefa642
perf: optimize realtime render loop with THREE.InterleavedBuffer (#72)
Replaced a manual JS array-copy loop inside the hot render mapping pipeline in RealtimeVisualization.tsx with a native memory copy via TypedArray.set() leveraging THREE.InterleavedBuffer. This avoids O(N) CPU operations and drops render/sync time significantly for high particle counts by correctly mapping the 16-byte aligned vec4 WebGPU data natively into Three.js attributes.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-04-24 04:58:12 +00:00
.github/workflows Drop Monte Carlo and CPU simulation paths, add CI (#53) 2026-04-07 17:25:02 -04:00
.jules perf: optimize realtime render loop with THREE.InterleavedBuffer (#72) 2026-04-24 04:58:12 +00:00
src perf: optimize realtime render loop with THREE.InterleavedBuffer (#72) 2026-04-24 04:58:12 +00:00
.gitignore Fix Barnes-Hut GPU simulation (#58) 2026-04-11 23:15:26 -04:00
eslint.config.js initial 2025-10-09 00:12:15 -04:00
index.html Default to Real-Time tab and simplify UI labels (#66) 2026-04-16 23:23:09 -04:00
package-lock.json Fix O(n^2) octree serialization in GPU Barnes-Hut loop (#57) 2026-04-11 13:48:28 -04:00
package.json Fix O(n^2) octree serialization in GPU Barnes-Hut loop (#57) 2026-04-11 13:48:28 -04:00
README.md docs: add project README (#59) 2026-04-11 23:26:27 -04:00
tsconfig.json Amortize octree rebuild in Barnes-Hut GPU loop (#63) 2026-04-15 21:00:14 -04:00
tsconfig.node.json initial 2025-10-09 00:12:15 -04:00
vite.config.ts initial 2025-10-09 00:12:15 -04:00
vitest.config.ts initial 2025-10-09 00:12:15 -04:00
wrangler.jsonc Update wrangler config name to nbody 2025-10-09 04:13:29 +00:00

N-Body Simulation Viewer

Interactive gravitational N-body simulation running in the browser. Simulates thousands of particles orbiting a central mass, with real-time 3D visualization.

Features

  • WebGPU-accelerated physics — compute shaders handle force calculations on the GPU, with automatic CPU fallback
  • Barnes-Hut algorithm — O(N log N) force approximation via octree spatial decomposition for the real-time GPU mode
  • Symplectic integration — kick-drift-kick leapfrog integrator preserves energy over long simulation runs
  • Two simulation modes:
    • Pre-computed — generates all timesteps up front, then plays back with interpolated frames for smooth animation
    • Real-time — steps physics live each frame on the GPU with adaptive performance monitoring
  • 3D orbit controls — pan, zoom, and rotate the camera around the simulation

Getting Started

npm install
npm run dev

Open http://localhost:5173 in a browser with WebGPU support for GPU acceleration. The simulation falls back to CPU if WebGPU is unavailable.

Usage

Use the top bar to switch between simulation modes. The control panel lets you adjust:

  • Particle count
  • Timestep size (delta T)
  • Number of snapshots (pre-computed mode)
  • Particle render size
  • Playback speed

How It Works

Particles are initialized in a disk around a heavy central body, with velocities set for approximately circular orbits. The simulation computes gravitational forces between all particle pairs (or uses Barnes-Hut approximation) and integrates their trajectories using the leapfrog method.

The particle data is stored in flat Float32Array buffers with a struct-of-arrays layout (8 floats per particle: position xyz, padding, velocity xyz, mass) that maps directly to GPU storage buffers.

Tech Stack

  • React + TypeScript
  • Three.js via React Three Fiber
  • WebGPU (WGSL compute shaders)
  • Zustand for state management
  • Vite + Vitest

Deployment

Static site deployed via Cloudflare Pages (wrangler.jsonc).

npm run build

Tests

npm test