(small) Ocean Update #7

Nothing major to show off in this update, as nothing has fundamentally changed in the rendering. Instead I’ve been porting the code to use arrays, instead of pre-defined Vectors for the wave functions. As you’ll note in previous blog posts, the wave function properties are defined in 3 Vectors (of the float4/Vector4 style). This was fine for testing, but leaves a lot to be desired if I want to add more Gerstner waves in (as I have to manually add all the code in for each new wave).

So instead, I’ve adjusted the code to use arrays. Alan Zucconi has a blog post that helped here and it served as a head start.

How it now works is that I have a C# script with options for wave properties as an array of Vector4s (xy: direction, z: steepness, w: wavelength). I can now make this list longer/shorter as I wish. I also have a similar array of floats for individual speed (because Vector5s aren’t a thing and I can’t yet be bothered to write my own struct with fancy UI – but that’s how I’d do it “properly”). 

There is a problem with shader arrays, in that they can’t be resized until Unity has restarted. Don’t ask me why Unity does this (not sure if bug or feature), but as a workaround, I added in a final int that allows me to control how many of the waves it actually calculates, i.e.:

  • I size an array of Gerstner Wave Properties to be 10, as I think that’s how much I’ll need as a maximum.
  • For testing, I just want to deal with 2 or 3 – changing this maxWavesToCalc property to 3 means that it only iterates over the first 3 waves, not all 10. I now don’t have to restart Unity every time I resize the array!
  • In the end, I decide 8 is enough, I can leave the spare two properties in the array but unused for now. When I’ve finally settled on an array number, I can manually resize the array to the exact size I need.

Now I’ve got better wave control, I can start making better waves.

Leave a Reply

Your email address will not be published. Required fields are marked *