Skip to content

Commit b2ac547

Browse files
committed
integrate orthographic camer depth fix from mmatl#40
1 parent a59963e commit b2ac547

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

pyrender/camera.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ def get_projection_matrix(self, width=None, height=None):
9191
"""
9292
pass
9393

94+
@abc.abstractmethod
95+
def inverse_transform_depth_ndc(self, depth_ndc):
96+
"""
97+
Unproject the depth in Normalized Device Coordinates
98+
to metric values in eye (camera) reference frame.
99+
:param
100+
depth_ndc: np.array np.float32
101+
depth in [-1, 1] in normalized device coordinates
102+
:return:
103+
depth: np.array np.float32
104+
depth in metric units in eye (camera) reference frame.
105+
"""
106+
pass
107+
94108

95109
class PerspectiveCamera(Camera):
96110

@@ -205,6 +219,19 @@ def get_projection_matrix(self, width=None, height=None):
205219

206220
return P
207221

222+
def inverse_transform_depth_ndc(self, depth_ndc):
223+
inf_inds = (depth_ndc == 1)
224+
noninf = np.logical_not(inf_inds)
225+
depth_img = depth_ndc
226+
if self.zfar is None:
227+
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
228+
else:
229+
depth_img[noninf] = ((2.0 * self.znear * self.zfar) /
230+
(self.zfar + self.znear - depth_img[noninf] *
231+
(self.zfar - self.znear)))
232+
depth_img[inf_inds] = 0.0
233+
return depth_img
234+
208235

209236
class OrthographicCamera(Camera):
210237
"""An orthographic camera for orthographic projection.
@@ -309,6 +336,17 @@ def get_projection_matrix(self, width=None, height=None):
309336
P[3][3] = 1.0
310337
return P
311338

339+
def inverse_transform_depth_ndc(self, depth_ndc):
340+
inf_inds = (depth_ndc == 1)
341+
noninf = np.logical_not(inf_inds)
342+
depth_img = depth_ndc
343+
if self.zfar is None:
344+
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
345+
else:
346+
depth_img[noninf] = (depth_img[noninf] * (self.zfar - self.znear) + self.zfar + self.znear) / 2.0
347+
depth_img[inf_inds] = 0.0
348+
return depth_img
349+
312350

313351
class IntrinsicsCamera(Camera):
314352
"""A perspective camera with custom intrinsics.
@@ -432,6 +470,19 @@ def get_projection_matrix(self, width, height):
432470

433471
return P
434472

473+
def inverse_transform_depth_ndc(self, depth_ndc):
474+
inf_inds = (depth_ndc == 1)
475+
noninf = np.logical_not(inf_inds)
476+
depth_img = depth_ndc
477+
if self.zfar is None:
478+
depth_img[noninf] = 2 * self.znear / (1.0 - depth_img[noninf])
479+
else:
480+
depth_img[noninf] = ((2.0 * self.znear * self.zfar) /
481+
(self.zfar + self.znear - depth_img[noninf] *
482+
(self.zfar - self.znear)))
483+
depth_img[inf_inds] = 0.0
484+
return depth_img
485+
435486

436487
__all__ = ['Camera', 'PerspectiveCamera', 'OrthographicCamera',
437488
'IntrinsicsCamera']

pyrender/renderer.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,18 +1150,8 @@ def _read_main_framebuffer(self, scene, flags):
11501150
depth_im = np.frombuffer(depth_buf, dtype=np.float32)
11511151
depth_im = depth_im.reshape((height, width))
11521152
depth_im = np.flip(depth_im, axis=0)
1153-
inf_inds = (depth_im == 1.0)
11541153
depth_im = 2.0 * depth_im - 1.0
1155-
z_near = scene.main_camera_node.camera.znear
1156-
z_far = scene.main_camera_node.camera.zfar
1157-
noninf = np.logical_not(inf_inds)
1158-
if z_far is None:
1159-
depth_im[noninf] = 2 * z_near / (1.0 - depth_im[noninf])
1160-
else:
1161-
depth_im[noninf] = ((2.0 * z_near * z_far) /
1162-
(z_far + z_near - depth_im[noninf] *
1163-
(z_far - z_near)))
1164-
depth_im[inf_inds] = 0.0
1154+
depth_im = scene.main_camera_node.camera.inverse_transform_depth_ndc(depth_im)
11651155

11661156
# Resize for macos if needed
11671157
if sys.platform == 'darwin':

0 commit comments

Comments
 (0)