Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Why Diffuse != Transparent/Diffuse (alpha = 1).

Discussion in 'Shaders' started by tr1te, Sep 5, 2012.

  1. tr1te

    tr1te

    Joined:
    Sep 5, 2012
    Posts:
    38
    I’m trying to make fade in/out object. All of what I found on web is change color.a in a transparent shader. I add the transparent/Diffuse shader (alpha = 1) and don’t get result similar as Diffuse shader.

    How can I fix that or may be you know better solution for making a fade in/out object?

    $TransparentShader.jpg
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    This is because transparent shaders do not write to the depth buffer. So for complex geometry like yours, you will get "wrong rendering order" effects.

    A workaround could be making a shader that is transparent, but also writes to the depth buffer, perhaps in a separate pass. Here's an example:

    Code (csharp):
    1.  
    2. Shader "Transparent/Diffuse ZWrite" {
    3. Properties {
    4.     _Color ("Main Color", Color) = (1,1,1,1)
    5.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6. }
    7. SubShader {
    8.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    9.     LOD 200
    10.  
    11.     // extra pass that renders to depth buffer only
    12.     Pass {
    13.         ZWrite On
    14.         ColorMask 0
    15.     }
    16.  
    17.     // paste in forward rendering passes from Transparent/Diffuse
    18.     UsePass "Transparent/Diffuse/FORWARD"
    19. }
    20.  
    21. Fallback "Transparent/VertexLit"
    22. }
    23.  
    Attached image, right side is using this shader.
     

    Attached Files:

    Morothar likes this.
  3. tr1te

    tr1te

    Joined:
    Sep 5, 2012
    Posts:
    38
    Aras, thx a lot. It's very helpful for me.