Search Unity

Animated underwater caustic Tutorial

Discussion in 'Made With Unity' started by janpec, Apr 15, 2011.

  1. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Since there has been quite few topics about that already i decided to put some informations how to make animated caustic for your underwater game.
    It works well and it has almost no impact on performance.


    Caustic Generator:

    First what you need is to get caustic generator: http://www.dualheights.se/caustics/

    It creates tiled caustic texture depending on inputed settings from software.
    You can tweak settings to see what works for you best, but there are two things i would recommend you to use.

    1.First is to use timing slider in options on first half, do not cross over second half otherwise your timing on textures would be too high and there would be visible texture "reloading".

    2.Second is that you have to tweak settings for background color good. Basically use grayscale background color, becouse using blue color as caustic is not realistic since realistic is only affected by light (sun).
    More black background will give you more darker spots in caustic and higher contrast between shadow and light parts.
    If you use very bright background color it will give you very bright caustic in overall and whole caustic shape will be less noticeable.

    Make sure that you tweak your test caustic texture well, and test it in Unity before creating all frames needed.
    I would suggest you to use at least 60 frames of textures, arround 80 would be perfect. It actually depends on your timing options. If you have high timing you will need more texture frames.


    Image editing:

    You will have to use alpha in every frame texture. I have done this by just duplicating blue channel in texture and rename it in Alpha. Yeh it takes some time to do that for 70 textures :D.


    Script:

    You will need AnimatedProjector script. This script is attached to projector. You can find C# code in that topic:

    http://answers.unity3d.com/questions/33160/using-a-projector-to-place-a-animated-image-on-a-terrain


    Projector:

    Place projector in scene and face it to project where do you want it. (for water it should project from sky to terrain in 90 angle).
    Important is to set projector as Ortographic. This way caustic is projected on every model the same and it does not relly on model UVs or projector distance or position.

    You will also have to use Projector Multiply or Light material to see proper caustic results. Put projector material under Projector material input slot.




    Here is example video from our game.




    Right now the only problem i am facing is that if i make projector scale bigger, it works well on all objects, but texture gets stretched way too much.
    I have tried to tile texture more under material (Projectors Multiply material) but it does not work. It would be great if someone could post solution for this.
     
    Last edited: Apr 15, 2011
  2. U2

    U2

    Joined:
    Aug 12, 2008
    Posts:
    216
    First of all thank you for this, I'm sure it will come handy for many people and you were very kind to take the time to share your own research.

    There was another thread a long time ago with an example project with a caustics effect. They also expressed the same problem with the limit of the projector. I believe a Unity dev finally piped in and said it was a limit of the Unity engine itself. I have not experimented with this a lot myself but you may want to find that old thread and see if they found a solution eventually or not. Sorry I'm not more of a help.
     
  3. Vinícius Sanctus

    Vinícius Sanctus

    Joined:
    Dec 14, 2009
    Posts:
    282
    That´s like the most awesome underwater i´ve seen in unity so far! Totally using it in my project!

    I think i speak for the community here:

    -Thanks for your share!

    =)

    Hugs!
     
  4. kenshin

    kenshin

    Joined:
    Apr 21, 2010
    Posts:
    940
    Very very useful, thanks for sharing!
     
  5. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    No problem i am glad that i could help. I know that many people has been asking for this including me before.

    I would just be really glad if someone could post solution for Projections shader tiling, which currently doesnt work.
     
  6. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Bumping topic. I would really like to see someone to fix projector shader with proper tiling.
     
  7. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
  8. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Hm interesting i will try it out, however i am not sure if it will work well since i really need this on every model in scene.
     
  9. U2

    U2

    Joined:
    Aug 12, 2008
    Posts:
    216
    I wonder if you could use a combination of the two. i.e. use the shader on the terrain and the projector on everything else. Maybe not a perfect solution but a little closer anyways....
     
  10. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Hm that wouldnt help. The thing is that it is problem in covering all assets in same projection texture tiling. When you stretch ortographic projector for whole level, tiling of texture also expands and it becomes useless. Above shader for terrain doesnt work quite good becouse it is not light applied caustic but rather detail texture which doesnt look that good.
     
  11. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    What i would suggest is(if you have Pro),
    1-render an ortographic depth of your scene from your sun pos excluding objects above water level.
    2-make a simple blend shader which blends according to the above depth texture and objects own texture with texture tiling as you please.
    3-camerarenderwithshader all or some of your objects which needs caustic lights shown.

    There you have caustics everywhere, and if you write your shader well, you might even prevent shadowed areas to recieve caustics as well.

    For refference, check out Nvidia snow accumulation shader (use google)
     
  12. janpec

    janpec

    Joined:
    Jul 16, 2010
    Posts:
    3,520
    Yeh that all sounds fine but i am not programmer, neither our programmer has time to do it.
     
  13. falkonragno

    falkonragno

    Joined:
    May 31, 2010
    Posts:
    29
    does any one have a working example?
     
  14. steffan

    steffan

    Joined:
    Jan 7, 2011
    Posts:
    12
    Hi could you upload the caustic bitmaps, which you created and prepared ? Cause I don'y know how to edit the Alpha chanel, and I'm getting the error
    Code (csharp):
    1. NullReferenceException
    2. caustic.NextFrame () (at Assets/caustic.cs:21)
    3. caustic.Start () (at Assets/caustic.cs:15)
    4.  
     
  15. egajar23

    egajar23

    Joined:
    Sep 18, 2014
    Posts:
    2
    Change your projector into ortographic, then change your ortographic size. thanks anyway for your tips :D. you really help me.
     
  16. dradb

    dradb

    Joined:
    Jan 10, 2015
    Posts:
    86
    It works fine under Windows but I can't find a shader that works on Android phones. Can anyone help please?