In this lesson, you will learn how to create realistic water surfaces by calculating screen-space refraction and fading transparency based on the distance between the water surface and submerged objects.
The Physics of Depth-Based Transparency
To make water feel voluminous rather than like a flat sticker, we use the Scene Depth node. By subtracting the Screen Position (scene depth) from the current Fragment Position (the surface depth), we calculate the thickness of the water at any given pixel.
We then pass this "Depth Difference" through a Saturate and Power node to create a linear gradient. This allows us to map the water’s alpha—making it fully transparent at the surface and increasingly opaque as the depth increases, effectively mimicking light absorption in deep water.
Implementing Screen-Space Refraction
Refraction occurs when light bends as it enters a denser medium. In Shader Graph, we simulate this by distorting the Screen Color sample.
- Calculate Distortion: Use the water’s
Normal Mapto create a vector. - Offset UVs: Add this normal-based offset to the
Screen Positioncoordinates used to sample theScene Colorbuffer. - Control Strength: Multiply your normal offset by a
RefractionStrengthproperty so the bending intensity is adjustable.
The final setup looks like this:
// Conceptual logic for Refraction UVs
float2 refractionOffset = normalVector.xy * RefractionStrength * depthAlpha;
float2 screenUV = (screenPos.xy / screenPos.w) + refractionOffset;
float3 refractedColor = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, screenUV);
By multiplying the refractionOffset by the depthAlpha we calculated earlier, you ensure that refraction only happens near the surface where objects are "near" the water, preventing the distortion from looking unnatural at great depths. When combined, the depth fade provides the "murkiness," while the refraction gives the surface that characteristic liquid shimmer.
Key Takeaway: By using the Scene Depth buffer to modulate both your alpha channel and your refraction UV offsets, you create a dynamic, volumetric water surface that responds realistically to submerged geometry.