How to modify shape.mesh for particle system at runtime

I want to do essentially this in a periodic coroutine:

this.updateVertices(vertices); // This permutes the vertex array
var shape = particleSystem.shape;
shape.mesh.vertices = vertices;

But, the particle renderer seems to completely ignore the updated vertices and continues to emit using the original mesh shape. I’ve also tried recreating the entire mesh and setting shape.mesh = mesh, which also does not work.

Using the same approach (even the same vertex array) to permute a MeshFilter\MeshRenderer works fine:

this.DebugRenderer.sharedMesh.vertices = vertices;

I can’t tell whether this is even supported or whether I need to be calling some update or recalc method or whether there’s some other trick to convince the particleSystem to update its emission shape. If anyone has advice, I’d be very grateful.

First thing I would try is disabling the PS, changing the mesh, then re-enabling it. Perhaps it caches the mesh.

If that doesn’t work, try running your game, the in the inspector dropping a different mesh (any mesh!) into the slot in the PS inspector, see if it changes.

Something is going to give you some intel or insight from the above two steps.

1 Like

I hadn’t even considered changing the mesh in the inspector; I’m so not used to being able to make changes to a scene at runtime! That did work, as did the enable\disable.

In fact, as I looked closer, it worked even without the enable\disable. I’m permuting z values and for some reason, the z-scaling being applied to the emission mesh is dramatically (like 100x) smaller than the scaling being applied when I render the mesh directly. Given the inherent randomness\fuzziness of a particle emission plane, I just couldn’t see the changes. When I multiplied the translation by 100, it was obviously working. I’ll see if I can figure out why the mesh is behaving so differently on two different objects with seemingly identical transforms.

All that said, thanks so much for the debugging tip. It made me realize the issue here and will be really useful in the future as well.

1 Like

You’re welcome. Once you start thinking about changing stuff in game at runtime, your brain will expand in so many ways you may need to invest in a new skull. It’s amazingly powerful, and only gets more powerful with creative applications of it.

1 Like

To resolve this, I had set the scale for the shape to 100,100,1, which was essentially hyper compressing the z axis (or, I guess, massively scaling the other two, but same diff) and creating the minimal visual movement for my mesh permutations.

As an interesting side note, I’m fairly confident the distribution of particles is computed based on the mesh surface area of the unscaled mesh, but they’re actually placed in world space based on the scaled mesh. While that initially sounds like an irrelevant bug, it actually means you can control the particle density fairly precisely using your mesh: if you have a big, flat mesh with very high peaks (that you mostly scale away in the particle system shape), you’ll end up with a ~flat plane of particles where they’re dense at the ‘peaks’ and sparse elsewhere. That’s not what I want for this application, but it’s actually kinda a cool way to control particle density, so I thought I’d mention it in case others find it useful.

1 Like