def update(self):
        """ Updates the commonly used resources, mostly the shader inputs """
        update = self._input_ubo.update_input

        # Get the current transform matrix of the camera
        view_mat = Globals.render.get_transform(self._showbase.cam).get_mat()

        # Compute the view matrix, but with a z-up coordinate system
        update("view_mat_z_up", view_mat * Mat4.convert_mat(CS_zup_right, CS_yup_right))

        # Compute the view matrix without the camera rotation
        view_mat_billboard = Mat4(view_mat)
        view_mat_billboard.set_row(0, Vec3(1, 0, 0))
        view_mat_billboard.set_row(1, Vec3(0, 1, 0))
        view_mat_billboard.set_row(2, Vec3(0, 0, 1))
        update("view_mat_billboard", view_mat_billboard)

        update("camera_pos", self._showbase.camera.get_pos(Globals.render))

        # Compute last view projection mat
        curr_vp = self._input_ubo.get_input("view_proj_mat_no_jitter")
        update("last_view_proj_mat_no_jitter", curr_vp)
        curr_vp = Mat4(curr_vp)
        curr_vp.invert_in_place()
        update("last_inv_view_proj_mat_no_jitter", curr_vp)

        proj_mat = Mat4(self._showbase.camLens.get_projection_mat())

        # Set the projection matrix as an input, but convert it to the correct
        # coordinate system before.
        proj_mat_zup = Mat4.convert_mat(CS_yup_right, CS_zup_right) * proj_mat
        update("proj_mat", proj_mat_zup)

        # Set the inverse projection matrix
        update("inv_proj_mat", invert(proj_mat_zup))

        # Remove jitter and set the new view projection mat
        proj_mat.set_cell(1, 0, 0.0)
        proj_mat.set_cell(1, 1, 0.0)
        update("view_proj_mat_no_jitter", view_mat  * proj_mat)

        # Store the frame delta
        update("frame_delta", Globals.clock.get_dt())
        update("smooth_frame_delta", 1.0 / max(1e-5, Globals.clock.get_average_frame_rate()))
        update("frame_time", Globals.clock.get_frame_time())

        # Store the current film offset, we use this to compute the pixel-perfect
        # velocity, which is otherwise not possible. Usually this is always 0
        # except when SMAA and reprojection is enabled
        update("current_film_offset", self._showbase.camLens.get_film_offset())

        max_clip_length = 1
        if self._pipeline.plugin_mgr.is_plugin_enabled("smaa"):
            max_clip_length = self._pipeline.plugin_mgr.instances["smaa"].history_length

        update("temporal_index", Globals.clock.get_frame_count() % max_clip_length)
        update("frame_index", Globals.clock.get_frame_count())
    def do_update(self):
        """ Updates the commonly used resources, mostly the shader inputs """
        update = self._input_ubo.update_input

        # Get the current transform matrix of the camera
        view_mat = Globals.render.get_transform(self._showbase.cam).get_mat()

        # Compute the view matrix, but with a z-up coordinate system
        update("view_mat_z_up", view_mat * Mat4.convert_mat(CS_zup_right, CS_yup_right))

        # Compute the view matrix without the camera rotation
        view_mat_billboard = Mat4(view_mat)
        view_mat_billboard.set_row(0, Vec3(1, 0, 0))
        view_mat_billboard.set_row(1, Vec3(0, 1, 0))
        view_mat_billboard.set_row(2, Vec3(0, 0, 1))
        update("view_mat_billboard", view_mat_billboard)

        update("camera_pos", self._showbase.camera.get_pos(Globals.render))
        update("last_view_proj_mat_no_jitter", self._input_ubo.get_input("view_proj_mat_no_jitter"))
        proj_mat = Mat4(self._showbase.camLens.get_projection_mat())

        # Set the projection matrix as an input, but convert it to the correct
        # coordinate system before.
        proj_mat_zup = Mat4.convert_mat(CS_yup_right, CS_zup_right) * proj_mat
        update("proj_mat", proj_mat_zup)

        # Set the inverse projection matrix
        update("inv_proj_mat", invert(proj_mat_zup))

        # Remove jitter and set the new view projection mat
        proj_mat.set_cell(1, 0, 0.0)
        proj_mat.set_cell(1, 1, 0.0)
        update("view_proj_mat_no_jitter", view_mat * proj_mat)

        # Store the frame delta
        update("frame_delta", Globals.clock.get_dt())
        update("frame_time", Globals.clock.get_frame_time())
