Shaders are not magic—they are simply mathematical functions executed in parallel across thousands of GPU cores. Mastering vertex and fragment shader mathematics allows technical artists to create breathtaking water ripples, forcefields, dissolving shields, and stylized post-processing effects with near-zero CPU overhead.
1. The Graphics Pipeline: Vertex vs Fragment
- Vertex Shader: Executes once per mesh vertex. Modifies 3D positions in space (used for wave displacement, cloth flutter, and foliage bending).
- Rasterization: The GPU interpolates geometric vertices into screen pixels (fragments).
- Fragment (Pixel) Shader: Executes once per visible screen pixel. Computes final color, specular highlights, and opacity.
2. Writing a Water Vertex Wave Shader (Godot Shader Language)
shader_type spatial;
uniform float wave_speed : hint_range(0.1, 5.0) = 1.5;
uniform float wave_height : hint_range(0.0, 2.0) = 0.3;
uniform float wave_frequency : hint_range(0.1, 10.0) = 2.0;
void vertex() {
// Calculate wave displacement along the Y-axis using sinusoidal UV math
float wave = sin((VERTEX.x * wave_frequency) + (TIME * wave_speed))
* cos((VERTEX.z * wave_frequency) + (TIME * wave_speed));
VERTEX.y += wave * wave_height;
// Recalculate normal vectors so lighting aligns with displaced surface
NORMAL = normalize(vec3(
-cos((VERTEX.x * wave_frequency) + (TIME * wave_speed)) * wave_height,
1.0,
-sin((VERTEX.z * wave_frequency) + (TIME * wave_speed)) * wave_height
));
}
void fragment() {
ALBEDO = vec3(0.05, 0.35, 0.65); // Deep ocean cyan
ROUGHNESS = 0.1; // Mirror-like reflections
METALLIC = 0.0;
}