Scene Graphs and Object Management
🎯 Introduction
By the end of this article, you will be able to organize complex models into manageable hierarchies, find objects by name or application data, control groups of objects, and calculate model centers and dimensions with bounding boxes.
🧱 Object3D is the shared base class
Scene, Camera, Mesh, and Group inherit from Object3D, so they all have properties such as position, rotation, scale, visible, parent, and children.
const building = new THREE.Group();
building.name = 'building';
const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
body.name = 'body';
body.userData = { type: 'building-part', id: 'body-01' };
const roof = new THREE.Mesh(roofGeometry, roofMaterial);
roof.name = 'roof';
building.add(body, roof);
scene.add(building);
Parent transforms affect all descendants. Setting visible = false temporarily excludes the whole subtree from rendering:
building.visible = false;
const roofFromName = building.getObjectByName('roof');
building.traverse((object) => {
if (object.isMesh) object.castShadow = true;
});
Use userData for application IDs, types, and status. Avoid attaching reactive objects, DOM nodes, or large non-serializable objects indiscriminately.
📦 add, remove, and resource ownership
An object can have only one parent. Calling add on a new parent removes it from its previous parent. Removing an object only disconnects it from the scene graph; it does not automatically release geometry, materials, or textures.
scene.remove(building);
building.traverse((object) => {
if (!object.isMesh) return;
object.geometry.dispose();
object.material.dispose();
});
When multiple meshes share a material, wait until none of them needs it before calling dispose. Otherwise, other objects lose access to that material's resources too.
📐 Box3 bounding boxes
Model origins and dimensions often do not suit a page's presentation needs. Box3 calculates an axis-aligned bounding box from an object's world transforms:
const box = new THREE.Box3().setFromObject(building);
const center = box.getCenter(new THREE.Vector3());
const size = box.getSize(new THREE.Vector3());
building.position.sub(center); // Move the model's center near the world origin
If the model already has rotation or scale, update its world matrices before calculating the bounding box:
building.updateWorldMatrix(true, true);
const box = new THREE.Box3().setFromObject(building);
This demo shows the relationship between parents, children, and bounding boxes:
🧾 Summary
Object3Dprovides shared transform and hierarchy capabilities.Groupmanages related objects as a unit.name,getObjectByName, andtraversehelp find objects and process them in batches.userDatastores lightweight application data.removedoes not release GPU resources. Clean up geometry, materials, and textures according to ownership.Box3is commonly used to calculate model centers, dimensions, and camera framing bounds.
❓ Questions and answers
Q1: Why does GPU memory usage stay the same after scene.remove(mesh)?
remove only changes the scene graph. Geometry, materials, and textures still hold GPU resources. Call their respective dispose() methods when they are no longer needed.
Q2: How do visible = false and remove differ?
visible = false preserves hierarchy and references for quick restoration. remove disconnects the parent-child relationship, but does not automatically release resources either.
Q3: Why does Box3 sometimes calculate the wrong center?
The hierarchy's world matrices may not be updated, or you may be measuring local geometry instead of the whole object tree with setFromObject.
🧪 Exercise
Create a Group containing three differently colored shapes. Set each object's name and userData.type, then use traverse to enable wireframe mode on all of them.
// Write your code here
🎉 A scene graph gives your models a clear structure instead of placing every object directly under Scene.
