WGSL做矩阵转换,矩阵运算
-
在着色器里做矩阵运算很常见。wgsl着色器语言里有没有能实现矩阵转换的标准库?以及类似4X4的矩阵相乘运算。
我写了下面一段代码,但是感觉过于繁琐了,是不是有常规库呢?
fn translation_matrix(v: vec3<f32>) -> mat4x4<f32>{ let c_1: vec4<f32> = vec4<f32>(1., 0., 0., v.x); let c_2: vec4<f32> = vec4<f32>(0., 1., 0., v.y); let c_3: vec4<f32> = vec4<f32>(0., 0., 1., v.z); let c_4: vec4<f32> = vec4<f32>(0., 0., 0., 1.); let translation_matrix = mat4x4<f32>(c_1, c_2, c_3, c_4); return translation_matrix; } fn rotation_matrix(q: vec4<f32>) -> mat4x4<f32> { let m11 = 2. * (q.x * q.x + q.y * q.y) - 1.; let m12 = 2. * (q.y * q.z - q.x * q.w); let m13 = 2. * (q.y * q.w - q.x * q.z); let m21 = 2. * (q.y * q.z + q.x * q.w); let m22 = 2. * (q.x * q.x + q.z * q.z) - 1.; let m23 = 2. * (q.z * q.w + q.x * q.y); let m31 = 2. * (q.y * q.w - q.x * q.z); let m32 = 2. * (q.z * q.w + q.x * q.y); let m33 = 2. * (q.x * q.x + q.w * q.w) - 1.; let c_1: vec4<f32> = vec4<f32>(m11, m21, m31, 0.); let c_2: vec4<f32> = vec4<f32>(m12, m22, m32, 0.); let c_3: vec4<f32> = vec4<f32>(m13, m23, m33, 0.); let c_4: vec4<f32> = vec4<f32>(0., 0., 0., 1.); let rotation_matrix: mat4x4<f32> = mat4x4<f32>(c_1, c_2, c_3, c_4); return rotation_matrix; }