Search Unity

Need help with gradient shader on sprites with fully transparent pixels.

Discussion in 'Shaders' started by Batka, Feb 7, 2017.

  1. Batka

    Batka

    Joined:
    May 23, 2016
    Posts:
    22
    Hi,

    I've been searching around for whole day, trying to understand shaders, but they are fairly complex. :)

    I have this sprite of a spike that looks cracked:


    And when I apply my material with a gradient shader, most of the transparent pixels are filled with the produced color:


    Here is my shader code:

    Code (CSharp):
    1. Shader "Unlit/GradientSpriteShader"
    2. {
    3.         Properties{
    4.             [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    5.         _Color("Left Color", Color) = (1,1,1,1)
    6.             _Color2("Right Color", Color) = (1,1,1,1)
    7.             _Scale("Scale", Float) = 1
    8.  
    9.         }
    10.  
    11.             SubShader{
    12.             Tags{
    13.             "Queue" = "Background"
    14.             "IgnoreProjector" = "True"
    15.             "RenderType" = "Transparent"
    16.             }
    17.             LOD 100
    18.  
    19.             ZWrite On
    20.             Blend SrcAlpha OneMinusSrcAlpha
    21.  
    22.             Pass{
    23.             CGPROGRAM
    24. #pragma vertex vert
    25. #pragma fragment frag
    26. #include "UnityCG.cginc"
    27.  
    28.             fixed4 _Color;
    29.         fixed4 _Color2;
    30.         fixed  _Scale;
    31.  
    32.         struct v2f {
    33.             float4 pos : SV_POSITION;
    34.             fixed4 col : COLOR;
    35.         };
    36.  
    37.         v2f vert(appdata_full v)
    38.         {
    39.          
    40.             v2f o;
    41.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    42.             o.col = lerp(_Color,_Color2, v.texcoord.x);
    43.             return o;
    44.         }
    45.         float4 frag(v2f i) : COLOR{
    46.             float4 c = i.col;
    47.             return c;
    48.         }
    49.  
    50.  
    51.             ENDCG
    52.         }
    53.         }
    54.  
    55. }
    Please, can someone explain me what's wrong with the shader?

    Here is another example with slightly more details, but not as the original texture:


    Thanks!


    EDIT: Never give up, never back down :)
    http://answers.unity3d.com/questions/1195044/gradient-transparent.html
     
    Last edited: Feb 7, 2017
    Oniony and CrandellWS like this.