Jump to content

How to remove mesh from scene in Babylon.js in runtime?


Recommended Posts

  • 2 years later...

It seems I was wrong in my understanding of the dispose function, it doesn't remove the object it's self only it's data from the scene, the object is still there, I can not find a way to remove the object, I set it to null for garbage collection but I am still having the draw calls going up so must be something else, back to debugging my code.. fun times..

Does BJS have a method for completely destroying an object by chance? Thanks!

Link to comment
Share on other sites

var terratiles = scene.getMeshesByTags("terra");
var meshes = scene.getMeshesByTags("mesh");
	for(var index=0; index < terratiles.length; index++){ //remove tiles outside of max range
	var tt = terratiles[index];
	var ttid = tt.id;
			if(Math.round(getdist({x: tt.position.x, y: tt.position.y, z: tt.position.z},{x: actor.physicsImpostor.getObjectCenter().x, 
			y: actor.physicsImpostor.getObjectCenter().y, z: actor.physicsImpostor.getObjectCenter().z})/terrasize)*terrasize > terrasize*6){
			var ttobs = scene.getMeshesByTags(tt.id+'_mesh');
				var eid = e.id;
					if(eid.indexOf(ttid) > -1){e.dispose();}
				if(ttobs.length <= 0){tt.dispose();}


At this point I have discovered for sure it isn't my problem however and it appears to be functioning just fine.
But basically what is going on there is it is checking distance from camera to the terrain tile, if it falls outside the max distance it removes all items associated with the tile then it removes the tile. Then I leave all variables used as null for GC.

I am still trying to hunt down my draw call / memory leak, so frustrating..
I thought .dispose completely removes the object but it doesn't, I think it leaves the rest to GC, I can simply add tt=null for that, which I do already, so no issue there.

Link to comment
Share on other sites

  • 1 year later...


I think there is a bug with dispose() with physicsImpostor  =>  in STABLE release, NOT in latest

I can reproduce it : https://www.babylonjs-playground.com/indexstable#JS6FR6#1

If we comment the impostor, it works, otherwise it freeze and we got this in console :

Uncaught TypeError: Cannot read property 'copyFrom' of undefined
    at r.i.computeWorldMatrix (babylon.js:8)
    at r.i.getPositionExpressedInLocalSpace (babylon.js:7)
    at r.i.translate (babylon.js:8)
    at World.beforeStep (babylon.js:39)
    at World.dispatchEvent (cannon.js:10576)
    at World.internalStep (cannon.js:13557)
    at World.BJSCANNON.World.step (babylon.js:40)
    at t.executeStep (babylon.js:40)
    at t._step (babylon.js:40)
    at i.render (babylon.js:12)

I opened an issue on github

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...