This is a little fluid dynamics demo that I wrote back in 2012–2014. The demo simulates water flows across a 3-D landscape. The water surface is modelled as a 2-D heightfield using the "shallow water equations". The calculations are done on the GPU and the simulation runs in real time for reasonable grid sizes.
The intention of this work was to investigate the feasibility of using the shallow water equations within the context of physics simulation in videogames. Therefore, emphasis has been placed on computational efficiency and the graphical appearance of the results, and not so much on strict physical accuracy.
Click here to download the demo zip file (2.1 M)
The demo should run on any reasonably modern Windows PC. (It can also be run in Linux using WINE.)
The source code (2.3 M) is also available.
The basic idea is behind the shallow water equations is that we take a vertical average of the equations of fluid dynamics (the Navier-Stokes equations). This leads to a new set of equations which describe the fluid depth and velocity as functions of x and y (the horizontal directions) only.
The advantage of doing this is that the computational cost of solving the equations is reduced (compared to a full 3-D simulation). The flip side is that the results are less realistic, since a 2-D "height field" approach (which is essentially what this is) fundamentally cannot represent certain features, such as breaking waves, complex splash/spray patterns, etc.
The equations themselves are well known and can be found in Acheson 1990 (see references below) or on Wikipedia for example.
We use the method of Kurganov and Petrova 2007 (see references below) to solve the equations. The calculation is done on the GPU in three passes (per simulation timestep) using custom pixel shaders (implemented in HLSL). It runs in real time for reasonable mesh sizes.
The rendering has been kept fairly basic. The water and terrain are rendered as simple height field meshes. The water shader takes into account attenuation (deeper water appears darker), reflection (but only the sky and clouds are reflected, not the terrain), refraction (in an approximate way), and the Fresnel effect.
It would probably look better if the landscape was reflected in the water, better textures were used, a few trees and bushes were added, etc.
What can we conclude from this experiment? Is using the shallow water equations a reasonable way to simulate water flows in games? I'm not sure. The demo shows that we can do a reasonably good simulation on the GPU, in something approaching real-time, at least if the grid resolution is kept fairly small. The problems are (a) one needs a relatively high grid resolution to get realistic looking results (and this means the simulation can't necessarily run in real time any more), and (b) you can sometimes get weird "spiky" looking artifacts in the water surface (I presume this is because of the shallow water approximation breaking down, once the water depth exceeds a certain amount).
I suspect, therefore, for real applications, it might be better to use either a linear approximation (for simulating waves on ocean or lake surfaces for example), or some kind of full 3D simulation (although that would be expensive in terms of computing time, of course).
An example of a 3D simulation, using smoothed particle hydrodynamics (SPH), can be found towards the end of this video by Sebastian Lague.
I suppose it might still be possible to use the shallow water equations, with a relatively low-resolution grid, to simulate the large scale flow of water over a landscape (i.e. to decide roughly where the rivers and lakes should be placed on some kind of open world landscape) and then use graphical tricks (textures, shaders, particles etc) to actually render those rivers and lakes in a more realistic way. That might be an interesting experiment to try in future.
I'm not sure which games actually include water physics simulation these days. Apparently Red Dead Redemption 2 includes some fairly realistic water simulation but I do not know what methods they use to achieve this.
D. J. Acheson, "Elementary Fluid Dynamics", Oxford University Press, 1990.
A. Kurganov and G. Petrova, "A Second-Order Well-Balanced Positivity Preserving Central-Upwind Scheme for the Saint-Venant System", Commun. Math. Sci., Vol. 5, No. 1, pp. 133-160, 2007. Available online at https://projecteuclid.org/journals/communications-in-mathematical-sciences/volume-5/issue-1/A-Second-Order-Well-Balanced-Positivity-Preserving-Central-Upwind-Scheme/cms/1175797625.full.
The source for the "grass" texture used in the demo is the Terrain Texture Pack created by Virtually Infinite Systems (unfortunately no longer available online it seems).
The skybox texture was created using Terragen (classic edition, non-commercial licence).
The Guichan open source library was used to create the sliders and other GUI elements visible in the demo.