Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions crates/bevy_pbr/src/render/mesh_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ fn get_previous_model_matrix(instance_index: u32) -> mat4x4<f32> {
return affine_to_square(mesh[get_instance_index(instance_index)].previous_model);
}

fn get_inverse_model_matrix(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].inverse_transpose_model_a,
mesh[instance_index].inverse_transpose_model_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_model_matrix(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;
}

fn mesh_position_local_to_world(model: mat4x4<f32>, vertex_position: vec4<f32>) -> vec4<f32> {
return model * vertex_position;
}
Expand Down