Had plans to follow GPU Gems to gain knowledge in Shaders - but my journey came to an abrupt end.
float Wave(x, y, t){
float w = 2 / _Wavelength;
float phase = _Speed * w;
return float(_Amplitude * sin(dot(_Direction.xz, float2(x, y)) * w + t * phase);
}
...
o.vertex.y += Wave(v.vertex.x, v.vertex.z, _Time.y);
Offsetting the vertices was no issue, but then I met partial derivatives to solve the normal for each vertex. Starting at equation 4a to equation 7.
I assumed looking at equation 7 that one didn't have to fully grasp derivatives seeing as the last step
Equation 7: $$\sum(w_i * D_i \cdot x * A_i * cos(D_i \cdot (x, y) * w_i + t * \varphi_i))$$
So I tried to update the normal (well actually color, but just to visualize) using this function;
float3 normal(float x, float y, float t){
float w = 2 / _Wavelength;
float phase = _Speed * w;
return float3(
w * dot(_Direction.xz, float2(x, 0)) * _Amplitude * cos(dot(_Direction.xz, float2(x, y)) * w + t * phase),
1,
w * dot(_Direction.xz, float2(0, y)) * _Amplitude * cos(dot(_Direction.xz, float2(x, y)) * w + t * phase)
)
}
...
o.color.xyz = normalize(normal(v.vertex.x, v.vertex.z, _Time.y));
It does however not seem to display what I would describe as good normals for the wave surface. So obviously I'm doing something wrong.
Ignoring the green channel here using the full code pasted below.
Would expect the colors to blend more smoothly and uniformly. Seeing I only got one wave this far.
What I am wondering is how to get the normals one would expect or if this is the expected result following the math (or rather what I could make of it) in GPU Gems.
Note: I am attemping this in Unity, so I did adapt what I assumed to be Z-up to Y-up from the GPU Gem book.