# 2. Depth Fade with Scene Depth Difference Part of [[Water and Stylized Surfaces]]. Back: [[1. Water Mesh and URP Depth Setup]]. Next: [[3. Shoreline Foam and Noise]]. > [!warning] Code not compiled in Unity > Described from Shader Graph 17.5.0 node docs (see the hub). No `.shadergraph` asset has been compiled in the Editor. The water should be a light, almost clear colour where it meets the bank, and a darker, more opaque colour where the lake bed drops away. That is a **depth fade**: how much water sits between this fragment and the opaque surface behind it. Shader Graph 17.5.0 ships an official node for exactly that comparison. ## 2.1 Scene Depth Difference **Scene Depth Difference** "returns the difference in depth between a world space position and a value from the depth buffer." | Port | What to do | |---|---| | **Scene UV** | Leave the default (normalized fragment screen position). See the **Screen Position** node modes in the 17.5.0 docs if you later offset the UV for refraction and want the depth to follow. | | **Position WS** | Leave the default (the fragment's world-space position). That is the water surface after any vertex displacement from part 4. | | **Out** | The difference. Units depend on **Sampling mode**. | Set **Sampling mode** to **Eye**. Official description: "Returns the distance in meters." That is the unit you want to expose as a "fade distance" on the material. The official sign convention matters: > The distance is **negative** if the depth value from Scene UV is closer to the camera than the depth from Position WS. On a normal shoreline, the lake bed is *farther* than the water surface, so Out is **positive** and is roughly "meters of water along the view ray." If a pier, a floating crate, or a character is *in front of* the water at that pixel, Out goes **negative**. You do not want negative depth to invert the lerp and paint the crate with "deep" colour. Clamp it: **Maximum** (or a comparison) with **0**, then proceed. A **Saturate** after you divide by the fade distance also clamps the lower bound, but only after the divide — still clamp the raw meters first so a large negative value cannot surprise you if you later use the meters for foam in part 3. ## 2.2 Shallow to deep colour On the Blackboard, add: | Property | Type | Suggested default | |---|---|---| | `ShallowColor` | Color | A light teal, alpha ~0.35 | | `DeepColor` | Color | A darker blue, alpha ~0.85 | | `DepthFadeMeters` | Float | `2` | Graph, in the fragment stage: 1. **Scene Depth Difference** (Eye) → **Maximum** with `0`. 2. **Divide** by `DepthFadeMeters`. 3. **Saturate** (official Saturate node: clamps 0..1). 4. **Lerp** from `ShallowColor` to `DeepColor` using that 0..1 as T. 5. Lerp output RGB → **Base Color** on the Lit fragment block. 6. Split the Lerp output's alpha (or lerp two explicit alpha floats if you prefer not to store alpha on the colour properties) → **Alpha**. Smoothness around 0.85–0.95 and Metallic 0 is a reasonable stylized water starting point. You will revise the normal in part 5. You should now see a band of shallow colour along every place an opaque object intersects the plane: banks, pier legs, submerged cubes. If the whole plane is a flat mid-deep colour, Depth Texture is probably off (part 1) or the graph is still Opaque. If the fade *widens* dramatically when you lower the camera, that is the view-ray measurement doing its job: looking grazingly through water makes the ray travel farther. For many stylized games that is acceptable, even desirable (a thicker "halo" of shallows at sunset angles). A strictly vertical depth is a different measurement and is not what this official node returns; this series stays with the documented Eye difference and treats the grazing widening as a known look, not a bug. ## 2.3 Make it a subgraph You will reuse the 0..1 fade in foam (part 3) and maybe in refraction (part 5). Select the Scene Depth Difference → Maximum → Divide → Saturate chain, convert it to a subgraph, and call it `DepthFade`. Output the saturated 0..1 as `Fade` and, if useful, the clamped meters as `Meters`. Part 3 reads `Meters` against a foam distance. Next: [[3. Shoreline Foam and Noise]].