Remove objects that intersect

How can I remove the cubes that intersect with the model?


Removing instances from InstancedMesh is not a trivial task.
You need to store all matrices you apply in setMatrixAt in a separate array.

If you want to remove instances, you need to remove the matrices from the array and re-apply the array to the instance mesh. Don’t forget to change .count property as well.

Can you give an example or a reference of this solution because I couldn’t find anything about it to understand how to do this

Can I store intersecting objects in a separate array?

If you’re using raycaster you can get the model’s name and remove from scene instance

Thanks for your example. It’s very helpful,
To be more clear
In this example, you defined a variable that is a number. To remove instances, how can I address the instances that collide with the model?
I mean this part:

const remove = n => { 
	if(n >= imesh.count) return;
  // delete data from arrays
	mtx.splice(n, 1); 
  clr.splice(n, 1);
get('btn').addEventListener('click', () => { remove(1); render(); });

I want to remove the cubes are intersect with model, not the models

You address them by their index in the instancedMesh, idx in your code.

In the example’s function remove it’s called n and the button click always removes an element with index 1 - remove(1).

To remove multiple elements you need first to strore all idx that intersect with your model in an array and then remake mtx and clr arrays without elements with indexes stored.

As I said it’s a relatively complex program, I can only provide a simple example.