Search Unity

Z Sorting Alpha shader?

Discussion in 'Shaders' started by Draco18s, Aug 31, 2011.

  1. Draco18s

    Draco18s

    Joined:
    Aug 15, 2011
    Posts:
    110
    I need a shader that will let me fade out objects from view to display objects behind them (with the same shader to allow multiple levels of alpha transitions) yet still respect the zbuffer.

    So I want to see the objects behind the transparent one, but have proper z-sorting.

    This has gotten me close, but causes weird clipping of full-alpha, but z-behind objects.

    Code (csharp):
    1. Shader "Transparent/VertexLit with Z" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     }
    6.  
    7.     SubShader {
    8.         Tags {"RenderType"="Transparent" "Queue"="Transparent"}
    9.         Pass {
    10.             Cull Front
    11.             Cull Back
    12.             ColorMask 0
    13.         }
    14.         // Render into depth buffer only
    15.         Pass {
    16.             ColorMask 0
    17.             AlphaTest Greater [_Cutoff]
    18.             SetTexture [_MainTex] {
    19.                 combine texture * primary, texture
    20.             }
    21.         }
    22.         // Render normally
    23.         Pass {
    24.             ZWrite Off
    25.             Blend SrcAlpha OneMinusSrcAlpha
    26.             ColorMask RGB
    27.             Material {
    28.                 Diffuse [_Color]
    29.                 Ambient [_Color]
    30.             }
    31.             Lighting On
    32.             SetTexture [_MainTex] {
    33.                 Combine texture * primary DOUBLE, texture * primary
    34.             }
    35.         }
    36.     }
    37. }
    Here's an image of what I get:


    1 points at an object with this shader at ~15% alpha
    2 is an object with a standard diffuse shader
    3 is an object with the above shader, but full 100% alpha. Note how the sides have been clipped by one of the objects in collection 1 (there would be more objects in this group, but due to the current scripting I have to turn on the renderer manually to see them, I'm trying to transition from a click -> visible/invisible to a click -> animated alpha tween shift).
     
  2. Draco18s

    Draco18s

    Joined:
    Aug 15, 2011
    Posts:
    110
    Anyone?
     
  3. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    I guess the important point is that you understand how difficult your problem is.

    Then we can talk further. ;)
     
  4. Draco18s

    Draco18s

    Joined:
    Aug 15, 2011
    Posts:
    110
    I do understand, the problem is getting non-technical people (e.g. my boss) to understand it. ;)
     
  5. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    What shader do you use for group 1?

    Do you need the alpha component of the framebuffer for anything of may the shaders use it for temporary data?
     
    Last edited: Sep 7, 2011
  6. Draco18s

    Draco18s

    Joined:
    Aug 15, 2011
    Posts:
    110
    The problem is, all that opaque geometry will at some point in its own lifetime also be transparent, thus I can only use one shader.

    Think Google Body and how you can turn layers on and off through a slider bar.

    Except I'm not building Google Body (I'm not, really, I swear, WHY DOES IT LOOK LIKE GOOGLE BODY!? D: Thank god I'm not responsible for the design or content. Or dropping $1000 on an anatomically correct 3D model, complete with individually modeled muscles and organs.)

    Hm. Might be able to swap shaders at runtime...will be kind of a bitch, but I think it might work.
     
  7. Draco18s

    Draco18s

    Joined:
    Aug 15, 2011
    Posts:
    110
    It's the VertexLitWithZ that's over on the unitywiki, although slightly edited to get a slightly better result (I posted it in the first post).

    Que? I'm afraid I don't understand the question. I was never too good with materials and textures and 3D in general (I couldn't model a paper bag to save my life).
     
  8. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    OK. I think you should consider using different shaders for different parts.

    In Unity, the framebuffer has color components (red, green, blue) and a depth for each pixel. Additionally, it has an alpha component. If you don't know about it, you are probably not using it.

    I thought I had an idea, but it probably is not as easy as I thought. I have to think more about it.
     
    Last edited: Sep 7, 2011
  9. Draco18s

    Draco18s

    Joined:
    Aug 15, 2011
    Posts:
    110
    I'm going to see tomorrow about setting a diffuse shader when alpha = 1.

    I understand that, but I don't get how the shader...script finds/tracks/uses those values.

    And none of that made any sense to me at all. XD
     
  10. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Not the script and the shader can use them only by making "clever" use of the blending factors: "destination alpha" = "DstAlpha" refers to the alpha component of the framebuffer (see http://unity3d.com/support/documentation/Components/SL-Blend.html ).

    Neither to me. :)