Ocean Simulation- or, Thank Goodness I Took Computer Graphics




OceanConstructor
Developer controlled settings


As I dove into ocean simulation, I remembered a joke we used to tell in undergrad computer science classes.  “We’re just coming up with increasingly complicated ways of being lazy.”  To avoid the tedium of manually animating water, I coded a model based on Gerstner waves.  Detailed in Mark Finch’s Nvidia’s GPU Gems chapter (linked here and below), Gerstner waves determine the horizontal and vertical shift of a point in space as a wave passes.  Summing the displacement of several varied waves produces a naturalistic undulation.

My current system in the Unreal Engine consists of three elements—a Blueprints ‘OceanConstructor’, a shader, and a C++ ‘OceanManager.’

The OceanConstructor class generates a dynamic material instance of the ocean shader, allowing for definition of parameters at runtime.  The developer chooses the number of waves to sum, the general wind direction, wave steepness, variation, and color of the ocean.  The Beaufort Scale setting determines wavelengths, speeds, and amplitudes based on an intensity scale from 1 to 12.

The shader, applied to a tessellated plane, uses a material function to determine and combine the physical displacement and normals produces by Equations 9 and 12 in the paper.  Panning normal maps add fine scale detail, and a simple Fresnel determines the color and influences the specularity of the water.

Gerstner Wave Material Function
It looks a lot nicer in C++.

The C++ ‘OceanManager’ class does the heavy lifting in terms of math.  It generates and stores specific parameters for each Gerstner wave based on the OceanConstructor’s general parameters.  It can calculate the height of a wave at any point in space, which the buoyancy model will later use to determine if an object is above or below water.

Wave Height Calculator
See?  So much nicer in C++.
There are plenty of tweaks and additional features I can return to in the future.  Adding more waves while optimizing the performance will create a more realistic appearance and reduce the effects of tiling.   Sea foam would add visual atmosphere.  I will additionally consider how to contain the water, whether in a more limited landscape or as an infinite ocean that follows the player.

Overall, I am quite happy with the results.  Despite being one of the hardest courses I had during undergrad, thank goodness I took Computer Graphics!

GPU Gems Paper: https://developer.nvidia.com/gpugems/GPUGems/gpugems_ch01.html

Comments

Popular posts from this blog

Topology - A Continuous Learning Experience

Screenshot Friday! March 2nd