- Home /
How to access vertex color in a code-based URP shader?
I have a shader in HLSL where I need to get the vertex color of the mesh in the fragment shader. Is it possible in URP?
Getting vertex colors was pretty simple in the Built-in RP, both in vert/frag and surface shaders.
Here is an example of URP shader, similar to mine: https://gist.github.com/phi-lira/225cd7c5e8545be602dca4eb5ed111ba
Answer by LittleDreamerGames · Apr 12, 2020 at 02:21 AM
I was wondering the same thing! After taking a closer look at one of my shaders that stopped working with vertex colors, I noticed the color output was set to SV_Target. I changed this back to Color and it started working again!
Any chance that you want to share that shader? I am currently in need of a URP shader that enables the visibility of vertex-colors but I'm not having any luck finding proper documentation on it. :)
SV_Target in the following shader is what I changed to Color to make it work. When you import this into your game, Unity "might" try to rename Color back to SV_Target. Unity tries to be smart like that sometimes but we don't like that ;).
Shader "Game/Unlit/Vector"
{
Properties { }
SubShader
{
Tags { "Queue"="Geometry" "RenderType"="Opaque" }
Pass
{
CGPROGRA$$anonymous$$
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = fixed4(v.color.rgb, v.color.a);
return o;
}
fixed4 frag (v2f i) : Color // was SV_Target
{
return i.color;
}
ENDCG
}
}
}
That shader is not URP, right? The keyword CGPROGRA$$anonymous$$ means it's the built-in pipeline, in URP it is HLSLPROGRA$$anonymous$$.
Your answer
Follow this Question
Related Questions
Is it possible to pass scene color to Sample Texture 2D LOD (URP, Shader Graph)? 0 Answers
How to Use DrawMeshInstancedIndirect with new Render Pipeline (LDRP or HDRP) 0 Answers
Unity 2019.3 URP VR [Htc Vive] - post-processing not working properly 0 Answers
Using PostProcessing on URP + Cinemachine? 1 Answer
GPU Instancing in HDRP 0 Answers