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

Gizmo does not work with reversed z projection matrix #46

Closed
YouSafe opened this issue Feb 24, 2024 · 2 comments
Closed

Gizmo does not work with reversed z projection matrix #46

YouSafe opened this issue Feb 24, 2024 · 2 comments

Comments

@YouSafe
Copy link

YouSafe commented Feb 24, 2024

Using a reversed z projection matrix leads to the gizmo not being interactable. It might be a good idea to document this behaviour. In the meanwhile, this issue exists to potentially save someone time if they stumble upon the same issue.

@Azkellas
Copy link
Contributor

Azkellas commented Apr 2, 2024

I'm having no problem using egui-guizmo with an infinite reversed-z right handed projection matrix (computed via glam helpers). How are you using yours?

@YouSafe
Copy link
Author

YouSafe commented Apr 3, 2024

Tried it again, and now it's working for me too. Unfortunately, I lost the code that I used to try it out with but after trying different theories, I think it did not work for me because the matrix was wrong.

At the time of writing this issue, I also created a PR for my math library of choice: ultraviolet. That PR fixes the incorrect reverse-z-projection matrix (fu5ha/ultraviolet#168) and I might have just used some adapted version of the old and incorrect projection matrix for the gizmo.

This might be the code I used for generating the incorrect reverse-z projection matrix, which also reproduces the behaviour I got when I wrote this issue:

pub fn perspective_reversed_incorrect(
    vertical_fov: f32,
    aspect_ratio: f32,
    z_near: f32,
    z_far: f32,
) -> Mat4 {
    let t = (vertical_fov / 2.0).tan();
    let sy = 1.0 / t;
    let sx = sy / aspect_ratio;
    let nmf = z_near - z_far;
    Mat4::new(
        Vec4::new(sx, 0.0, 0.0, 0.0),
        Vec4::new(0.0, sy, 0.0, 0.0),
        Vec4::new(0.0, 0.0, z_far / nmf, -1.0),
        Vec4::new(0.0, 0.0, -z_near * z_far / nmf, 0.0),
    )
}

Changing the third column of the matrix in the following way makes the gizmo interactable again:

- Vec4::new(0.0, 0.0, z_far / nmf, -1.0),
+ Vec4::new(0.0, 0.0, -z_near / nmf, -1.0),

This matches the implementation of the current perspective_reversed_z_wgpu_dx_gl function (Note: $\frac{-z_n}{z_n-z_f}=\frac{-z_f}{z_n-z_f}-1$).

I apologize for wrongly assuming that this was an issue with the gizmo crate.

@YouSafe YouSafe closed this as completed Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants