AUDIO DRIVEN
SPHERE PARTICLES
Last updated
Colorful 3D spheres that bounce at different heights in real-time response to music using Three.js AudioAnalyser.
Quick Answer
To make 3D objects react to music in Three.js, attach an AudioListener to the camera, play the song through THREE.Audio or PositionalAudio after a click, and wrap it in new THREE.AudioAnalyser(sound, 128). Every frame, call getFrequencyData(), average the bins into a 0 to 1 loudness value, and multiply each object's bounce, scale or glow by it.
LIVE DEMO
Sign in with Google, press Play and the page clears so you can watch 500 spheres bounce to the music. Drag to orbit, Esc to exit.
Frequently Asked Questions
How do you create audio-reactive 3D spheres in Three.js?
Create an array of SphereGeometry meshes positioned on a grid. Connect audio to THREE.AudioAnalyser. Each frame, read frequency data and map values to each sphere's Y position (bounce height). Apply sine waves with per-sphere phase offsets for organic movement that follows the music.
How does Three.js AudioAnalyser map music to 3D objects?
AudioAnalyser performs FFT on the audio stream and returns frequency amplitudes as numbers 0-255. You assign different frequency bins to different spheres — low frequencies drive some spheres, high frequencies drive others. Normalize the values and use them as scale multipliers or position offsets in the render loop.
Can you build a music visualizer in the browser with Three.js?
Yes. Three.js has built-in audio support through AudioListener, Audio, and AudioAnalyser classes that wrap the Web Audio API. Combined with 3D geometry and real-time rendering, you can build full music visualizers that run at 60fps in any modern browser without plugins or downloads.
How do you give each sphere a unique bounce pattern?
Assign each sphere a unique phase offset, amplitude multiplier, and speed value when creating it. In the animation loop, calculate bounce height as: baseHeight + sin(time * speed + phase) * amplitude * frequencyValue. This makes every sphere react to the same music data but with individual timing and intensity.
Why does my Three.js audio not play until I click?
Browsers block sound until the visitor interacts with the page (the autoplay policy). Create the AudioListener and call sound.play() inside a click or tap handler, like the Play button in this tutorial, and the AudioContext starts normally.
What fftSize should I use with THREE.AudioAnalyser?
fftSize must be a power of two between 32 and 32768. The analyser returns fftSize / 2 frequency bins, so 128 gives 64 bins. Small values like 64 or 128 are fast and great for a bouncing effect; larger values like 1024 give finer detail for bar-style equalizers.
How many spheres can I animate before it slows down?
500 separate meshes run well on most desktops and recent phones. For thousands, share one SphereGeometry, lower the segment count, or switch to InstancedMesh so the whole field draws in a single call.
Introduction
Audio visualization is one of the most satisfying things to build in 3D. This tutorial creates 500 colorful spheres that bounce in real-time to music using Three.js's built-in AudioAnalyser. Each sphere has its own unique bounce height, speed, and phase — creating an organic, living particle field that reacts to every beat and frequency in the audio.
The technique combines Three.js's audio system (AudioListener, PositionalAudio, AudioAnalyser) with simple sine-wave animation driven by frequency data. The result is a visually stunning effect that's surprisingly simple to implement — under 100 lines of core logic.
Watch: Three.js Helmet HUD: FPS Visor Overlay (Indie Forge Studio) · Play Super Soldier Battle and climb the leaderboard
What You'll Learn
- Setting up Three.js AudioListener and PositionalAudio
- Reading real-time frequency data with AudioAnalyser
- Creating hundreds of spheres with unique bounce properties using userData
- Driving animation with sine waves multiplied by audio frequency
- Dynamic color changes using HSL color space and emissive glow
- OrbitControls with auto-rotate for cinematic camera movement
- Fog, shadows, and lighting for a polished visual presentation
Prerequisites
Basic Three.js knowledge (Scene, Camera, Renderer). Understanding of the animation loop (requestAnimationFrame). No audio programming experience needed — Three.js handles all the Web Audio API complexity for you.
How It Works
- Set up audio — An AudioListener on the camera and a PositionalAudio source play the music when you press Play.
- Read the frequencies — An AudioAnalyser reads real-time frequency data from the music every frame.
- Create the spheres — Hundreds of colorful spheres are created, and each stores its own bounce height, speed, and phase in userData.
- Bounce to the beat — Each sphere's height is a sine wave using its own speed and phase, multiplied by the audio frequency, so every sphere moves differently to the same music.
- Shift the colors — Sphere colors change using the HSL color space with an emissive glow.
- Polish the view — OrbitControls with auto-rotate, fog, shadows, and lighting give the scene a cinematic look.
How AudioAnalyser Turns Music Into Numbers
Sound is a wave, but a visualizer needs plain numbers. THREE.AudioAnalyser wraps the Web Audio AnalyserNode, which runs a Fast Fourier Transform on the last slice of audio and splits it into frequency bins, from deep bass on the left to bright treble on the right. With an fftSize of 128 you get 64 bins, and each holds a value from 0 (silent) to 255 (loudest).
This tutorial adds all 64 bins and divides by 64 × 255, which gives a single loudness value between 0 and 1. When the music stops the value drops to 0 and every sphere settles on the grid. If you would rather have the bass drive one group and the high notes drive another, read data[i] for a single bin instead of the average, or call analyser.getAverageFrequency() for a quick 0 to 255 average.
Keeping 500 Spheres Fast
- Cap the pixel ratio.
renderer.setPixelRatio(Math.min(devicePixelRatio, 2))stops 3x phone screens from drawing nine times the pixels. - Lower the segments. A sphere this small looks the same at 16 segments as at 32, with a quarter of the triangles.
- Share geometry. One
SphereGeometryscaled per mesh uses far less memory than 500 separate ones. - Go instanced for thousands.
InstancedMeshdraws every sphere in one call. Set each matrix and color per frame withsetMatrixAtandsetColorAt.
Common Audio Visualizer Mistakes
- Playing on page load. Browsers block audio until a click or tap, so start the sound from a button.
- Adding the listener on every click. Two AudioListeners stack and the sound doubles. Create it once and keep a flag.
- Reading the analyser before the buffer loads. Create the AudioAnalyser inside the AudioLoader callback, after
sound.play(). - Loading audio from another site. The file must allow cross-origin requests (CORS), or the analyser reads only zeros.
- Old script tags.
three.min.jsandexamples/jsare gone from current Three.js. Use ES modules with an import map, as the full code here does.
What To Try Next
- Split the field in two: let the bass bins bounce the left half and the treble bins bounce the right half.
- Scale each sphere with the music as well as moving it, using
mesh.scale.setScalar(1 + freq). - Swap the sound file for a microphone with
navigator.mediaDevices.getUserMediaandsound.setMediaStreamSource. - Turn the spheres into points with Interactive Audio Driven Particle Systems.
- See music and 3D action together in Super Soldier Battle.