Skip to content

Commit

Permalink
WIP: Further speedup.
Browse files Browse the repository at this point in the history
  • Loading branch information
duburcqa committed Jul 15, 2024
1 parent f2d2875 commit d1ce804
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions python/jiminy_py/src/jiminy_py/viewer/panda3d/panda3d_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ def __init__(self, # pylint: disable=super-init-not-called
config = ViewerConfig()
config.set_window_size(*WINDOW_SIZE_DEFAULT)
config.set_window_fixed(False)
config.enable_antialiasing(True, multisamples=4)
config.enable_antialiasing(False, multisamples=0)
# config.set_value('want-pstats', True)
config.set_value('framebuffer-software', False)
config.set_value('framebuffer-hardware', False)
config.set_value('load-display', 'pandagl')
Expand All @@ -418,6 +419,7 @@ def __init__(self, # pylint: disable=super-init-not-called
config.set_value('sync-video', False)
config.set_value('default-near', 0.1)
config.set_value('gl-version', '3 1')
config.set_value('gl-check-errors', '#t')
config.set_value('notify-level', 'fatal')
config.set_value('notify-level-x11display', 'fatal')
config.set_value('notify-level-device', 'fatal')
Expand Down Expand Up @@ -606,16 +608,20 @@ def open_window(self) -> None:
if self.has_gui():
raise RuntimeError("Only one graphical window can be opened.")

# Force enabling multi-sampling for onscreen graphical window
fbprops = FrameBufferProperties(FrameBufferProperties.getDefault())
fbprops.set_multisamples(4)

# Replace the original offscreen window by an onscreen one if possible
is_success = True
size = self.win.get_size()
try:
self.windowType = 'onscreen'
self.open_main_window(size=size)
self.open_main_window(size=size, fbprops=fbprops)
except Exception: # pylint: disable=broad-except
is_success = False
self.windowType = 'offscreen'
self.open_main_window(size=size)
self.open_main_window(size=size, fbprops=fbprops)

# Enable Physics-based rendering
enable_pbr_shader(self.cam.node())
Expand Down Expand Up @@ -791,6 +797,7 @@ def add_camera(self,
if is_depthmap:
fbprops.set_depth_bits(32)
fbprops.set_float_depth(True)
fbprops.set_multisamples(0)
else:
fbprops.set_rgba_bits(8, 8, 8, 8)
fbprops.set_float_color(False)
Expand Down Expand Up @@ -1019,9 +1026,9 @@ def move_orbital_camera_task(self,
def _make_light_ambient(self, color: Tuple3FType) -> NodePath:
"""Patched to fix wrong color alpha.
"""
node = super()._make_light_ambient(color)
node.get_node(0).set_color((*color, 1.0))
return node
light = super()._make_light_ambient(color)
light.node().set_color((*color, 1.0))
return light

def _make_light_direct(self,
index: int,
Expand All @@ -1031,9 +1038,9 @@ def _make_light_direct(self,
) -> NodePath:
"""Patched to fix wrong color alpha.
"""
light_path = super()._make_light_direct(index, color, pos, target)
light_path.get_node(0).set_color((*color, 1.0))
return light_path
light = super()._make_light_direct(index, color, pos, target)
light.node().set_color((*color, 1.0))
return light

def _make_axes(self) -> NodePath:
model = GeomNode('axes')
Expand Down Expand Up @@ -1094,7 +1101,7 @@ def _make_floor(self,
# Adjust frustum of the lights to project shadow over the whole scene
for light_path in self._lights[1:]:
bmin, bmax = node.get_tight_bounds(light_path)
lens = light_path.get_node(0).get_lens()
lens = light_path.node().get_lens()
lens.set_film_offset((bmin.xz + bmax.xz) * 0.5)
lens.set_film_size(bmax.xz - bmin.xz)
lens.set_near_far(bmin.y, bmax.y)
Expand Down Expand Up @@ -1719,8 +1726,8 @@ def get_camera_transform(self) -> Tuple[np.ndarray, np.ndarray]:
representation of the orientation (X, Y, Z, W) as a
pair of `np.ndarray`.
"""
return (np.array(self.camera.get_pos()),
np.array(self.camera.get_quat()))
return (np.array(self.camera.get_pos(), dtype=np.float64),
np.array(self.camera.get_quat(), dtype=np.float64))

def set_camera_transform(self,
pos: Tuple3FType,
Expand Down Expand Up @@ -1856,16 +1863,30 @@ def get_screenshot(self,
else:
buffer = self._user_buffers[camera_name]

# Get frame as raw texture
texture = buffer.get_texture()
is_depth_map = texture.format == Texture.F_depth_component32

# Disable shadow casting for depth map computation since it is useless
shadow_buffers = []
if is_depth_map:
for light in self._lights:
if not light.node().is_ambient_light():
shadow_buffer = light.node().getShadowBuffer(self.win.gsg)
if shadow_buffer is not None:
shadow_buffer.active = False
shadow_buffers.append(shadow_buffer)

# Refresh the scene
buffer.trigger_copy()
buffer.set_one_shot(True)
self.graphics_engine.render_frame()

# Get frame as raw texture
texture = buffer.get_texture()
# Restore shadow casting
for shadow_buffer in shadow_buffers:
shadow_buffer.active = True

# Extract raw array buffer from texture
is_depth_map = texture.format == Texture.F_depth_component32
if is_depth_map:
image = texture.get_ram_image()
else:
Expand Down

0 comments on commit d1ce804

Please sign in to comment.