Object Transforms and Coordinate Systems
🎯 Introduction
By the end of this article, you will be able to move, rotate, and scale objects, understand the x-, y-, and z-axis directions, work with local coordinates in parent-child hierarchies, and use lookAt to aim an object or camera at a target.
🧱 position、rotation、scale
Three.js uses a right-handed coordinate system: x points right, y points up, and z typically points toward the viewer. The three basic object transforms describe position, rotation, and scale.
cube.position.set(1, 0.5, -2); // Move right, up, and away from the camera
cube.rotation.set(0, Math.PI / 4, 0); // rotation uses radians
cube.scale.set(2, 1, 1); // Scale along the x-axis only
rotation is an Euler object whose three values specify rotation around the x-, y-, and z-axes. Write 90 degrees as Math.PI / 2, rather than 90.
⚡ Local and world coordinates
When you add one object to another, its position represents local coordinates relative to its parent. Moving the parent also moves the child, but the child's local position values stay the same.
const group = new THREE.Group();
group.position.x = 2;
const cube = new THREE.Mesh(geometry, material);
cube.position.x = 1; // Position relative to group
group.add(cube);
scene.add(group);
const worldPosition = new THREE.Vector3();
cube.getWorldPosition(worldPosition); // Read the cube's world position
Think of a Group as a display stand. Moving the stand moves everything on it, while each object's own position still records where it sits on the stand.
⚡ Rotation order and lookAt
When rotating around multiple axes, the order affects the result. Three-dimensional rotations cannot simply be added like two-dimensional angles. Set rotation.order when you need a specific order:
cube.rotation.order = 'YXZ';
cube.rotation.set(0.3, 0.8, 0);
lookAt calculates an orientation from a target point. Use it to aim a camera, turret, or model at a position:
camera.position.set(3, 2, 4);
camera.lookAt(0, 0.5, 0);
const target = new THREE.Vector3(0, 1, 0);
object.lookAt(target);
If the target moves in an animation loop, call lookAt again each frame. It calculates a rotation once from the current target; it does not establish a persistent link.
This demo illustrates a parent-child hierarchy. The small cube's local position stays fixed, but rotating the parent ring changes its world position and its position on screen.
🧾 Summary
position,rotation, andscaleare the most common object transforms.- Rotations use radians, and the
Eulerorder affects combined rotations. - Child transforms are local to their parent by default.
getWorldPositionandgetWorldQuaternionread world transforms.lookAtcalculates an orientation once. Call it again when the target changes.
❓ Questions and answers
Q1: How do I move an object along world axes?
Keep it outside a rotating parent, or use world-coordinate methods such as translateOnWorldAxis, taking parent transforms into account.
Q2: Does setting x, y, and z to 2 distort an object?
Using the same scale on all three axes scales uniformly without distortion. Changing just one axis stretches the object.
Q3: What if a model faces the wrong way after lookAt?
lookAt assumes a default forward direction along the negative z-axis. If the model uses a different direction, add a parent with a fixed rotation or adjust the model's initial rotation.
🧪 Exercise
Create a Group containing two spheres. Rotate the group around the y-axis, and move one sphere 2 units along the x-axis relative to the group.
// Write your code here
🎉 Transforms underpin all 3D interaction. Next, you will use cameras and controls to present a scene consistently.
