Skip to content

Commit e783193

Browse files
committed
Use p_cam_projection.get_endpoints() to calculate the viewport location
Fixes #104193 In OpenXR the viewport location is not centered on the transform origin
1 parent 0028fd6 commit e783193

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

core/math/projection.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ Vector2 Projection::get_far_plane_half_extents() const {
433433
return Vector2(w / columns[0][0], w / columns[1][1]);
434434
}
435435

436+
bool Projection::get_viewport_endpoints(Vector3 &p_bottom_left, Vector3 &p_top_right) const {
437+
// get all 6 planes even though we only need 5
438+
Vector<Plane> planes = get_projection_planes(Transform3D());
439+
Plane near = planes[PLANE_NEAR];
440+
bool res = near.intersect_3(planes[PLANE_BOTTOM], planes[PLANE_LEFT], &p_bottom_left);
441+
ERR_FAIL_COND_V(!res, false);
442+
res = near.intersect_3(planes[PLANE_TOP], planes[PLANE_RIGHT], &p_top_right);
443+
ERR_FAIL_COND_V(!res, false);
444+
return true;
445+
}
446+
436447
bool Projection::get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const {
437448
Vector<Plane> planes = get_projection_planes(Transform3D());
438449
const Planes intersections[8][3] = {

core/math/projection.h

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct [[nodiscard]] Projection {
108108
Vector<Plane> get_projection_planes(const Transform3D &p_transform) const;
109109

110110
bool get_endpoints(const Transform3D &p_transform, Vector3 *p_8points) const;
111+
bool get_viewport_endpoints(Vector3 &p_top_left, Vector3 &p_bottom_right) const;
111112
Vector2 get_viewport_half_extents() const;
112113
Vector2 get_far_plane_half_extents() const;
113114

modules/raycast/raycast_occlusion_cull.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,14 @@ void RaycastOcclusionCull::buffer_set_size(RID p_buffer, const Vector2i &p_size)
525525
buffers[p_buffer].resize(p_size);
526526
}
527527

528-
Vector2 RaycastOcclusionCull::_jitter_half_extents(const Vector2 &p_half_extents, const Size2i &p_viewport_size) {
528+
Vector2 RaycastOcclusionCull::_get_jitter(const Vector2 &p_half_extents, const Size2i &p_viewport_size) {
529529
if (!_jitter_enabled) {
530-
return p_half_extents;
530+
return Vector2();
531531
}
532532

533533
// Prevent divide by zero when using NULL viewport.
534534
if ((p_viewport_size.x <= 0) || (p_viewport_size.y <= 0)) {
535-
return p_half_extents;
535+
return Vector2();
536536
}
537537

538538
int32_t frame = Engine::get_singleton()->get_frames_drawn();
@@ -578,7 +578,7 @@ Vector2 RaycastOcclusionCull::_jitter_half_extents(const Vector2 &p_half_extents
578578
// False shown can lower percentage that are occluded, and therefore performance.
579579
jitter *= 0.66f;
580580

581-
return p_half_extents + jitter;
581+
return jitter;
582582
}
583583

584584
void RaycastOcclusionCull::buffer_update(RID p_buffer, const Transform3D &p_cam_transform, const Projection &p_cam_projection, bool p_cam_orthogonal) {
@@ -594,10 +594,14 @@ void RaycastOcclusionCull::buffer_update(RID p_buffer, const Transform3D &p_cam_
594594

595595
Scenario &scenario = scenarios[buffer.scenario_rid];
596596
scenario.update();
597-
598-
Vector2 viewport_half = p_cam_projection.get_viewport_half_extents();
599-
Vector2 jitter_viewport_half = _jitter_half_extents(viewport_half, buffer.get_occlusion_buffer_size());
600-
Vector3 near_bottom_left = Vector3(-jitter_viewport_half.x, -jitter_viewport_half.y, -p_cam_projection.get_z_near());
597+
Vector3 near_top_right;
598+
Vector3 near_bottom_left;
599+
p_cam_projection.get_viewport_endpoints(near_bottom_left, near_top_right);
600+
Vector3 viewport_extents = near_bottom_left - near_top_right;
601+
Vector2 viewport_half = Vector2(viewport_extents.x, viewport_extents.y).abs() * 0.5;
602+
Vector2 jitter = _get_jitter(viewport_half, buffer.get_occlusion_buffer_size());
603+
604+
near_bottom_left += Vector3(jitter.x, jitter.y, 0);
601605

602606
buffer.update_camera_rays(p_cam_transform, near_bottom_left, 2 * viewport_half, p_cam_projection.get_z_far(), p_cam_orthogonal);
603607

modules/raycast/raycast_occlusion_cull.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class RaycastOcclusionCull : public RendererSceneOcclusionCull {
161161
bool _jitter_enabled = false;
162162

163163
void _init_embree();
164-
Vector2 _jitter_half_extents(const Vector2 &p_half_extents, const Size2i &p_viewport_size);
164+
Vector2 _get_jitter(const Vector2 &p_half_extents, const Size2i &p_viewport_size);
165165

166166
public:
167167
virtual bool is_occluder(RID p_rid) override;

0 commit comments

Comments
 (0)