Mathematical Explanation of the Heightfield Ocean Shader
Mathematical Explanation of the Heightfield Ocean Shader
1. Definition of the Ocean Surface (Heightfield)
The water surface is defined as a scalar function on the x-z plane:
In this shader, several sine waves are superimposed:
where Ai is the amplitude, ki the wave vector, and ωi the time-dependent frequency.
2. Ray Parametric Equation
With camera position r0 and ray direction d, the ray is:
The y-component is:
3. Intersection Condition
The intersection occurs when:
Equivalently, solving the root-finding equation:
4. Numerical Root-Finding
- Bracket search: Step forward exponentially until f(t) changes sign, i.e. f(ta)·f(tb) < 0.
- Hybrid Secant + Bisection:
tn+1 = tb − f(tb) (tb − ta) / (f(tb) − f(ta))
If unstable, fallback to bisection:
tn+1 = (ta + tb) / 2
The converged solution t* gives the intersection with the water surface.
5. Surface Normal
The gradient of the heightfield gives the normal:
Normalized:
6. Shading Model
- Diffuse: Ldiff ∝ max( n·l, 0 )
- Specular (Blinn-Phong): Lspec ∝ (max(n·h,0))p
- Fresnel (Schlick): F(θ) = F0 + (1 − F0)(1 − cosθ)5
- Reflection: r = d − 2(n·d)n
- Fog: C = e−α t*Cwater + (1−e−α t*)Csky
In summary: The shader formulates the water intersection as a 1D root-finding problem f(t)=0, solves it numerically, computes the surface normal, and then applies a physically motivated shading model combining diffuse, specular, Fresnel reflection, foam, and distance fog.
Comments
Post a Comment