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.