Helper function for getting inverse model matrix in WGSL shaders (#10462)

In 0.11 you could easily access the inverse model matrix inside a WGSL
shader with `transpose(mesh.inverse_transpose_model)`. This was changed
in 0.12 when `inverse_transpose_model` was removed and it's now not as
straightfoward. I wrote a helper function for my own code and thought
I'd submit a pull request in case it would be helpful to others.
This commit is contained in:
Jak Kos 2025-03-06 16:43:22 +00:00 committed by GitHub
parent a85a3a2a15
commit 47509ef6a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,6 +22,33 @@ fn get_previous_world_from_local(instance_index: u32) -> mat4x4<f32> {
return affine3_to_square(mesh[instance_index].previous_world_from_local);
}
fn get_local_from_world(instance_index: u32) -> mat4x4<f32> {
// the model matrix is translation * rotation * scale
// the inverse is then scale^-1 * rotation ^-1 * translation^-1
// the 3x3 matrix only contains the information for the rotation and scale
let inverse_model_3x3 = transpose(mat2x4_f32_to_mat3x3_unpack(
mesh[instance_index].local_from_world_transpose_a,
mesh[instance_index].local_from_world_transpose_b,
));
// construct scale^-1 * rotation^-1 from the 3x3
let inverse_model_4x4_no_trans = mat4x4<f32>(
vec4(inverse_model_3x3[0], 0.0),
vec4(inverse_model_3x3[1], 0.0),
vec4(inverse_model_3x3[2], 0.0),
vec4(0.0,0.0,0.0,1.0)
);
// we can get translation^-1 by negating the translation of the model
let model = get_world_from_local(instance_index);
let inverse_model_4x4_only_trans = mat4x4<f32>(
vec4(1.0,0.0,0.0,0.0),
vec4(0.0,1.0,0.0,0.0),
vec4(0.0,0.0,1.0,0.0),
vec4(-model[3].xyz, 1.0)
);
return inverse_model_4x4_no_trans * inverse_model_4x4_only_trans;
}
#endif // MESHLET_MESH_MATERIAL_PASS
fn mesh_position_local_to_world(world_from_local: mat4x4<f32>, vertex_position: vec4<f32>) -> vec4<f32> {