Three.js Visible Property vs Removing an Object

In Three.js, managing whether an object is displayed in your 3D scene is crucial for both performance and user experience. This article explains the fundamental differences between hiding an object using its visible property and completely removing it from the scene graph using the remove() method. By understanding how each approach affects rendering, memory, and CPU overhead, you can optimize your WebGL applications more effectively.

The visible Property

Every 3D object in Three.js inherits from the Object3D class, which includes a boolean property called visible. By default, this is set to true.

// Hiding an object
myMesh.visible = false;

// Showing the object again
myMesh.visible = true;

How it Works

When you set object.visible = false, Three.js simply skips rendering this object during the render loop. However, the object remains fully intact within the scene graph hierarchy.

Best Use Cases

Use the visible property when you need to toggle an object’s presence frequently (e.g., showing/hiding a menu, toggling UI elements, or flashing a laser beam). Because the object is never unloaded, toggling visibility is instantaneous and causes zero performance stutter.


Removing an Object (scene.remove())

To completely detach an object from the scene, you call the .remove() method on its parent node (which is often the scene itself).

// Removing an object from the scene
scene.remove(myMesh);

How it Works

Calling scene.remove(object) detaches the object from the active scene hierarchy.

Best Use Cases

Use scene.remove() when an object is no longer needed for a long period or permanently (e.g., destroying an enemy in a game, transitioning to a completely different level, or clearing a workspace).


Key Differences Summary

Feature object.visible = false scene.remove(object)
Scene Graph Object remains in the scene tree. Object is detached from the tree.
GPU/CPU Memory Fully retained in memory. Retained until garbage collected and manually disposed.
Performance (Toggling) Extremely fast; no allocation overhead. Slower; triggers scene graph rebuilding.
Matrix Calculations Still calculated by the CPU. Ignored entirely by the CPU.
Raycasting Ignored. Ignored.

To keep your Three.js applications running smoothly, use visible = false for rapid toggling of objects, and reserve scene.remove() (combined with .dispose() for materials and geometries) for permanent cleanup.