The extractBasis()
method of Matrix4
class in Three.js library is used to extract the basis vectors of the 3D coordinate system from the given matrix.
extractBasis(xAxis, yAxis, zAxis);
The extractBasis()
method takes three parameters:
xAxis
- An instance of Vector3
class, representing the x-axis or right-handed vector of the 3D coordinate system.yAxis
- An instance of Vector3
class, representing the y-axis or up vector of the 3D coordinate system.zAxis
- An instance of Vector3
class, representing the z-axis or forward vector of the 3D coordinate system.The extractBasis()
method does not return anything. It sets the given Vector3
objects as the basis vectors of the 3D coordinate system.
const matrix = new THREE.Matrix4();
matrix.makeRotationX(Math.PI / 2);
const xAxis = new THREE.Vector3();
const yAxis = new THREE.Vector3();
const zAxis = new THREE.Vector3();
matrix.extractBasis(xAxis, yAxis, zAxis);
console.log(xAxis); // Output: Vector3(1, 0, 0)
console.log(yAxis); // Output: Vector3(0, 0, 1)
console.log(zAxis); // Output: Vector3(0, -1, 0)
In the above example, we create a rotation matrix around the x-axis by half of the pi radians. Then we call the extractBasis()
method with three Vector3
objects representing the x, y, and z-axis of the 3D coordinate system. The extractBasis()
method updates the xAxis
, yAxis
and zAxis
objects with their respective vectors from the matrix.
The extractBasis()
method is a useful tool for converting orientation data from one format to another. The matrix representation of 3D orientation stored in Matrix4
can be converted to other formats like Euler angles or quaternions. The extractBasis()
method can be used for this conversion by extracting the orientation vectors from the matrix and plugging them into the desired format.