Search Unity

Decent Desktop Glass Shader for Unity?

Discussion in 'Shaders' started by AlanMattano, Apr 22, 2017.

  1. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    There are a lot of glass shaders. Can you suggest the best desktop one that includes transparency, reflections , fresnel, etc...?



    for making this

     
    Last edited: Apr 24, 2017
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The standard shader transparent mode should look pretty close to that cockpit assuming you have a decent enough probe to capture the sky etc
     
  3. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    AH! Oh... Thank HippoCoder ! I was using "Fade mode" by inertia... Transparency mode is much better (sharp and bright) than the fade mode. Is so nice that looks like Is missing refraction option, but is working nice and well!

    Unity5 "Fade" mode shader on the left and on the "Transparency" mode on right.

    Unity transparency vs Fade.jpg
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
  5. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Ok I'm trying to understand what and how to use the command buffer. I was able to make a transparent Unity5 shader and It was fun until I get stuck in the refraction.

    Here is a screenshot
    In order from left to right: Unity5 Fade, Unity5 Transparent and this Custom
    upload_2017-4-24_19-13-12.png
    upload_2017-4-24_18-21-0.png
    upload_2017-4-24_19-24-20.png

    Here is the code

    Code (CSharp):
    1. Shader "Custom/Glass" {
    2.     Properties {
    3.         _ColorMult("Luminocity", Range(-1,2)) = 1.1
    4.         _Color ("Color", Color) = (1,1,1,0.37)
    5.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    6.         _Glossiness ("Smoothness", Range(0,1)) = 0.973
    7.         _Metallic ("Metallic", Range(0,1)) = 0.916
    8.         _Reflection("Reflection Intencity", Range(0.1,10)) = 5.2
    9.         _Saturation("Reflection Saturation", Range(0.6,2.2)) = 1.2
    10.         _DotProduct("Rim effect", Range(-1,1)) = 0.044
    11.         _Fresnel("Fresnel Coefficient", Range(-1,10)) = 5.0
    12.         _Refraction("Refration Index", float) = 0.9
    13.         _Reflectance("Reflectance", Range(-1,1)) = 1.0
    14.     }
    15.     SubShader {
    16.         Tags {
    17.             "Queue" = "Transparent"
    18.             "IgnoreProjector" = "True"
    19.             "RenderType"="Transparent"
    20.         }
    21.         LOD 200
    22.  
    23.         CGPROGRAM
    24.         // Physically based Standard lighting model, and enable shadows on all light types
    25.         // take out "fullforwardshadows" and add "alpha:fade"
    26.         #pragma surface surf Standard fullforwardshadows alpha:fade
    27.  
    28.         // Use shader model 3.0 target, to get nicer looking lighting
    29.         #pragma target 3.0
    30.  
    31.         sampler2D _MainTex;
    32.         float _ColorMult;
    33.         float _Reflection;
    34.         float _DotProduct;
    35.         float _Saturation;
    36.         float _Fresnel;
    37.         float _Refraction;
    38.         float _Reflectance;
    39.  
    40.         struct Input {
    41.             float2 uv_MainTex;
    42.             float3 worldNormal;
    43.             float3 viewDir;
    44.         };
    45.  
    46.         half _Glossiness;
    47.         half _Metallic;
    48.         fixed4 _Color;
    49.         samplerCUBE _Cube;
    50.  
    51.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    52.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    53.         // #pragma instancing_options assumeuniformscaling
    54.         UNITY_INSTANCING_CBUFFER_START(Props)
    55.             // put more per-instance properties here
    56.         UNITY_INSTANCING_CBUFFER_END
    57.  
    58.         void surf (Input IN, inout SurfaceOutputStandard o) {
    59.             // Albedo comes from a texture tinted by color
    60.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    61.             o.Albedo = c.rgb * _ColorMult;
    62.             // Metallic and smoothness come from slider variables
    63.             o.Metallic = _Metallic * _Saturation;
    64.             o.Smoothness = _Glossiness;
    65.  
    66.             // Add transparency in the center (if more perpendicular)
    67.             // And more reflections in the side (if more angle).
    68.             float border = 1 - (abs(dot(IN.viewDir,IN.worldNormal)));
    69.             float alpha = (border * (1 - _DotProduct) + _DotProduct);
    70.             o.Alpha = ((c.a * _Reflection)  * alpha);
    71.             o.Emission = c.a;
    72.         }
    73.         ENDCG
    74.     }
    75.  
    76.     // Fallback to your shader: with a given name
    77.     //Fallback "SimpleTransparent";
    78. }
    Is missing refraction sections code; source [link]

    Code (CSharp):
    1. float4 frag(vertexOutput input) : COLOR
    2.          {
    3.             float refractiveIndex = 1.5;
    4.             float3 refractedDir = refract(normalize(input.viewDir),
    5.                normalize(input.normalDir), 1.0 / refractiveIndex);
    6.             return texCUBE(_Cube, refractedDir);
    7.          }
    Or another better source [link2]
    Code (CSharp):
    1. //Refraction Texture Grab
    2. float2 offset = NewNormals * _DistAmt * _GrabTexture_TexelSize.xy;
    3. IN.proj.xy = offset * IN.proj.z + IN.proj.xy;
    4. half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(IN.proj));
    What I purchase was not better than Unity 5 shader. Shader forge was ok but I like the new Unity5 standard shader asset as input. Probably I will try this new visual shader scripting that Unity is offering. But I wish to know more about all other glass pay options. (public and private, free and pay)
     
    Last edited: Apr 25, 2017
    pinksloyd and ahsen35813 like this.
  6. Pode

    Pode

    Joined:
    Nov 13, 2013
    Posts:
    145
    With a grabtexture, it's not a "real" refraction, it's just displacing the pixels of what has already been drawn onscreen.

    The refract() is better, you sample a pixel in the envmap. If you want to sample a pixel of the 3D geometry around you, you can refract() inside a reflection probe.
     
    AlanMattano likes this.
  7. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Thanks, Pode! probably you refer to http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter07.html
    or the optimise vr
    http://http.developer.nvidia.com/Cg/refract.html
    ?
    In Shader Forge is simple because has a Refraction input. But coding the shader using refract() into the Unity5 shader is out of my capacity. If is possible same help.

    Adding NormalMap on top of the "Custom Glass": Duplicate the transparent game object and scale it to 0.99 Then import the GlassRefraction Standard Asset Package Effect. Assets -> Import package -> Effects... Then apply the GlassRefractive material to this new smaller GameObject. You get the shader that supports a normal map but still is missing the refraction.
    upload_2017-4-26_2-44-6.png upload_2017-4-26_2-45-4.png
     
    Last edited: Apr 26, 2017
  8. ahsen35813

    ahsen35813

    Joined:
    Sep 15, 2019
    Posts:
    9
    For my purposes, this glass shader is far better than any other solution I've found so far. Thank you very much!
     
    AlanMattano likes this.