Search Unity

Share - Transparent Sorting

Discussion in 'Scripting' started by AllanMSmith, Nov 25, 2014.

  1. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Hey,

    Im new to shaders but Im trying to achieve a very simple ambient lit transparent shader. The problem I have is represented on the image bellow.



    Either the transparent pixels make transparent objects on the background invisible, or back objects are rendered last, in front of front objects. My shader code is as follows:

    Code (CSharp):
    1. Shader "Diffuse Ambient Lit Transparent" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     }
    5.     SubShader {
    6.         Pass {
    7.             Tags { "RenderType" = "Transparent" "Queue" = "Transparent" "IgnoreProjector"="True"}
    8.             ZWrite Off
    9.             Blend SrcAlpha OneMinusSrcAlpha
    10.             CGPROGRAM
    11.             #pragma vertex vert
    12.             #pragma fragment frag
    13.          
    14.             sampler2D _MainTex;
    15.             fixed4 _MainTex_ST;
    16.          
    17.             struct vertexInput {
    18.                 float4 vertex : POSITION;
    19.                 float4 texcoord0 : TEXCOORD0;
    20.             };
    21.          
    22.             struct vertexOutput {
    23.                 float4 pos : SV_POSITION;
    24.                 fixed2 uv : TEXCOORD0;
    25.             };
    26.          
    27.             vertexOutput vert(vertexInput v) {
    28.                 vertexOutput o;
    29.              
    30.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    31.                 o.uv = v.texcoord0;
    32.              
    33.                 return o;
    34.             }
    35.          
    36.             fixed4 frag (vertexOutput i) : COLOR {
    37.                 fixed4 finalColor = tex2D(_MainTex, i.uv) * (UNITY_LIGHTMODEL_AMBIENT * 2);
    38.                 return finalColor;
    39.             }
    40.             ENDCG
    41.         }
    42.     }
    43. }
    The difference between screenshots is only turning On or Off the ZWrite.

    Anybody can give me a hand?

    Thanks in advance,
    Best regards,
    Allan
     
  2. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Sorry for the bump but I think it should be a relatively simple thing and yet I apparently cannot find the solution =/

    Anyway, thanks again for the attention
    Cya
     
  3. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Not much of a shader programmer, but vaguely remember some shaders that drop the pixel if alpha is less than cutoff value, something like

    Code (csharp):
    1.  
    2. if ( finalColor.a < _CutOff )
    3. return;
    4.  
    inside the fragment shader...
     
  4. AllanMSmith

    AllanMSmith

    Joined:
    Oct 2, 2012
    Posts:
    180
    Thx image28, it does solve the main problem, however it creates a new problem (actually 2). First, I hear that using Ifs inside shaders is not really good because of how the GPU reads it and any code ramifications make it considerably slower, but main problem is, because this is kind of a cutout, the edges are really ugly. Is there some sort of... "partial" discard? haha, I mean, all I want is a result similar to alpha blended but apparently its not as easy as I thought it would be.

    Anyway, thx for your attention,
    Best regards,
    Allan