Write a Wave Shader

Three.js lessons
/

Write a Wave Shader

Advanced

Shaders & effects

Lesson 14 of 15

Displace vertices on the GPU and pass a height value to a fragment shader.

Animate a subdivided plane with ShaderMaterial, a time uniform, and a height-based color gradient.

How this scene works

A vertex shader runs once per vertex, so the plane needs subdivisions for smooth displacement. uTime is a uniform shared by all vertices for a frame. vHeight is interpolated across each triangle and used in the fragment shader to blend two linear-space colors. The material does not respond to scene lights because this shader implements its own color calculation. Tone-mapping and color-space shader chunks convert the final output correctly. A padded bounding sphere prevents culling from ignoring the GPU displacement.

Write a Wave Shader

Ready to run

Helper snippet · Three.js 0.183.1 · WebGL

Loading draft

1 files · local only

Ctrl/Cmd+Enter: run · Ctrl/Cmd+F: find · Tab: leave editor
Run your code to start the scene.
Drag to orbit, scroll to zoom, right-drag to pan.
Drag: orbit · Scroll: zoom · Right-drag: pan

Make it your own

Double the wave frequency and lower the amplitude to 0.2. Then reduce the plane subdivisions from 100 to 10 to see why a shader cannot bend triangles that do not exist.

Change one thing, run the scene, and compare. Pause animation to inspect the result; reset the example to return to its original code and files.

FAQ