Accessing Particle Colour in Unity Shaders

Alongside my ren’py game, I’m currently working on a campfire scene that incorporates a number of elements including vfx, shaders and prop modelling.

I’ve just started on the particle fire shader, for which I need to access particle colour. Doing this in CG/Shaderlab is a little more complicated than the “drop a particle color node” that I’m used to in unreal/shaderforge/at work!

Capture

When you change colour over life etc. in Shuriken, you are actually changing the vertex colour of the particle.

To access vertex colour, you need to first define it in the input structs for both your vertex and pixel shaders.

The syntax shown below is for an HLSL semantic, which is a string attached to a variable that indicates its intended use. This will then allow the variable to be passed between shader pipeline stages. There a a number of built in semantics that allow us to access things like the tangent or the texture coord, I’ve used the built in one for colour below.

fixed4 color : COLOR;

Once you’ve done this, you need to set the color being output from your vertex shader to the vertex colour as defined in the struct (v).

o.color = v.color;
return o;

You can then use this in the pixel shader, as long as the vertex shader (i) is being used as an input.

fixed4 col = i.color

Full code for an unlit shader that takes vertex colour below!

Shader “Unlit/color only”
{
Properties
{
}
SubShader
{
Tags { “RenderType”=”Opaque” }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog

#include “UnityCG.cginc”

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};

struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.color = v.color;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
fixed4 col = i.color
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}