Repository: Genesis-Embodied-AI/Genesis
File: genesis/engine/sensors/camera.py (Python)

Code before:

        camera_wrapper = self._get_camera_wrapper()
        self._shared_metadata.renderer.add_camera(camera_wrapper)
        self._update_camera_pose()

        n_envs = max(self._manager._sim._B, 1)
        h, w = self._options.res[1], self._options.res[0]
        self._shared_metadata.image_cache[self._idx] = torch.zeros(
            (n_envs, h, w, 3), dtype=torch.uint8, device=gs.device
        )

    def _create_standalone_context(self, scene):
        """Create a simplified RasterizerContext for camera sensors."""
        if not scene.sim._rigid_only and scene.n_envs > 1:
            gs.raise_exception("Rasterizer with n_envs > 1, does not work when using non rigid simulation")
        if sys.platform == "darwin":
            if scene.n_envs > 1:
                gs.raise_exception(
                    "Rasterizer with n_envs > 1, does not work on Metal because it doesn't support OpenGL 4.3"
                )
            env_separate_rigid = False
        else:
            env_separate_rigid = True
        vis_options = VisOptions(
            show_world_frame=False,
            show_link_frame=False,
            show_cameras=False,
            rendered_envs_idx=range(max(self._manager._sim._B, 1)),
            env_separate_rigid=env_separate_rigid,
        )

        context = RasterizerContext(vis_options)
        context.build(scene)
        context.reset()
        return context

    def _convert_light_config_to_rasterizer(self, light_config):
        """Convert a light config dict to rasterizer format."""
        # Default values for rasterizer
        light_type = light_config.get("type", "directional")
        pos = light_config.get("pos", (0.0, 0.0, 5.0))
        dir = light_config.get("dir", (0.0, 0.0, -1.0))
        color = light_config.get("color", (1.0, 1.0, 1.0))
        intensity = light_config.get("intensity", 1.0)

        return {
            "type": light_type,
            "pos": pos,
            "dir": dir,
            "color": tuple(np.array(color) * intensity),
            "intensity": intensity,

Review comment (performance):
You should probably add a warning about bad performance using Batched rendering with rasterizer, plus mention that camera pose is not batched.

Code after:
            self._shared_metadata.renderer.build()

        self._shared_metadata.sensors.append(self)

        # Add lights from options to the shared metadata
        for light_config in self._options.lights:
            if self._shared_metadata.lights is not None:
                # Convert light config to rasterizer format
                light_dict = self._convert_light_config_to_rasterizer(light_config)
                # self._shared_metadata.lights.append(light_dict)
                self._shared_metadata.context.add_light(light_dict)

        camera_wrapper = self._get_camera_wrapper()
        self._shared_metadata.renderer.add_camera(camera_wrapper)
        self._update_camera_pose()

        n_envs = max(self._manager._sim._B, 1)
        h, w = self._options.res[1], self._options.res[0]
        self._shared_metadata.image_cache[self._idx] = torch.zeros(
            (n_envs, h, w, 3), dtype=torch.uint8, device=gs.device
        )

    def _create_standalone_context(self, scene):
        """Create a simplified RasterizerContext for camera sensors."""
        if not scene.sim._rigid_only and scene.n_envs > 1:
            gs.raise_exception("Rasterizer with n_envs > 1, does not work when using non rigid simulation")
        if sys.platform == "darwin":
            if scene.n_envs > 1:
                gs.raise_exception(
                    "Rasterizer with n_envs > 1, does not work on Metal because it doesn't support OpenGL 4.2"
                )
            env_separate_rigid = False
        else:
            if self._link is not None:
                gs.raise_exception("Rasterizer with n_envs > 1, does not work with attached cameras yet.")

            if scene.n_envs > 1:
                gs.logger.warning(
                    "Rasterizer with n_envs > 1 is slow as it doesn't do batched rendering consider using BatchRenderer instead."
                )
            env_separate_rigid = True
        vis_options = VisOptions(
            show_world_frame=False,
            show_link_frame=False,
            show_cameras=False,
            rendered_envs_idx=range(max(self._manager._sim._B, 1)),
            env_separate_rigid=env_separate_rigid,
        )

        context = RasterizerContext(vis_options)
        context.build(scene)