Hello, Alchemists,
Making things look good takes work...
To illustrate the challenges we are facing, with Codex's help we are going to explain all the techniques we have been using to create a realistic, optimized sea. Here they are:
Water Rendering Techniques in Unity (Built-in RP) — Master of Cladia
- Subdivided Mesh with Shoreline Vertex Colors
The problem: A quad with 4 vertices does not support vertex displacement. The solution: A grid with 128×128 segments (16,641 vertices). During mesh generation, terrain.SampleHeight is called for each vertex to calculate a shore value (0=open sea, 1=shoreline), stored in the vertex color's R channel. This data reaches the shader for free and allows any effect to be modulated by area.
- UV Scroll → UV Wobble
The problem: Classic linear scrolling (uv = speed × time) always moves the texture in the same direction → the eye interprets it as a water current. The solution: Replace it with sinusoidal oscillation (uv = sin(time × freq) × amplitude). The texture moves back and forth without advancing → no current effect.
- Omnidirectional Waves (Sum of Sines)
The problem: Two directional sine waves create wobble with a dominant direction. The solution: Add 4 sine waves at 0°, 45°, 90° and 135°, with slightly different frequencies. The directions cancel each other out → the water rises and falls locally without flowing → a calm lake effect.

- Gerstner Waves
The industry standard for realistic waves. The key difference from simple sine waves:
- Simple sine: vertices move only along Y → it looks like a vibrating floor
- Gerstner: vertices move along XYZ → sharp crests and broad troughs, like real water
Formula for each wave:
float f = k * dot(dir, posXZ) - t;
offset.x = Q * a * dir.x * cos(f); // horizontal displacement
offset.z = Q * a * dir.y * cos(f);
offset.y = a * sin(f); // vertical displacement
Q (steepness) controls how sharp the crest is. 4 waves at different angles and frequencies create a convincing omnidirectional pattern. Performance: practically identical to simple sine waves.
- Shoreline Alpha Fade
The problem: The water mesh intersects the terrain along a hard, artificial line. The solution: Use the shore vertex color in the fragment shader with smoothstep to gradually reduce alpha near the shoreline. The water becomes transparent where it meets the beach, making the line disappear. The depth test, with opaque terrain written to the Z-buffer first, correctly discards pixels over land at no extra cost.
col.a = _Color.a * (1.0 - smoothstep(_ShoreFade, 1.0, i.shore));
- Normal Map and Custom Lighting
Instead of relying on scene lights (_LightColor0 may be black), the shader includes its own sun direction, configurable in the material.
- Half-Lambert for soft diffuse lighting without completely black areas: dot(N,L) * 0.5 0.5
- Normalized Blinn-Phong for specular lighting: the (n 8)/8π factor ensures that peak brightness remains constant regardless of Shininess. Without normalization, raising the slider makes the reflection disappear instead of simply reducing its size.
float normFactor = (_Shininess 8.0) / 25.13274;
float spec = pow(NdotH, _Shininess) * normFactor * _SpecularStr;

- Perlin Noise Texture Generator (in the Unity Editor)
A C# script that generates two tileable PNG files in the Editor through [ContextMenu]:
- Diffuse: multi-octave noise with adjustable contrast
- Normal map: calculated using central differences from the heightmap, with wrapping to maintain seamless tiling
The trick to perfect tiling is to map the UV coordinates onto a 4D torus before sampling Perlin noise:
float nx = Mathf.Cos(u * PI2) * freq;
float ny = Mathf.Sin(u * PI2) * freq;
float nz = Mathf.Cos(v * PI2) * freq;
float nw = Mathf.Sin(v * PI2) * freq;
sample = (PerlinNoise(nx, nz) PerlinNoise(ny, nw)) * 0.5f;
Discarded Techniques and Why
|
Technique |
Why It Did Not Work |
|
Depth map using SampleHeight |
The surface area was hidden by the terrain |
|
Depth map using hexagonal distance |
Coordinate offset bug (terrain at Z≠0), with a lot of time lost |
|
Foam using a shader texture |
The visual result was not convincing |
|
Animation of a "wave moving toward the shore" |
Several attempts at formulas, none convincing |
|
Plains1_h.psd as a water texture |
It was a heightmap with topographic contour lines, which appeared in the water |
Lessons Learned
- Unity Built-in Shaders: no non-ASCII characters in the code. The → and — symbols, and accented characters inside a CGPROGRAM block, cause parse errors
- Unnormalized Blinn-Phong: has a ridiculously narrow useful Shininess range (~4–10). With normalization, the range expands to 8–128
- Wave Amplitude versus Camera Height: an 8cm wave is invisible from 50m up. A strategy camera needs 1–3m of amplitude
- Normal maps inherit patterns from the source texture. Do not use terrain heightmaps as the basis for water normal maps
- Mobile Performance: the real bottleneck is fill rate (transparent pixels), not the vertex shader. Reducing subdivisionesAgua from 128 to 64 cuts the vertex count to 1/4 with no noticeable visual loss

And of course, here are plenty of images we have captured during all our attempts at making a "beautiful sea".
Fancy trying to make a beautiful sea yourselves? Send us pictures of your attempts. 😉
Comments (1)
"Os vamos a explicar, con la ayuda de Codex" oye al menos es sincero
Leave a comment