InstancedMesh color doesn't work when initial count is 0

I am trying to dynamically change the number of instances in an InstancedMesh. Because the buffers cannot change size, when the count gets too large I create new instanceMatrix and instanceColor buffer attributes.

This works fine: (click the + and - buttons to add/remove cubes)

However, if you change the initial number of instances to 0 by changing line 26 (the parameter to new InstancedMesh()), then all the future instances are white (the material’s base color).

How can I start with 0 instances, and dynamically grow the number of instances, while keeping instanceColor working?

The initial “count” of InstancedMesh is the maximum it can allocate later. You’ll need to construct it with a larger number, and reduce the count afterward. In general, geometry-related resources on the GPU cannot be increased in size.

I thought reallocating the attribute buffers would take care of this issue, and it does work for growing above 1 if the initial count is 1. What other resources are involved here? Would it make sense to consider ability to change the size a feature request?

I determined that reallocating the material works to fix this issue: jovial-christian-x6oldd - CodeSandbox

It seems like setting material.needsUpdate = true; also works.

The documentation for InstancedMesh.count is pretty specific about this requirement… I don’t know what allocations might allow you to override it, but I’d guess any workarounds like that may be fragile against future releases. Increasing size is not intended to be supported. You can simply do this instead:

const mesh = new InstancedMesh( geom, mat, maxCount );
mesh.count = 0;

// ...

I actually still had to reset the material as well, or the new mesh showed the same problem.

…is basically the same as mesh.geometry = newGeometry; which is why they are not going to support it.