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

Transparent water for indie

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

  1. Reznic

    Reznic

    Joined:
    Sep 13, 2005
    Posts:
    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

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    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

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    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

    Joined:
    Jun 27, 2006
    Posts:
    1,108
    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

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    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

    Joined:
    Sep 13, 2005
    Posts:
    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

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    >>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

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    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

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    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

    Joined:
    Sep 13, 2005
    Posts:
    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.

     
  13. nickavv

    nickavv

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

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    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

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    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

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    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

    Unity Technologies

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

    nickavv

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    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

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    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

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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

    Joined:
    Sep 13, 2005
    Posts:
    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
    Posts:
    32,401
    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

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    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

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    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

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

    MetroidHunter

    Joined:
    May 22, 2007
    Posts:
    213


    Code (csharp):
    1. cola water

    ROFLMAO!



    What does that part do anyway?
     
  27. Aras

    Aras

    Unity Technologies

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

    MetroidHunter

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

    What Does That Do? :)
     
  29. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    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

    Joined:
    Aug 2, 2006
    Posts:
    1,801
    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

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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

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

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    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

    Joined:
    Mar 30, 2009
    Posts:
    719
    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: