Search Unity

Making Glass?

Discussion in 'Shaders' started by robertseadog, Aug 16, 2005.

  1. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    How can I make a glass shader in Unity? I mean a simple one, preferably in indie (it's for the widgetcontest and it needs to be transparent at least)..
    Thanks!
     
  2. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Come on, no advice? [this section of the forum seems a bit unused.. Maybe im mistaken?]
     
  3. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    If you want reall glass reflections, you'll need Unity Pro, but you can fake them with a cube map.



    If you just want semi-transparent material, you don't even have to write a new shader. You can use one of the supplied semi-transparent shaders. (The ones in the Alpha submenu in the shader property of the material you are modifying.)
     
  4. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    The fake part seems to be the best alternative here.. So I can make it work with a transparent shader with a cubemap? I'll try that.
    (does Unity pro have a ready made glass/refraction shader?)
     
  5. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Actually, you cannot assign a cubemap to the alpha shader. Try this shader instead

    Code (csharp):
    1.  
    2. Shader "Glass" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB) Transparency (A)", 2D) = "white"
    6.         _Reflections ("Reflection cubemap", Cube) = "skybox" { TexGen CubeReflect }
    7.     }
    8.     SubShader {
    9.         Pass {
    10.             Tags {"Queue" = "Transparent" }
    11.             Blend SrcAlpha OneMinusSrcAlpha
    12.             ZWrite Off
    13.             Material {
    14.                 Diffuse [_Color]
    15.             }
    16.             Lighting On
    17.             SetTexture [_MainTex] {
    18.                 combine texture * primary double, texture * primary
    19.             }
    20.         }
    21.         Pass {
    22.             Blend One One
    23.             Material {
    24.                 Diffuse [_Color]
    25.             }
    26.             Lighting On
    27.             SetTexture [_Reflections] {
    28.                 combine texture
    29.                 Matrix [_Reflection]
    30.             }
    31.         }
    32.     }
    33.     SubShader {
    34.         Pass {
    35.             Tags {"Queue" = "Transparent" }
    36.             Blend SrcAlpha OneMinusSrcAlpha
    37.             Material {
    38.                 Diffuse [_Color]
    39.             }
    40.             Lighting On
    41.             SetTexture [_MainTex] {
    42.                 combine texture * primary double, texture * primary
    43.             }
    44.         }
    45.     }
    46. }
    47.  
    Btw, the trick to making this one look good is to use a cubemap with few powerful highlights - don't use the skybox, but make another one that only contains a few hoglights and a hint of the clouds...

    Note: This shader actually has some issues in the 1.1 beta - I'm on that
     
  6. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Wow this more than I expected.. I will have to get in on your shader programming, it seems really cool.

    Now what kind of issues are you talking about? When I render I don't see anything but a grey box, but that might just be my stoneage grafics card.. Do you expect to fix it before the 22.august? :roll:

    Thank you Nicholas, Im a little behind schedual but this is great stuff you are handing me here! I will do more tests on better Video cards and see what it yeilds..
     
  7. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    one thing though, when I make a shader, I now have to shift to expert mode in order to attach the cubemap etc. It's no biggie but how do you guys make the interface for the shaders?
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It won't be picked up if you change it in expert mode.

    You have to define them using the properties section:

    Code (csharp):
    1.  
    2. Properties {
    3.       _Color ("Main Color", Color) = (1,1,1,1)
    4.       _MainTex ("Base (RGB) Transparency (A)", 2D) = "white"
    5.       _Reflections ("Reflection cubemap", Cube) = "skybox" { TexGen CubeReflect }
    6.    }
    7.  
     
  9. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The fact that you even _can_ assign a cubemap in expert mode could be considered a bug... Some shaders will accept a cube map where they expect a 2D texture, others will fail in weird ways.

    We have extra checking in place for the normal inspector to only allow you to assign the correct type of texture. If you can't assign it there, there's a reason for it....
     
  10. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ok, so thats why my box turned grey. I just edit the shader source then; Im on it!

    Btw: Is all reference names in Unity global? What I mean to say is; if you reference "skybox" and it's in a subfolder way down below it will still pick it up? (if so I will have to start planning my name applying.. coming from flash doesn't exactly make it easier on me..)
     
  11. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    No... We just have a few builtins for the global names (white, black, dot3, etc).

    Atm, we don't even support 'skybox', but that will change soon.

    In Unity, you cannot use file names - you assign the textures by dragging dropping on to the material.
     
  12. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Now, I can't really seem to figure this out properly, if I've got a generated cubemap- How can I apply it when there is no interface to drag it onto?

    Now when I use the Shadercode, create a new shader out of it, assign it to a material; then in simple mode it calls the shader "default", and I can't drag any cubemaps onto it (same in Full mode)..

    In expert mode there is a texture slot, so if that doesn't work, I just don't see how..(as you said, Unity wont take file names, and _Reflections doesn't show up in expert mode either..)

    I guess Im actually confused :eek:
     
  13. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    When you write the new shader, you need to set the property type to be a 'cube' for it to accept cubemaps.

    See the property decl in line 3

    Code (csharp):
    1.  
    2. Shader "cubedemo" {
    3.   Properties {
    4.     _Cube ("drag texture here", CUBE) = "" { TexGen cubereflect }
    5.   }
    6.   subshader {
    7.     Pass {
    8.       SetTexture [_Cube] { combine texture }
    9.     }
    10.   }
    11. }
    12.  
    All the reflective shaders accept cubemaps for their reflection texture as well...
     
  14. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The 'Default' shader pops up because there was a syntax error in the shader. I just tried copying the script myself, and it worked fine. Are you sure you got your selection right prior to copy-pasting?
     
  15. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Well I've done it now three times, each time there's the same old Default showing up..

    I tried exproting it, maybe you can find some errors for me?:
     

    Attached Files:

  16. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Hmm... For some reason, I couldn't unpack the package... I created one for you
     

    Attached Files:

  17. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Ok, now for some reason it works, although the script looks the same..?
     
  18. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Are you running beta Nicholas?
     
  19. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Im running later than beta, but nothing has changed in that respect... All efforts are going into getting RTT working in a nice way for 1.1
     
  20. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Well then this is one of those weird things that happens in life then.. I don't see how you got that shader to work in your file..
     
  21. robertseadog

    robertseadog

    Joined:
    Jul 23, 2005
    Posts:
    374
    Wow the shader is behaving quite nicely, all there is to do now is to make a halfway decent Cubemap..
     
  22. gon

    gon

    Joined:
    Jul 6, 2006
    Posts:
    53
    Hi I'd like to know how to combine 2 shaders,
    here namely, am trying to combine the bakedvertexlight shader with this glass shader you are providing here...

    thank you so!

    cheers

    gon
     
  23. Hogge

    Hogge

    Joined:
    Aug 7, 2012
    Posts:
    12
    This is a really useful shader.
    The only problem I can see is that the cubemap is put on upside down. Got any solution to this?
     
  24. unity_007fkj

    unity_007fkj

    Joined:
    Mar 23, 2019
    Posts:
    9
    Its 2020, and you can create glass with some standard shader setting and reflection probe: Watch this: