ANIMATED SPHERES
& FRAGMENT SHADERS

Last updated

Float a calm cloud of glossy 3D spheres over a slowly rippling GLSL fragment-shader floor in the browser with Three.js — real GPU shader animation, beginner friendly, no extra libraries.

✓ FREE Level 1 · Beginner GLSL fragment shader MeshPhysicalMaterial
See it live

LIVE DEMO

The glossy spheres drifting over the rippling shader floor behind this page are a live Three.js scene. Drag the empty background to orbit it, or launch the full-screen demo to drag to orbit and scroll to zoom. Launch asks for a free Google sign-in first. Press Esc or Exit to return.

Free Google sign-in · Orbit: drag · Zoom: scroll · Esc to exit

Introduction

This free tutorial combines two of the most rewarding Three.js techniques: animating many objects and writing your own GLSL shader. You will float 32 glossy pastel spheres on slow, gently bobbing orbits and lay them over a floor painted by a live fragment shader that ripples softly outward over time.

The shader runs on the GPU, once per pixel, driven by a single time uniform set from a clock each frame. Keeping the numbers next to time small is what makes it calm instead of flashy. It is the same technique behind gradients, water, fire, and glow effects. Copy it, paste it, and it runs.

Frequently Asked Questions

What is a fragment shader in Three.js?

A fragment shader is a small GLSL program that runs on the GPU once for every pixel of a surface, deciding that pixel's final color. In Three.js you supply it through a ShaderMaterial. Because it runs per-pixel on the GPU, you can create smooth animated gradients, patterns, and effects that would be far too slow to compute in JavaScript.

How do you animate a GLSL shader in Three.js?

Pass a uniform such as time into the ShaderMaterial, then set it every frame from a THREE.Clock (material.uniforms.time.value = clock.elapsedTime). Inside the fragment shader, use that time value with sin(), mix() and smoothstep() to move ripples and blend colors. Multiplying time by a small number like 0.15 keeps the animation slow and soothing.

What is the difference between a vertex and a fragment shader?

A vertex shader runs once per vertex and positions the geometry (it outputs gl_Position). A fragment shader runs once per pixel that the geometry covers and outputs the pixel color (gl_FragColor). A ShaderMaterial in Three.js needs both: the vertex shader places the shape, the fragment shader paints it.

How do you make many 3D objects move smoothly in Three.js?

Store a few numbers on each object (orbit radius, speed, bob height, phase) and use real elapsed time from a THREE.Clock to place it each frame with cos(), sin() for the circle and sin() again for a gentle up-and-down bob. Because sin() eases in and out, the motion looks graceful. Reusing one shared SphereGeometry keeps it fast.

Is this Three.js shader tutorial free?

Yes. This is a completely free tutorial. The full source code is on the page and you can copy it. Interacting with the live demo of calm spheres over a rippling shader floor requires a quick, free Google sign-in, but no paid membership is needed.

How It Works

  1. Scene, camera and renderer. We make an empty 3D world, a camera to look at it, and a renderer that draws it.
  2. Write the fragment shader floor. A fragment shader is a tiny program that runs once for every pixel and decides its color.
  3. Add gentle lights. A sky-and-ground light fills everything softly, and one warm light from above gives the spheres a highlight.
  4. Create a calm cloud of glossy spheres. We make 32 spheres from one shared shape, color them from a five-color pastel palette, and give each a glossy clear-coat with a faint glow.
  5. Animate smoothly with real time. A Clock tells us how many seconds passed, so motion is the same speed on every computer.

The detailed code breakdown and the full copy-paste file are below. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.

What To Try Next

  • Try a bigger number in sin(d * 2.2 - time * 0.6) to see tighter, faster rings, then dial it back.
  • Feed the mouse position in as a uniform and let the shader colors react to the cursor.
  • Change the three deep, mid and glow uniform colors to give the floor a sunset or ocean mood.
  • Use InstancedMesh to push the sphere count into the thousands.