Course contentsShow
← Unity Game Development
Lesson 623 of 8289. Rendering and VisualsFree lesson

Simulating Surface Waves with Gerstner Equations

Implement mathematical wave functions in Shader Graph to create dynamic, rolling water surfaces with realistic displacement patterns.

In this lesson, you will learn how to transition from simple vertical vertex displacement to the realistic, rolling motion of water using Gerstner Equations.

Beyond Simple Sine Waves

While basic vertical displacement moves vertices up and down like a piston, water molecules in nature move in circular orbits. The Gerstner equation simulates this by displacing vertices both vertically and horizontally. As a wave crests, the horizontal displacement pulls vertices toward the peak, creating those characteristic sharp, narrow crests and broad, flat troughs seen in realistic oceans.

The Mathematical Core

To implement this in Shader Graph, you need to apply two simultaneous offsets to your vertex position based on the wave’s Amplitude, Wavelength, and Direction.

For a single Gerstner wave, the position (x,y,z)(x, y, z) is calculated as:

  • Horizontal Offset (xx and zz): Amplitude * Direction * sin(dot(Direction, WorldPosition) * Frequency + (Time * Speed))
  • Vertical Offset (yy): Amplitude * cos(dot(Direction, WorldPosition) * Frequency + (Time * Speed))

Implementation Strategy

In Shader Graph, you will construct a Custom Function Node or a series of math nodes to handle these offsets.

  1. Define Inputs: Create properties for Wave Speed, Steepness, and Direction.
  2. Calculate Phase: Use the Time node multiplied by Speed added to the World Position dot product with your Direction vector.
  3. Apply Displacement: Connect the Sine output to your horizontal axes (X and Z) and the Cosine output to your vertical axis (Y).
  4. Vertex Stage: Ensure your master node is set to Vertex Position output. Because this modifies geometry directly, make sure your mesh has enough subdivisions; a simple flat plane won't show the rolling displacement if it lacks the necessary vertex density.

By offsetting the horizontal position, you stop the wave from looking like a rhythmic pulse and start seeing the "rolling" motion that defines fluid movement. To get that iconic look, you can sum multiple Gerstner wave nodes together with varying directions and frequencies to create complex, chaotic interference patterns.

Key Takeaway: Gerstner equations produce realistic water movement by coupling vertical elevation with horizontal vertex displacement, creating the sharp, circular-orbit motion of natural waves.