Search Unity

Simple vertex and fragment shader adds unintended border

Discussion in 'Shaders' started by MikeDrako, Jan 20, 2019.

  1. MikeDrako

    MikeDrako

    Joined:
    Jun 28, 2018
    Posts:
    3
    Hi, I have created a very simple shader, and a material based on the shader.
    Then I have assigned the new material to a sprite.
    But the material/shader adds a unintended boarder to sprite, like on below picture.
    The upper object contains my new material, the lower object contains defaul material/shader for sprite (and it is ok)
    upload_2019-1-20_11-39-19.png

    My shader:
    Code (CSharp):
    1.  
    2. Shader "Hidden/NewImageEffectShader"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         // No culling or depth
    11.         Cull Off ZWrite Off ZTest Always
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.  
    19.             #include "UnityCG.cginc"
    20.  
    21.             struct appdata
    22.             {
    23.                 float4 vertex : POSITION;
    24.                 float2 uv : TEXCOORD0;
    25.             };
    26.  
    27.             struct v2f
    28.             {
    29.                 float2 uv : TEXCOORD0;
    30.                 float4 vertex : SV_POSITION;
    31.             };
    32.  
    33.             v2f vert (appdata v)
    34.             {
    35.                 v2f o;
    36.                 o.vertex = UnityObjectToClipPos(v.vertex);
    37.                 o.uv = v.uv;
    38.                 return o;
    39.             }
    40.  
    41.             sampler2D _MainTex;
    42.  
    43.             fixed4 frag (v2f i) : SV_Target
    44.             {
    45.                 fixed4 col = tex2D(_MainTex, i.uv);
    46.                 return col;
    47.             }
    48.             ENDCG
    49.         }
    50.     }
    51. }
    52.  
    What I am doing wrong?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    My first question is what are you looking to do? That shader is the default image effect shader that gets created in the inspector, which isn’t intended to be used on sprites, or really anything that exists as a renderer component in the scene. You should probably start with a sprite shader.

    However the main issue is that by default a sprite shader uses alpha blending, and that shader does not.
    https://docs.unity3d.com/Manual/SL-Blend.html
     
    MikeDrako likes this.
  3. MikeDrako

    MikeDrako

    Joined:
    Jun 28, 2018
    Posts:
    3
    It is only simple shader to show my problem with diffrent shader that I wrote (dynamic hanging of colors based on texture colors + normal maps). Thanks for your help :)