Merge pull request #29248 from Cheeseness/camera_project_fix

Add a depth parameter to Camera::project_position()
This commit is contained in:
Rémi Verschelde 2019-05-28 19:25:37 +02:00 committed by GitHub
commit 0e441e9a1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 9 deletions

View file

@ -76,8 +76,10 @@
</return> </return>
<argument index="0" name="screen_point" type="Vector2"> <argument index="0" name="screen_point" type="Vector2">
</argument> </argument>
<argument index="1" name="z_depth" type="float" default="0">
</argument>
<description> <description>
Returns the 3D point in worldspace that maps to the given 2D coordinate in the [Viewport] rectangle. Returns the 3D point in worldspace that maps to the given 2D coordinate in the [Viewport] rectangle on a plane that is the given distance into the scene away from the camera.
</description> </description>
</method> </method>
<method name="project_ray_normal" qualifiers="const"> <method name="project_ray_normal" qualifiers="const">

View file

@ -127,7 +127,7 @@ Point2 ARVRCamera::unproject_position(const Vector3 &p_pos) const {
return res; return res;
}; };
Vector3 ARVRCamera::project_position(const Point2 &p_point) const { Vector3 ARVRCamera::project_position(const Point2 &p_point, float p_z_depth) const {
// get our ARVRServer // get our ARVRServer
ARVRServer *arvr_server = ARVRServer::get_singleton(); ARVRServer *arvr_server = ARVRServer::get_singleton();
ERR_FAIL_NULL_V(arvr_server, Vector3()); ERR_FAIL_NULL_V(arvr_server, Vector3());
@ -135,7 +135,7 @@ Vector3 ARVRCamera::project_position(const Point2 &p_point) const {
Ref<ARVRInterface> arvr_interface = arvr_server->get_primary_interface(); Ref<ARVRInterface> arvr_interface = arvr_server->get_primary_interface();
if (arvr_interface.is_null()) { if (arvr_interface.is_null()) {
// we might be in the editor or have VR turned off, just call superclass // we might be in the editor or have VR turned off, just call superclass
return Camera::project_position(p_point); return Camera::project_position(p_point, p_z_depth);
} }
if (!is_inside_tree()) { if (!is_inside_tree()) {
@ -155,7 +155,7 @@ Vector3 ARVRCamera::project_position(const Point2 &p_point) const {
point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0; point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
point *= vp_size; point *= vp_size;
Vector3 p(point.x, point.y, -get_znear()); Vector3 p(point.x, point.y, -p_z_depth);
return get_camera_transform().xform(p); return get_camera_transform().xform(p);
}; };

View file

@ -55,7 +55,7 @@ public:
virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const; virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
virtual Point2 unproject_position(const Vector3 &p_pos) const; virtual Point2 unproject_position(const Vector3 &p_pos) const;
virtual Vector3 project_position(const Point2 &p_point) const; virtual Vector3 project_position(const Point2 &p_point, float p_z_depth = 0) const;
virtual Vector<Plane> get_frustum() const; virtual Vector<Plane> get_frustum() const;
ARVRCamera(); ARVRCamera();

View file

@ -391,13 +391,17 @@ Point2 Camera::unproject_position(const Vector3 &p_pos) const {
return res; return res;
} }
Vector3 Camera::project_position(const Point2 &p_point) const { Vector3 Camera::project_position(const Point2 &p_point, float p_z_depth) const {
if (!is_inside_tree()) { if (!is_inside_tree()) {
ERR_EXPLAIN("Camera is not inside scene."); ERR_EXPLAIN("Camera is not inside scene.");
ERR_FAIL_COND_V(!is_inside_tree(), Vector3()); ERR_FAIL_COND_V(!is_inside_tree(), Vector3());
} }
if (p_z_depth == 0) {
return get_global_transform().origin;
}
Size2 viewport_size = get_viewport()->get_visible_rect().size; Size2 viewport_size = get_viewport()->get_visible_rect().size;
CameraMatrix cm; CameraMatrix cm;
@ -415,7 +419,7 @@ Vector3 Camera::project_position(const Point2 &p_point) const {
point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0; point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
point *= vp_size; point *= vp_size;
Vector3 p(point.x, point.y, -near); Vector3 p(point.x, point.y, -p_z_depth);
return get_camera_transform().xform(p); return get_camera_transform().xform(p);
} }
@ -490,7 +494,7 @@ void Camera::_bind_methods() {
ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera::project_ray_origin); ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera::project_ray_origin);
ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera::unproject_position); ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera::unproject_position);
ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera::is_position_behind); ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera::is_position_behind);
ClassDB::bind_method(D_METHOD("project_position", "screen_point"), &Camera::project_position); ClassDB::bind_method(D_METHOD("project_position", "screen_point", "z_depth"), &Camera::project_position, DEFVAL(0));
ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera::set_perspective); ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera::set_perspective);
ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera::set_orthogonal); ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera::set_orthogonal);
ClassDB::bind_method(D_METHOD("set_frustum", "size", "offset", "z_near", "z_far"), &Camera::set_frustum); ClassDB::bind_method(D_METHOD("set_frustum", "size", "offset", "z_near", "z_far"), &Camera::set_frustum);

View file

@ -143,7 +143,7 @@ public:
virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const; virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const;
virtual Point2 unproject_position(const Vector3 &p_pos) const; virtual Point2 unproject_position(const Vector3 &p_pos) const;
bool is_position_behind(const Vector3 &p_pos) const; bool is_position_behind(const Vector3 &p_pos) const;
virtual Vector3 project_position(const Point2 &p_point) const; virtual Vector3 project_position(const Point2 &p_point, float p_z_depth = 0) const;
Vector<Vector3> get_near_plane_points() const; Vector<Vector3> get_near_plane_points() const;