Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Z-order issue with Transparent Vertex Shader

Discussion in 'Shaders' started by malu05b, Jan 4, 2015.

  1. malu05b

    malu05b

    Joined:
    Apr 30, 2013
    Posts:
    13
    I have a bit of a problem with Z-ordering on my shader here.
    If i have the shader alone with a blank background i see it clearly, however if something comes behind it, it will be put ontop of the object with the shader on.

    if i take the #pragma and remove the alpha part the Z-ordering issue is fixed but offcause sadly also disables transparency.

    Can anyone point out what i am missing to get the Z-ordering back in place?

    Code (CSharp):
    1.   Shader "Example/Displacement" {
    2.     Properties {
    3.       _MainTex ("Texture", 2D) = "white" {}
    4.       _DisplaceTex ("Displacement", 2D) = "white" {}
    5.       _Amount ("Extrusion Amount", Range(-1,1)) = 0.5
    6.     }
    7.     SubShader {
    8.       Tags { "RenderType" = "Transparent" }
    9.       CGPROGRAM
    10.      
    11.       //REMOVING "alpha" from the following statement will fix the zorder issue
    12.       #pragma surface surf Lambert alpha vertex:vert
    13.      
    14.       uniform float4 _DisplaceTex_ST;
    15.       struct Input {
    16.           float2 uv_MainTex;
    17.       };
    18.       float _Amount;
    19.       sampler2D _DisplaceTex;
    20.  
    21.       void vert (inout appdata_full v) {
    22.             float2 sh_uv =  v.texcoord.xy * _DisplaceTex_ST.xy + _DisplaceTex_ST.zw;
    23.           float4 sh_tex = tex2Dlod(_DisplaceTex, float4(fmod(sh_uv.x,0.999999f),sh_uv.y,0,0));
    24.           v.vertex.xyz += v.normal * _Amount * sh_tex.y;
    25.       }
    26.       sampler2D _MainTex;
    27.       void surf (Input IN, inout SurfaceOutput o) {
    28.           o.Emission = tex2D (_MainTex, IN.uv_MainTex).rgb;
    29.           o.Alpha = 1.0f; //tex2D (_MainTex, IN.uv_MainTex).w;
    30.       }
    31.       ENDCG
    32.     }
    33.     Fallback "Diffuse"
    34.   }
     
  2. CountFrolic

    CountFrolic

    Joined:
    Mar 15, 2013
    Posts:
    20