Back to projects
Unity / Shaders2025

Stylized Water Shader

Feature-rich water shader with tessellation, planar reflections, and Gerstner wave simulation for Unity's Built-in Pipeline.

UnityHLSLShaderLabC#
TessellationPlanar ReflectionGerstner WavesVoronoiCausticsFlow MapsFoamDepth-Based Effects

Overview

A professional stylized water shader demonstrating 10+ advanced graphics programming techniques. Implements physically-based wave displacement, real-time planar reflections, Voronoi caustics, and GPU tessellation for adaptive surface detail.

Key Features

  • Gerstner wave displacement with tangent/binormal calculation
  • Distance-based GPU tessellation (Hull/Domain shaders)
  • Real-time planar reflections via reflection camera
  • Procedural Voronoi-based caustics patterns
  • Flow maps for directional water movement
  • Shore waves with animated foam
  • Underwater fog with depth-based visibility
  • Blinn-Phong specular highlights

Technical Breakdown

How It Works

  1. 1Vertex shader applies Gerstner wave formula: displacement based on wavelength, amplitude, and time
  2. 2Hull shader calculates tessellation factors based on camera distance
  3. 3Domain shader interpolates vertices and reapplies wave displacement
  4. 4Fragment shader composites multi-layer surface: reflections, refractions, foam, caustics
  5. 5GrabPass captures screen for refraction distortion
  6. 6Depth texture samples scene depth for edge foam and underwater fog

Architecture

  • StylizedWater.shader — Main water shader (vertex/hull/domain/fragment)
  • EnableDepthTexture.cs — Camera depth texture setup
  • PlanarReflection.cs — Real-time reflection camera management

Notable Details

  • Hull/Domain shader pipeline enables up to 64x tessellation based on distance
  • Gerstner waves provide realistic ocean motion vs simple sine waves
  • Voronoi algorithm generates procedural caustic patterns without textures
  • Dual-phase UV system for flow maps prevents obvious tiling
  • Requires Shader Model 4.6+ and DirectX 11 for tessellation support

Setup & Usage

Installation

  1. 1Clone repository: git clone https://github.com/SametKaras/stylized-water-shader.git
  2. 2Open project in Unity 2020.3+ (Built-in RP)
  3. 3Open Assets/Scenes/SampleScene.unity
  4. 4Attach EnableDepthTexture.cs to Main Camera
  5. 5Attach PlanarReflection.cs to water plane for reflections

Parameters

NameTypeEffect
Wave AmplitudeRangeHeight of wave displacement
Wave WavelengthRangeDistance between wave peaks
Tessellation FactorRangeSurface subdivision density
Foam ThresholdRangeDepth at which shoreline foam appears
Caustics IntensityRangeStrength of underwater light patterns

Challenges & Learnings

  • Mastered GPU tessellation pipeline (hull/domain shader stages)
  • Implemented physically-based Gerstner wave formula for realistic motion
  • Learned Voronoi procedural generation for caustics without texture lookups
  • Managed reflection camera synchronization with main camera

Outcomes

  • Demonstrates 10+ advanced rendering techniques in a single shader
  • Production-ready quality suitable for stylized game projects
  • Includes detailed BREAKDOWN.md with mathematical foundations