Search Unity

Unlit Transparent Texture Shader on Quad.

Discussion in 'Shaders' started by SENSEARENA, Mar 23, 2020.

  1. SENSEARENA

    SENSEARENA

    Joined:
    Jan 11, 2019
    Posts:
    7
    Hello,
    I am trying to create a shader for my 2D fans that are renderer on Quad. I am facing an issue with depth sorting.
    When I have overlapping items the sorting is wrong.
    upload_2020-3-23_16-55-31.png
    Same goes for UI
    upload_2020-3-23_16-56-20.png

    My shader is here
    Code (CSharp):
    1. Shader "Unlit/TransparentCustom" {
    2. Properties {
    3.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.     _Color ("Color", Color) = (1,1,1,1)
    5. }
    6.  
    7. SubShader {
    8.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    9.     LOD 100
    10.  
    11.     ZWrite Off
    12.     Blend SrcAlpha OneMinusSrcAlpha
    13.  
    14.     Pass {
    15.         CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #pragma target 2.0
    19.             #pragma multi_compile_fog
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata_t {
    24.                 float4 vertex : POSITION;
    25.                 float2 texcoord : TEXCOORD0;
    26.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    27.             };
    28.  
    29.             struct v2f {
    30.                 float4 vertex : SV_POSITION;
    31.                 float2 texcoord : TEXCOORD0;
    32.                 float height : TEXCOORD1;
    33.                 UNITY_FOG_COORDS(1)
    34.                 UNITY_VERTEX_OUTPUT_STEREO
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.             fixed4 _Color;
    40.  
    41.             v2f vert (appdata_t v)
    42.             {
    43.                 v2f o;
    44.                 UNITY_SETUP_INSTANCE_ID(v);
    45.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    46.                 o.vertex = UnityObjectToClipPos(v.vertex);
    47.                 float3 worldPos = mul (unity_ObjectToWorld, v.vertex).xyz;
    48.                 o.height = worldPos.y;
    49.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    50.                 UNITY_TRANSFER_FOG(o,o.vertex);
    51.                 return o;
    52.             }
    53.  
    54.             fixed4 frag (v2f i) : SV_Target
    55.             {              
    56.                 //fixed4 col =(1-lerp(0,0.8,i.height/20))*tex2D(_MainTex, i.uv);
    57.                 fixed4 col = _Color*(1-lerp(0.3,1,i.height/25));
    58.                 col.a = 1;
    59.                 col = col* tex2D(_MainTex, i.texcoord);
    60.                 UNITY_APPLY_FOG(i.fogCoord, col);
    61.                 return col;
    62.             }
    63.         ENDCG
    64.     }
    65. }
    66.  
    67. }
    68.  
    Can you help me with it?
    Thanks.
     
  2. SENSEARENA

    SENSEARENA

    Joined:
    Jan 11, 2019
    Posts:
    7
    I fixed it by using Opaque and Alpha to Mask


    Code (CSharp):
    1. Shader "Unlit/TransparentCustom" {
    2. Properties {
    3.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    4.     _Color ("Color", Color) = (1,1,1,1)
    5. }
    6.  
    7. SubShader {
    8. Tags { "RenderType"="Opaque" }
    9.     LOD 100
    10.  
    11.     Pass {
    12.         AlphaToMask On
    13.         CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #pragma target 2.0
    17.             #pragma multi_compile_fog
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata_t {
    22.                 float4 vertex : POSITION;
    23.                 float2 texcoord : TEXCOORD0;
    24.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    25.             };
    26.  
    27.             struct v2f {
    28.                 float4 vertex : SV_POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.                 float height : TEXCOORD1;
    31.                 UNITY_FOG_COORDS(1)
    32.                 UNITY_VERTEX_OUTPUT_STEREO
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.             fixed4 _Color;
    38.  
    39.             v2f vert (appdata_t v)
    40.             {
    41.                 v2f o;
    42.                 UNITY_SETUP_INSTANCE_ID(v);
    43.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 float3 worldPos = mul (unity_ObjectToWorld, v.vertex).xyz;
    46.                 o.height = worldPos.y;
    47.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    48.                 UNITY_TRANSFER_FOG(o,o.vertex);
    49.                 return o;
    50.             }
    51.  
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {              
    54.                 //fixed4 col =(1-lerp(0,0.8,i.height/20))*tex2D(_MainTex, i.uv);
    55.                 fixed4 col = _Color*(1-lerp(0.3,1,i.height/25));
    56.                 col.a = 1;
    57.                 col = col* tex2D(_MainTex, i.texcoord);
    58.                 UNITY_APPLY_FOG(i.fogCoord, col);
    59.                 return col;
    60.             }
    61.         ENDCG
    62.     }
    63. }
    64.  
    65. }
    66.  
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    If you're not using MSAA,
    AlphaToMask On
    on desktop is the same as using an alpha tested shader. On mobile it may not do anything at all. But you should probably add this to your properties:
    [HideInInspector] _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5

    And add this to your shader just after sampling the texture:
    clip(col.a - _Cutoff);


    Also for opaque materials that use alpha testing you shouldn't be using
    "RenderType"="Opaque"
    , but rather:
    "Queue"="AlphaTest" "RenderType"="TransparentCutout"


    Alpha testing is more expensive to render than fully opaque objects, as well as make all objects that are rendered after one alpha tested object slightly more expensive to render, so it's good practice to make sure all fully opaque objects render before any alpha tested object. All of the above changes will also ensure things work with post processing properly.
     
  4. SENSEARENA

    SENSEARENA

    Joined:
    Jan 11, 2019
    Posts:
    7
    I am using MSAA 4x on mobile for VR so AlphaToMask is good for me. Thanks for the advice anyways :)