Updating meshes frequently+efficiently at runtime?

So I’m trying to build a very simple decal system. Just drawing a load of quads. Where the number of quads can change on a frame-to-frame basis.

This should be trivial - I want to be able to pre-allocate a buffer of, say, a maximum limit of MAX_DECALS*4 vertices. And on any given frame, I’ll update the first N vertices in the buffer, then render them - using the first N indices in a pre-allocated index buffer (big enough for MAX_DECALS)

But Unity doesn’t appear to allow this…

Whenever the number of verts in a mesh changes, I’ve got to provide positions/normals/UVs/colours in arrays of the exact size. You can’t say ‘take this range from this array’. And I’ve got to do the same with indices. Truckloads of garbage incoming!..

I can’t see any decent workarounds. I suppose I can avoid re-allocating the vertices, and only do the indices - but then Unity will still be copying the full-length buffer around internally, even if I’m only drawing a quad.

And there’s uglier solutions involving drawing degenerate polygons rather than reallocating - drawing the whole buffer every frame, but putting all the unused verts at the same position. But that’s not a great solution either, especially on mobile.

I’ve not missed any nicer solutions have I?..

It’d be really nice to see some improvements to this interface, to make this sort of use-case more efficient. As my case (decals) really isn’t a one-off. It’s an issue faced by custom UI systems, sprite systems, particle-like-effects, and so on.

Interesting question. I don’t have a solution for you, but just wanted to ask if you’d tried the degenerate triangles approach and profiled how much overhead the degenerate tris actually have? Just curious since that would have been my first approach to the problem.

Why not use the GL API? Doesn’t seem a lot of point using a retained mode Mesh API if you’re really after an immediate mode solution.

I’ll admit that I’d almost forgotten that that existed…

But for now, I’ve got things working fairly well by using a larger-than-necessary buffer, and filling the end of the index buffer with degenerate polygons. I start with a fairly small buffer, and grow it by a couple of hundred verts at a time, and also resize back down if the vertex count drops significantly.

I’m also only updating the buffers when a decal is added or removed - I now fade out old decals in a vertex shader, by storing the despawn time of each decal in it’s secondary UV set.

So the reallocations should now be fairly infrequent, and the updates aren’t every single frame - I haven’t profiled on a mobile device yet, but profiling in the editor looks promising, it’s a lot less spiky now.