How do I delete lots of meshes?

I have created several hundred instances of houses in my scene (using Mesh.createInstance). I want to only delete the houses, excluding other meshes like the terrain for example.

What is the best way to do this without having to remember the houses that I created? Is there anything like a “layer” concept so that BJS could delete all meshes on a certain layer? I could assign a layer to the houses and I would then simply delete everything on that layer.

Hi mattes3,

when you want to save everything to a layer, why not just saving it to an array? While creating the instances you can make housesArray.push(house) and when you want to delete all houses you just do housesArray.forEach(function (house){house.dispose();});

1 Like

Alternatively you can add your own parameter to the houses. While creating them you can just do something like:

house.layer = “houseLayer”;

and when you want to delete them you can do:

if(m.layer == “houseLayer”){

I agree with @CLB_ZReality, it seems best to track this yourself via an array or other datastructure and create methods to manage them.

When you load your mesh into the scene, there is no concept of what type of mesh it is or “layer” it should belong to (unlike an authoring program where this is more significant), that type of logic seems best left to the business logic to manage itself, rather than the engine.