I’m having trouble using VertexID. It definitely gives an index value, but it doesn’t seem to correspond to the particle system. I’m trying to output a 200x200 rectangle of particles, with a gradient that goes from black to red with each subsequent particle.
I have a particle system that immediately emits 40,000 particles at start, with an Infinite lifetime.
In the particle system vertex shader I have the following code:
Here’s the full shader. If you could take a quick look I’d really appreciate it.
When I assign pos to vertex.xyz nothing shows up, but when I add it then the particles appear. I’m not really sure why it’s doing that. If it matters, I’m using Unity 2019.4.12.
The incoming vertex contains the default positional data. It looks like you are replacing it with vertex data that would put all particles at 0,0,0 in the world.
Probably the correct solution is to use the Custom Vertex Streams feature in the renderer module, send the Center stream in, and set v.vertex as Center.xyz + float3(pos.x, pos.y, 0).
Your answer to ParticleID is to / 4 the VertexID. But if we have mesh-based particles with different amount of vertexes for each mesh, then how do we get ParticleID? I assume meshes are selected randomly for each particle.
Hmm that’s not so easy.
We have a custom vertex stream called MeshIndex. So you can use that to know which mesh the vertex belongs to, so you can use the appropriate mesh count. You’d have to pass the mesh counts in a Vector4 or something.
Or if you’re using a gpu instanced shader, the particle ID would be available as the InstanceID. Custom instanced particle shaders are quite advanced to set up tho, and only work on certain hardware.