9

If I remove() an Object3D from the scene, it won't be rendered but it will remain in memory. If I set that object's visible property to false it won't be rendered but it will remain in memory. What's the difference?

Context: I am experiencing performance issues when I have a lot of complex meshes in existence. Only one needs to be visible at any one time. The others are usually hidden with visible = false.

wagster
  • 498
  • 4
  • 15
  • Im facing the same, i think it is not possible due a basics function of rendering. You will need always re-calculate scene materials, lights etc... – Martin Jun 19 '15 at 10:18
  • Sorry, to clarify: the question is not about if it is possible. It is possible to remove an object from a scene and it is also possible to set an objects visibility to false. The question is - what is the technical difference between the two approaches as both will prevent the object from rendering? – wagster Jun 20 '15 at 18:39
  • My bad.Technically don't know, see the function class of remove(), but i solving to remove and visible false will not remove mesh from buffer anyway, it is still calculated in other parts of scene, which need to be reloaded again. Maybe to manage whole scene as a function and reload whole scene again.. – Martin Jun 21 '15 at 19:23

1 Answers1

10

Well, the difference is that when you remove the object in the scene it is removed from the scene, i.e. is no longer among the children there. Whereas when it's just set to invisible, it still stays in the scene data structure and can be used in calculations for example to rotate some other object towards it.

But yes for the rendering there is no difference in the end, both are ways to omit that object from drawing.

A practically useful difference is that if you need to hide & show objects a lot, setting the visible flag is quick and light whereas manipulating the scene is a bit more complex heavier operation. So to temporarily hide and object that you know you'll show again soon, is a good idea to configure the visibility flag, and to remove an object that you might not bring back anymore better remove it from the scene. Or indeed if you need it for calculations like rotating something towards it (and it perhaps moves in some hierarchy itself).

In order to actually free memory, you need to remove the object from the scene but also dispose the data it is using like shown in e.g. freeing memory in three.js

Community
  • 1
  • 1
antont
  • 2,676
  • 1
  • 19
  • 21
  • So if my scene graph contains 100 objects and only ten are visible, it's still calculating the transforms? So for performance I should really remove them? – wagster Aug 01 '15 at 23:05
  • No I was not actually referring to that, but to the cost of manipulating the scene. Regarding what you say, I checked the code and the renderer skips calculations for not visible objects so they don't probably cause a measurable perf hit: https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLRenderer.js#L3353 . I'd say that it's good to do the way that makes sense for the logic of your application. Or if you are tuning performance, profile what's optimal in your case. – antont Aug 02 '15 at 08:22
  • 1
    Ok - sounds like visible=false is the way to go for me then. It is a simpler operation and doesn't have an impact on rendering. The more complex objects get disposed of anyway. – wagster Aug 06 '15 at 13:33
  • at least if it's temporary and you're gonna bring the object back soon, flagging visiblity is nice and handy i figure. – antont Aug 06 '15 at 17:28