Example #3
0
    def __init__(self):
        load_prc_file_data("", "win-size 512 512")
        load_prc_file_data("", "window-type offscreen")
        load_prc_file_data("", "model-cache-dir")
        load_prc_file_data("", "model-cache-textures #f")
        load_prc_file_data("", "textures-power-2 none")
        load_prc_file_data("", "alpha-bits 0")
        load_prc_file_data("", "print-pipe-types #f")

        # Construct render pipeline
        self.render_pipeline = RenderPipeline()
        self.render_pipeline.mount_mgr.config_dir = "config/"
        self.render_pipeline.set_empty_loading_screen()
        self.render_pipeline.create(self)

        self.setup_scene()

        # Disable model caching
        BamCache.get_global_ptr().cache_models = False

        self.update_queue = []
        self.start_listen()

        # Render initial frames
        for i in range(10):
            self.taskMgr.step()

        last_update = 0.0
        self.scene_node = None

        # Wait for updates
        while True:

            # Update once in a while
            curr_time = time.time()
            if curr_time > last_update + 1.0:
                last_update = curr_time
                self.taskMgr.step()

            if self.update_queue:
                if self.scene_node:
                    self.scene_node.remove_node()

                # Only take the latest packet
                payload = self.update_queue.pop(0)
                print("RENDERING:", payload)

                scene = self.loader.loadModel(
                    Filename.from_os_specific(payload["scene"]))

                for light in scene.find_all_matches("**/+PointLight"):
                    light.remove_node()
                for light in scene.find_all_matches("**/+Spotlight"):
                    light.remove_node()

                # Find camera
                main_cam = scene.find("**/Camera")
                if main_cam:
                    transform_mat = main_cam.get_transform(
                        self.render).get_mat()
                    transform_mat = Mat4.convert_mat(
                        CS_zup_right, CS_yup_right) * transform_mat
                    self.camera.set_mat(transform_mat)
                else:
                    print("WARNING: No camera found")
                    self.camera.set_pos(0, -3.5, 0)
                    self.camera.look_at(0, -2.5, 0)

                self.camLens.set_fov(64.0)

                self.scene_node = scene
                scene.reparent_to(self.render)

                # Render scene
                for i in range(8):
                    self.taskMgr.step()

                dest_path = Filename.from_os_specific(payload["dest"])
                print("Saving screenshot to", dest_path)
                self.win.save_screenshot(dest_path)
                self.notify_about_finish(int(payload["pingback_port"]))
    def update(self):
        """ Updates the commonly used resources, mostly the shader inputs """
        update = self._input_ubo.update_input

        # Get the current transform matrix of the camera
        view_mat = Globals.render.get_transform(self._showbase.cam).get_mat()

        # Compute the view matrix, but with a z-up coordinate system
        update("view_mat_z_up", view_mat * Mat4.convert_mat(CS_zup_right, CS_yup_right))

        # Compute the view matrix without the camera rotation
        view_mat_billboard = Mat4(view_mat)
        view_mat_billboard.set_row(0, Vec3(1, 0, 0))
        view_mat_billboard.set_row(1, Vec3(0, 1, 0))
        view_mat_billboard.set_row(2, Vec3(0, 0, 1))
        update("view_mat_billboard", view_mat_billboard)

        update("camera_pos", self._showbase.camera.get_pos(Globals.render))

        # Compute last view projection mat
        curr_vp = self._input_ubo.get_input("view_proj_mat_no_jitter")
        update("last_view_proj_mat_no_jitter", curr_vp)
        curr_vp = Mat4(curr_vp)
        curr_vp.invert_in_place()
        curr_inv_vp = curr_vp
        update("last_inv_view_proj_mat_no_jitter", curr_inv_vp)

        proj_mat = Mat4(self._showbase.camLens.get_projection_mat())

        # Set the projection matrix as an input, but convert it to the correct
        # coordinate system before.
        proj_mat_zup = Mat4.convert_mat(CS_yup_right, CS_zup_right) * proj_mat
        update("proj_mat", proj_mat_zup)

        # Set the inverse projection matrix
        update("inv_proj_mat", invert(proj_mat_zup))

        # Remove jitter and set the new view projection mat
        proj_mat.set_cell(1, 0, 0.0)
        proj_mat.set_cell(1, 1, 0.0)
        update("view_proj_mat_no_jitter", view_mat  * proj_mat)

        # Store the frame delta
        update("frame_delta", Globals.clock.get_dt())
        update("smooth_frame_delta", 1.0 / max(1e-5, Globals.clock.get_average_frame_rate()))
        update("frame_time", Globals.clock.get_frame_time())

        # Store the current film offset, we use this to compute the pixel-perfect
        # velocity, which is otherwise not possible. Usually this is always 0
        # except when SMAA and reprojection is enabled
        update("current_film_offset", self._showbase.camLens.get_film_offset())
        update("frame_index", Globals.clock.get_frame_count())

        # Compute frustum corners in the order BL, BR, TL, TR
        ws_frustum_directions = Mat4()
        vs_frustum_directions = Mat4()
        inv_proj_mat = Globals.base.camLens.get_projection_mat_inv()
        view_mat_inv = Mat4(view_mat)
        view_mat_inv.invert_in_place()

        for i, point in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1))):
            result = inv_proj_mat.xform(Vec4(point[0], point[1], 1.0, 1.0))
            vs_dir = result.xyz.normalized()
            vs_frustum_directions.set_row(i, Vec4(vs_dir, 1))
            ws_dir = view_mat_inv.xform(Vec4(result.xyz, 0))
            ws_frustum_directions.set_row(i, ws_dir)

        update("vs_frustum_directions", vs_frustum_directions)
        update("ws_frustum_directions", ws_frustum_directions)
