Reducing memory footprint on CPU Side with large number of thin instances

I have a scene that has potentially a few million thin instanced cubes used to render a model. After the meshes are loaded I have a large memory footprint from all of the position matrices. Is there a way to clear out all the position matrices from the thin instance meshes since the position will no longer be updated?

1 Like

Hi, after you set the buffer you can add those 2 lines

    mesh.thinInstanceSetBuffer('matrix', matrixBuffer);
    // add
    mesh.doNotSyncBoundingInfo = true;
    mesh.thinInstanceRefreshBoundingInfo(false);
5 Likes

I’ll give this a go and report back, have a few items I am addressing along with this.

cc @Evgeni_Popov

You can do:

mesh._thinInstanceDataStorage.matrixData = null;
mesh._thinInstanceDataStorage.matrixBuffer._data = null;
mesh._thinInstanceDataStorage.matrixBufferSize = 0;

to get rid of all CPU memory related to matrices.

Note, however, that picking and automatic recovery from a context lost won’t work anymore.

3 Likes

Thanks for your help guys! With these recommendations and a few tweaks throughout the code base I was able to cut back the memory usage quite a bit and boost performance.

This is a rewrite of a library I wrote a few years back. Taking advantage of Web Workers, Offscreen Canvas, and writing custom shaders. This is one of the larger samples I test with. It ends up a little over 7 million thin instances of a a cube.

As far as picking goes I used the approach provided in this post GPU picking demo and have had great performance/success with it.

2 Likes