Thin Instance remove by idx

Hi!
Is there an easy way to remove a thin instance by index?

var matrix = BABYLON.Matrix.Translation(-2, 2, 0);
var idx = sphere.thinInstanceAdd(matrix);
sphere.thinInstanceRemove(idx) // this is the method I miss

I had a naive thought, that sphere.thinInstanceSetMatrixAt(idx, undefined); might work, but it throws an Error.

No there is not.

The goal of thin instances is to be fast and so you don’t have easy adding/removing. To do that, you must update the buffer of matrices yourself and pass it to the underlying function.

Maybe passing a matrix with all 0 would work to remove an instance, but that would be quite an edge case and I don’t know if it would work on all GPU…

2 Likes

Thanks for the quick response and explanation!
In this case I will use plain instances which are removable.

There’s an easy way to do this - just go into the matrixData array, and overwrite the data for the instance you want to remove with the data of the last thin instance in your set, and then decrement thinInstanceCount.

2 Likes

Thanks this is really a good solution, works perfectly!