Example #5
0
    def update(self):
        """ Updates the commonly used resources, mostly the shader inputs """
        update = self._input_ubo.update_input

        # Get the current transform matrix of the camera
        view_mat = Globals.render.get_transform(self._showbase.cam).get_mat()

        # Compute the view matrix, but with a z-up coordinate system
        zup_conversion = Mat4.convert_mat(CS_zup_right, CS_yup_right)
        update("view_mat_z_up", view_mat * zup_conversion)

        # Compute the view matrix without the camera rotation
        view_mat_billboard = Mat4(view_mat)
        view_mat_billboard.set_row(0, Vec3(1, 0, 0))
        view_mat_billboard.set_row(1, Vec3(0, 1, 0))
        view_mat_billboard.set_row(2, Vec3(0, 0, 1))
        update("view_mat_billboard", view_mat_billboard)

        update("camera_pos", self._showbase.camera.get_pos(Globals.render))

        # Compute last view projection mat
        curr_vp = self._input_ubo.get_input("view_proj_mat_no_jitter")
        update("last_view_proj_mat_no_jitter", curr_vp)
        curr_vp = Mat4(curr_vp)
        curr_vp.invert_in_place()
        curr_inv_vp = curr_vp
        update("last_inv_view_proj_mat_no_jitter", curr_inv_vp)

        proj_mat = Mat4(self._showbase.camLens.get_projection_mat())

        # Set the projection matrix as an input, but convert it to the correct
        # coordinate system before.
        proj_mat_zup = Mat4.convert_mat(CS_yup_right, CS_zup_right) * proj_mat
        update("proj_mat", proj_mat_zup)

        # Set the inverse projection matrix
        update("inv_proj_mat", invert(proj_mat_zup))

        # Remove jitter and set the new view projection mat
        proj_mat.set_cell(1, 0, 0.0)
        proj_mat.set_cell(1, 1, 0.0)
        update("view_proj_mat_no_jitter", view_mat * proj_mat)

        # Store the frame delta
        update("frame_delta", Globals.clock.get_dt())
        update("smooth_frame_delta",
               1.0 / max(1e-5, Globals.clock.get_average_frame_rate()))
        update("frame_time", Globals.clock.get_frame_time())

        # Store the current film offset, we use this to compute the pixel-perfect
        # velocity, which is otherwise not possible. Usually this is always 0
        # except when SMAA and reprojection is enabled
        update("current_film_offset", self._showbase.camLens.get_film_offset())
        update("frame_index", Globals.clock.get_frame_count())

        # Compute frustum corners in the order BL, BR, TL, TR
        ws_frustum_directions = Mat4()
        vs_frustum_directions = Mat4()
        inv_proj_mat = Globals.base.camLens.get_projection_mat_inv()
        view_mat_inv = Mat4(view_mat)
        view_mat_inv.invert_in_place()

        for i, point in enumerate(((-1, -1), (1, -1), (-1, 1), (1, 1))):
            result = inv_proj_mat.xform(Vec4(point[0], point[1], 1.0, 1.0))
            vs_dir = (zup_conversion.xform(result)).xyz.normalized()
            vs_frustum_directions.set_row(i, Vec4(vs_dir, 1))
            ws_dir = view_mat_inv.xform(Vec4(result.xyz, 0))
            ws_frustum_directions.set_row(i, ws_dir)

        update("vs_frustum_directions", vs_frustum_directions)
        update("ws_frustum_directions", ws_frustum_directions)

        update("screen_size", Globals.resolution)
        update("native_screen_size", Globals.native_resolution)
        update("lc_tile_count", self._pipeline.light_mgr.num_tiles)
