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

Cheap (and easy) Glass

Discussion in 'Shaders' started by cgoran, Mar 15, 2009.

  1. cgoran

    cgoran

    Joined:
    Jun 11, 2005
    Posts:
    176
    Hey kids,

    I thought I would share some quick and easy glass for low rent applications.
    We are using this in an iPhone title and it's working great.

    I've includes two images. One shows it in action and the other provides an example of how to create a specular map for it.






    ///////////////////////////////////////////////////////

    Shader "EnvMapGlass" {

    Properties {
    _EnvMap ("EnvMap", 2D) = "black" { TexGen SphereMap }
    }

    SubShader {
    SeperateSpecular On
    Pass {
    Name "BASE"
    Cull Front
    //Blend One OneMinusDstColor
    Blend One One
    BindChannels {
    Bind "Vertex", vertex
    Bind "normal", normal
    }

    SetTexture [_EnvMap] {
    combine texture
    }
    }
    Pass {
    Name "BASE"
    ZWrite on
    Blend One One
    BindChannels {
    Bind "Vertex", vertex
    Bind "normal", normal
    }

    SetTexture [_EnvMap] {
    combine texture
    }
    }

    }

    Fallback off
    }
     
  2. kenlem

    kenlem

    Joined:
    Oct 16, 2008
    Posts:
    1,611
    Sweet! Nice effect and thanks for sharing.
     
  3. cgoran

    cgoran

    Joined:
    Jun 11, 2005
    Posts:
    176
    take away the back face culling pass and the screening and you have really good reflection annd chrome.
     
  4. kenlem

    kenlem

    Joined:
    Oct 16, 2008
    Posts:
    1,611
    So...

    Code (csharp):
    1.  
    2.  
    3. ///////////////////////////////////////////////////////
    4.  
    5. Shader "EnvMapGlass" {
    6.  
    7. Properties {
    8.    _EnvMap ("EnvMap", 2D) = "black" { TexGen SphereMap }
    9.    }
    10.  
    11. SubShader {
    12.    SeperateSpecular On
    13.       Pass {
    14.          Name "BASE"
    15.             Cull Front
    16.             Blend One One
    17.             BindChannels {
    18.                Bind "Vertex", vertex
    19.                Bind "normal", normal
    20.             }
    21.  
    22.             SetTexture [_EnvMap] {
    23.                combine texture
    24.             }
    25.       }
    26.  
    27.       Pass {
    28.          Name "BASE"
    29.          ZWrite on
    30.          Blend One One
    31.          BindChannels {
    32.          Bind "Vertex", vertex
    33.          Bind "normal", normal
    34.       }
    35.  
    36.       SetTexture [_EnvMap] {
    37.          combine texture
    38.       }
    39.    }
    40. }
    41.  
    42. Fallback off
    43. }
    becomes...

    Code (csharp):
    1.  
    2.  
    3. ///////////////////////////////////////////////////////
    4.  
    5. Shader "EnvMapGlass" {
    6.  
    7. Properties {
    8.    _EnvMap ("EnvMap", 2D) = "black" { TexGen SphereMap }
    9.    }
    10.  
    11. SubShader {
    12.    SeperateSpecular On
    13.  
    14.       Pass {
    15.          Name "BASE"
    16.          ZWrite on
    17.          Blend One One
    18.          BindChannels {
    19.          Bind "Vertex", vertex
    20.          Bind "normal", normal
    21.       }
    22.  
    23.       SetTexture [_EnvMap] {
    24.          combine texture
    25.       }
    26.    }
    27. }
    28.  
    29. Fallback off
    30. }
    Is that right?
     
  5. dolphin boy

    dolphin boy

    Joined:
    Nov 11, 2008
    Posts:
    29
    Great shader, really simple but totally effective!

    Just one question: how can I modify this shader in order to get access to transparency and "glass tint"?

    Thanks
     
  6. pBarrelas

    pBarrelas

    Joined:
    Jan 19, 2008
    Posts:
    52
    Thanks!! This shader is really neat!!
     
  7. Spacemonkey

    Spacemonkey

    Joined:
    Oct 24, 2008
    Posts:
    89
    Yello,

    just gonna dig up this and say thanks for a sweet shader! Very useful.
     
  8. Caedus

    Caedus

    Joined:
    Oct 15, 2008
    Posts:
    5
    The specular map is missing for me, could someone re-post it?
     
  9. Spacemonkey

    Spacemonkey

    Joined:
    Oct 24, 2008
    Posts:
    89
    Here you go.
     

    Attached Files:

  10. SteveB

    SteveB

    Joined:
    Jan 17, 2009
    Posts:
    1,448
    *bump*

    Yea I'd like to second eXKR's request for some guidance on how to access transparency and the like...I tried to no avail...

    Thanks!
    -Steve
     
  11. Wolfram

    Wolfram

    Joined:
    Feb 16, 2010
    Posts:
    175
    Thank you for this quick and simple env mapping shader.

    However, there are two problems with it:

    First, we had the effect that the shader became non-transparent in our builds, while it worked fine in the editor. This is a problem with the rendering queue, which seems to differ (or maybe even yield non-deterministic results) between those two. We solved that problem by explicitly assigning the correct render queue position in the shader.

    Second, the "Blend One One" is a fully additive blending, and will often burn out (="overexpose") areas with a reflection. There are two variations that yield better results.
    The first is "Blend One OneMinusSrcColor", which is probably what you want. It prevents overexposure, while still giving full brightness where the reflection is strongest. NOTE that the Unity ShaderLab documentation for this is WRONG, listing this as "Blend One OneMinusDstColor" - which results in strong color distortion, amongst other problems). Note that this is only correct for greyscale envmaps.
    The other would be "Blend SrcAlpha OneMinusSrcAlpha", which would be the standard setting for "correct" alpha blending, and can be used if your Envmap is a "real" color map with a designated alpha channel. As long as your envmap is greyscale without alpha, you should use the former blendfunction.

    So the modified shader looks like this:
    Code (csharp):
    1. ///////////////////////////////////////////////////////
    2.  
    3. Shader "EnvMapGlass" {
    4.  
    5. Properties {
    6.    _EnvMap ("EnvMap", 2D) = "black" { TexGen SphereMap }
    7.    }
    8.  
    9. SubShader {
    10.    SeperateSpecular On
    11.    Tags {"Queue" = "Transparent" }
    12.  
    13.       Pass {
    14.          Name "BASE"
    15.          ZWrite on
    16.          //Blend One One                       // additive
    17.          Blend One OneMinusSrcColor          // soft additive
    18.          //Blend SrcAlpha OneMinusSrcAlpha     // real alpha blending
    19.          BindChannels {
    20.          Bind "Vertex", vertex
    21.          Bind "normal", normal
    22.       }
    23.  
    24.       SetTexture [_EnvMap] {
    25.          combine texture
    26.       }
    27.    }
    28. }
    29.  
    30. Fallback off
    31. }
     
  12. theguy101

    theguy101

    Joined:
    Nov 24, 2009
    Posts:
    11
    Hey how can i add ad a difuse color to this?
     
  13. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,015
    Thanks for share..
     
  14. Bruell

    Bruell

    Joined:
    Jan 12, 2011
    Posts:
    13
    So in my scene, this shader does nothing, I don't see a material, am I missing something?
     
  15. chaneya

    chaneya

    Joined:
    Jan 12, 2010
    Posts:
    413
    I thought I would give some input on this thread.

    The shader posted by cgoran is great but it appears to break batching. If you are doing an iOS game, your enemy is draw calls and your friend is the automatic batching. If you have poly objects under 300 verts they will be automatically batched thus reducing your draw calls. So you can have... say... 4 poly objects, and if they are batched, they only take one draw call. I need batching in my application. Of course if you are only applying the shader to one object, then batching may not be an issue for you. I'm not a shader expert so I don't know why cgoran's shader breaks batching. If anyone knows, I would love to learn. This brings me to Wolfram's adjustments.

    Wolfram's shader appears to have some mistyped brackets. I fixed the bracket situation and figured I would post his corrected code. It works for me and more importantly it allows the glass transparancy while reenabling batching. (very useful in iOS development).

    My only problem with this implementation of glass for iOS is it seems to work great for spheres but if you flatten out a cube to a scale of say X:2 Y:.1 Z:2 (forming somewhat of a platform), then depending on the x position on the screen, your platform appears almost entirely opaque white. I think this is what Wolfram was observing. If you stick with the additive command of Blend One One and comment out the soft additive and real alpha, then that white opaqueness is really overexposed and blown out. It looks pretty bad. That's why I use the soft additive command Blend One OneMinusSrcColor and have commented out the other two options.

    If anyone knows if there is a way to adjust or further minimize that white opaqueness, that would be really helpful. It is my guess it has something to do with the fact that we are use a spheremap as our texture instead of say a cubemap. When a cubemap would be less beneficial for performance and memory in iOS.

    Allan

    Code (csharp):
    1.  
    2. // Upgrade NOTE: replaced 'SeperateSpecular' with 'SeparateSpecular'
    3. Shader "Mobile/EnvMapGlass1"
    4. {
    5.  
    6.  Properties
    7.  {
    8.     _EnvMap ("EnvMap", 2D) = "black" { TexGen SphereMap }
    9.  }
    10.  
    11.  SubShader
    12.  {
    13.      SeparateSpecular On
    14.      Tags {"Queue" = "Transparent" }
    15.  
    16.       Pass
    17.       {
    18.          Name "BASE"
    19.          ZWrite on
    20.          //Blend One One                       // additive
    21.          Blend One OneMinusSrcColor          // soft additive
    22.          //Blend SrcAlpha OneMinusSrcAlpha     // real alpha blending
    23.          BindChannels
    24.          {
    25.           Bind "Vertex", vertex
    26.           Bind "normal", normal
    27.         }
    28.  
    29.        SetTexture [_EnvMap]
    30.        {
    31.           combine texture
    32.        }
    33.       }
    34.    }
    35. }
    36.  
     
  16. podda999

    podda999

    Joined:
    Apr 26, 2010
    Posts:
    69
    Thanks for the share :)
     
  17. tonytage

    tonytage

    Joined:
    May 26, 2010
    Posts:
    28




    how can I make this shader work

    thank for help
     
  18. ivant@n

    ivant@n

    Joined:
    Jan 10, 2011
    Posts:
    1
    thx for sharing, this looks great!!
     
  19. mcwind

    mcwind

    Joined:
    Feb 7, 2011
    Posts:
    10
    It's great, but the second image doesn't show so I have no idea to create the texture. Can anybody re-post the image or make some describe?
    Thinks.
     
  20. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Read the whole thread, instead of the first post.... -_-
     
  21. siberman

    siberman

    Joined:
    Oct 12, 2011
    Posts:
    31
    So does this shader only work on Spheres?

    It's fine in the engine, but when i Build to ios only the spheres display correctly, have i missed something?

    Thanks.
     
    Last edited: Sep 27, 2012
unityunity