Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Water Simulation - DirectX 11 Competition Entry

Discussion in 'Made With Unity' started by RC-1290, Jan 18, 2013.

  1. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    [Edit] I continued working on this simulation under the name 'Surface Waves'.

    Original Post:

    Hey Everyone,

    The quality of the other entries to the DirectX 11 Competition has really impressed me.
    If you haven't checked them out, you should definitely do that, here are some of my favorites:
    Museum of the Microstar by RUST LTD and co
    Arkhivrag's entry
    Fluidity by smb02dunnal


    Now I would like to share my demo with you as well. I made a water simulation.


    It is based on a Directx 9 water simulation I made in Unity earlier, to learn about 3D rendering and shaders. This one has been completely rebuilt to use Compute Shaders, in stead of hacked together Vertex and Fragment shaders. The availability of the ComputeBuffer in Unity 4 means that it is now possible to use the high precision (floating point) height map data that is used for the terrain and water, with Unity's built in physics. This proof of concept shows an object being carried away by the flow of the water.

    My original plan for this water simulation was to make a tool to make it easier to create realistic terrain with rivers, by implementing several erosion algorithms. (This is still possible, but is currently on hold because I need to focus on being able to buy food :p)

    How it works
    The whole simulation is based on the manipulation of heightmap data. GPU's are pretty fast at handling this simple 2D information (you don't say ;) ), so the simulation itself is very lightweight. The following image shows the texture that is used to store the amount of water that should be moved between texels.

    $DirectCompute%20watersim%20basics%20are%20working.PNG

    This data is then used to update a heightmap of the water surface, and to estimate velocity of the water (for Erosion and rigidbody physics). Then it's a simple matter of creating displacement maps based on the height map. Currently, that happens to be at the exact same resolution as the simulation.

    $Water%20Displacement%20looks%20a%20lot%20cooler%20in%20wireframe.PNG

    The terrain layers are combined and written to a ComputeBuffer instead, to create a regular mesh, so a regular mesh collider can be used. You might think that moving data from the GPU memory to system memory might be slow, but since the regular simulation still has an enormous amount of time left each frame, the first generation DX11 hardware I'm using (HD5770) still has so much time left that you don't notice a difference in framerate when using vsync. The bottleneck currently lies in the naive generation of a mesh collider of 255 by 255 vertices. (This is why the demo takes a few seconds to load).

    So, while the terrain isn't yet update in realtime, the same technique is used to simulation buoyancy of objects. For this demo, I calculate the forces of the water on a single object, and use that data to apply regular PhysX forces. This means that it still interacts normally with any other physics objects. Ideally, the buoyancy system should update a list of these floating objects, and send them all to the GPU to be processed in parallel (right now, the entire GPU is waiting for that one object to be updated ^^).



    The water surface is rendering using standard techniques. A cubemap reflection, refraction of a GrabPass, and the use of a depth texture to add fog and to smooth out the shores. Although the way I handle fog might be slightly unusual, since I don't rely on a gradient texture to blend from shallow to deep water, but instead change how much the fog color contributes to the final result. Regular emissive fog constantly looked too much like ink, or radioactive goo.

    Although that also had its charms:



    Obvious next steps for the simulation would include:
    • Underwater Fog Rendering
    • Interactive terrain editing
    • Render to heightmap, to allow water to flow over other convex objects.
    • Multi layer Erosion
    • Flow Mapping (Animating a traditional uv offset 'flowmap', based on flow in water simulation)
    • Improved buoyancy physics
    • Fake Caustics
    • Foam
    • Forces (like tides)
    • Output from one water simulation to another
    • Waterfalls
    • Dynamic Tessellation of the water surface at steep angles
    • Etc.

    I'll leave you with a slightly entertaining view of some early tessellation tests achieving the exact opposite of what I was trying to accomplish:


    (Hint: don't forget to actually adjust sampling in the domain shader ;))​



    Please let me know what you think about my little demo.
    - RC-1290
     
    Last edited: Jun 24, 2014
  2. smb02dunnal

    smb02dunnal

    Joined:
    May 5, 2012
    Posts:
    439
    This is really nice! Great work! :D
     
  3. laakerules

    laakerules

    Joined:
    Aug 29, 2012
    Posts:
    153
    Wow not bad! Does it produce a shadow if a massive wave goes over some water!
     
  4. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Thanks :D
    Currently none of the lights produce shadows, because it has no sides, and you'd see some odd lights and shadows near the edges in the shape of the waves, where the sun shines underneath the surface, but they do work fine otherwise :).
     
  5. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    591
    Great work! I've been following the development of this on twitter, cool to have a breakdwon of the technique's here! Question: Those velocity fields look a lot like your logo! Incident? ;)


    [dutch]Trouwens, leuk om van een mede-nederlander te horen! Bedankt voor het noemen van de Microstar :)[/dutch]
     
  6. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Glad to hear you like it!
    Yeah, my logo is from a version I created in Flash 2 years ago, though ;). But that one never made it into 3D. Until I started using Unity a year ago, during the last Global Game Jam. Unity made it easier for me to understand how the rendering pipeline works, so I decided to give it a shot.

    Ha! Cool, I didn't realize you were Dutch before (or well, I don't remember realizing that :p). And of course I'd mention the Microstar, that thing is awesome!
     
  7. Deleted User

    Deleted User

    Guest

    Very pretty! Bet you could a tidy profit off this tech if you put it up on the store. ;)

    Also, that visualization looks like it would make for a cool stylized effect. XD
     
  8. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    A video please. ;)
     
  9. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Hmmm, that might be a good idea, actually ^^. I think I can try to make one in the coming night or tomorrow.
     
  10. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Thx
     
  11. VeganApps

    VeganApps

    Joined:
    Jun 30, 2006
    Posts:
    263
    This is a very cool demo! I love it! I wanna swim in it! :D
     
  12. nipoco

    nipoco

    Joined:
    Sep 1, 2011
    Posts:
    2,008
    Oh I overlooked that...

    Looks really cool! It would be perfect with caustics and chromatic aberration and such details.

    Keep up the good work!
     
  13. omarzonex

    omarzonex

    Joined:
    Jan 16, 2012
    Posts:
    158
    very cool gold cold water

    amazing
     
  14. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Thanks everyone for the nice comments!
    Caustics and underwater lighting are definitely on the TODO list. I think I'll go for something simpler first though. I think flow mapping will be an obvious improvement too, especially when it comes to river-like behavior.

    I've made some attempts to make a video but so far I'm not happy with the quality of the recordings, so it might take a while.
    I think I need to do some more research before I can get better results out of the opensource screen capture software I'm using :(. I'll update the thread if I do.
     
  15. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Really cool! Especially if you lower the time value, the default is a bit to slow to look realistic : )
     
  16. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    The speed of movement is currently framerate dependent (It does exactly one simulation step per rendered frame), so it tends to have different movement speeds on different machines.

    I got this feedback a lot though, so I should probably do something about it :).
     
  17. nipoco

    nipoco

    Joined:
    Sep 1, 2011
    Posts:
    2,008
    Yay for flow mapping dude :D
    If you keep that up, I see the killer water shader a lot Unity folks waiting for ages. I wouldn't mind to pay $100 for that if it's really good.
     
  18. e-bonneville

    e-bonneville

    Joined:
    Mar 15, 2010
    Posts:
    76
    I don't have DX11, can you make a video of this? Your screenshots look gorgeous.
     
  19. Sir-Tiddlesworth

    Sir-Tiddlesworth

    Joined:
    Oct 19, 2011
    Posts:
    908
    This had better win ;)
     
  20. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    Can you make a standalone? Webplayer's not working for me (defaults to integrated card in webplayer can't override it for me, so just displays pink flat)
     
  21. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Yeah, the standalone link is below the webplayer link.
    Or click here ;)
     
  22. ronan-thibaudau

    ronan-thibaudau

    Joined:
    Jun 29, 2012
    Posts:
    1,722
    Sorry missed it, thanks! :)
     
  23. superjayman

    superjayman

    Joined:
    May 31, 2013
    Posts:
    185
    Can you share your code please?? Cheers
     
  24. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    I'm currently in the process of making sure the shader works on multiple platforms. First on both DX11 and DX9, but after that it should be relatively easy to add support for OpenGL. When that is done, I plan to release a free and a premium version on the Unity asset store.
     
  25. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    627
    Man this looks great and all those desired features you listed have me excited!
     
  26. zelmund

    zelmund

    Joined:
    Mar 2, 2012
    Posts:
    437
    standing near your door with my money.... =)
     
  27. reefwirrax

    reefwirrax

    Joined:
    Sep 20, 2013
    Posts:
    137
    hi there, very interesting! I ran the demo, and I could make cubes appear, and I clicked on the landscape quite a lot with the mouse and no water appeared at all! Running on the latest Nvidia drivers an gtx470.
     
  28. inafield

    inafield

    Joined:
    Feb 26, 2013
    Posts:
    281
    GeForce 670MX, 3GB GDDR5, web demo doesn't work. Stand alone works on Good and Fantastic settings.
     
  29. reefwirrax

    reefwirrax

    Joined:
    Sep 20, 2013
    Posts:
    137
    I didn't manage to download the standalone, I got to copies that was it-corrupted files, and the web page said Google Chrome could not connect to codeanimo.com page http://codeanimo.com/projects/watersimulation/directx11/RC4.zip, but I could get the web version running with invisible water, from the same website,! maybe it's just a temporary webpage issue, worth saying.
     
  30. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Nice to see people are still trying out my water simulation. However, the version for the DX11 competition is a bit old. I'm working on a version that can be used as tool. However, to make it future proof I had to rewrite a whole bunch of stuff. Buoyancy and Water depth rendering aren't implemented yet in this newer version, but I'll upload it as a preview, in case you're interested anyway. (Actually, with the current Unity Web Player version, I'm seeing problems with the depth map as well)

    One of the problems with the web demo version I've noticed before is that it doesn't always detect DX11 hardware correctly. This new version runs on both DX9 and DX11, so it should simply fall back to DX9 when that is the problem.


    I'm not sure why the download didn't work. It seems to work fine for me now. Is it still not working right now?
     
    Last edited: Oct 31, 2013
  31. dnnkeeper

    dnnkeeper

    Joined:
    Jul 7, 2013
    Posts:
    84
    Your link is broken. Fixed it :)
    Great work! Compute shaders are my passion now too! They're awesome.
     
  32. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Hahaha, I linked to my local version of the site.

    Note to self: when writing a post is the last thing you do on a day, check it again in the morning.
     
  33. eee2

    eee2

    Joined:
    Oct 21, 2012
    Posts:
    2
    When you release it on Asset Store?
     
    Last edited: Nov 1, 2013
  34. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    627
  35. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    627
  36. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Unfortunately, I don't know yet, because I still have quite a few issues I want to solve first. But I'm trying to make it this year, because it would allow me to afford spending more time on it.

    In the long run I would like to add various kinds of forces like wind and tides, but for the first public version, the only force I'm going to focus on is displacement by objects. So for example: when you drop a buoy in the water, the water is pushed out of the way. This should also allow you to make some waves simply by moving a water displacer through the water. Hopefully that's an acceptable workaround until other forces are implemented.

    Like buoyancy, that would require sending data about the simulation back from the GPU to the CPU, which isn't straight forward. I first want to make buoyancy work better.
     
  37. jinsediaoying

    jinsediaoying

    Joined:
    Nov 27, 2013
    Posts:
    13
    Great work, I'm really love this demo!
    here are some questions:
    Can this technique deal with real time changing terrain?
    Can this technique transfer into a spherical terrain version?
     
  38. zelmund

    zelmund

    Joined:
    Mar 2, 2012
    Posts:
    437
    set terrain texture on surface =)
     
  39. jinsediaoying

    jinsediaoying

    Joined:
    Nov 27, 2013
    Posts:
    13
    I mean the geometry of the terrain mesh, think about "from dust"
    It will be really cool to have a game combines "from dust"'s terrain effect and "populous 3"'s game play.
     
    Last edited: Dec 12, 2013
  40. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Yes, I tested updating the terrain in realtime earlier this week, and it worked without noticeable changes to the FPS (which is still capped at 60fps in the demo). In the current version I have a few different lists of steps that are executed. By default the terrain height rendering step is in the 'at the start of the simulation' list, but it can also be placed in the 'once-per frame' list.

    Not yet, but I would love to try that some time. The difficulty lies in handling the map projection (for the simulation itself, and for getting terrain height data), and making the water flow continuously from one edge to the other. I think I would have to look at tiling multiple simulations first. You're bound to get a few odd projection artifacts using 2D heightmaps around a sphere, but with high enough resolution, and enough tiled simulations, I think you can hide those problems pretty well.

    It would also be necessary to update the water rendering, to offset the vertices around a sphere, in stead of straight up. Similar to Bending Solid Geometry in Planetary Annihilation.
     
  41. noania

    noania

    Joined:
    Apr 19, 2014
    Posts:
    33
    What happened to the project? This is pretty impressive work. I hope its not abandoned
     
  42. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Most definitely not abandoned ;).

    I've spent the past year trying to make it easier to use, maintain and extend. And it's crossplatform too now.

    Please check out the forum thread.


    [Edit] Also:
    Sorry it took so long, but there is a video now!
    Same thread :)