Trying to get vertex color from SpriteRenderer in shader

here’s the code:

Shader "Debug/VertexColor"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" { }      // SpriteRenderer needs this
    }

    SubShader
    {
        Tags
        {
            "Queue" = "Transparent"
            "RenderType" = "Transparent"
            "RenderPipeline" = "UniversalPipeline"
        }
        
        Cull Off
        ZWrite Off
        ZTest Always
        Blend SrcAlpha OneMinusSrcAlpha
        
        Pass
        {
            Tags { "LightMode" = "Universal2D" }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct Attributes
            {
                float3 position   : POSITION;
                float4 color      : COLOR;
            };
            
            struct v2f
            {
                float4 position : SV_POSITION;
                float4 color : COLOR;
            };
            
            v2f vert(Attributes v)
            {
                v2f o;
                o.position = UnityObjectToClipPos(v.position);
                o.color = v.color;
                return o;
            }
            
            float4 frag(v2f i) : SV_Target
            {
                return i.color;
            }
            
            ENDCG
        }
    }
}

I’m using SpriteRenderer to test the material, and the returned i.color is always white.
I created another shader by ShaderGraph, with a Vertex Color node inside, and it performed well.
I created my own mesh with mesh.SetColors, and render it using MeshRenderer, this shader then can display vertex color as well.
Why this shader fails to access vertex color from SpriteRenderer?
I think there’s something I’ve missed…
btw I’m using URP 2D.

The issue you’re facing occurs because SpriteRenderer in Unity doesn’t use per-vertex color attributes directly in the same way as a MeshRenderer. Instead, it handles color by applying a uniform tint color (_Color) across the entire sprite, and this tint is not stored in the vertex attributes (COLOR semantics).

Updated Shader

Shader "Debug/VertexColor"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" { }  
        _Color ("Color Tint", Color) = (1, 1, 1, 1)  // SpriteRenderer color
    }

    SubShader
    {
        Tags
        {
            "Queue" = "Transparent"
            "RenderType" = "Transparent"
            "RenderPipeline" = "UniversalPipeline"
        }

        Cull Off
        ZWrite Off
        ZTest Always
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            Tags { "LightMode" = "Universal2D" }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct Attributes
            {
                float3 position : POSITION;
                float4 color : COLOR;
            };

            struct v2f
            {
                float4 position : SV_POSITION;
                float4 color : COLOR;
            };

            UNITY_DECLARE_COLOR;

            v2f vert(Attributes v)
            {
                v2f o;
                o.position = UnityObjectToClipPos(v.position);
                o.color = v.color * UNITY_ACCESS_COLOR; // Multiply by SpriteRenderer color
                return o;
            }

            float4 frag(v2f i) : SV_Target
            {
                return i.color;
            }

            ENDCG
        }
    }
}

Hope this helps

wow, this is the first time I realize that Vertex Color is not only the color attribute from vertex input… Thanks a lot.