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

glass shader

Discussion in 'Shaders' started by u2xxl, Feb 17, 2008.

  1. u2xxl

    u2xxl

    Joined:
    Dec 22, 2007
    Posts:
    5
    I wana a real glass shader.It should contain transpanrency and reflection.
    I tried use build-in transpancy shader but it have no reflection.
    Somebody help me.
     

    Attached Files:

  2. Dragon Rider

    Dragon Rider

    Joined:
    Jan 17, 2008
    Posts:
    280
    Do you mean Reflection, as in a mirror effect, or Refraction, as in the bending of light as it passes through the lens...?
     
  3. u2xxl

    u2xxl

    Joined:
    Dec 22, 2007
    Posts:
    5
    what I mean reflection is fake reflection using cubemap.If you can combine with refration,that's perfect.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's often very simple to combine different shaders to get the effect you want. In this case, for example, take the Reflective/VertexLit shader and add a couple of lines from the Alpha/VertexLit shader. Namely, replace

    Code (csharp):
    1.     Blend AppSrcAdd AppDstAdd
    with

    Code (csharp):
    1.     Blend SrcAlpha OneMinusSrcAlpha
    Then add a line in the category which says

    Code (csharp):
    1.     Tags {Queue=Transparent}
    Then give it a new name (like Reflective/VertexLitTransparent) and there ya go.

    --Eric
     
  5. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I followed Erich's instructions and built my first custom shader ;-)

    Very nice... although I do find a lot of the code (and the compiler warnings) incomprehensible.

    I also wasn't able to extend this approach to create a diffuse or shiny (vs. vertexlit) equivalent. The corresponding shaders seem to refer to named passes from other shaders ...
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Me too, although you shouldn't get any compiler warnings. Are you using the Unity 2.0 shader source?

    --Eric
     
  7. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I thought I was (using 2.0 shader sources). Edit: yes I was.

    I get something like:

    // vertex vert should be #pragma vertex vert

    (I made the recommended change and Very Bad Things happened...)
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Are you really really sure? ;) There isn't any of the "//vertex vert" syntax in the 2.0 shaders anymore. This is what I have:

    Code (csharp):
    1. Shader "Reflective/VertexLitTransparent" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _SpecColor ("Spec Color", Color) = (1,1,1,1)
    5.     _Shininess ("Shininess", Range (0.03, 1)) = 0.7
    6.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    7.     _MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
    8.     _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    9. }
    10.  
    11. Category {
    12.     Blend SrcAlpha OneMinusSrcAlpha
    13.     Fog { Color [_AddFog] }
    14.     Tags {Queue=Transparent}
    15.  
    16.     // ------------------------------------------------------------------
    17.     // ARB fragment program
    18.    
    19.     SubShader {
    20.    
    21.         // First pass does reflection cubemap
    22.         Pass {
    23.             Name "BASE"
    24.             Tags {"LightMode" = "Always"}
    25. CGPROGRAM
    26. #pragma vertex vert
    27. #pragma fragment frag
    28. #pragma fragmentoption ARB_fog_exp2
    29. #pragma fragmentoption ARB_precision_hint_fastest
    30. #include "UnityCG.cginc"
    31.  
    32. struct v2f {
    33.     V2F_POS_FOG;
    34.     float2 uv : TEXCOORD0;
    35.     float3 I : TEXCOORD1;
    36. };
    37.  
    38. uniform float4 _MainTex_ST;
    39.  
    40. v2f vert(appdata_tan v)
    41. {
    42.     v2f o;
    43.     PositionFog( v.vertex, o.pos, o.fog );
    44.     o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    45.  
    46.     // calculate object space reflection vector
    47.     float3 viewDir = ObjSpaceViewDir( v.vertex );
    48.     float3 I = reflect( -viewDir, v.normal );
    49.    
    50.     // transform to world space reflection vector
    51.     o.I = mul( (float3x3)_Object2World, I );
    52.    
    53.     return o;
    54. }
    55.  
    56. uniform sampler2D _MainTex;
    57. uniform samplerCUBE _Cube;
    58. uniform float4 _ReflectColor;
    59.  
    60. half4 frag (v2f i) : COLOR
    61. {
    62.     half4 texcol = tex2D (_MainTex, i.uv);
    63.     half4 reflcol = texCUBE( _Cube, i.I );
    64.     reflcol *= texcol.a;
    65.     return reflcol * _ReflectColor;
    66. }
    67. ENDCG
    68.         }
    69.        
    70.         // Second pass adds vertex lighting
    71.         Pass {
    72.             Lighting On
    73.             Material {
    74.                 Diffuse [_Color]
    75.                 Emission [_PPLAmbient]
    76.                 Specular [_SpecColor]
    77.                 Shininess [_Shininess]
    78.             }
    79.             SeparateSpecular On
    80. CGPROGRAM
    81. #pragma fragment frag
    82. #pragma fragmentoption ARB_fog_exp2
    83. #pragma fragmentoption ARB_precision_hint_fastest
    84.  
    85. #include "UnityCG.cginc"
    86.  
    87. struct v2f {
    88.     float2 uv : TEXCOORD0;
    89.     float4 diff : COLOR0;
    90.     float4 spec : COLOR1;
    91. };
    92.  
    93. uniform sampler2D _MainTex : register(s0);
    94. uniform float4 _ReflectColor;
    95. uniform float4 _SpecColor;
    96.  
    97. half4 frag (v2f i) : COLOR
    98. {
    99.     half4 temp = tex2D (_MainTex, i.uv);   
    100.     half4 c;
    101.     c.xyz = (temp.xyz * i.diff.xyz + temp.w * i.spec.xyz ) * 2;
    102.     c.w = temp.w * (i.diff.w + Luminance(i.spec.xyz) * _SpecColor.a);
    103.     return c;
    104. }
    105. ENDCG
    106.             SetTexture[_MainTex] {}
    107.         }      
    108.     }
    109.    
    110.     // ------------------------------------------------------------------
    111.     // Radeon 9000
    112.    
    113.     SubShader {
    114.    
    115.         // First pass does reflection cubemap
    116.         Pass {
    117.             Name "BASE"
    118.             Tags {"LightMode" = "Always"}
    119. CGPROGRAM
    120. #pragma vertex vert
    121. #include "UnityCG.cginc"
    122.  
    123. struct v2f {
    124.     V2F_POS_FOG;
    125.     float2 uv : TEXCOORD0;
    126.     float3 I : TEXCOORD1;
    127. };
    128.  
    129. uniform float4 _MainTex_ST;
    130.  
    131. v2f vert(appdata_tan v)
    132. {
    133.     v2f o;
    134.     PositionFog( v.vertex, o.pos, o.fog );
    135.     o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    136.  
    137.     // calculate object space reflection vector
    138.     float3 viewDir = ObjSpaceViewDir( v.vertex );
    139.     float3 I = reflect( -viewDir, v.normal );
    140.    
    141.     // transform to world space reflection vector
    142.     o.I = mul( (float3x3)_Object2World, I );
    143.    
    144.     return o;
    145. }
    146. ENDCG
    147.             Program "" {
    148.                 SubProgram {
    149.                     Local 0, [_ReflectColor]
    150.                     "!!ATIfs1.0
    151.                     StartConstants;
    152.                         CONSTANT c0 = program.local[0];
    153.                     EndConstants;
    154.                     StartOutputPass;
    155.                         SampleMap r0, t0.str;
    156.                         SampleMap r1, t1.str;
    157.                         MUL r1, r1, r0.a;
    158.                         MUL r0, r1, c0;
    159.                     EndPass;
    160.                     "
    161.                 }
    162.             }
    163.             SetTexture [_MainTex] {combine texture}
    164.             SetTexture [_Cube] {combine texture}
    165.         }
    166.        
    167.         // Second pass adds vertex lighting
    168.         Pass {
    169.             Material {
    170.                 Diffuse [_Color]
    171.                 Ambient [_Color]
    172.                 Shininess [_Shininess]
    173.                 Specular [_SpecColor]
    174.                 Emission [_Emission]
    175.             }
    176.             Lighting On
    177.             SeparateSpecular On
    178.             SetTexture [_MainTex] {
    179.                 constantColor [_Color]
    180.                 Combine texture * previous DOUBLE, texture * constant
    181.             }
    182.         }
    183.     }
    184.  
    185.     // ------------------------------------------------------------------
    186.     // Old cards
    187.    
    188.     SubShader {
    189.         Pass {
    190.             Name "BASE"
    191.             Tags {"LightMode" = "Always"}
    192.             Material {
    193.                 Diffuse [_Color]
    194.                 Ambient (1,1,1,1)
    195.                 Shininess [_Shininess]
    196.                 Specular [_SpecColor]
    197.             }
    198.             Lighting On
    199.             SeparateSpecular on
    200.             SetTexture [_MainTex] {
    201.                 combine texture * primary DOUBLE, texture * primary
    202.             }
    203.             SetTexture [_Cube] {
    204.                 combine texture * previous alpha + previous, previous
    205.             }
    206.         }
    207.     }
    208. }
    209.  
    210. // Fallback for cards that don't do cubemapping
    211. FallBack "VertexLit", 1
    212.  
    213. }
    To make a diffuse variation, you'd use that as the base, like so:

    Code (csharp):
    1. Shader "Reflective/DiffuseTransparent" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    5.         _MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
    6.         _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    7.     }
    8.     SubShader {
    9.         UsePass "Reflective/VertexLitTransparent/BASE"
    10.         UsePass "Diffuse/PPL"
    11.     }
    12.     FallBack "Reflective/VertexLitTransparent", 1
    13. }
     
  9. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    The screenshot shows the vertexlit variant in operation (it's the cube of "ice").

    Here's my shader (very slightly modified version of above suggestion; I copied the properties across from the Reflective/Glossy shader code for consistency and to make sure I hadn't messed up something):

    Code (csharp):
    1.  
    2. Shader "Transparent/ReflectiveVertexLit" {
    3. Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    6.         _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    7.         _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    8.         _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    9.         _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    10. }
    11.  
    12. Category {
    13.     Tags {Queue=Transparent}
    14.     Blend AppSrcAdd OneMinusSrcAlpha
    15.     Fog { Color [_AddFog] }
    16.  
    17.     // ------------------------------------------------------------------
    18.     // ARB fragment program
    19.    
    20.     SubShader {
    21.    
    22.         // First pass does reflection cubemap
    23.         Pass {
    24.             Name "BASE"
    25.             Tags {"LightMode" = "Always"}
    26. CGPROGRAM
    27. // profiles arbfp1
    28. // fragment frag
    29. // fragmentoption ARB_fog_exp2
    30. // fragmentoption ARB_precision_hint_fastest
    31. // vertex vert
    32. #include "UnityCG.cginc"
    33.  
    34. struct v2f {
    35.     V2F_POS_FOG;
    36.     float2 uv : TEXCOORD0;
    37.     float3 I : TEXCOORD1;
    38. };
    39.  
    40. v2f vert(appdata_tan v)
    41. {
    42.     v2f o;
    43.     PositionFog( v.vertex, o.pos, o.fog );
    44.     o.uv = TRANSFORM_UV(0);
    45.  
    46.     // calculate object space reflection vector
    47.     float3 viewDir = ObjSpaceViewDir( v.vertex );
    48.     float3 I = reflect( -viewDir, v.normal );
    49.    
    50.     // transform to world space reflection vector
    51.     o.I = mul( (float3x3)_Object2World, I );
    52.    
    53.     return o;
    54. }
    55.  
    56. uniform sampler2D _MainTex : register(s0);
    57. uniform samplerCUBE _Cube : register(s1);
    58. uniform float4 _ReflectColor;
    59.  
    60. half4 frag (v2f i) : COLOR
    61. {
    62.     half4 texcol = tex2D (_MainTex, i.uv);
    63.     half4 reflcol = texCUBE( _Cube, i.I );
    64.     reflcol *= texcol.a;
    65.     return reflcol * _ReflectColor;
    66. }
    67. ENDCG
    68.             SetTexture[_MainTex] {combine texture}
    69.             SetTexture[_Cube] {combine texture}
    70.         }
    71.        
    72.         // Second pass adds vertex lighting
    73.         Pass {
    74.             Lighting On
    75.             Material {
    76.                 Diffuse [_Color]
    77.                 Emission [_PPLAmbient]
    78.                 Specular [_SpecColor]
    79.                 Shininess [_Shininess]
    80.             }
    81.             SeparateSpecular On
    82. CGPROGRAM
    83. // profiles arbfp1
    84. // fragment frag
    85. // fragmentoption ARB_fog_exp2
    86. // fragmentoption ARB_precision_hint_fastest
    87.  
    88. #include "UnityCG.cginc"
    89.  
    90. struct v2f {
    91.     float2 uv : TEXCOORD0;
    92.     float4 diff : COLOR0;
    93.     float4 spec : COLOR1;
    94. };
    95.  
    96. uniform sampler2D _MainTex : register(s0);
    97. uniform float4 _ReflectColor;
    98. uniform float4 _SpecColor;
    99.  
    100. half4 frag (v2f i) : COLOR
    101. {
    102.     half4 temp = tex2D (_MainTex, i.uv);   
    103.     half4 c;
    104.     c.xyz = (temp.xyz * i.diff.xyz + temp.w * i.spec.xyz ) * 2;
    105.     c.w = temp.w * (i.diff.w + Luminance(i.spec.xyz) * _SpecColor.a);
    106.     return c;
    107. }
    108. ENDCG
    109.             SetTexture[_MainTex] {combine texture}
    110.         }      
    111.     }
    112.    
    113.     // ------------------------------------------------------------------
    114.     // Radeon 9000
    115.    
    116.     SubShader {
    117.    
    118.         // First pass does reflection cubemap
    119.         Pass {
    120.             Name "BASE"
    121.             Tags {"LightMode" = "Always"}
    122. CGPROGRAM
    123. // vertex vert
    124. #include "UnityCG.cginc"
    125.  
    126. struct v2f {
    127.     V2F_POS_FOG;
    128.     float2 uv : TEXCOORD0;
    129.     float3 I : TEXCOORD1;
    130. };
    131.  
    132. v2f vert(appdata_tan v)
    133. {
    134.     v2f o;
    135.     PositionFog( v.vertex, o.pos, o.fog );
    136.     o.uv = TRANSFORM_UV(0);
    137.  
    138.     // calculate object space reflection vector
    139.     float3 viewDir = ObjSpaceViewDir( v.vertex );
    140.     float3 I = reflect( -viewDir, v.normal );
    141.    
    142.     // transform to world space reflection vector
    143.     o.I = mul( (float3x3)_Object2World, I );
    144.    
    145.     return o;
    146. }
    147. ENDCG
    148.             Program "" {
    149.                 SubProgram {
    150.                     Local 0, [_ReflectColor]
    151.                     "!!ATIfs1.0
    152.                     StartConstants;
    153.                         CONSTANT c0 = program.local[0];
    154.                     EndConstants;
    155.                     StartOutputPass;
    156.                         SampleMap r0, t0.str;
    157.                         SampleMap r1, t1.str;
    158.                         MUL r1, r1, r0.a;
    159.                         MUL r0, r1, c0;
    160.                     EndPass;
    161.                     "
    162.                 }
    163.             }
    164.             SetTexture [_MainTex] {combine texture}
    165.             SetTexture [_Cube] {combine texture}
    166.         }
    167.        
    168.         // Second pass adds vertex lighting
    169.         Pass {
    170.             Material {
    171.                 Diffuse [_Color]
    172.                 Ambient [_Color]
    173.                 Shininess [_Shininess]
    174.                 Specular [_SpecColor]
    175.                 Emission [_Emission]
    176.             }
    177.             Lighting On
    178.             SeperateSpecular On
    179.             SetTexture [_MainTex] {
    180.                 constantColor [_Color]
    181.                 Combine texture * previous DOUBLE, texture * constant
    182.             }
    183.         }
    184.     }
    185.  
    186.     // ------------------------------------------------------------------
    187.     // Old cards
    188.    
    189.     SubShader {
    190.         Pass {
    191.             Name "BASE"
    192.             Tags {"LightMode" = "Always"}
    193.             Material {
    194.                 Diffuse [_Color]
    195.                 Ambient (1,1,1,1)
    196.                 Shininess [_Shininess]
    197.                 Specular [_SpecColor]
    198.             }
    199.             Lighting On
    200.             SeparateSpecular on
    201.             SetTexture [_MainTex] {
    202.                 combine texture * primary DOUBLE
    203.             }
    204.             SetTexture [_Cube] {
    205.                 combine texture * previous alpha + previous
    206.                 Matrix [_Reflection]
    207.             }
    208.         }
    209.     }
    210. }
    211.  
    212. // Fallback for cards that don't do cubemapping
    213. FallBack " VertexLit", 1
    214.  
    215. }
    216.  
    And here's my per pixel lit glossy version:

    Code (csharp):
    1.  
    2. Shader "Transparent/ReflectiveGlossy" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    6.         _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    7.         _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    8.         _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    9.         _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    10.     }
    11.     SubShader {
    12.         UsePass "Transparent/ReflectiveVertexLit/BASE"
    13.         UsePass " Glossy/PPL"
    14.     }
    15.     FallBack "Transparent/ReflectiveVertexLit", 1
    16. }
    17.  
    Here's where I'm confused:

    1) The alpha of the main color affects the brightness of the shader but not its transparency (more transparent => brighter ...?!)

    2) The glossy version looks VERY different (much darker) so that I suspect I've done something terribly wrong. The main difference seems to be that it doesn't get the mysterious brightness boost from the alpha (they look the same if the main color is 100% opaque).

    I'd love to get this right since it's a pretty gorgeous effect.
     

    Attached Files:

  10. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Aha!

    Looks like the Unity 2 shaders archive is named "builtin shaders.zip" but expands to produce "Unity Builtin Shaders" while the 1.6.2 archive expands to create "Builtin Shaders" which is what confused me.

    So my compiler warning is gone, but all the other weirdness remains.

    Edit: for the nonce, I can get nice results out of the Reflective/TransparentVertexLit shader built as per Erich's suggestion, even though its behavior doesn't make sense to me ... so I guess I'll live with that.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Glad you got it sorted (more or less)...I don't know enough about shaders to suggest any ways to change the behavior, alas.

    --Eric
     
  12. danilonishimura

    danilonishimura

    Joined:
    Jul 13, 2010
    Posts:
    70
    Dude, I modified your code a bit and I think I got what you where looking for. First, I set the _ReflectColor alpha to the given cubemap texture, than I multiplied the ObjSpaceViewDir to about a third of the given vertex calculation.. worked fine for me:

    Code (csharp):
    1.  
    2. Shader "Transparent/ReflectiveVertexLit" {
    3. Properties {
    4.       _Color ("Main Color", Color) = (1,1,1,1)
    5.       _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
    6.       _Shininess ("Shininess", Range (0.01, 1)) = 0.078125
    7.       _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    8.       _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    9.       _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    10. }
    11.  
    12. Category {
    13.    //Tags {Queue=Transparent}
    14.    Blend AppSrcAdd OneMinusSrcAlpha
    15.    Fog { Color [_AddFog] }
    16.  
    17.    // ------------------------------------------------------------------
    18.    // ARB fragment program
    19.    
    20.    SubShader {
    21.    
    22.       // First pass does reflection cubemap
    23.       Pass {
    24.          Name "BASE"
    25.          Tags {"LightMode" = "Always"}
    26. CGPROGRAM
    27. // profiles arbfp1
    28. // fragment frag
    29. // fragmentoption ARB_fog_exp2
    30. // fragmentoption ARB_precision_hint_fastest
    31. // vertex vert
    32. #include "UnityCG.cginc"
    33.  
    34. struct v2f {
    35.    V2F_POS_FOG;
    36.    float2 uv : TEXCOORD0;
    37.    float3 I : TEXCOORD1;
    38. };
    39.  
    40. v2f vert(appdata_tan v)
    41. {
    42.    v2f o;
    43.    PositionFog( v.vertex, o.pos, o.fog );
    44.    o.uv = TRANSFORM_UV(0);
    45.  
    46.    // calculate object space reflection vector    
    47.    float3 viewDir = ObjSpaceViewDir( v.vertex * 0.3 );
    48.    float3 I = reflect( -viewDir, v.normal );
    49.    
    50.    // transform to world space reflection vector
    51.    o.I = mul( (float3x3) _Object2World, I );
    52.    
    53.    return o;
    54. }
    55.  
    56. uniform sampler2D _MainTex : register(s0);
    57. uniform samplerCUBE _Cube : register(s1);
    58. uniform float4 _ReflectColor;
    59.  
    60. half4 frag (v2f i) : COLOR
    61. {
    62.    half4 texcol = tex2D (_MainTex, i.uv);
    63.    half4 reflcol = texCUBE( _Cube, i.I );
    64.    reflcol *= texcol.a;
    65.    return reflcol * _ReflectColor * _ReflectColor.a;
    66. }
    67. ENDCG
    68.          SetTexture[_MainTex] {combine texture}
    69.          SetTexture[_Cube] {combine texture}
    70.       }
    71.        
    72.       // Second pass adds vertex lighting
    73.       Pass {
    74.          Lighting On
    75.          Material {
    76.             Diffuse [_Color]
    77.             Emission [_PPLAmbient]
    78.             Specular [_SpecColor]
    79.             Shininess [_Shininess]
    80.          }
    81.          SeparateSpecular On
    82. CGPROGRAM
    83. // profiles arbfp1
    84. // fragment frag
    85. // fragmentoption ARB_fog_exp2
    86. // fragmentoption ARB_precision_hint_fastest
    87.  
    88. #include "UnityCG.cginc"
    89.  
    90. struct v2f {
    91.    float2 uv : TEXCOORD0;
    92.    float4 diff : COLOR0;
    93.    float4 spec : COLOR1;
    94. };
    95.  
    96. uniform sampler2D _MainTex : register(s0);
    97. uniform float4 _ReflectColor;
    98. uniform float4 _SpecColor;
    99.  
    100. half4 frag (v2f i) : COLOR
    101. {
    102.    half4 temp = tex2D (_MainTex, i.uv);    
    103.    half4 c;
    104.    c.xyz = (temp.xyz * i.diff.xyz + temp.w * i.spec.xyz ) * 2;
    105.    c.w = temp.w * (i.diff.w + Luminance(i.spec.xyz) * _SpecColor.a);
    106.    return c;
    107. }
    108. ENDCG
    109.          SetTexture[_MainTex] {combine texture}
    110.       }      
    111.    }
    112.    
    113.    // ------------------------------------------------------------------
    114.    // Radeon 9000
    115.    
    116.    SubShader {
    117.    
    118.       // First pass does reflection cubemap
    119.       Pass {
    120.          Name "BASE"
    121.          Tags {"LightMode" = "Always"}
    122. CGPROGRAM
    123. // vertex vert
    124. #include "UnityCG.cginc"
    125.  
    126. struct v2f {
    127.    V2F_POS_FOG;
    128.    float2 uv : TEXCOORD1;
    129.    float3 I : TEXCOORD0;
    130. };
    131.  
    132. v2f vert(appdata_tan v)
    133. {
    134.    v2f o;
    135.    PositionFog( v.vertex, o.pos, o.fog );
    136.    o.uv = TRANSFORM_UV(0);
    137.  
    138.    // calculate object space reflection vector    
    139.    float3 viewDir = ObjSpaceViewDir( v.vertex * 0.3);
    140.    float3 I = reflect( -viewDir, v.normal );
    141.    
    142.    // transform to world space reflection vector
    143.    o.I = mul( (float3x3)_Object2World, I );
    144.    
    145.    return o;
    146. }
    147. ENDCG
    148.          Program "" {
    149.             SubProgram {
    150.                Local 0, [_ReflectColor]
    151.                "!!ATIfs1.0
    152.               StartConstants;
    153.                  CONSTANT c0 = program.local[0];
    154.               EndConstants;
    155.               StartOutputPass;
    156.                  SampleMap r0, t0.str;
    157.                  SampleMap r1, t1.str;
    158.                  MUL r1, r1, r0.a;
    159.                  MUL r0, r1, c0;
    160.               EndPass;
    161.               "
    162.             }
    163.          }
    164.          SetTexture [_MainTex] {combine texture}
    165.          SetTexture [_Cube] {combine texture}
    166.       }
    167.        
    168.       // Second pass adds vertex lighting
    169.       Pass {
    170.          Material {
    171.             Diffuse [_Color]
    172.             Ambient [_Color]
    173.             Shininess [_Shininess]
    174.             Specular [_SpecColor]
    175.             Emission [_Emission]
    176.          }
    177.          Lighting On
    178.          SeperateSpecular On
    179.          SetTexture [_MainTex] {
    180.             constantColor [_Color]
    181.             Combine texture * previous DOUBLE, texture * constant
    182.          }
    183.       }
    184.    }
    185.  
    186.    // ------------------------------------------------------------------
    187.    // Old cards
    188.    
    189.    SubShader {
    190.       Pass {
    191.          Name "BASE"
    192.          Tags {"LightMode" = "Always"}
    193.          Material {
    194.             Diffuse [_Color]
    195.             Ambient (1,1,1,1)
    196.             Shininess [_Shininess]
    197.             Specular [_SpecColor]
    198.          }
    199.          Lighting On
    200.          SeparateSpecular on
    201.          SetTexture [_MainTex] {
    202.             combine texture * primary DOUBLE
    203.          }
    204.          SetTexture [_Cube] {
    205.             combine texture * previous alpha + previous
    206.             Matrix [_Reflection]
    207.          }
    208.       }
    209.    }
    210. }
    211.  
    212. // Fallback for cards that don't do cubemapping
    213. FallBack " VertexLit", 1
    214.  
    215. }
    216.  
     
  13. GameLvr

    GameLvr

    Joined:
    Mar 22, 2009
    Posts:
    115
    Hi all,

    has anyone modified this to work with 3.1?

    I'm getting several errors (and haven't got a clue what to do about them :) )

    thanks
     
  14. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    What errors are you getting?
     
  15. winterk

    winterk

    Joined:
    Feb 8, 2011
    Posts:
    1
    When I tried dnishimura's version on Unity 3.1 I received the following error:
    Code (csharp):
    1. Shader warning in 'Transparent/ReflectiveVertexLit': Compiling shaders to OpenGL ES 2.0 requires both vertex and fragment programs at line 85
     
  16. linusnorden

    linusnorden

    Joined:
    Sep 27, 2010
    Posts:
    123
    same problem :-( what to do i suck at shader programing ........
     
  17. danilonishimura

    danilonishimura

    Joined:
    Jul 13, 2010
    Posts:
    70
    Hi,

    It's been a while since I last posted here.

    Here's the current version of this same shader.
    It's a lot cleaner and is working on Unity 3.4.0f5


    Code (csharp):
    1.  
    2. Shader "Transparent/Glass" {
    3.     Properties {
    4.         _Color ("Color", Color) = (1,1,1,1)
    5.         _SpecColor ("Specular Color", Color) = (1,1,1,1)
    6.         _Shininess ("Specular Falloff", Range (0.01, 1)) = 0.7
    7.         _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    8.         _MainTex ("Main Texture", 2D) = "white" {}
    9.         _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    10.     }
    11.    
    12.     Category {
    13.         Tags {"Queue"="Transparent"}
    14.        
    15.         SubShader {
    16.            
    17.             Pass {
    18.                 Cull Off
    19.                 ZWrite Off
    20.                 ZTest Less
    21.                 Lighting On
    22.                 SeparateSpecular On
    23.                 Blend SrcAlpha OneMinusSrcAlpha
    24.                 AlphaTest Greater 0.01
    25.                
    26.                
    27.                
    28.                 Material {
    29.                     Diffuse [_Color]
    30.                     Ambient [_Color]
    31.                     Shininess [_Shininess]
    32.                     Specular [_SpecColor]
    33.                 }
    34.                
    35.                
    36.                 //Reflection
    37.                 SetTexture [_Cube] {
    38.                     ConstantColor [_ReflectColor]
    39.                     combine texture * constant alpha, texture
    40.                     Matrix [_Cube]
    41.                 }
    42.                
    43.                 //Reflection illumination
    44.                 SetTexture [_Cube] {
    45.                     ConstantColor [_ReflectColor]
    46.                     combine constant * constant alpha - previous, previous
    47.                     Matrix [_Cube]
    48.                 }
    49.                
    50.                 //Texture
    51.                 SetTexture [_MainTex] {
    52.                     ConstantColor [_Color]
    53.                     combine texture +- previous, constant
    54.                 }
    55.                
    56.                 //Texture illumination
    57.                 SetTexture [_MainTex] {
    58.                     ConstantColor (1,1,1,0.5)
    59.                     combine previous * primary double , previous
    60.                 }
    61.                
    62.             }
    63.            
    64.            
    65.         }//End of Subshader
    66.        
    67.         Fallback "Diffuse"
    68.        
    69.     }//End of Category
    70.    
    71. }//End of Shader
    72.  
     
    Last edited: Jul 26, 2011
  18. Wolves_Realm

    Wolves_Realm

    Joined:
    Nov 30, 2009
    Posts:
    84
    Hey,
    I am using this shader that you wrote. It works fine but it is not very transparent. In you last code, which line can I change to make the shader more transparent?
     
  19. danilonishimura

    danilonishimura

    Joined:
    Jul 13, 2010
    Posts:
    70
    Just change the alpha channels for the color and reflection on the color inspector.
     
  20. GaryB

    GaryB

    Joined:
    Nov 26, 2011
    Posts:
    1
    Hey guys, I tried using dnishimura's shader but got some strange results. The cubemap texture was inverted and it was aligning with the camera. I haven't gotten any errors and I'm new to this so i'm not quite sure what to modify in the code.
     
  21. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    381
    thanx for sharing exactly what i've been looking for!
     
  22. Venryx

    Venryx

    Joined:
    Sep 25, 2012
    Posts:
    444
  23. archaismic

    archaismic

    Joined:
    Mar 30, 2011
    Posts:
    10
  24. danilonishimura

    danilonishimura

    Joined:
    Jul 13, 2010
    Posts:
    70
    Updated version working on Unity 4.3

    Code (csharp):
    1. Shader "TransparentReflection" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    5.     _MainTex ("Base (RGB) RefStrength (A)", 2D) = "white" {}
    6.     _Cube ("Reflection Cubemap", Cube) = "_Skybox" { TexGen CubeReflect }
    7. }
    8. SubShader {
    9.     LOD 300
    10.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    11.    
    12. CGPROGRAM
    13. #pragma surface surf Lambert alpha
    14.  
    15. sampler2D _MainTex;
    16. samplerCUBE _Cube;
    17.  
    18. fixed4 _Color;
    19. fixed4 _ReflectColor;
    20.  
    21. struct Input {
    22.     float2 uv_MainTex;
    23.     float3 worldRefl;
    24. };
    25.  
    26. void surf (Input IN, inout SurfaceOutput o) {
    27.     fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    28.     fixed4 c = tex * _Color;
    29.     o.Albedo = c.rgb;
    30.     o.Alpha = c.a;
    31.    
    32.     fixed4 reflcol = texCUBE (_Cube, IN.worldRefl);
    33.     reflcol *= tex.a;
    34.     o.Emission = reflcol.rgb * _ReflectColor.rgb;
    35.     o.Alpha = reflcol.a * _ReflectColor.a;
    36. }
    37. ENDCG
    38. }
    39.    
    40. FallBack "Reflective/VertexLit"
    41. }
     
    tobetobe likes this.
  25. Dayonel

    Dayonel

    Joined:
    Aug 29, 2013
    Posts:
    4
    Hello guys, I got a very good effect with this shader combined to a cubemap, can I use this final shader that you have been posted for my commercial videogame? Please answer, thank you :)
     
  26. huangt101

    huangt101

    Joined:
    Jul 17, 2016
    Posts:
    2
    Hello guys, your shader is realy good, but a problem is the shadow which it produced shows very deep, how to make the shadow fade? In addition, in my observation, if the reflection of glass is bright than the object behind the glass, then the transparency will be low, and vice versa. So I changed the code "o.Alpha = reflcol.a * _ReflectColor.a" as "1- reflcol.a - _ReflectColor.a" , and it looks better.
     
  27. huangt101

    huangt101

    Joined:
    Jul 17, 2016
    Posts:
    2
    Hello guys, your shader is realy good, but a problem is the shadow which it produced shows very deep, how to make the shadow fade? In addition, in my observation, if the reflection of glass is bright than the object behind the glass, then the transparency will be low, and vice versa. So I changed the code "o.Alpha = reflcol.a * _ReflectColor.a" as "1- reflcol.a - _ReflectColor.a" , and it looks better.
     
  28. Andrea_Marchetti

    Andrea_Marchetti

    Joined:
    Apr 23, 2017
    Posts:
    9