The Matrix3.toArray()
method in Three.js is used to convert a Three.js Matrix3 object to a flat array of 9 numbers.
matrix.toArray(array, offset);
array
- An (optional) array to which the elements of the matrix will be copied. If not provided, a new array will be created.offset
- An (optional) index offset into the target array at which to begin storing values. If not provided, the array is assumed to start at index 0.The method has no return value. The elements of the matrix will be copied to the target array starting at the specified offset.
const matrix = new THREE.Matrix3();
matrix.set(
1, 0, 0,
0, 1, 0,
0, 0, 1
);
const array = [];
matrix.toArray(array);
In the above example, a new Matrix3
object is created and initialized with the identity matrix. Then, a new empty array is created and the elements of the matrix are copied to it using the toArray()
method. The resulting array will be [1, 0, 0, 0, 1, 0, 0, 0, 1]
.
toArray()
method can be useful for passing matrix data to external libraries or APIs that expect a flat array of values.toArray()
method with an existing array, care should be taken to ensure that the array is large enough to store all the values of the matrix. If the array is not large enough, the toArray()
method will throw an error.