Access GPU Particle vec4 seed in Custom Effect

Hi, I am colorizing my particles with a simple fragment shader and would like to do a HSL jitter that is a little different for each particle. Is there a way to access either the seed or the id or the particle number in my fragment shader for this purpose? I saw that the GPU Vertex Shader has a vec4 seed attribute, would I be able to somehow pass that along to my custom effect fragment shader?

You can use gl_VertexID with a division to compute the particle number. Any vertex attribute can be forward to the fragment shader.

1 Like

Ah, that sounds promising – what division do you mean exactly? And I was hoping it would be easy to forward vertex attribute to the fragment shader but I don’t see that listed in the Custom Effect documentation:

Looking at existing code, it looks like it will need to forward vertex id from vertex shader to fragment. We are in a code freeze but that can be done after the 8.0 release.
Am I missing something here @Evgeni_Popov ?

A division is needed to convert from a vertex id to a particle id. vertex 0…3 for particle 0. vertex 4…7 for particle 1 and so on.

1 Like

Ah darn, I see… ok if there is no per-particle attribute I can use to uniquely colorize per-particle in the fragment shader maybe I’ll switch back to CPU particles. Thanks for the info!

1 Like

As we use instancing, you could use gl_instanceID to get a unique value per particle. However, I’m not sure this value will stay the same during the lifespan of a particle, because particles are recycled, so they may not always have the same position in the vertex stream…

Ooh that’s better than nothing! At least that is something I can use if I am doing a single-frame fixed-number particle burst (I have been using emitCount in my GPU systems instead of emitRate).

I also had an idea this morning that I haven’t tried yet – I believe there is way to tell the GPU particles to use a random cell of a spritesheet as their texture. If that is true, then I can make a spritesheet with like 100 cells and then use the particle’s uv coordinate as my seed.

It won’t be completely unique, but unique modulo 100 which is probably good enough to get the shading variation I am looking for with my HSV jitter custom effect.

Thanks!

EDIT: Isn’t gl_instanceID only available in the vertex shader? For Particle System Custom Effect I think you can only pass in a fragment shader:

Yes, gl_instanceID is only available in the vertex shader, you will have to pass it from the vertex to the fragment.

This PR will let you pass the vertex shader name:

2 Likes