Search Unity

transparent shader - need to switch on and off the zwrite

Discussion in 'Shaders' started by blagthen, Feb 26, 2015.

  1. blagthen

    blagthen

    Joined:
    Oct 21, 2014
    Posts:
    1
    Hello, i'm pretty new to all this shaders code and i've been trying for days to find a solution for my problem.
    I have found the source of the problem, but i have absolutly no idea on how i could fix it...
    So i need to have a transparent material which display like this (i used transparent vertex lit), and this is totally wht i want, but the problem comes after...


    i also need to use this material on alpha 1, but this is what i got:


    but i need this:


    I fixed the problem by turning the zwrite on, but now i have issus with the mesh when it's semi-transparent...

    So my question is:

    Is it possible to add a "Zwrite on and off" thing so when my alpha is at 255 the Zwrite is On but that as soon as my alpha is at 254 the Zwrite is turned off?
    And if so could someone help me with it?

    here is the shader i'm currently using (transparent vertex lit), and the alpha is off...

    If you need any more information don't hesitate, and a huge thank you if you find a solution this would save my life!


    Shader "Transparent/VertexLit" {
    Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _SpecColor ("Spec Color", Color) = (1,1,1,0)
    _Emission ("Emissive Color", Color) = (0,0,0,0)
    _Shininess ("Shininess", Range(0.1,1)) = 0.7
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    }
    SubShader {
    LOD 100
    Tags { "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" }
    Pass {
    Tags { "LIGHTMODE"="Vertex" "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" }
    Lighting On
    SeparateSpecular On
    Material {
    Ambient [_Color]
    Diffuse [_Color]
    Emission [_Emission]
    Specular [_SpecColor]
    Shininess [_Shininess]
    }
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    AlphaTest Greater 0
    ColorMask RGB
    SetTexture [_MainTex] { combine texture * primary double, texture alpha * primary alpha }
    }
    Pass {
    Tags { "LIGHTMODE"="VertexLM" "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" }
    BindChannels {
    Bind "vertex", Vertex
    Bind "normal", Normal
    Bind "texcoord1", TexCoord0
    Bind "texcoord", TexCoord1
    }
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    AlphaTest Greater 0
    ColorMask RGB
    SetTexture [unity_Lightmap] { Matrix [unity_LightmapMatrix] ConstantColor [_Color] combine texture * constant }
    SetTexture [_MainTex] { combine texture * previous double, texture alpha * primary alpha }
    }
    Pass {
    Tags { "LIGHTMODE"="VertexLMRGBM" "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" }
    BindChannels {
    Bind "vertex", Vertex
    Bind "normal", Normal
    Bind "texcoord1", TexCoord0
    Bind "texcoord1", TexCoord1
    Bind "texcoord", TexCoord2
    }
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    AlphaTest Greater 0
    ColorMask RGB
    SetTexture [unity_Lightmap] { Matrix [unity_LightmapMatrix] combine texture * texture alpha double }
    SetTexture [unity_Lightmap] { ConstantColor [_Color] combine previous * constant }
    SetTexture [_MainTex] { combine texture * previous quad, texture alpha * primary alpha }
    }
    }
    }
     

    Attached Files:

  2. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    The easiest way i can think of right now is having two shader passes, in the first you have zwrite off and you render your object with whatever transparency you want, this pass should not be affected by alpha changes and serves only as the reference to the lowest alpha value, say you put alpha at 0 and instead of your object disappear completely it renders like in this first pass.

    Then in the second pass your object is rendered once again but now with zwrite on and it's affected by the alpha value, this way you have a seamless transition between the two ideal cases that you need

    Just keep in mind that two passes could be expensive if your object only uses this effect at certain times. For example if this effect is just used in some sort of object creation i would use this only in that time and then switch the shader to a single pass opaque shader from then on.

    Also, this is just an idea, haven't tested yet so i don't know if it brings any problem i'm not currently seeing.

    cheers