Skip to content

Orthographic camera show_object distance. #1081

@cassiersg

Description

@cassiersg

I am having issues when using the orthographic camera's show_object method to view very large objects. For a camera looking at the XY plane and calling show_object on a large object (located within the plane z=0, X and Y dimensions ~ 1e6) then zooming in (by a factor ~1e6), the object very often gets outside the camera frustum.

I found the following solution:

  1. fix depth_range to something reasonable, like (-1000, 1000)
  2. keep the camera located at z=0

For 2., I modified PerspectiveCamera.show_object as follows:

diff --git a/pygfx/cameras/_perspective.py b/pygfx/cameras/_perspective.py
index fa6f099..c23bc28 100644
--- a/pygfx/cameras/_perspective.py
+++ b/pygfx/cameras/_perspective.py
@@ -456,12 +456,15 @@ class PerspectiveCamera(Camera):
         extent = radius * 2 * scale
 
         # Apply
-        distance = fov_distance_factor(self.fov) * extent
+        # Need a non-zero distance for look_at to work when fov==0.0.
+        distance = 1.0 if self.fov == 0.0 else fov_distance_factor(self.fov) * extent
         self.local.position = view_pos - view_dir * distance
         self.look_at(view_pos)
+        if self.fov == 0.0:
+            self.local.position = view_pos
         self._set_extent(extent)
 
-        if match_aspect and bbox is not None:
+        if self.fov != 0 and match_aspect and bbox is not None:
             # Re-calculate width and height using the aligned bbox, so that the
             # contents keep fitting snugly as the viewport is resized.
             bbox = la.aabb_transform(bbox, self.world.inverse_matrix)

Is this a good solution or is there a better one?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions