# 11. Custom GLSL shader, explained

**Goal:** write your own vertex and fragment shaders with `ShaderMaterial`: displace vertices with a moving wave and color the surface from that wave.

## The GPU pipeline in one table

| Stage | Runs | Inputs | Outputs |
| --- | --- | --- | --- |
| Vertex shader | Once per vertex | Attributes (`position`, `normal`, `uv`), uniforms | `gl_Position` (where the vertex lands on screen), varyings |
| Rasterization | Fixed by the GPU | Triangles | Pixels (fragments), with varyings interpolated across the triangle |
| Fragment shader | Once per pixel | Varyings, uniforms, textures | `gl_FragColor` (the pixel color) |

- **Uniforms**: the same value for every vertex/pixel in a draw call (time, colors, textures). Set from JavaScript.
- **Attributes**: per-vertex data from the geometry.
- **Varyings**: values the vertex shader passes to the fragment shader, interpolated in between.

## Walkthrough

```js
const uniforms = { uTime: { value: 0 }, uColorA: { value: new THREE.Color(0x1e2a78) }, uColorB: { value: new THREE.Color(0xff6f61) } };
```
Each uniform is an object with a `value`. Change `uniforms.uTime.value` each frame and three.js uploads it before drawing. The `u` prefix is just a naming convention.

```glsl
float wave = sin(position.y * 6.0 + uTime * 2.0) * 0.08 + sin(position.x * 5.0 + uTime * 1.3) * 0.05;
vec3 displaced = position + normal * wave;
```
Moves each vertex outward along its normal by a sum of two moving sine waves. Doing this on the GPU costs almost nothing even for hundreds of thousands of vertices, unlike the CPU approach in example 05. GLSL is strict about types: write `6.0`, not `6`, for floats.

```glsl
gl_Position = projectionMatrix * modelViewMatrix * vec4(displaced, 1.0);
```
The standard transform: object space to camera space (`modelViewMatrix`) to clip space (`projectionMatrix`). `ShaderMaterial` declares these uniforms and the `position`, `normal` and `uv` attributes for you. With `RawShaderMaterial` you declare everything yourself.

```glsl
float t = smoothstep(-0.12, 0.12, vWave);
vec3 color = mix(uColorA, uColorB, t);
```
`smoothstep` maps the wave height into a smooth 0..1 range; `mix` blends the two colors.

```glsl
float rim = pow(1.0 - abs(vNormal.z), 2.0);
```
In camera space, `normal.z` is near 1 where the surface faces the camera and near 0 at the silhouette. This brightens the edges, a cheap "rim light".

```glsl
#include <colorspace_fragment>
```
Converts the final color to the renderer's output color space, so your colors match the rest of three.js. three.js expands `#include <...>` chunks before compiling.

```js
new THREE.IcosahedronGeometry(1, 64)
```
A sphere-like shape with evenly distributed vertices. High detail (64) gives the wave enough vertices to look smooth.

## ShaderMaterial vs alternatives

| Option | When to use |
| --- | --- |
| `ShaderMaterial` | Full control, custom look, effects that ignore three.js lighting |
| `material.onBeforeCompile` | Small tweaks to a built-in material (keep its lighting, add a wave) |
| TSL (three.js Shading Language) with `WebGPURenderer` | Node-based shaders written in JavaScript that compile to WGSL or GLSL; the direction three.js is heading |
| Post-processing pass | Effects on the whole screen (example 12) |

## Debugging shaders

- Compile errors appear in the browser console with the line number of the generated shader.
- Output intermediate values as colors: `gl_FragColor = vec4(vec3(vWave * 5.0 + 0.5), 1.0);`.
- Check for NaN: dividing by zero or `pow` of a negative base gives black or flickering pixels.

## Try this

1. Add a `uMouse` uniform and make the waves react to the pointer.
2. Replace the gradient with a texture: add `uniform sampler2D uMap;` and a `uv` varying.
3. Rebuild the effect with `onBeforeCompile` on a `MeshStandardMaterial` to keep realistic lighting.
