GameCrew AIEarly access
Sign in
Godot 4waterRevision 2 · By @maddmann

Rideable Breaking Surf: Analytic Wave Train, Shoaling, Peeling and Per-Wave Character in Godot 4 Compatibility

Updated

Surf you can ride: a train of breaking waves written as one analytic height function shared by GDScript (the board's physics) and a vertex shader (the water). Waves shoal, steepen and break where they are too tall for the water, peel down the line from a break point, come in uneven sets with their own size and hollowness, and hand over from one to the next without a vertex jumping.

  • surf
  • breaking wave
  • wave train
  • shoaling
  • peeling
  • sets
  • ocean
  • surfing
  • barrel
  • analytic water
  • vertex displacement
  • webgl
The demo of Rideable Breaking Surf: Analytic Wave Train, Shoaling, Peeling and Per-Wave Character in Godot 4 Compatibility, as its check took it

Play the demo

Demo passed on r2: a small game that shows only this technique, built and checked by GameCrew AI's engine.

Play the demo

Rideable Breaking Surf in Godot 4 Compatibility

When to use it

A surfing, bodyboarding or beach game where a player rides a wave, not just looks at one. Gerstner or FFT oceans give you swell; they never stand up, throw a lip and peel down a point. A fluid simulation can, but not in a browser, and gameplay can't query it at 60 Hz.

The approach: the wave is a function, not a mesh. One analytic height function h(x, z) is written twice, once in GDScript (the board samples it for height, normal and slope) and once in a vertex shader (a dense grid is displaced by it). Both are the same maths, so what you see is exactly what you ride. Sets, shoaling, breaking, peeling and hand-over are all changes to that function's parameters over time.

It works in the Compatibility renderer (WebGL 2) with an opaque water material: no depth or screen texture.

The frame: fixed land, moving wave

Work in the wave's frame. x goes across the wave (crest at x = 0, face falling away to +x towards the beach, back to -x); z goes down the line, the way it peels. The land does not move; a WaveFrame node holding the water grid, the surfer and the camera slides by -shore, so frame x = world x + shore. The wave coming in is shore shrinking. The grid only has to be fine around the crest, which never moves in the frame.

# main.gd: everything in the wave's frame moves; the coast does not
func _physics_process(_delta: float) -> void:
	_frame.position.x = -wave.shore

One wave's profile

A breaking face is not a sine. Use a Gaussian back and a power-law face, steepest at the top, with the very crest rounded over so it never reads as a knife edge:

const BACK := 3.2        # back of the wave, in wave heights
const FACE_POWER := 2.2  # (1 - u)^p from the lip (u = 0) to the trough (u = 1)

static func face_width(s: float, h: float, k: Kind) -> float:
	# s: metres ahead of the break point. In the pocket the face is short and steep; it draws out on the shoulder
	return h * lerpf(1.25, 3.2, sstep(0.0, k.wall, s)) * k.wide

static func surface_s(x: float, s: float, z: float, h: float, goal: float, k: Kind) -> float:
	var a := amp(s, z, goal, k) * h
	var w := face_width(s, h, k)
	var y := 0.0
	if x < 0.0:
		var b := BACK * h
		y = a * exp(-(x * x) / (b * b))
	else:
		var u := clampf(x / w, 0.0, 1.0)
		u = u * u / (u + 0.12)          # rounded over at the top
		y = a * pow(1.0 - u, FACE_POWER)
	# behind the break the collapsed wave piles up as a bore of white water, then spreads and dies
	var p := sstep(-2.0, -14.0, s) * (1.0 - 0.7 * sstep(-30.0, -80.0, s)) * channel(z, goal)
	var v := (x - 1.3 * w) / (1.6 * w)
	return y + p * 0.42 * h * exp(-v * v)

s = z - peel_z is the most useful variable in the system: s > 0 is unbroken face (pocket near 0, shoulder far ahead), -tube_len < s < 0 is the barrel, behind that is white water. Height, width, foam, the barrel and the gameplay zones are all functions of s.

amp() lowers the crest along the shoulder and behind the break, and puts lumps along the line so the crest is never ruled level:

static func amp(s: float, z: float, goal: float, k: Kind) -> float:
	var a := lerpf(1.0, 0.55, sstep(4.0, k.wall, s)) if s > 0.0 else lerpf(1.0, 0.5, sstep(0.0, -22.0, s))
	return a * channel(z, goal) * (1.0 + k.lump * sin(z * 0.11 + k.p1) + 0.6 * k.lump * sin(z * 0.37 + k.p2) + k.roll * sin(z * 0.031 + k.p3))

Use a smoothstep that accepts reversed edges (sstep(a, b, x) with a > b); GLSL's own smoothstep is undefined for that, and half the falloffs here go backwards.

A train of waves, shoaling

Draw the reef wave and its neighbours: k = -1 (gone by, heading up the beach), 0 (at the reef), 1 and 2 (coming in), SPACING apart, and sum their heights.

Waves slow as the water shallows, so inshore they bunch up and steepen. Do it by remapping x: work each wave's shape out at a "nominal" x on a sea of even depth.

// distance off the beach d -> where that point would be on an even sea; repeated in GDScript
float slow(float d) {
	if (d >= 45.0) { return d; }
	float dd = max(d, 0.0);
	float t = dd / 45.0;
	return d + 0.8 * (dd - 45.0 * (t * t * t - 0.5 * t * t * t * t)) - 18.0;
}
float nominal(float x) {
	float c0 = 56.0 + shore_x;     // the beach, in the frame
	return c0 - slow(c0 - x);
}
float depth_at(float c) { return 0.5 + 0.09 * max(c, 0.0); }

Depth-limited breaking: a wave breaks where height over depth passes about 0.78. Give it a soft band, and break everything at the reef edge whatever its size, so small waves don't sail through unbroken:

float up = mix(0.3, 1.0, sstep(91.0, 50.0, c)) * sstep(-16.0, 6.0, c);   // how far it has stood up
brk = max(sstep(0.6, 0.85, wave_full * size * up / depth_at(c)), sstep(54.0, 44.0, c));
float broken = brk * sstep(42.0, 32.0, c);   // inside the reef: white water all along

Big waves break further out and small ones further in, for free.

Every wave is its own (Kind)

Identical waves are what makes surf look fake. Give each wave a character from its number, deterministically, so the wave you watched coming in is the wave that arrives:

class Kind:
	var size := 1.0     # of the swell's height
	var hollow := 1.0   # 0 crumbles, 1 throws a wide barrel
	var sect := 0.08    # how much hollowness changes section to section
	var pace := 1.0     # peel speed
	var wall := 45.0    # metres of wall before the shoulder
	var wide := 1.0     # fat or steep face
	var lump := 0.05
	var roll := 0.0
	var p1 := 0.0
	var p2 := 1.3
	var p3 := 0.0
	var early := 0.0    # metres ahead of its even place in the train

static func trait_of(i: int, n: int) -> float:
	var h := (i * 374761393 + n * 668265263) & 0x7fffffff
	h = ((h ^ (h >> 13)) * 1274126177) & 0x7fffffff
	return float((h ^ (h >> 16)) & 0xffff) / 65535.0

static func kind_of(i: int) -> Kind:
	var k := Kind.new()
	if i == 0:
		return k  # the first wave of a session is the classic
	var beat := 0.5 + 0.5 * cos(i * TAU / 7.0)      # sets: about 3 big, 4 small
	k.size = clampf(0.5 + 0.5 * pow(beat, 0.8) + 0.07 * cos(i * TAU / 4.3 + 1.0) + 0.12 * (trait_of(i, 1) - 0.5), 0.45, 1.12)
	var big := clampf((k.size - 0.5) / 0.55, 0.0, 1.0)
	k.hollow = clampf(0.1 + 0.75 * big + 0.5 * (trait_of(i, 2) - 0.4), 0.05, 1.0)  # big ones hit harder
	k.sect = 0.1 + 0.2 * trait_of(i, 3)
	k.pace = 0.84 + 0.2 * k.hollow + 0.3 * (trait_of(i, 4) - 0.5)
	k.wall = 30.0 + 32.0 * trait_of(i, 5)
	k.wide = lerpf(1.3, 0.97, k.hollow) + 0.1 * (trait_of(i, 6) - 0.5)
	k.lump = 0.035 + 0.06 * trait_of(i, 7)
	k.roll = 0.03 + 0.07 * trait_of(i, 8)
	k.p1 = TAU * trait_of(i, 9)
	k.p2 = TAU * trait_of(i, 10)
	k.p3 = TAU * trait_of(i, 11)
	k.early = (trait_of(i, 12) - 0.5) * 14.0
	return k

Hollowness also varies down the line, so one wave has barrelling sections and crumbling ones:

func hollow_at(z: float) -> float:
	var k := kind()
	return clampf(k.hollow + k.sect * (0.65 * sin(z * 0.043 + k.p3) + 0.35 * sin(z * 0.097 + k.p1 + 2.0)), 0.0, 1.0)

Pass the four waves' kinds to the shader as vec3 uniform arrays (uniform vec3 kind_a[4]; holding size, wall, wide, and so on), set only when the wave index changes. A PackedVector3Array fills them.

Peeling, ridden or not

A breaking wave peels from a break point that moves down the line at the wave's pace, faster and slower in sections:

func peel_speed() -> float:
	var k := kind()
	var base := 10.5 * k.pace
	return base * (1.0 + 0.16 * sin(peel_z * 0.021 + 1.0 + k.p1) + 0.07 * sin(peel_z * 0.057 + k.p2 - 1.3)) * (1.0 + rush)

The trap: if only the ridden wave peels, every other wave is frozen or breaks all at once. Give every wave its own break point. The one that has gone by keeps peeling from where it got to; the next one in starts up the line from the player and moves towards them as it stands up at the reef, stopping short of the player while they wait, so the open face is where they are when it arrives:

func _peel(pz: float, crest: float, speed: float, free: bool, delta: float) -> float:
	var step := speed * sstep(76.0, 52.0, crest) * delta   # only once it is standing up at the reef
	if free:
		return pz + step
	if crest >= 76.0:
		return focus_z - 34.0                              # out to sea: kept up the line from the player
	return maxf(pz, minf(pz + step, focus_z - 14.0))       # never backwards, never onto the player

Up the line from the break point the wave has only collapsed as far as it is breaking at all. Scale negative s by the breaking amount so a wave stands walled up before it goes, instead of being white water before it reaches the reef:

float sp = z - peel;
sp = sp < 0.0 ? sp * brk : sp;
s = mix(mix(40.0, sp, sstep(76.0, 52.0, c)), -40.0, broken);

Handing over without a jump

When the reef wave has gone by, the train steps back one wave: everything moves out by the gap to the next wave and the next one becomes wave 0. Done naively, the passing wave pops into a different shape and the beach water level snaps. The rule that fixes it: every wave of the train is drawn with exactly the formula wave 0 uses, and the one that has gone keeps the state it had:

func hand_over(by: float) -> void:
	gone_broken = ride_w > 0.3        # a ridden wave goes on in as white water
	gone_peel_z = peel_z              # and keeps peeling from where it was
	gone_ride = ride_w                # eases out of its ridden shape over about 2 s
	gone_rise = rise
	index += 1
	shore += by
	peel_z = next_peel_z              # the new one breaks where it was already going to
	next_peel_z = focus_z - 34.0
	ride_w = 0.0

Waves don't arrive evenly spaced: offset each by k.early, and step by SPACING - (early(1) - early(0)), not a constant.

Hand over only when the reef wave has no lip left to lose: broken right through inside the reef, or still so far out it hasn't begun to break. In between it is throwing, and stepping then is visible.

The wave *before* the one that has gone by is still draining off the sand: keep its lace and wash (one more vec2 uniform: distance off the beach, size) or the shallows pop clean at each hand-over.

The ridden shape eases in

When the player catches a wave its shape switches from "standing up at the reef" to "peeling from the break point". Ease that with a weight ride_w over about 1.4 s (s = mix(s, z - peel_z, ride_w)), and test it: the height under the take-off must not jump by more than a few centimetres in the frame the player stands.

The water must meet the sand at one line

A dense grid near the player plus a flat sea plane to the horizon gives a seam. Ease the grid down to the plane's level over its last few metres and close in to the beach, with the same function in GDScript so the player floats on it, and discard the plane where the grid is:

float sink(vec2 p) {
	float off = 56.0 + shore_x + shore_bend(p.y) - p.x;
	float inshore = 1.0 - sstep(4.0, 16.0, off);
	float mx = max(sstep(86.0, 98.0, p.x), sstep(-126.0, -138.0, p.x));
	float mz = max(sstep(focus_z + 150.0, focus_z + 168.0, p.y), sstep(focus_z - 90.0, focus_z - 108.0, p.y));
	return max(inshore, max(mx, mz));
}
// vertex(): y -= 0.3 * sink(wp.xz);

Put grid columns exactly where the sink reaches full, or the last quad still slopes and a sliver of seam shows.

The grid

Rows one metre apart down the line; columns fine where the faces are: 0.1 m across the reef wave's crest, 0.5 m down its face, 1 m across the whole band the other crests can occupy (unevenly spaced waves move by up to 7 m either way). Rows snap in whole metres with the player (position.z = floor(focus / ROW) * ROW) so nothing swims. Height per vertex; the normal by central differences of the same function.

Performance

  • The shader evaluates four waves per sample and the normal needs five samples per vertex: keep the grid near 60k vertices for phones.
  • The fragment shader loops the four waves again for foam. Keep it branch-light: pow, exp, value noise.
  • Scale render resolution down when the frame rate drops (get_viewport().scaling_3d_scale), but not under a software renderer, which is slow whatever you do.

How to check it

  • Unit tests on the GDScript copy: a crest, a face, flat water either side, no step at x = 0, the pocket steeper than the shoulder, each Kind still a sane wave, kind_of(i) the same every call.
  • A play test that sits the player outside while four waves go by, recording the worst per-frame change of the water height under them and of the distance to the beach: it must stay under a few centimetres, and the waves must not all be one size.
  • Capture views that pin a wave number and a moment (a "wave" and "shore" on the view), so pictures show a set wave, a soft one, white water and the swell, not only the first wave.

Official references

  • Godot 4 Spatial Shader Reference (vertex(), varyings, uniform arrays)
  • Robert G. Dean and Robert A. Dalrymple, Water Wave Mechanics for Engineers and Scientists (shoaling and the breaker criterion)
  • Battjes, Surf Similarity (Coastal Engineering 1974): spilling versus plunging breakers
  • Jerry Tessendorf, Simulating Ocean Water (SIGGRAPH 2001 course notes)
  • Fournier and Reeves, A Simple Model of Ocean Waves (SIGGRAPH 1986)

Who made it

This guide is by @maddmann.

More Godot 4 games

Games made with GameCrew on the same engine. No game has been built with this guide yet: these show what Godot 4 games here look like.

Fernlight — Raise a carved whistle to your lips in a fog-drowned forest, coax a glowing bird onto your open hand, and point to send it off to light the dark.
Plays in VR

Fernlight

Raise a carved whistle to your lips in a fog-drowned forest, coax a glowing bird onto your open hand, and point to send it off to light the dark.

More Godot 4 techniques

water

Stylized Water, Gerstner Waves & Depth Shoreline Foam in Godot 4 Compatibility →

Godot 4.7 Compatibility spatial water shader using analytical Gerstner vertex waves, hint_depth_texture shoreline foam, Beer-Lambert depth absorption, and Fresnel horizon sheen.

physics

Arcade Raycast Vehicle Physics, Enter/Exit Driving & Modular City RP Loop in Godot 4 →

Crisp 4-wheel raycast spring-damper vehicle physics on RigidBody3D in Godot 4 with controllable drifting, seamless CharacterBody3D enter/exit driving handoff, and MultiMeshInstance3D modular city/traffic loops that hold 60 fps in WebGL.

shaders

Cel-Shading, Shadow Ramps & Width-Compensated Outlines in Godot 4 Compatibility →

Godot 4.7 Compatibility cel-shading spatial shader using custom light() shadow ramps, colored shadow tints, sun-gated Fresnel rim highlights, and a next_pass clip-space width-compensated inverted-hull outline.

physics

CharacterBody3D Slopes, Snap & Coyote Jump in Godot 4 →

Crisp Godot 4 CharacterBody3D locomotion combining floor_snap_length, apex hang gravity, jump-cut, coyote time, and zero-arg move_and_slide.

Read via free MCP — or sign in to add techniques & beta test

Use /mcp/library free with no login to query guides, or connect via OAuth at /mcp to contribute technique guides and beta test cloud builds.

Connect via MCP