Search Unity

Question Question about transparency and rear faces

Discussion in 'Shader Graph' started by alanmthomas, Nov 8, 2019.

  1. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    I am working on a transparent shader and am coming across an issue I'm hoping there is an easy solution to that I am simply not aware of. I am fading an object, but would like to essentially take the opaque view of the object and fade that. The transparent view shows rear faces and other geometry that I'd prefer to be hidden. In the screen shot, you can see the orange bricks coming through. Some of them are on the back of the object. Does anyone have an ideas?
     

    Attached Files:

  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Sorry I'm not sure exactly what I'm looking at as it looks a bit abstract, but could you maybe use alpha clip to fade your objects? Then they would pretty much fade like opaque objects... Without sorting artifacts. But I'm not sure if that's the whole thing you are fading or what. It's usually better to show a clear example case.

    Could something like this work for you? (the fading object is the standard Unity sphere mesh)
    ditherfade_demo.PNG
     
  3. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    I think that is what I am talking about. Here are more clear example. First is the object rendered as opaque. Next the same view with transparency. In the transparent view, you can see the little rectangular shape showing through. This is what I'm hoping to avoid.
    Screen Shot 2019-11-08 at 1.19.09 PM.png Screen Shot 2019-11-08 at 1.19.23 PM.png
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Which renderer are you using? Standard, URP, HDRP? It would be good nowadays to tell that when posting graphics related issues as there's so many things done in different ways on different renderers.
     
  5. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    Good point. I'm using URP.
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Try this graph, something similar should work. I just put it together quickly, so...
    Expose the parameter to interface. On HDRP you have to enable alpha clip in that gear (settings) menu of the Master node but I think URP had it by default visible, if I remember correctly.

    ditherfade.PNG
     
    RaL likes this.
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    if it is one single mesh you may try to enable depth write.
     
    Ds-Patrick and macka1080 like this.
  8. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    Thanks for this. I'm thinking that I'm overcomplicating this. I eliminated the excess geometry and got something that is close to the effect I'm going for. Doing the alpha threshold dithering seems to work somewhat, but eliminating the geometry definitely gets the job done.
     
  9. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    How does one go about enabling that?
     
  10. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    when it comes to shader graph - hmm, get the shader code from the master node, create a new shader and paste it there.
     
  11. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,902
    but sorry, if it one mesh it won't work...
     
  12. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    Okay. Thanks.
     
  13. LandonTownsend

    LandonTownsend

    Unity Technologies

    Joined:
    Aug 21, 2019
    Posts:
    35
    This might help for Universal RP.
    How to add a Z-test pass to any transparent object:
    1. Create a layer for your Z-tested transparent objects:
    upload_2019-11-25_15-26-9.png
    2. Create an unlit transparent material with 0 alpha called ZTestOnly
    upload_2019-11-25_15-27-7.png
    3. Select your Universal Pipeline Renderer asset:
    upload_2019-11-25_15-36-42.png
    4. Add a new renderer feature. Name it whatever you want, but make sure the rest is filled out like so, with the Layer Mask set to whatever your layer is that you want a prepass on, and Material set to your unlit transparent material. Make sure "Depth" and "Write Depth" is checked and "Depth Test" is set to "Less Equal"
    upload_2019-11-25_15-37-13.png
    Then put whatever transparent object you want to have a ZTest pass on that layer.
    upload_2019-11-25_15-41-23.png

    Advantages over dithering:
    No pixelated effect
    Allows for additive or multiplicative blending

    Disadvantages over dithering:
    Other transparent objects will not render behind it
    Does not work correctly with materials whose shaders have vertex modification
     
    BogdanM, Michael-Lam, Aps74 and 12 others like this.
  14. alanmthomas

    alanmthomas

    Joined:
    Sep 7, 2015
    Posts:
    197
    I could kiss you! I was trying this approach but couldn't get it to work. This did precisely the trick. I'm not doing any vertex modification so this is ideal. Thanks so much!
     
  15. CharlieBudd

    CharlieBudd

    Joined:
    Jul 24, 2017
    Posts:
    17
    So Ive been attempting to solve this problem aswell. Bit disapointed in URP seaming to take away shader passes, but I am happy to learn a new way if that is what Unity thinks is best.

    I did notice however that this problem of a transparent mesh drawing over itself is solved by the default URP shaders...
    upload_2020-8-22_18-50-58.png
    (Left is simple custom transparent shader, Right the same terrible programmer art (points if you can guess what it is) but with the URP Unlit shader set to transparent mode)

    So time to figure out how this default shader does this with out all the... "intricate"... setup proposed above. I found the default URP shader in question in the URP package (See below). It seams that the shader does include multiple passes includeing the
    ZWrite On ColorMask 0
    depth only pass that I would have used for this job before URP. So how come this works for Unity shaders not custom ones? Would be nice to have access to this ability as it seams to still be possible within URP.

    My guess is it is to do with the "DepthOnly" program #defines which dont seam to be docummented anywhere.
    Code (CSharp):
    1. #pragma vertex DepthOnlyVertex
    2. #pragma fragment DepthOnlyFragment
    Whole shader source code...
    Code (CSharp):
    1. Shader "Universal Render Pipeline/Unlit"
    2. {
    3.     Properties
    4.     {
    5.         [MainTexture] _BaseMap("Texture", 2D) = "white" {}
    6.         [MainColor]   _BaseColor("Color", Color) = (1, 1, 1, 1)
    7.         _Cutoff("AlphaCutout", Range(0.0, 1.0)) = 0.5
    8.  
    9.         // BlendMode
    10.         [HideInInspector] _Surface("__surface", Float) = 0.0
    11.         [HideInInspector] _Blend("__blend", Float) = 0.0
    12.         [HideInInspector] _AlphaClip("__clip", Float) = 0.0
    13.         [HideInInspector] _SrcBlend("Src", Float) = 1.0
    14.         [HideInInspector] _DstBlend("Dst", Float) = 0.0
    15.         [HideInInspector] _ZWrite("ZWrite", Float) = 1.0
    16.         [HideInInspector] _Cull("__cull", Float) = 2.0
    17.  
    18.         // Editmode props
    19.         [HideInInspector] _QueueOffset("Queue offset", Float) = 0.0
    20.  
    21.         // ObsoleteProperties
    22.         [HideInInspector] _MainTex("BaseMap", 2D) = "white" {}
    23.         [HideInInspector] _Color("Base Color", Color) = (0.5, 0.5, 0.5, 1)
    24.         [HideInInspector] _SampleGI("SampleGI", float) = 0.0 // needed from bakedlit
    25.     }
    26.     SubShader
    27.     {
    28.         Tags { "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
    29.         LOD 100
    30.  
    31.         Blend [_SrcBlend][_DstBlend]
    32.         ZWrite [_ZWrite]
    33.         Cull [_Cull]
    34.  
    35.         Pass
    36.         {
    37.             Name "Unlit"
    38.             HLSLPROGRAM
    39.             // Required to compile gles 2.0 with standard srp library
    40.             #pragma prefer_hlslcc gles
    41.             #pragma exclude_renderers d3d11_9x
    42.  
    43.             #pragma vertex vert
    44.             #pragma fragment frag
    45.             #pragma shader_feature _ALPHATEST_ON
    46.             #pragma shader_feature _ALPHAPREMULTIPLY_ON
    47.  
    48.             // -------------------------------------
    49.             // Unity defined keywords
    50.             #pragma multi_compile_fog
    51.             #pragma multi_compile_instancing
    52.  
    53.             #include "UnlitInput.hlsl"
    54.  
    55.             struct Attributes
    56.             {
    57.                 float4 positionOS       : POSITION;
    58.                 float2 uv               : TEXCOORD0;
    59.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    60.             };
    61.  
    62.             struct Varyings
    63.             {
    64.                 float2 uv        : TEXCOORD0;
    65.                 float fogCoord  : TEXCOORD1;
    66.                 float4 vertex : SV_POSITION;
    67.  
    68.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    69.                 UNITY_VERTEX_OUTPUT_STEREO
    70.             };
    71.  
    72.             Varyings vert(Attributes input)
    73.             {
    74.                 Varyings output = (Varyings)0;
    75.  
    76.                 UNITY_SETUP_INSTANCE_ID(input);
    77.                 UNITY_TRANSFER_INSTANCE_ID(input, output);
    78.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    79.  
    80.                 VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    81.                 output.vertex = vertexInput.positionCS;
    82.                 output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
    83.                 output.fogCoord = ComputeFogFactor(vertexInput.positionCS.z);
    84.  
    85.                 return output;
    86.             }
    87.  
    88.             half4 frag(Varyings input) : SV_Target
    89.             {
    90.                 UNITY_SETUP_INSTANCE_ID(input);
    91.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
    92.  
    93.                 half2 uv = input.uv;
    94.                 half4 texColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
    95.                 half3 color = texColor.rgb * _BaseColor.rgb;
    96.                 half alpha = texColor.a * _BaseColor.a;
    97.                 AlphaDiscard(alpha, _Cutoff);
    98.  
    99. #ifdef _ALPHAPREMULTIPLY_ON
    100.                 color *= alpha;
    101. #endif
    102.  
    103.                 color = MixFog(color, input.fogCoord);
    104.                 alpha = OutputAlpha(alpha);
    105.          
    106.                 return half4(color, alpha);
    107.             }
    108.             ENDHLSL
    109.         }
    110.         Pass
    111.         {
    112.             Tags{"LightMode" = "DepthOnly"}
    113.  
    114.             ZWrite On
    115.             ColorMask 0
    116.  
    117.             HLSLPROGRAM
    118.             // Required to compile gles 2.0 with standard srp library
    119.             #pragma prefer_hlslcc gles
    120.             #pragma exclude_renderers d3d11_9x
    121.             #pragma target 2.0
    122.  
    123.             #pragma vertex DepthOnlyVertex
    124.             #pragma fragment DepthOnlyFragment
    125.  
    126.             // -------------------------------------
    127.             // Material Keywords
    128.             #pragma shader_feature _ALPHATEST_ON
    129.  
    130.             //--------------------------------------
    131.             // GPU Instancing
    132.             #pragma multi_compile_instancing
    133.  
    134.             #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    135.             #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
    136.             ENDHLSL
    137.         }
    138.  
    139.         // This pass it not used during regular rendering, only for lightmap baking.
    140.         Pass
    141.         {
    142.             Name "Meta"
    143.             Tags{"LightMode" = "Meta"}
    144.  
    145.             Cull Off
    146.  
    147.             HLSLPROGRAM
    148.             // Required to compile gles 2.0 with standard srp library
    149.             #pragma prefer_hlslcc gles
    150.             #pragma exclude_renderers d3d11_9x
    151.             #pragma vertex UniversalVertexMeta
    152.             #pragma fragment UniversalFragmentMetaUnlit
    153.  
    154.             #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
    155.             #include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitMetaPass.hlsl"
    156.  
    157.             ENDHLSL
    158.         }
    159.     }
    160.     FallBack "Hidden/Universal Render Pipeline/FallbackError"
    161.     CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.UnlitShader"
    162. }
    163.  
     
    Last edited: Aug 22, 2020
  16. wrobel221

    wrobel221

    Joined:
    Jan 21, 2013
    Posts:
    5
    I think it's better to go for just any material settings for the unlit shader and to pass 1 in the Pass Index (which corresponds to DepthOnly pass).

    The other thing I've noticed is I didn't need any overrides, no material, no Depth override. Just Filters settings. In the newer version of URP there's a Light Mode tags setting and I provided DepthOnly and it was enough. In the older version as on the screenshot above it matches Shader Passes. But you need to make sure, the shader has the DepthOnly pass.
    hands-settings.jpg
     
    asrichesson likes this.
  17. masterahuck

    masterahuck

    Joined:
    Sep 10, 2015
    Posts:
    94
    Is there anyway we can have the same effect for built in?
     
    smash-ter likes this.
  18. Ian1994

    Ian1994

    Joined:
    Jan 21, 2020
    Posts:
    1
     

    Attached Files: