In Three.js, you can group objects together using THREE.Group. A THREE.Group is a special type of Object3D that acts as a container for other objects. When you add objects to a group, their transformations (position, rotation, scale) become relative to the group's transformation. This allows you to move, rotate, or scale multiple objects as a single unit.
THREE.GroupTHREE.Group instance:const group = new THREE.Group();
You can add any Object3D (like Mesh, Light, Camera, or even other Groups) to a group using its add() method.
const boxGeometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const boxMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
const box = new THREE.Mesh(boxGeometry, boxMaterial);
box.position.set(-0.75, 0, 0); // Position relative to the group
const sphereGeometry = new THREE.SphereGeometry(0.4, 32, 32);
const sphereMaterial = new THREE.MeshBasicMaterial({ color: 0x0000ff });
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.set(0.75, 0, 0); // Position relative to the group
group.add(box);
group.add(sphere);
Once objects are added to the group, you add the group itself to your scene.
scene.add(group);
Now, any transformations applied to the group will affect all its children.
group.position.set(0, 0, 0); // Moves the entire group
group.rotation.y = Math.PI / 4; // Rotates the entire group
group.scale.set(1.5, 1.5, 1.5); // Scales the entire group
This example demonstrates how to create a group of objects and apply transformations to the entire group.