How to convert a Matrix4<f32> into a [f32; 16] using nalgebra

156 Views Asked by At

I have a Matrix4<f32> representing a transformation. I would like to convert it to a [f32; 16] to use with wgpu.

I've not managed to find the right method (if there is one at all).

1

There are 1 best solutions below

0
On

You can use as_slice() to convert it into a &[f32], and then convert that into a [f32; 16] using try_into().

Here's an example:

fn matrix_to_array(m: Matrix4<f32>) -> [f32; 16] {
    m.as_slice().try_into().unwrap()
}