Vector3
Quick answer
Vector3 stores x, y, z and serves as position, direction, normal, and scale throughout three.js. Chainable methods like set(), add(), cross(), normalize(), applyMatrix4(), and project() make it the workhorse type for geometry and camera math. Remember that these methods mutate the vector and return this.
new THREE.Vector3 ( x , y , z ) → Vector3
Key points
- cross() overwrites the vector with the cross product, while dot() returns a number.
- distanceToSquared() avoids a square root when only comparing distances.
- project() and unproject() convert between world and normalized device coordinates.
Example
const a = new THREE.Vector3(1, 2, 3);
const b = new THREE.Vector3(4, 0, -2);
const mid = new THREE.Vector3().addVectors(a, b).multiplyScalar(0.5);
const offset = new THREE.Vector3().subVectors(b, a);
const dist = offset.length();
const dir = offset.normalize(); // 注意:normalize 会改写 offset
mesh.position.copy(mid);
mesh.lookAt(b);const R = 6371000; // 地球平均半径,单位米
function lonLatToVector3(lon, lat, radius) {
const phi = THREE.MathUtils.degToRad(90 - lat);
const theta = THREE.MathUtils.degToRad(lon);
return new THREE.Vector3(
radius * Math.sin(phi) * Math.cos(theta),
radius * Math.cos(phi),
radius * Math.sin(phi) * Math.sin(theta)
);
}
const beijing = lonLatToVector3(116.39, 39.9, 1);
Notes
- Assuming Vector3 picks a unit: choose meters or degrees and never mix scales in one scene.
- equals() compares exact floats, so near-identical results may report false.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). Vector3. https://www.jingweizhi.com/en/three/api/vector3.html (accessed 2026-11-03).