Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Water with depth in Unity Indie

Discussion in 'Made With Unity' started by andor_p, Jun 20, 2009.

  1. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    First of all, hi everyone, I'm new here. Just downloaded the trial of Unity a few days ago, and I must say it blew me away!

    So, without further ado, here goes my first try at doing something worthwile:



    Water in Unity Indie that has depth that changes with the point of view. It's obviously far away from water you can do with RenderToTexture functionality, but hey, not everyone can afford Unity Pro and it's way better than simple transparent water :)

    It's more impressive in motion, I'll try to attach a demo application so you can check it out.

    If anyone's interested, I'm glad to share some details and scripts, but the explanation of the technique used is a bit on the lengthy side so I won't type it out if noone cares anyway :)

    EDIT: you can try it out here:
    http://approject.comxa.com/

    If that fails, download here:
    http://www.megaupload.com/?d=WRD4UV1I

    Mouse Scroll to zoom, keys 1 and 2 switch between standard transparent and depthmapped water.

    Note that the color calculation part of the two shaders is identical (with the same crappy artifacts :) ), the difference is in the alpha calculation only.
     
  2. nickavv

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    I'd love to see it in action. :) Could you try some sort of archive other than rar?
     
  3. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,245
    That's not very interesting to Mac users, of which there are many here...you might want to upload a web player instead (which works on Mac + Windows and is smaller).

    --Eric
     
  5. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    Oops, sorry about that, I kindof forgot about the Mac. Hmm, I don't have my own webspace, seems like it's time to go hunt for a free one on the tubes... Any tips?

    EDIT: here we go: http://approject.comxa.com/
    Mouse Scroll to zoom, keys 1 and 2 switch between standard transparent and depthmapped water.
    Note that the color calculation part of the two shaders is identical (with the same crappy artifacts :) ), the difference is in the alpha calculation only.
     
  6. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,670
    Looked fantastic. :)

    Great work.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,245
    Actually I meant to zip the .html+.unityweb file together and upload the zip file. I tried the website link, but I guess they are suspicious of .unityweb files, and it's not working. ;) Of course, you could always upload a Mac build too (make it a Universal Binary). But the .unityweb file is cross-platform and takes up the least space.

    --Eric
     
  8. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    Heh, so many ways to go, yet so few of them lead somewhere :D
    Megaupload link for web player: http://www.megaupload.com/?d=WRD4UV1I
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,245
    Yay, that works. :) Cool effect...even if you're using Pro water, that would make a better fallback than the standard water (for old machines, or if you just want more speed).

    --Eric
     
  10. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    Thank you!

    Thanks. I guess it could work well as a fallback, although it takes a bit more work to set up than just switching a shader.
     
  11. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,060
    Really great looking water you have there. Three thumbs up!
     
  12. tolm

    tolm

    Joined:
    Feb 4, 2008
    Posts:
    61
    That looks really good! I'm sure a lot of people would like to see the source, or know how this it's done. I do at least. :)
     
  13. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    Thanks for the compliments!

    There's two parts to the technique: a precomputation of a "depth map" done by an editor script, and a shader which utilizes the depth map to compute the depth of the water along the current viewing direction.

    The precomputation script is in principle similar to lightmapping (in fact I wrote it by modifying Talzor's awesome PixelLightMapper script - thanks Talzor!). It casts a bunch of rays in four major directions from every texel, and after some weighted averaging stores the depths in the four channels of an RGBA texture.

    The shader then makes somewhat of an educated guess about the water depth along the viewing direction by weighting the depth values in the depth map with the dot product of the view direction and the major direction corresponding to that channel in the texture.

    That's the gist of it, I hope I wasn't too confusing :) I'll attach a package so you can check it out if you'd like to.
     

    Attached Files:

  14. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    Thanks for sharing this, I'm having trouble downloading the file "depthmapped_water.unitypackage" though...

    Could it be zipped up or something first please?
     
  15. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    Strange, it's working here. I'll try uploading a zip, don't know if it will help any though.
     

    Attached Files:

  16. RobbieDingo

    RobbieDingo

    Joined:
    Jun 2, 2008
    Posts:
    484
    The zipped version worked fine, thanks very much for taking the time to do that - appreciated

    - taking a look now... Cheers!
     
  17. Nilore

    Nilore

    Joined:
    Jul 3, 2010
    Posts:
    18
    Hey, I took a look at the web preview, and thought that this looked wonderful. I downloaded the .unitypackage and installed it, everything imported just fine.

    Now, I'm not sure if I'm doing this right, since I'm fairly new to Unity.

    After I imported it, I got the Depthmapped water object into my scene, placed it where I wanted it, but it was invisible in the editor, and in the game. So I ran the depthmapper script from the GameObject menu, had it run on the water plane, and it gave me a depth map, though the depthmap it gave me was solid black, and the water plane is still invisible.

    Any thoughts on what could be the cause? Thanks in advance.
     
  18. mikesgames

    mikesgames

    Joined:
    Apr 16, 2010
    Posts:
    1,071
    wow, this is really cool!

    Thanks for the downloads!
     
  19. kosmopol

    kosmopol

    Joined:
    Jun 7, 2010
    Posts:
    55
    I habe the same problem as Nilore: I attached Depthmapped Water to the plane (which pretends to be Waterplane), but I don't see anything. I know, my question sounds n00bisch, but I'm at the beginning of my Unity-Way, so I'll be ultra happy if somebody could just give a small hint how to append this tool (which looks awesome in the demo) :)
     
  20. cyangamer1

    cyangamer1

    Joined:
    Feb 17, 2010
    Posts:
    162
    The web demo looks fantastic! Definitely a step up for non-Pro users. :)

    It looks like your Editor script has an error in it though. It throws this exception when generating a new depthmap:
    Code (csharp):
    1. TargetInvocationException: Exception has been thrown by the target of an invocation.
    2. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000]
    3. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000]
    4. UnityEditor.HostView.Invoke (System.String methodName)
    5. UnityEditor.HostView.OnGUI ()    at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000]
    6. System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000]
    7. UnityEditor.ScriptableWizard.OnGUI ()
    8. UnityEngine.Texture2D.SetPixels (UnityEngine.Color[] colors, Int32 miplevel) [0x00000]
    9. UnityEngine.Texture2D.SetPixels (UnityEngine.Color[] colors) [0x00000]
    10. DepthMapper.OnWizardCreate ()   (at Assets\Depthmapped Water\Editor\DepthMapper.cs:65)
    11. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000]
    Looks like the issue is coming from something about the System.Globalization.CultureInfo. I didn't see any mention of that in the code itself, so I'm not sure why it's mentioned here, but there ya go. Thanks for showing as the demo though. It restored my faith in Unity Free's water.
     
  21. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    Sorry for not replying, I didn't notice someone replied to the thread after all this time :)

    I will try fixing the error, it's probably a Unity 2.6 thing, the script was originally written for Unity 2.5
     
  22. andor_p

    andor_p

    Joined:
    Jun 20, 2009
    Posts:
    29
    Here is the updated depthmapper editor script.

    Usage:
    - add depthmapped water to scene
    - set the depthmap texture to be readable in the texture importer
    - run the script from GameObject menu -> Depthmap wizard

    To use with your own water plane:
    - assign the "depthmapped water simple" material to your water plane
    - set the textures as needed - the depthmap texture has to be set to readable, and its format has to be ARGB32bit
    - run the script from GameObject menu -> Depthmap wizard
     

    Attached Files:

  23. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    It is beautiful. 5/5
     
  24. HolBol

    HolBol

    Joined:
    Feb 9, 2010
    Posts:
    2,884
    hey any chance you could get this to work on unity 3?
     
  25. uncrtnMind

    uncrtnMind

    Joined:
    Oct 30, 2010
    Posts:
    1
    I'd like to second the vote for this to work with Unity 3. It looks amazing!
     
  26. Koropokuruh

    Koropokuruh

    Joined:
    Mar 23, 2010
    Posts:
    18
    It would be really nice to see this water working in Unity 3.
     
  27. gl33mer

    gl33mer

    Joined:
    May 24, 2010
    Posts:
    280
    What is the problem with Unity 3 and this water wizard?
     
  28. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,823
    This wizard is written for 2.5/2.6, and not 3.0. 3.0 accomplishes many things differently than 2.5/6 do.

    Still, I'm using 2.6 Indie, and this is a great resource. Downloaded, and soon to be used. Thank you OP!
     
  29. milesamallow

    milesamallow

    Joined:
    Sep 9, 2010
    Posts:
    31
    That is excellent this is much closer to what water should look like.
     
  30. BenAllen

    BenAllen

    Joined:
    Sep 11, 2010
    Posts:
    13
    Hi. This was good when it worked in 2.6. I would use it again if it worked with version 3.x.

    I have found a workaround. I wrote this shader using ShaderFusion. It is very simple. When you create a height map to make a terrain, make a copy of the .RAW file as a .PSD and flip it horizontally, invert the grey scale and import into Unity. Then attach this to the shader as the AlphaMapTexture. See the attached example to learn how this is put together.

    Peace.
     

    Attached Files:

  31. Dan the Man

    Dan the Man

    Joined:
    May 24, 2011
    Posts:
    23
    yeah. it doesn't work in 3.4 :(
     
  32. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    227
    Necrobump for hopes of seeing it in Unity 3.x
     
  33. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    753
    If you want good looking water (still not looking half as good as this) in Unity Free, use the bundled pro only water and set the quality to low.
     
  34. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    227
    Thanks Wolfos! I spent half this week looking for a shader that would do this, you saved me a'lot of time!
     
  35. vegenarie

    vegenarie

    Joined:
    Jan 5, 2011
    Posts:
    287
    It gives lot of errors using Unity basic 3.5.5 and also has destroyed the waterplanemesh i had and now i just have nurbstopoly
     
  36. Pato707

    Pato707

    Joined:
    Nov 9, 2012
    Posts:
    3
    That's a great work, but me can't use it.
    I get pink textured water. I use Unity 4. There is some way to solve it?
     
  37. diman3d

    diman3d

    Joined:
    Jul 8, 2012
    Posts:
    1
    Hi, everybody!

    Water looks really cool in the demo!

    I have two questions...

    1) Will it works with OpenGL ES 1.1?
    2) How to launch it in Unity 3.x?
     
    Last edited: Jan 2, 2013
  38. AndOProd

    AndOProd

    Joined:
    Sep 20, 2010
    Posts:
    101
    Hey I spent last night figuring out how to achieve that effect lol yay it can be done :)
     
  39. SevenUnearth

    SevenUnearth

    Joined:
    Jun 26, 2013
    Posts:
    8
    As I don't know much about Shader, could anyone explain how to use this in Unity 4.x?
     
unityunity