Search Unity

Standard Specular transparent shader

Discussion in 'Shaders' started by ghiboz, Mar 11, 2017.

  1. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hi all!
    i'm trying to create a car window shader, with some hacks, but my basical issue is the smooth material if completely transparent...
    here is the window if is a bit dirt:

    and is good, you can see the sun reflection on the window... but if I clean the window:

    seems that there is not any window...
    it's possible show the smooth glow also if completely transparent?

    thanks in advance
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Are you using the Fade or Transparent mode on the shader? You should be using Transparent and not Fade. If you are using Transparent then you're going to need to give us more information and probably show us the textures & shader values you're using. However the short version is for glass you should have the alpha of the main texture be black, or very dark, but still have a dark grey specular color with maybe middle grey to light grey alpha. I would suggest playing with the color values with out any textures to see if you can get something you like and copy those values into your texture.
     
  3. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    thanks @bgolus
    here is my shader:
    Code (CSharp):
    1. Shader "gRally/Window"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Spec Color", Color) = (1, 1, 1, 1)
    6.         _Smooth("Smoothness", Range(0,1)) = 0.5
    7.         _MainTex("Albedo", 2D) = "white" {}
    8.         _Dirty("Dirty", Range(0,5)) = 0.0
    9.     }
    10.  
    11.     SubShader
    12.     {
    13.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "False" "RenderType" = "Transparent" }
    14.         LOD 300
    15.  
    16.         CGPROGRAM
    17.         #pragma surface surf StandardSpecular alpha:blend vertex:vert
    18.         #pragma target 3.0
    19.  
    20.         sampler2D _MainTex;
    21.         half _MainTexID;
    22.         half _Dirty;
    23.         half _Smooth;
    24.         fixed _DIRT_1_LEVEL;
    25.  
    26.         fixed4 _Color;
    27.  
    28.         struct Input
    29.         {
    30.             float2 uv_MainTex;
    31.             half3 normal;
    32.             float3 worldRefl;
    33.         };
    34.  
    35.         void vert(inout appdata_full v, out Input o)
    36.         {
    37.             // appdata_full
    38.             UNITY_INITIALIZE_OUTPUT(Input, o);
    39.             o.normal = v.normal;
    40.         }
    41.  
    42.         void surf(Input IN, inout SurfaceOutputStandardSpecular o)
    43.         {
    44.             // diffuse color
    45.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    46.  
    47.             o.Albedo = c.rgb;
    48.             o.Specular = _Color;
    49.  
    50.             if (_Dirty == 0.0f)
    51.             {
    52.                 _Dirty = _DIRT_1_LEVEL;
    53.             }
    54.  
    55.             if (c.a < 0.8f)
    56.             {
    57.                 o.Alpha = c.a * _Dirty;
    58.             }
    59.             else
    60.             {
    61.                 o.Alpha = c.a;
    62.             }
    63.             o.Smoothness = _Smooth;
    64.         }
    65.         ENDCG
    66.     }
    67. }
    68.  
    and the texture is white as albedo and the alpha channel is like this:
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You want alpha: premul not alpha:blend

    alpha: premul == the standard shader "transparent"
    alpha:fade == the standard shader "fade"
    alpha:blend is pretty close (if not identical) to alpha:fade

    Note: I'm adding a space between the : and p in alpha: premul but there shouldn't be when you use it, but the forums turn it into alpha:premul
     
  5. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    thanks @bgolus !! great info! now it works...
    another thing with this shader: if I use the dof component of the postprocessing (unity addon) i have this issue:

    appears something wrong with another transparent material (same shader) is on the back...
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Any post process / image effect that relies on scene depth won't really work properly with transparent objects. That's just kind of a fact of real time rendering.

    That said, Unity's depth of field effects have always had odd artifacts that I've not seen elsewhere, or looked into too deeply since I've been using Unity for mobile, console VR, and mobile VR which means we can't afford depth of field, we can barely afford bloom. You might try posting in the image effects forum to see if anyone has tips for avoiding those kinds of visual artifacts.
     
  7. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    thanks @bgolus !
    I've tried to post in image effects.. ;)
     
  8. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hello @bgolus !
    I have another question about my windscreen shader:
    this is the behaviour of the shader, on the right side the mesh is hidden:

    as you can see, the things I need (like dirt, etc...) are ok, but where the window is clear, the result is too dark...
    I tried also with something like this shader:
    https://docs.unity3d.com/Manual/SL-CullAndDepth.html
    but the result is almost the same..
    any ideas?
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If you hardcore the alpha to 0.0 is it still dark?
     
  10. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    No idea then, an o.Alpha of zero and alpha: premul should be completely invisible.
     
  12. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    :eek:
    if I force this:

    o.Specular = float4(1, 1, 1, 1);

    I obtain this:

     
  13. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    if I use Legacy Shaders/Transparent/Diffuse I obtain this:

    except for the full dirt, the transparent part are transparent, not darkened
     
  14. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    and this is with Standard with cutout selected

    seems that pbr have some issues
     
  15. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I wonder if you might not want to have the windows on the outside using alpha: premul, and windows on the inside using alpha:fade. I suspect you're seeing a small amount of the reflection fresnel modifying the transparency with alpha: premul. Actually, what happens if you set the specular to black?
     
  16. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Actually, the fact the cutout option is also darkening is very confusing.
     
  17. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    here is with o.Specular = float4(0, 0, 0, 0); or o.Specular = float4(0, 0, 0, 1);



    when I drive incar, the windscreen outside is not shown