Transparent water for indie

Discussion in 'ShaderLab' started by Reznic, Jun 23, 2006.

  1. Reznic

    Reznic

    New Member

    Joined:
    Sep 13, 2005
    Messages:
    21
    How would I go about making a transparent versoin of the non-reflective/refractive water that comes with Unity indie?
    I've been using unity for a few months now and I'm sill desperately lost when it comes to shaders.
    I'm trying to use the fresnel effect in the water shader to conrol alpha as well as color, and I can't figure out how. Has anyone else tried this?
  2. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,217
    Here's the modified Non-reflective water shader from Unity 1.5, made to be transparent based on fresnel. Just add this to project and select new shader in water material.

    The changes were: added
    Code (csharp):
    1.  
    2. Tags { "Queue" = "Transparent" }
    3. Blend SrcAlpha OneMinusSrcAlpha
    4. ColorMask RGB
    5.  
    at start of each SubShader; this puts water into Transparent objects rendering queue, sets up standard blending and sets it to not modify alpha (just in case you'll want glow...).

    Then I set both fragment program and R9000 subshaders to output fresnel into alpha:
    Code (csharp):
    1. col.a = water.a;
    at end of fragment program and
    Code (csharp):
    1. MOV r0.a, r2.a;
    at end of R9000 fragment assembly shader

    Attached Files:

  3. Foxxis

    Foxxis

    Member

    Joined:
    Jun 27, 2006
    Messages:
    893
    What is the correct way to modify the fresnel influence? Let's say I want the surface to maintain some level of opacity regardless of the angle relative to the camera?

    I've modified it by multiplying the water.a (col.a=water.a*2 for example), which works but I am fairly certain it is a dirty and dangerous practice. I will read up on the shaders ASAP, but in the meantime I would be very thankful for some hints. ;)

    TIA!
    Daniel
  4. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,217
    You can just do something like
    Code (csharp):
    1. col.a = 0.5;
    Or better yet, expose it as a color property that can be tweaked. Otherwise, the fresnel is read from the Color/fresnel texture; if you want alpha to be different from fresnel maybe it makes sense to expose a new texture that just controls alpha.
  5. NicholasFrancis

    NicholasFrancis

    Member

    Joined:
    Apr 8, 2005
    Messages:
    1,588
    Your original idea is exactly what I would do. Except - I wouldn't multiply by 2, but instead modify the lookup textures.

    If you think about it: The color gradients used in the value are used for exactly that purpose; to change appearance depending on viewer angle-of-incidence.

    Since you're talking about the indie water, there are no reflections to be affected by the gradient alphs - knock yourself out and make a nice gradient for your transparent.

    And besides; With shaders, if it looks good, ship it!
  6. Foxxis

    Foxxis

    Member

    Joined:
    Jun 27, 2006
    Messages:
    893
    Many thanks!
    Will look into how to expose properties to enable me to tweak a gradient for the alpha. Will definitely post the shader and source stuff if I make something spectacular. :)

    Thanks again!
  7. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    I know this is sorta gravedigging, but how would you go about changing the amount of alpha on this shader? Sorry, I'm terrible with that ShaderLab language. >.< Thanks!
  8. Reznic

    Reznic

    New Member

    Joined:
    Sep 13, 2005
    Messages:
    21
    You don't need to touch the shader. The alpha is controlled by the "oceangradient" image's alpha channel, which you can easily alter in Photoshop.

    One idea I've been toying with is inverting the alpha and using a upside-down duplicate of the scene for limited reflection effects.
  9. pete

    pete

    New Member

    Joined:
    Jul 21, 2005
    Messages:
    1,640
    >>I know this is sorta gravedigging

    not at all! this is a great water shader for indie. i didn't look :oops: should be in the standard assets if it isn't. *cough* but then again... i didn't look! :oops:
  10. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    Hey Reznic. What exactly am I supposed to do with the Alpha channel? I tried messing around with the Channel settings, changing numbers and such, but it didn't really have any effect.
  11. Marble

    Marble

    Member

    Joined:
    Aug 29, 2005
    Messages:
    938
    I suppose I could use a little help here, too. I'd like to alter it so the angle of incidence is not so extreme, but I don't quite understand how the gradient affects it.
  12. Reznic

    Reznic

    New Member

    Joined:
    Sep 13, 2005
    Messages:
    21
    To access the incidence gradient you look in Standard Assets/Water/Sources/oceangradient. If you bring that into Photoshop (I'm not sure about substitutes) you can alter the color of the gradient and/or alter the alpha of the gradient by going to the "Channels" tab of the layer window. You can run Brightness/Contrast, Curves, or anything else on the alpha to change the steepness of the incidence angle. you can even create a completely different gradient for different effects. Of course, if part of the image is white it be completely opaque, or completely transparent if it's black. You can make it so that the water is always slightly opaque by making sure the lowest value is something lighter than black.
    I'm not sure precisely how the opacity/intesity of the "Horizon Color" property is conrolled, but it too seems to be tied to the incidence gradient.

    [​IMG]
  13. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    Thanks Rez. I think I found the perfect consistancy for my water. It looks great!
  14. Marble

    Marble

    Member

    Joined:
    Aug 29, 2005
    Messages:
    938
    Editing the gradient and the horizon color allow some interesting effects, for sure. Is there a way I can get it so, when viewed from directly above, the ripples aren't invisible right in front of the camera? I can tell it must have something to do with what's being done to viewDir, but the code is opaque to me.
  15. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    I had to transfer my files between computers and on my current computer the Shader is broken. It isn't translucent and there are no waves on the surface. :(
  16. pete

    pete

    New Member

    Joined:
    Jul 21, 2005
    Messages:
    1,640
    check your vid drivers or if your card isn't good enough. doubt the shader's broken. more likely something with your card.
  17. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,217
    Moldorma: what is your video card?
  18. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    GeForce 2 MX. I didn't even think about that. This computer is two or three years older than the other one, and I never upgraded the video card.
  19. Marble

    Marble

    Member

    Joined:
    Aug 29, 2005
    Messages:
    938
    Is there a special way to prepare the transparent water material to use it with a different mesh? I'm getting this "smearing" effect when I apply it to an imported mesh, as if its uvs were weird.

    Attached Files:

  20. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,217
    How do you apply it? The easiest way to change the water mesh is described here - just changing the mesh used.

    If you're setting up water from scratch, you also have to add all required cameras, scripts etc.
  21. Reznic

    Reznic

    New Member

    Joined:
    Sep 13, 2005
    Messages:
    21
    I'm not quite sure how the shader handles the texture, but it seems to be a planar projection. The problem you're having is that your 3D app loks at the axes differently than unity. In your 3D app (which one are you using by the way?) you may have to rotate the water mesh 90 degrees on one of the axes (most likely x), re-export it and correct the rotation in unity.
  22. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    25,666
    I'm not having any issues applying the water material to arbitrary objects here. Actually it's pretty darn cool-looking; I hadn't really thought of using anything other than a plane before. :) Wonder if I can find some use for that....

    --Eric
  23. Marble

    Marble

    Member

    Joined:
    Aug 29, 2005
    Messages:
    938
    Thanks. I was just plopping the material onto other meshes and thinking it would work. I suppose this is a classic case of using something without understanding how it runs!
  24. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    Sorry to super-gravedig, but I want to use this in Unity and I can't seem to get it transparent anymore. :eek: I've changed the Alpha channel on that image, but nothing happens. :( I have an Intel iMac now, so Graphics Card shouldn't be a problem. Shed some light Unity Community! Thanks!
  25. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    Ah, got it transparent, but the waves are gone. Any reason why?
  26. MetroidHunter

    MetroidHunter

    New Member

    Joined:
    May 22, 2007
    Messages:
    213


    Code (csharp):
    1. cola water

    ROFLMAO!



    What does that part do anyway?
  27. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,217
    What are you actually talking about?
  28. MetroidHunter

    MetroidHunter

    New Member

    Joined:
    May 22, 2007
    Messages:
    213
    Code (csharp):
    1. col.a = 0.5;

    What Does That Do? :)
  29. bronxbomber92

    bronxbomber92

    New Member

    Joined:
    Nov 11, 2006
    Messages:
    890
    lowers the alpha value (opacity, transparency, trnaslucency,) to .5 (the values range from 0.0 to 1.0, 0.0 being fully transparent).

    Think RGBA color values.
  30. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    It would seem that this shader doesn't work in Unity 2. I'm messing with the new Water Shader to see if I can gift it transparency, but I'm pretty bad at ShaderLab.
  31. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,217
    The water used in Island Demo that comes with Unity actually uses a transparent water. You might try just using that (it's in Water folder in the project IIRC).
  32. nickavv

    nickavv

    New Member

    Joined:
    Aug 2, 2006
    Messages:
    1,796
    Yeah, I noticed and snagged it. :p It should really be in the Standard Assets.
  33. Aras

    Aras

    Graphics Plumber Unity Technologies

    Joined:
    Nov 7, 2005
    Messages:
    4,217
    The reason it is not is because water often tends to be a large object. And large transparent objects can cause all sorts of transparency sorting problems when there are multiple transparent objects in the scene.

    In island demo the water is about the only thing that is transparent, so there are no problems. But in general case it might not be that useable.
  34. RHD

    RHD

    Member

    Joined:
    Mar 30, 2009
    Messages:
    610
    Hi,

    I'm a newbie I'm afraid and I'm having some trouble wuth this.
    I get a load of error messages and the waves don't move:

    Could you give me some more detailed instructions?

    Unity Indie 2.5

    Thanks a million!

    Attached Files: