I was using perspective projection before, and my code for casting a camera ray was:
let mouse_position = /* get from events */;
let (camera, camera_transform) = /* get from query */;
let Vec2 { x: width, y: height } = camera.logical_viewport_size()?;
let x = (mouse_position.x / width) * 2.0 - 1.0;
let y = (mouse_position.y / height) * 2.0 - 1.0;
let camera_inverse_matrix =
(camera.projection_matrix() * camera_transform.compute_matrix().inverse()).inverse();
let near = camera_inverse_matrix.project_point3(Vec3::new(x, y, -1.0));
let far = camera_inverse_matrix.project_point3(Vec3::new(x, y, 1.0));
if near.is_nan() || far.is_nan() {
warn!("Unprojection failed");
return None;
}
let dir = far - near;
return dir.try_normalize()?;
From what I understand, I just had to change project_point3 to manually multiplying camera_inverse_matrix with Vec4(x, y, z, 1.0) and it would work. However, it did not. The resulting dir vector is always the same whatever my inputs are. I've seen a comment in OrthographicProjection that near and far planes are swapped, is my problem has to do something with this and "infinite reverse perspective projections" ? 😵💫