Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Color Tint shader for vector sprite

Discussion in 'Shaders' started by ByteflowXxX, Mar 5, 2021.

  1. ByteflowXxX

    ByteflowXxX

    Joined:
    Dec 5, 2017
    Posts:
    14
    Hello!

    Unity 2020.2.7
    Vector Graphics 2.0.0 preview 14


    My shader works like this: if color alpha is 255 - I can tint any color.
    If color alpha is 0 - the original color.
    It works great if the .svg generated asset type is a textured sprite.
    But if I switch to the generated asset type - vector sprite, my shader doesn't work. How to fix it?
    Thanks

    Code (CSharp):
    1. Shader "Custom/TintSprite"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
    6.         _Color("Tint", Color) = (1,1,1,1)
    7.         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
    8.     }
    9.  
    10.         SubShader
    11.         {
    12.             Tags
    13.             {
    14.                 "Queue" = "Transparent"
    15.                 "IgnoreProjector" = "True"
    16.                 "RenderType" = "Transparent"
    17.                 "PreviewType" = "Plane"
    18.                 "CanUseSpriteAtlas" = "True"
    19.             }
    20.  
    21.             Cull Off
    22.             Lighting Off
    23.             ZWrite Off
    24.             Fog { Mode Off }
    25.             Blend One OneMinusSrcAlpha
    26.  
    27.             Pass
    28.             {
    29.             CGPROGRAM
    30.                 #pragma vertex vert
    31.                 #pragma fragment frag
    32.                 #pragma multi_compile DUMMY PIXELSNAP_ON
    33.                 #pragma multi_compile_instancing
    34.                 #include "UnityCG.cginc"
    35.  
    36.                 struct appdata_t
    37.                 {
    38.                     float4 vertex   : POSITION;
    39.                     float4 color    : COLOR;
    40.                     float2 texcoord : TEXCOORD0;
    41.                 };
    42.  
    43.                 struct v2f
    44.                 {
    45.                     float4 vertex   : SV_POSITION;
    46.                     fixed4 color : COLOR;
    47.                     half2 texcoord  : TEXCOORD0;
    48.                 };
    49.  
    50.                 fixed4 _Color;
    51.  
    52.                 v2f vert(appdata_t IN)
    53.                 {
    54.                     v2f OUT;
    55.                     OUT.vertex = UnityObjectToClipPos(IN.vertex);
    56.                     OUT.texcoord = IN.texcoord;
    57.                     OUT.color = IN.color * _Color;
    58.                     #ifdef PIXELSNAP_ON
    59.                     OUT.vertex = UnityPixelSnap(OUT.vertex);
    60.                     #endif
    61.  
    62.                     return OUT;
    63.                 }
    64.  
    65.                 sampler2D _MainTex;
    66.  
    67.                 fixed4 frag(v2f IN) : SV_Target
    68.                 {
    69.                     fixed4 c = tex2D(_MainTex, IN.texcoord);
    70.                     fixed a = c.a;
    71.                     if (c.a >= 0.1) {
    72.                         c = c + (IN.color - c) * IN.color.a;
    73.                         c.a = a;
    74.                     }
    75.                     c.rgb *= c.a;
    76.                     return c;
    77.                 }
    78.             ENDCG
    79.             }
    80.         }
    81. }
    1.png

    2.png


    3.png
     
    Last edited: Mar 5, 2021
  2. whaledevelop

    whaledevelop

    Joined:
    Dec 10, 2018
    Posts:
    2
    Love this. Again and again finding needed questions with no answers within years
     
  3. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,546
    maybe vector sprite mode doesn't use vertex colors?