Using Custom Vertex Streams in Unity Shaders

Custom vertex streams are a nice new addition to Unity that allows you to access per particle data in shaders. This is really nice for velocity or lifetime based shader effects.

To set it up in your particle system, go to the rendering tab and enabled custom vertex streams, then add the data that you want. In brackets, it shows the semantic that this is packed into.

Capture

Declare the semantic in your appdata, making sure that you’re using the right data type. As AgePercent is packed into TEXCOORD0, normally used for particle UVs, it defaults to a float2. Not changing this caught me out!

float4 uv : TEXCOORD0;

This then needs to go into your v2f struct so you can declare the variables you’ll want to use. For the new variable (in my case age), use a new TEXCOORD.

float2 uv : TEXCOORD0;
float age : TEXCOORD1;

This can then be output from your vertex shader, using the variable name you declared in the struct and the data from appdata that matches with the information in the custom vertex streams dropdown.

o.age = v.uv.z;

This can then be used to do whatever you want in your fragment shader!

fixed4 col = i.age;

As you can see below, the particle colour is equal to the age percentage of the particle – going from 0 when it is born to 1 when it dies.

Capture2

Pretty excited to see what I can do with this!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s