Skip to content

Commit

Permalink
Fix Camera3D project_* methods not accounting for frustum offset
Browse files Browse the repository at this point in the history
This does not fix Camera3D::project_ray_normal().
Adds Camera3D::get_camera_projection() and exposes it to GDScript
  • Loading branch information
bcmpinc committed Apr 10, 2023
1 parent c151d32 commit b0aa194
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
6 changes: 6 additions & 0 deletions doc/classes/Camera3D.xml
Expand Up @@ -29,6 +29,12 @@
Returns the transform of the camera plus the vertical ([member v_offset]) and horizontal ([member h_offset]) offsets; and any other adjustments made to the position and orientation of the camera by subclassed cameras such as [XRCamera3D].
</description>
</method>
<method name="get_camera_projection" qualifiers="const">
<return type="Projection" />
<description>
Returns the camera projection matrix.
</description>
</method>
<method name="get_cull_mask_value" qualifiers="const">
<return type="bool" />
<param index="0" name="layer_number" type="int" />
Expand Down
67 changes: 32 additions & 35 deletions scene/3d/camera_3d.cpp
Expand Up @@ -170,6 +170,29 @@ Transform3D Camera3D::get_camera_transform() const {
return tr;
}

Projection Camera3D::_get_camera_projection(real_t p_near) const {
Size2 viewport_size = get_viewport()->get_visible_rect().size;
Projection cm;

switch (mode) {
case PROJECTION_PERSPECTIVE: {
cm.set_perspective(fov, viewport_size.aspect(), p_near, far, keep_aspect == KEEP_WIDTH);
} break;
case PROJECTION_ORTHOGONAL: {
cm.set_orthogonal(size, viewport_size.aspect(), p_near, far, keep_aspect == KEEP_WIDTH);
} break;
case PROJECTION_FRUSTUM: {
cm.set_frustum(size, viewport_size.aspect(), frustum_offset, p_near, far);
} break;
}

return cm;
}

Projection Camera3D::get_camera_projection() const {
return _get_camera_projection(near);
}

void Camera3D::set_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far) {
if (!force_change && fov == p_fovy_degrees && p_z_near == near && p_z_far == far && mode == PROJECTION_PERSPECTIVE) {
return;
Expand Down Expand Up @@ -286,8 +309,7 @@ Vector3 Camera3D::project_local_ray_normal(const Point2 &p_pos) const {
if (mode == PROJECTION_ORTHOGONAL) {
ray = Vector3(0, 0, -1);
} else {
Projection cm;
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
Projection cm = _get_camera_projection(near);
Vector2 screen_he = cm.get_viewport_half_extents();
ray = Vector3(((cpos.x / viewport_size.width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (cpos.y / viewport_size.height)) * 2.0 - 1.0) * screen_he.y, -near).normalized();
}
Expand All @@ -302,9 +324,7 @@ Vector3 Camera3D::project_ray_origin(const Point2 &p_pos) const {
Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
ERR_FAIL_COND_V(viewport_size.y == 0, Vector3());

if (mode == PROJECTION_PERSPECTIVE) {
return get_camera_transform().origin;
} else {
if (mode == PROJECTION_ORTHOGONAL) {
Vector2 pos = cpos / viewport_size;
real_t vsize, hsize;
if (keep_aspect == KEEP_WIDTH) {
Expand All @@ -321,6 +341,8 @@ Vector3 Camera3D::project_ray_origin(const Point2 &p_pos) const {
ray.z = -near;
ray = get_camera_transform().xform(ray);
return ray;
} else {
return get_camera_transform().origin;
};
};

Expand All @@ -333,15 +355,7 @@ bool Camera3D::is_position_behind(const Vector3 &p_pos) const {
Vector<Vector3> Camera3D::get_near_plane_points() const {
ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector<Vector3>(), "Camera is not inside scene.");

Size2 viewport_size = get_viewport()->get_visible_rect().size;

Projection cm;

if (mode == PROJECTION_ORTHOGONAL) {
cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
} else {
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
}
Projection cm = _get_camera_projection(near);

Vector3 endpoints[8];
cm.get_endpoints(Transform3D(), endpoints);
Expand All @@ -361,13 +375,7 @@ Point2 Camera3D::unproject_position(const Vector3 &p_pos) const {

Size2 viewport_size = get_viewport()->get_visible_rect().size;

Projection cm;

if (mode == PROJECTION_ORTHOGONAL) {
cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
} else {
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
}
Projection cm = _get_camera_projection(near);

Plane p(get_camera_transform().xform_inv(p_pos), 1.0);

Expand All @@ -389,13 +397,7 @@ Vector3 Camera3D::project_position(const Point2 &p_point, real_t p_z_depth) cons
}
Size2 viewport_size = get_viewport()->get_visible_rect().size;

Projection cm;

if (mode == PROJECTION_ORTHOGONAL) {
cm.set_orthogonal(size, viewport_size.aspect(), p_z_depth, far, keep_aspect == KEEP_WIDTH);
} else {
cm.set_perspective(fov, viewport_size.aspect(), p_z_depth, far, keep_aspect == KEEP_WIDTH);
}
Projection cm = _get_camera_projection(p_z_depth);

Vector2 vp_he = cm.get_viewport_half_extents();

Expand Down Expand Up @@ -508,6 +510,7 @@ void Camera3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_current", "enabled"), &Camera3D::set_current);
ClassDB::bind_method(D_METHOD("is_current"), &Camera3D::is_current);
ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera3D::get_camera_transform);
ClassDB::bind_method(D_METHOD("get_camera_projection"), &Camera3D::get_camera_projection);
ClassDB::bind_method(D_METHOD("get_fov"), &Camera3D::get_fov);
ClassDB::bind_method(D_METHOD("get_frustum_offset"), &Camera3D::get_frustum_offset);
ClassDB::bind_method(D_METHOD("get_size"), &Camera3D::get_size);
Expand Down Expand Up @@ -653,13 +656,7 @@ bool Camera3D::get_cull_mask_value(int p_layer_number) const {
Vector<Plane> Camera3D::get_frustum() const {
ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());

Size2 viewport_size = get_viewport()->get_visible_rect().size;
Projection cm;
if (mode == PROJECTION_PERSPECTIVE) {
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
} else {
cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
}
Projection cm = _get_camera_projection(near);

return cm.get_projection_planes(get_camera_transform());
}
Expand Down
3 changes: 3 additions & 0 deletions scene/3d/camera_3d.h
Expand Up @@ -105,6 +105,8 @@ class Camera3D : public Node3D {

static void _bind_methods();

Projection _get_camera_projection(real_t p_near) const;

public:
enum {
NOTIFICATION_BECAME_CURRENT = 50,
Expand Down Expand Up @@ -138,6 +140,7 @@ class Camera3D : public Node3D {
void set_frustum_offset(Vector2 p_offset);

virtual Transform3D get_camera_transform() const;
virtual Projection get_camera_projection() const;

virtual Vector3 project_ray_normal(const Point2 &p_pos) const;
virtual Vector3 project_ray_origin(const Point2 &p_pos) const;
Expand Down

0 comments on commit b0aa194

Please sign in to comment.