Mathematical Explanation of the Heightfield Ocean Shader

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:

y = h(x,z,t)

In this shader, several sine waves are superimposed:

h(x,z,t) = Σ Ai · sin( ki · (x,z) + ωi t )

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:

r(t) = r0 + td,   t ≥ 0

The y-component is:

yray(t) = r0y + t dy

3. Intersection Condition

The intersection occurs when:

yray(t) = h(xray(t), zray(t), tglobal)

Equivalently, solving the root-finding equation:

f(t) = (r0y + t dy) − h(r0x+t dx, r0z+t dz, tglobal) = 0

4. Numerical Root-Finding

  1. Bracket search: Step forward exponentially until f(t) changes sign, i.e. f(ta)·f(tb) < 0.
  2. 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:

∇h(x,z) = ( ∂h/∂x,   1,   ∂h/∂z )

Normalized:

n = (−hx, 1, −hz) / √(hx2 + hz2 + 1)

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

Popular posts from this blog

Japan Jazz Anthology Select: Jazz of the SP Era

In practice, the most workable approach is to measure a composite “civility score” built from multiple indicators.