Search Unity

Standard Shader Fade - white still transparent

Discussion in 'Shaders' started by Helgemonster, Feb 28, 2017.

  1. Helgemonster

    Helgemonster

    Joined:
    Jan 19, 2014
    Posts:
    10
    Hi guys,

    I've been trying to google to see if I could find an answer to this, but no luck so far, hoping someone here will be able to answer!

    I've got a set of hands for a VR game and what I want to do is have hands that are 100% opaque, but fading out from the wrist. None of the rendering modes seem to get the job done. Opaque has no transparency, Cutout can't handle fading, while Fade and Transparency can't both have the fade effect while remaining 100% opaque for the rest of the model. Fade is definitely the closest to these (see images), but it's still not perfect.

    Any clues?

     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    This is because transparent objects don't depth sort. See my more in-depth explanation here:
    https://forum.unity3d.com/threads/render-mode-transparent-doesnt-work-see-video.357853/#post-2315934

    Now there are a few options for how to fix this:
    Split the geometry up so the hand is actually opaque and only use a fade material on the wrist.

    Use custom shader with a ZWrite pass. There are several posts discussing this on the forum and some assets on the store I think. I should just write a basic example someday for this as it gets asked about often and I can never find a good post showing how to do this with a standard surface shader. I posted a two sided example a while ago for use with hair, but that's a slightly different case than this.

    Use AlphaToMask (aka alpha to coverage) which is single pass, depth sorted transparency! It requires you're using MSAA, which for VR is usually the case, but it requires doing some custom dithering otherwise it produces fairly bad banding. I've used it in the past, but the ZWrite pass is usually more flexible.
     
  3. Helgemonster

    Helgemonster

    Joined:
    Jan 19, 2014
    Posts:
    10
    Thanks a lot for the quick reply! Super helpful!
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Code (CSharp):
    1. Shader "Custom/PreZ Standard Fade" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjectors"="True"}
    10.         LOD 200
    11.  
    12.         // ZWrite pre-pass
    13.         Pass {
    14.             ZWrite On
    15.             ColorMask 0
    16.  
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #include "UnityCG.cginc"
    21.  
    22.             float4 vert(float4 vertex : POSITION) : SV_POSITION { return UnityObjectToClipPos(vertex); }
    23.             fixed4 frag() : SV_Target { return 0; }
    24.             ENDCG
    25.         }
    26.      
    27.         CGPROGRAM
    28.         // Physically based Standard lighting model, and enable shadows on all light types
    29.         #pragma surface surf Standard fullforwardshadows alpha:fade
    30.  
    31.         // Use shader model 3.0 target, to get nicer looking lighting
    32.         #pragma target 3.0
    33.  
    34.         sampler2D _MainTex;
    35.  
    36.         struct Input {
    37.             float2 uv_MainTex;
    38.         };
    39.  
    40.         half _Glossiness;
    41.         half _Metallic;
    42.         fixed4 _Color;
    43.  
    44.         void surf (Input IN, inout SurfaceOutputStandard o) {
    45.             // Albedo comes from a texture tinted by color
    46.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    47.             o.Albedo = c.rgb;
    48.             // Metallic and smoothness come from slider variables
    49.             o.Metallic = _Metallic;
    50.             o.Smoothness = _Glossiness;
    51.             o.Alpha = c.a;
    52.         }
    53.         ENDCG
    54.     }
    55.     FallBack "Diffuse"
    56. }
     
  5. Straafe

    Straafe

    Joined:
    Oct 15, 2012
    Posts:
    73
    bglogus - That shader is nearly exactly what I need, but I still need the options for other maps that the normal standard shader supports (like normal maps and maybe the AO, all those other ones), is there a shader out there that allows for the nice transparency fading but still has all of the other map options of the default standard shader?
     
  6. Straafe

    Straafe

    Joined:
    Oct 15, 2012
    Posts:
    73
    I've never attempted to write a shader and know nothing about it, but I downloaded the standard specular shader and added your zwrite prepass to it. Frankensteining the 2 together this way achieved perfect results - i Have the complete standard specular shader with all maps and the fading out works perfectly. Wish I knew more about how it all worked, but this appears to work great.
    Code (CSharp):
    1. // Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
    2.  
    3. Shader "Standard (Specular setup) + TransparencyFade"
    4. {
    5.     Properties
    6.     {
    7.         _Color("Color", Color) = (1,1,1,1)
    8.         _MainTex("Albedo", 2D) = "white" {}
    9.      
    10.         _Cutoff("Alpha Cutoff", Range(0.0, 1.0)) = 0.5
    11.  
    12.         _Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5
    13.         _GlossMapScale("Smoothness Factor", Range(0.0, 1.0)) = 1.0
    14.         [Enum(Specular Alpha,0,Albedo Alpha,1)] _SmoothnessTextureChannel ("Smoothness texture channel", Float) = 0
    15.  
    16.         _SpecColor("Specular", Color) = (0.2,0.2,0.2)
    17.         _SpecGlossMap("Specular", 2D) = "white" {}
    18.         [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
    19.         [ToggleOff] _GlossyReflections("Glossy Reflections", Float) = 1.0
    20.  
    21.         _BumpScale("Scale", Float) = 1.0
    22.         _BumpMap("Normal Map", 2D) = "bump" {}
    23.  
    24.         _Parallax ("Height Scale", Range (0.005, 0.08)) = 0.02
    25.         _ParallaxMap ("Height Map", 2D) = "black" {}
    26.  
    27.         _OcclusionStrength("Strength", Range(0.0, 1.0)) = 1.0
    28.         _OcclusionMap("Occlusion", 2D) = "white" {}
    29.  
    30.         _EmissionColor("Color", Color) = (0,0,0)
    31.         _EmissionMap("Emission", 2D) = "white" {}
    32.      
    33.         _DetailMask("Detail Mask", 2D) = "white" {}
    34.  
    35.         _DetailAlbedoMap("Detail Albedo x2", 2D) = "grey" {}
    36.         _DetailNormalMapScale("Scale", Float) = 1.0
    37.         _DetailNormalMap("Normal Map", 2D) = "bump" {}
    38.  
    39.         [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0
    40.  
    41.  
    42.         // Blending state
    43.         [HideInInspector] _Mode ("__mode", Float) = 0.0
    44.         [HideInInspector] _SrcBlend ("__src", Float) = 1.0
    45.         [HideInInspector] _DstBlend ("__dst", Float) = 0.0
    46.         [HideInInspector] _ZWrite ("__zw", Float) = 1.0
    47.     }
    48.  
    49.     CGINCLUDE
    50.         #define UNITY_SETUP_BRDF_INPUT SpecularSetup
    51.     ENDCG
    52.  
    53.  
    54.  
    55.  
    56.  
    57.     SubShader
    58.     {
    59.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    60.         LOD 300
    61.  
    62.  
    63.  
    64.             // ZWrite pre-pass
    65.             Pass{
    66.             ZWrite On
    67.             ColorMask 0
    68.  
    69.             CGPROGRAM
    70. #pragma vertex vert
    71. #pragma fragment frag
    72. #include "UnityCG.cginc"
    73.  
    74.             float4 vert(float4 vertex : POSITION) : SV_POSITION{ return UnityObjectToClipPos(vertex); }
    75.         fixed4 frag() : SV_Target{ return 0; }
    76.             ENDCG
    77.         }
    78.  
    79.  
    80.         // ------------------------------------------------------------------
    81.         //  Base forward pass (directional light, emission, lightmaps, ...)
    82.         Pass
    83.         {
    84.             Name "FORWARD"
    85.             Tags { "LightMode" = "ForwardBase" }
    86.  
    87.             Blend [_SrcBlend] [_DstBlend]
    88.             ZWrite [_ZWrite]
    89.  
    90.             CGPROGRAM
    91.             #pragma target 3.0
    92.  
    93.             // -------------------------------------
    94.  
    95.             #pragma shader_feature _NORMALMAP
    96.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    97.             #pragma shader_feature _EMISSION
    98.             #pragma shader_feature _SPECGLOSSMAP
    99.             #pragma shader_feature ___ _DETAIL_MULX2
    100.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    101.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    102.             #pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
    103.             #pragma shader_feature _PARALLAXMAP
    104.  
    105.             #pragma multi_compile_fwdbase
    106.             #pragma multi_compile_fog
    107.             #pragma multi_compile_instancing
    108.  
    109.             #pragma vertex vertBase
    110.             #pragma fragment fragBase
    111.             #include "UnityStandardCoreForward.cginc"
    112.  
    113.             ENDCG
    114.         }
    115.         // ------------------------------------------------------------------
    116.         //  Additive forward pass (one light per pass)
    117.         Pass
    118.         {
    119.             Name "FORWARD_DELTA"
    120.             Tags { "LightMode" = "ForwardAdd" }
    121.             Blend [_SrcBlend] One
    122.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    123.             ZWrite Off
    124.             ZTest LEqual
    125.  
    126.             CGPROGRAM
    127.             #pragma target 3.0
    128.  
    129.             // -------------------------------------
    130.  
    131.             #pragma shader_feature _NORMALMAP
    132.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    133.             #pragma shader_feature _SPECGLOSSMAP
    134.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    135.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    136.             #pragma shader_feature ___ _DETAIL_MULX2
    137.             #pragma shader_feature _PARALLAXMAP
    138.  
    139.             #pragma multi_compile_fwdadd_fullshadows
    140.             #pragma multi_compile_fog
    141.  
    142.             #pragma vertex vertAdd
    143.             #pragma fragment fragAdd
    144.             #include "UnityStandardCoreForward.cginc"
    145.  
    146.             ENDCG
    147.         }
    148.         // ------------------------------------------------------------------
    149.         //  Shadow rendering pass
    150.         Pass {
    151.             Name "ShadowCaster"
    152.             Tags { "LightMode" = "ShadowCaster" }
    153.  
    154.             ZWrite On ZTest LEqual
    155.  
    156.             CGPROGRAM
    157.             #pragma target 3.0
    158.  
    159.             // -------------------------------------
    160.  
    161.  
    162.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    163.             #pragma shader_feature _SPECGLOSSMAP
    164.             #pragma shader_feature _PARALLAXMAP
    165.             #pragma multi_compile_shadowcaster
    166.             #pragma multi_compile_instancing
    167.  
    168.             #pragma vertex vertShadowCaster
    169.             #pragma fragment fragShadowCaster
    170.  
    171.             #include "UnityStandardShadow.cginc"
    172.  
    173.             ENDCG
    174.         }
    175.         // ------------------------------------------------------------------
    176.         //  Deferred pass
    177.         Pass
    178.         {
    179.             Name "DEFERRED"
    180.             Tags { "LightMode" = "Deferred" }
    181.  
    182.             CGPROGRAM
    183.             #pragma target 3.0
    184.             #pragma exclude_renderers nomrt
    185.  
    186.  
    187.             // -------------------------------------
    188.  
    189.             #pragma shader_feature _NORMALMAP
    190.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    191.             #pragma shader_feature _EMISSION
    192.             #pragma shader_feature _SPECGLOSSMAP
    193.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    194.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    195.             #pragma shader_feature ___ _DETAIL_MULX2
    196.             #pragma shader_feature _PARALLAXMAP
    197.  
    198.             #pragma multi_compile_prepassfinal
    199.             #pragma multi_compile_instancing
    200.  
    201.             #pragma vertex vertDeferred
    202.             #pragma fragment fragDeferred
    203.  
    204.             #include "UnityStandardCore.cginc"
    205.  
    206.             ENDCG
    207.         }
    208.  
    209.         // ------------------------------------------------------------------
    210.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    211.         // This pass it not used during regular rendering.
    212.         Pass
    213.         {
    214.             Name "META"
    215.             Tags { "LightMode"="Meta" }
    216.  
    217.             Cull Off
    218.  
    219.             CGPROGRAM
    220.             #pragma vertex vert_meta
    221.             #pragma fragment frag_meta
    222.  
    223.             #pragma shader_feature _EMISSION
    224.             #pragma shader_feature _SPECGLOSSMAP
    225.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    226.             #pragma shader_feature ___ _DETAIL_MULX2
    227.             #pragma shader_feature EDITOR_VISUALIZATION
    228.  
    229.             #include "UnityStandardMeta.cginc"
    230.             ENDCG
    231.         }
    232.     }
    233.  
    234.     SubShader
    235.     {
    236.         Tags { "RenderType"="Opaque" "PerformanceChecks"="False" }
    237.         LOD 150
    238.  
    239.         // ------------------------------------------------------------------
    240.         //  Base forward pass (directional light, emission, lightmaps, ...)
    241.         Pass
    242.         {
    243.             Name "FORWARD"
    244.             Tags { "LightMode" = "ForwardBase" }
    245.  
    246.             Blend [_SrcBlend] [_DstBlend]
    247.             ZWrite [_ZWrite]
    248.  
    249.             CGPROGRAM
    250.             #pragma target 2.0
    251.          
    252.             #pragma shader_feature _NORMALMAP
    253.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    254.             #pragma shader_feature _EMISSION
    255.             #pragma shader_feature _SPECGLOSSMAP
    256.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    257.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    258.             #pragma shader_feature _ _GLOSSYREFLECTIONS_OFF
    259.             #pragma shader_feature ___ _DETAIL_MULX2
    260.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    261.  
    262.             #pragma skip_variants SHADOWS_SOFT DYNAMICLIGHTMAP_ON DIRLIGHTMAP_COMBINED
    263.          
    264.             #pragma multi_compile_fwdbase
    265.             #pragma multi_compile_fog
    266.  
    267.             #pragma vertex vertBase
    268.             #pragma fragment fragBase
    269.             #include "UnityStandardCoreForward.cginc"
    270.  
    271.             ENDCG
    272.         }
    273.         // ------------------------------------------------------------------
    274.         //  Additive forward pass (one light per pass)
    275.         Pass
    276.         {
    277.             Name "FORWARD_DELTA"
    278.             Tags { "LightMode" = "ForwardAdd" }
    279.             Blend [_SrcBlend] One
    280.             Fog { Color (0,0,0,0) } // in additive pass fog should be black
    281.             ZWrite Off
    282.             ZTest LEqual
    283.          
    284.             CGPROGRAM
    285.             #pragma target 2.0
    286.  
    287.             #pragma shader_feature _NORMALMAP
    288.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    289.             #pragma shader_feature _SPECGLOSSMAP
    290.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    291.             #pragma shader_feature _ _SPECULARHIGHLIGHTS_OFF
    292.             #pragma shader_feature ___ _DETAIL_MULX2
    293.             // SM2.0: NOT SUPPORTED shader_feature _PARALLAXMAP
    294.             #pragma skip_variants SHADOWS_SOFT
    295.          
    296.             #pragma multi_compile_fwdadd_fullshadows
    297.             #pragma multi_compile_fog
    298.          
    299.             #pragma vertex vertAdd
    300.             #pragma fragment fragAdd
    301.             #include "UnityStandardCoreForward.cginc"
    302.  
    303.             ENDCG
    304.         }
    305.         // ------------------------------------------------------------------
    306.         //  Shadow rendering pass
    307.         Pass {
    308.             Name "ShadowCaster"
    309.             Tags { "LightMode" = "ShadowCaster" }
    310.          
    311.             ZWrite On ZTest LEqual
    312.  
    313.             CGPROGRAM
    314.             #pragma target 2.0
    315.  
    316.             #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
    317.             #pragma shader_feature _SPECGLOSSMAP
    318.             #pragma skip_variants SHADOWS_SOFT
    319.             #pragma multi_compile_shadowcaster
    320.  
    321.             #pragma vertex vertShadowCaster
    322.             #pragma fragment fragShadowCaster
    323.  
    324.             #include "UnityStandardShadow.cginc"
    325.  
    326.             ENDCG
    327.         }
    328.         // ------------------------------------------------------------------
    329.         // Extracts information for lightmapping, GI (emission, albedo, ...)
    330.         // This pass it not used during regular rendering.
    331.         Pass
    332.         {
    333.             Name "META"
    334.             Tags { "LightMode"="Meta" }
    335.  
    336.             Cull Off
    337.  
    338.             CGPROGRAM
    339.             #pragma vertex vert_meta
    340.             #pragma fragment frag_meta
    341.  
    342.             #pragma shader_feature _EMISSION
    343.             #pragma shader_feature _SPECGLOSSMAP
    344.             #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
    345.             #pragma shader_feature ___ _DETAIL_MULX2
    346.             #pragma shader_feature EDITOR_VISUALIZATION
    347.  
    348.             #include "UnityStandardMeta.cginc"
    349.             ENDCG
    350.         }
    351.     }
    352.  
    353.     FallBack "VertexLit"
    354.     CustomEditor "StandardShaderGUI"
    355. }
    356.  
     
    ChameleonBlair likes this.
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    Yep, there shouldn't be any problems using that code, though it may potentially have some z fighting issues if instancing or single pass stereo is used.
     
    ROBYER1 likes this.
  8. Anthony_cts

    Anthony_cts

    Joined:
    Mar 11, 2017
    Posts:
    7
    Thanks a lot for this solution. I had a similar issue with a model that had some thickness. The included shader works perfectly.
     
    Straafe likes this.
  9. Dan_G

    Dan_G

    Joined:
    Dec 4, 2017
    Posts:
    31
    @Helgemonster can you please share the final shader you used? i cannot find a way to make it work....