Position, Rotation & Scale
Every object in a scene (Mesh, Group, Camera, Light) is an Object3D, and every Object3D has a transform made of three parts:
| Property | Type | Meaning |
|---|---|---|
position |
Vector3 |
where the object is, relative to its parent |
rotation |
Euler |
rotation in radians around X, Y and Z (applied in the order 'XYZ' by default) |
quaternion |
Quaternion |
the same rotation stored as a quaternion (kept in sync with rotation) |
scale |
Vector3 |
size multiplier per axis, (1, 1, 1) by default |
mesh.position.set(2, 0, -3); // x, y, z
mesh.position.y += 0.5; // components are plain numbers
mesh.rotation.y = Math.PI / 2; // 90° around the vertical axis
mesh.rotation.x = THREE.MathUtils.degToRad(30);
mesh.scale.set(1, 2, 1); // twice as tall
mesh.scale.setScalar(0.5); // half size in every directionNote:
position,rotation,quaternionandscaleare read-only properties: you can't assign a new vector (mesh.position = vthrows in strict mode). Change their components, or use.set()/.copy(v).
The order: scale, rotate, translate
Three.js combines the three parts into one 4 × 4 matrix (object.matrix) and applies them to the object's vertices in a fixed order: scale first, then rotate, then translate. That order is why scaling a rotated object stretches it along its own axes, not the world's. This plain-JS model applies the steps to one vertex both ways:
// Apply scale, rotation about Y, and translation to a point, in two different orders.
const rotY = ([x, y, z], a) => [x * Math.cos(a) + z * Math.sin(a), y, -x * Math.sin(a) + z * Math.cos(a)];
const scale = ([x, y, z], [sx, sy, sz]) => [x * sx, y * sy, z * sz];
const move = ([x, y, z], [tx, ty, tz]) => [x + tx, y + ty, z + tz];
const fmt = (v) => `(${v.map((n) => (Math.abs(n) < 1e-9 ? 0 : n).toFixed(2)).join(', ')})`;
const vertex = [1, 0, 0]; // a point on the object's local +X side
const s = [2, 1, 1], angle = Math.PI / 2, t = [5, 0, 0];
const trs = move(rotY(scale(vertex, s), angle), t); // Three.js order: S, then R, then T
const wrong = scale(rotY(move(vertex, t), angle), s); // T, then R, then S
console.log('S then R then T:', fmt(trs));
console.log('T then R then S:', fmt(wrong));S then R then T: (5.00, 0.00, -2.00)
T then R then S: (0.00, 0.00, -6.00)
With the Three.js order, the point is stretched to x = 2 along the object's own X axis, turned 90° (a positive Y rotation turns +X toward −Z), then moved 5 units: it ends at (5, 0, −2), right next to the object's new position. In the other order the translation is rotated and scaled too, and the object ends up somewhere else entirely. You rarely build matrices by hand in Three.js, but knowing this order explains a lot of "why did it move there?" moments. The TRS lesson in Math for Games covers the matrices themselves.
Local movement: translateZ and rotateY
Setting position.z -= 1 moves along the world (or parent) Z axis, whatever way the object faces. Games usually want to move along the object's own axes: "forward" for a tank is wherever its barrel points. Object3D has methods for that:
| Method | Effect |
|---|---|
translateX/Y/Z(d) |
move d units along the object's own axis |
translateOnAxis(axis, d) |
the same along any local axis |
rotateX/Y/Z(a) |
rotate a radians around the object's own axis |
getWorldDirection(v) |
write the direction the object's local +Z points to, in world space |
Drive the tank below. A/D (or Left/Right) turn it with rotateY, W/S move it with translateZ along its own forward axis. The HUD shows its world position and heading.
<style>
html, body { margin: 0; height: 100%; overflow: hidden; background: #0e1018; }
canvas { display: block; outline: none; touch-action: none; }
#hud { position: fixed; left: 10px; top: 8px; color: #fff; font: 13px system-ui, sans-serif; pointer-events: none; text-shadow: 0 1px 2px #000; }
#veil { position: fixed; inset: 0; display: grid; place-content: center; background: rgba(0, 0, 0, 0.55); color: #fff; font: 600 20px system-ui, sans-serif; text-align: center; pointer-events: none; }
#veil small { display: block; margin-top: 6px; font-weight: 400; font-size: 13px; }
#veil.off { display: none; }
</style>
<div id="hud"></div>
<div id="veil">Click to drive<small>W/S forward and back, A/D turn</small></div>
<script type="module">
import * as THREE from 'three';
// ---- 3D play kit: renderer, click to focus, keys, pause on blur, fixed-step loop ----
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.prepend(renderer.domElement);
const cv = renderer.domElement, veil = document.getElementById('veil');
const keys = new Set(), pressed = new Set(); // held now / pressed since the last update
let active = false, drawn = false;
cv.tabIndex = 0;
cv.addEventListener('pointerdown', () => cv.focus());
cv.addEventListener('focus', () => { active = true; veil.classList.add('off'); });
cv.addEventListener('blur', () => { active = false; drawn = false; veil.classList.remove('off'); keys.clear(); pressed.clear(); });
addEventListener('keydown', (e) => {
if (e.code.startsWith('Arrow') || e.code === 'Space') e.preventDefault(); // don't scroll the lesson
if (!e.repeat) pressed.add(e.code);
keys.add(e.code);
});
addEventListener('keyup', (e) => keys.delete(e.code));
function run(scene, camera, update) {
const STEP = 1 / 60, timer = new THREE.Timer();
let acc = 0;
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
drawn = false;
});
renderer.setAnimationLoop((t) => {
timer.update(t);
if (active) {
acc += Math.min(timer.getDelta(), 0.1);
while (acc >= STEP) { update(STEP); pressed.clear(); acc -= STEP; }
} else if (drawn) return; // paused: the last frame stays on screen
renderer.render(scene, camera);
drawn = true;
});
}
// ---- end of play kit ----
const scene = new THREE.Scene();
scene.background = new THREE.Color('#1b2233');
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 100);
camera.position.set(0, 8, 7);
camera.lookAt(0, 0, 0);
scene.add(new THREE.HemisphereLight('#fff', '#334', 1.4));
const sun = new THREE.DirectionalLight('#fff', 2);
sun.position.set(4, 8, 3);
scene.add(sun);
scene.add(new THREE.GridHelper(24, 24, '#5a6a88', '#2c3448'));
scene.add(new THREE.AxesHelper(2));
// The tank is a Group: a body plus a barrel that sticks out along local +Z ("forward").
const tank = new THREE.Group();
const body = new THREE.Mesh(new THREE.BoxGeometry(1.2, 0.5, 1.6), new THREE.MeshStandardMaterial({ color: '#7cb342' }));
body.position.y = 0.25;
const turret = new THREE.Mesh(new THREE.CylinderGeometry(0.4, 0.45, 0.35, 16), new THREE.MeshStandardMaterial({ color: '#558b2f' }));
turret.position.y = 0.65;
const barrel = new THREE.Mesh(new THREE.CylinderGeometry(0.08, 0.08, 1, 8), new THREE.MeshStandardMaterial({ color: '#33691e' }));
barrel.rotation.x = Math.PI / 2; // the cylinder stands along Y; lay it along Z
barrel.position.set(0, 0.65, 0.8);
tank.add(body, turret, barrel);
scene.add(tank);
const hud = document.getElementById('hud');
const forward = new THREE.Vector3();
run(scene, camera, (dt) => {
const turn = (keys.has('KeyA') || keys.has('ArrowLeft') ? 1 : 0) - (keys.has('KeyD') || keys.has('ArrowRight') ? 1 : 0);
const drive = (keys.has('KeyW') || keys.has('ArrowUp') ? 1 : 0) - (keys.has('KeyS') || keys.has('ArrowDown') ? 1 : 0);
tank.rotateY(turn * 2 * dt); // radians per second around the tank's own Y axis
tank.translateZ(drive * 4 * dt); // units per second along the tank's own +Z
tank.getWorldDirection(forward);
const deg = THREE.MathUtils.radToDeg(tank.rotation.y);
hud.textContent = `position (${tank.position.x.toFixed(1)}, ${tank.position.z.toFixed(1)}) rotation.y ${deg.toFixed(0)}° forward (${forward.x.toFixed(2)}, ${forward.z.toFixed(2)})`;
});
</script>The forward vector printed in the HUD is getWorldDirection: the tank's local +Z axis expressed in world coordinates. It is a unit vector, so position + forward * 10 is the point 10 m in front of the tank, which is how you would aim a shell or place a muzzle flash.
Tip: Decide early which local axis is "forward" for your objects and stick to it. glTF models face +Z; Three.js cameras (and
lookAton cameras and lights) use −Z. The Quaternions & lookAt lesson deals with the difference.
Euler angles and their traps
rotation is an Euler triple: three angles applied one after the other around fixed axes, in the order given by rotation.order (default 'XYZ'). Euler angles are easy to type and to show in an editor, and fine for simple cases (a turret's yaw, a door's swing). They have two problems:
- Order matters. Rotating 90° around X then 90° around Y is not the same as the reverse. For a first-person camera you want yaw (Y) applied before pitch (X), so set
camera.rotation.order = 'YXZ'. - Gimbal lock. When the middle rotation is ±90°, the first and last axes line up and one degree of freedom is lost; interpolating Euler angles near that point makes objects flip wildly.
For anything that turns freely (aircraft, physics bodies, smooth turning), use the quaternion. Three.js keeps rotation and quaternion in sync, so you can set one and read the other.
Matrices are updated for you
Before each render, renderer.render calls scene.updateMatrixWorld(), which rebuilds every object's matrix from position, rotation and scale and multiplies it by its parent's matrixWorld. So you normally just change the three properties and render. Two cases need care:
- If you read a world position in the same frame you changed things (
getWorldPosition, raycasting), the matrices may be stale. Callobject.updateMatrixWorld()first. (getWorldPosition,getWorldDirectionandgetWorldQuaterniondo this for the object itself.) - For thousands of static objects you can skip the per-frame recompute with
object.matrixAutoUpdate = false, then callobject.updateMatrix()once after placing it.
Test yourself
A tank has rotation.y = Math.PI / 2. Which call moves it 1 unit forward along its own +Z axis?
tank.position.z += 1tank.translateZ(1)tank.rotateZ(1)tank.position.set(0, 0, 1)
In what order does Three.js apply an object's transform to its vertices?
- Translate, rotate, scale
- Scale, rotate, translate
- Rotate, scale, translate
- It depends on
rotation.order
Why do first-person cameras set camera.rotation.order = 'YXZ'?
- It is faster
- So yaw (around Y) is applied before pitch (around X), and looking up never tilts the horizon
- WebGL requires it
- To avoid z-fighting