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

Is there any Glass shader that works on Android?

Discussion in 'Shaders' started by Salvador.Limones, Mar 18, 2013.

  1. Salvador.Limones

    Salvador.Limones

    Joined:
    Mar 18, 2013
    Posts:
    6
    Hi!

    I am looking for a glass shader that works on android.

    I have tried HardSurface shader Free but its working in iOS, but not on Android?

    Does their pro version supports Android?

    Have you come across any Cg program or a shader that can do specular translucent glass on Android?

    Much thanks for the help.
     
  2. Jessespike

    Jessespike

    Joined:
    Jul 9, 2012
    Posts:
    44
    Unity has a Transparent/Specular shader by default. Is it not suffice?
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    All the built-in transparent shaders modulate all lighting effects by alpha. This means that when a surface is completely transparent (as most glass is), it will not appear to reflect any light. A glass shader should show reflections (specular, cubemap or otherwise) additively, even in fully transparent areas.

    Depending on your needs, these shaders on the wiki might work.
     
  4. leitecunha

    leitecunha

    Joined:
    Jun 26, 2013
    Posts:
    7
    Hi there, I wrote a shader for glass (and translucency or custom lightmaps too) called Standard Plus. I think it does what you want. My glass has various controls on refraction, as well as fresnel. And on top of that, you can use thickness map to count for the model different thickness, producing much better results, because the thickness will affect not only the refraction but the glass color as well. And on top of that, it has the same functionality of the standard shader.
    Take a look! https://www.assetstore.unity3d.com/en/#!/content/93745
     
  5. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    You are lucky.

    DirtTex is a texture to put on some well, dirt on glass.
    MaskTex is where the reflective parts are.
    MaskPower is how powerful the mask is..etc.

    Code (CSharp):
    1. Shader "TOZ/Object/FX/Glass" {
    2.     Properties {
    3.         _Color("Tint Color (RGB)", Color) = (1, 1, 1, 1)
    4.         _DirtTex("Dirt Texture (RGB)", 2D) = "black" {}
    5.         _MaskTex("Reflection Mask (R)", 2D) = "white" {}
    6.         _MaskPower("Mask Power", Range(0.0, 1.0)) = 0.5
    7.         _Reflectivity("Reflections Power", Range(0.0, 1.0)) = 0.5
    8.     }
    9.  
    10.     SubShader {
    11.         Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True" }
    12.         LOD 100
    13.        
    14.         Pass {
    15.             Name "BASE"
    16.             Tags { "LightMode" = "Always" }
    17.            
    18.             ZWrite Off
    19.             Blend SrcAlpha OneMinusSrcAlpha
    20.  
    21.             CGPROGRAM
    22.             #include "UnityCG.cginc"
    23.             //#pragma target 2.0
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.             #pragma multi_compile_fog
    27.  
    28.             fixed4 _Color;
    29.             sampler2D _DirtTex, _MaskTex;
    30.             float4 _DirtTex_ST, _MaskTex_ST;
    31.             float _MaskPower, _Reflectivity;
    32.  
    33.             struct a2v {
    34.                 float4 vertex : POSITION;
    35.                 float3 normal : NORMAL;
    36.                 float4 texcoord : TEXCOORD0;
    37.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    38.             };
    39.  
    40.             struct v2f {
    41.                 float4 pos : SV_POSITION;
    42.                 float4 coord0 : TEXCOORD0;
    43.                 float3 norm : TEXCOORD1;
    44.                 float3 eye : TEXCOORD2;
    45.                 UNITY_FOG_COORDS(3)
    46.                 UNITY_VERTEX_OUTPUT_STEREO
    47.             };
    48.  
    49.             v2f vert (a2v v) {
    50.                 v2f o;
    51.                 UNITY_SETUP_INSTANCE_ID(v);
    52.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    53.                 o.pos = UnityObjectToClipPos(v.vertex);
    54.                 o.coord0.xy = TRANSFORM_TEX(v.texcoord, _DirtTex);
    55.                 o.coord0.zw = TRANSFORM_TEX(v.texcoord, _MaskTex);
    56.                 o.norm = UnityObjectToWorldNormal(v.normal);
    57.                 o.eye = _WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, v.vertex).xyz;
    58.                 UNITY_TRANSFER_FOG(o, o.pos);
    59.                 return o;
    60.             }
    61.  
    62.             fixed4 frag(v2f i) : SV_Target {
    63.                 fixed3 dirt = tex2D(_DirtTex, i.coord0.xy).rgb;
    64.                 fixed mask = tex2D(_MaskTex, i.coord0.zw).r * _MaskPower;
    65.                 fixed4 refl = UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflect(-i.eye, i.norm));
    66.                 refl.rgb = DecodeHDR(refl, unity_SpecCube0_HDR) * _Reflectivity;
    67.                 fixed3 result = lerp(refl * _Color.rgb, dirt, mask);
    68.                 UNITY_APPLY_FOG(i.fogCoord, result);
    69.                 return fixed4(result, saturate(mask + _Reflectivity));
    70.             }
    71.             ENDCG
    72.         }
    73.     }
    74.  
    75.     Fallback Off
    76. }
     
    GengarGames777 and aaadragon2 like this.
  6. oliiix

    oliiix

    Joined:
    Nov 8, 2018
    Posts:
    9
    I believe many of you do not take in account that it should be a solution that works for mobiles ;) looking for the exactsame thing, nearly two years later =)
     
  7. OwlBoy-

    OwlBoy-

    Joined:
    Dec 11, 2015
    Posts:
    24
    Yeah, what oliiix said.
     
  8. aaadragon2

    aaadragon2

    Joined:
    May 5, 2021
    Posts:
    1
    That's cool shader. Simple and fancy. Thank you!
     
  9. GengarGames777

    GengarGames777

    Joined:
    Jul 8, 2017
    Posts:
    16
    based