Example #6
0
    def __init__(self):
        load_prc_file_data("", "win-size 512 512")
        # load_prc_file_data("", "window-type offscreen")
        load_prc_file_data("", "model-cache-dir")
        load_prc_file_data("", "model-cache-textures #f")
        load_prc_file_data("", "textures-power-2 none")
        load_prc_file_data("", "alpha-bits 0")
        load_prc_file_data("", "print-pipe-types #f")

        # Construct render pipeline
        self.render_pipeline = RenderPipeline()
        self.render_pipeline.mount_mgr.config_dir = "config/"
        self.render_pipeline.set_empty_loading_screen()
        self.render_pipeline.create(self)

        self.setup_scene()

        # Disable model caching
        BamCache.get_global_ptr().cache_models = False

        self.update_queue = []
        self.start_listen()

        # Render initial frames
        for i in range(10):
            self.taskMgr.step()

        last_update = 0.0
        self.scene_node = None

        current_lights = []
        current_envprobes = []

        # Wait for updates
        while True:

            # Update once in a while
            curr_time = time.time()
            if curr_time > last_update + 1.0:
                last_update = curr_time
                self.taskMgr.step()

            if self.update_queue:
                if self.scene_node:
                    self.scene_node.remove_node()

                # Only take the latest packet
                payload = self.update_queue.pop(0)
                print("RENDERING:", payload)

                scene = loader.loadModel(Filename.from_os_specific(payload["scene"]))

                for light in scene.find_all_matches("**/+PointLight"):
                    light.remove_node()
                for light in scene.find_all_matches("**/+Spotlight"):
                    light.remove_node()

                # Find camera
                main_cam = scene.find("**/Camera")
                if main_cam:
                    transform_mat = main_cam.get_transform(render).get_mat()
                    transform_mat = Mat4.convert_mat(CS_zup_right, CS_yup_right) * transform_mat
                    base.camera.set_mat(transform_mat)
                else:
                    print("WARNING: No camera found")
                    base.camera.set_pos(0, -3.5, 0)
                    base.camera.look_at(0, -2.5, 0)

                base.camLens.set_fov(64.0)

                self.scene_node = scene
                scene.reparent_to(render)

                # Render scene
                for i in range(8):
                    self.taskMgr.step()

                dest_path = Filename.from_os_specific(payload["dest"])
                print("Saving screenshot to", dest_path)
                self.win.save_screenshot(dest_path)
                self.notify_about_finish(int(payload["pingback_port"]))