def __init__(self): assert engine_initialized( ), "You should initialize engine before adding camera to vehicle" config = get_global_config()["vehicle_config"]["depth_camera"] self.BUFFER_W, self.BUFFER_H = config[0], config[1] self.VIEW_GROUND = config[2] super(DepthCamera, self).__init__() cam = self.get_cam() lens = self.get_lens() cam.lookAt(0, 2.4, 1.3) lens.setFov(60) lens.setAspectRatio(2.0) if get_engine( ).mode == RENDER_MODE_NONE or not AssetLoader.initialized(): return # add shader for it if get_global_config()["headless_machine_render"]: vert_path = AssetLoader.file_path("shaders", "depth_cam_gles.vert.glsl") frag_path = AssetLoader.file_path("shaders", "depth_cam_gles.frag.glsl") else: from pgdrive.utils import is_mac if is_mac(): vert_path = AssetLoader.file_path("shaders", "depth_cam_mac.vert.glsl") frag_path = AssetLoader.file_path("shaders", "depth_cam_mac.frag.glsl") else: vert_path = AssetLoader.file_path("shaders", "depth_cam.vert.glsl") frag_path = AssetLoader.file_path("shaders", "depth_cam.frag.glsl") custom_shader = Shader.load(Shader.SL_GLSL, vertex=vert_path, fragment=frag_path) cam.node().setInitialState( RenderState.make(ShaderAttrib.make(custom_shader, 1))) if self.VIEW_GROUND: self.GROUND = GeoMipTerrain("mySimpleTerrain") self.GROUND.setHeightfield( AssetLoader.file_path("textures", "height_map.png")) # terrain.setBruteforce(True) # # Since the terrain is a texture, shader will not calculate the depth information, we add a moving terrain # # model to enable the depth information of terrain self.GROUND_MODEL = self.GROUND.getRoot() self.GROUND_MODEL.setPos(-128, 0, self.GROUND_HEIGHT) self.GROUND_MODEL.hide(CamMask.AllOn) self.GROUND_MODEL.show(CamMask.DepthCam) self.GROUND.generate()
def __init__(self, pure_background: bool = False): super(SkyBox, self).__init__() self._accumulate = 0 self.f = 1 if not self.render or pure_background: self.node_path = NodePath("pure_background") return skybox = self.loader.loadModel( AssetLoader.file_path("models", "skybox.bam")) skybox.hide(CamMask.MiniMap | CamMask.RgbCam | CamMask.Shadow | CamMask.ScreenshotCam) skybox.set_scale(20000) skybox_texture = self.loader.loadTexture( AssetLoader.file_path("textures", "skybox.jpg")) skybox_texture.set_minfilter(SamplerState.FT_linear) skybox_texture.set_magfilter(SamplerState.FT_linear) skybox_texture.set_wrap_u(SamplerState.WM_repeat) skybox_texture.set_wrap_v(SamplerState.WM_mirror) skybox_texture.set_anisotropic_degree(16) skybox.set_texture(skybox_texture) gles = ConfigVariableString("load-display").getValue() if gles == "pandagles2": skybox_shader = Shader.load( Shader.SL_GLSL, AssetLoader.file_path("shaders", "skybox_gles.vert.glsl"), # FIXME a potential bug here? AssetLoader.file_path("shaders", "skybox_gles.frag.glsl")) else: if is_mac(): vert_file = "skybox_mac.vert.glsl" frag_file = "skybox_mac.frag.glsl" else: vert_file = "skybox.vert.glsl" frag_file = "skybox.frag.glsl" skybox_shader = Shader.load( Shader.SL_GLSL, AssetLoader.file_path("shaders", vert_file), # FIXME a potential bug here? AssetLoader.file_path("shaders", frag_file)) skybox.set_shader(skybox_shader) self.node_path = skybox skybox.setZ(-4400) skybox.setH(30)
from direct.controls.InputState import InputState from pgdrive.utils import is_win, is_mac if (not is_win()) and (not is_mac()): import evdev from evdev import ecodes, InputDevice from pgdrive.utils import import_pygame pygame = import_pygame() class Controller: def process_input(self, vehicle): raise NotImplementedError class KeyboardController(Controller): INCREMENT = 2e-1 def __init__(self): # Input # self.pygame_control = True if engine.highway_render is not None else False self.pygame_control = False if not self.pygame_control: self.inputs = InputState() self.inputs.watchWithModifiers('forward', 'w') self.inputs.watchWithModifiers('reverse', 's') self.inputs.watchWithModifiers('turnLeft', 'a') self.inputs.watchWithModifiers('turnRight', 'd')
def __init__(self, config: dict = None): # Setup config and Panda3d self.pg_config = self.default_config() if config is not None: self.pg_config.update(config) if self.pg_config["pstats"]: # pstats debug provided by panda3d loadPrcFileData("", "want-pstats 1") loadPrcFileData("", "win-size {} {}".format(*self.pg_config["window_size"])) # Setup onscreen render if self.pg_config["use_render"]: self.mode = RENDER_MODE_ONSCREEN # Warning it may cause memory leak, Pand3d Official has fixed this in their master branch. # You can enable it if your panda version is latest. loadPrcFileData("", "threading-model Cull/Draw") # multi-thread render, accelerate simulation when evaluate else: if self.pg_config["use_image"]: self.mode = RENDER_MODE_OFFSCREEN else: self.mode = RENDER_MODE_NONE if is_mac() and (self.mode == RENDER_MODE_OFFSCREEN): # Mac don't support offscreen rendering self.mode = RENDER_MODE_ONSCREEN # Setup some debug options if self.pg_config["headless_image"]: # headless machine support loadPrcFileData("", "load-display pandagles2") if self.pg_config["debug"]: # debug setting PGWorld.DEBUG = True _free_warning() setup_logger(debug=True) self.accept('1', self.toggleDebug) self.accept('2', self.toggleWireframe) self.accept('3', self.toggleTexture) self.accept('4', self.toggleAnalyze) else: # only report fatal error when debug is False _suppress_warning() # a special debug mode if self.pg_config["debug_physics_world"]: self.accept('1', self.toggleDebug) super(PGWorld, self).__init__(windowType=self.mode) # Change window size at runtime if screen too small assert int(self.pg_config["use_topdown"]) + int(self.pg_config["use_image"]) <= 1, ( "Only one of use_topdown and use_image options can be selected." ) main_window_position = (0, 0) if self.mode == RENDER_MODE_ONSCREEN: loadPrcFileData("", "compressed-textures 1") # Default to compress h = self.pipe.getDisplayHeight() w = self.pipe.getDisplayWidth() if self.pg_config["window_size"][0] > 0.9 * w or self.pg_config["window_size"][1] > 0.9 * h: old_scale = self.pg_config["window_size"][0] / self.pg_config["window_size"][1] new_w = int(min(0.9 * w, 0.9 * h * old_scale)) new_h = int(min(0.9 * h, 0.9 * w / old_scale)) self.pg_config["window_size"] = tuple([new_w, new_h]) from panda3d.core import WindowProperties props = WindowProperties() props.setSize(self.pg_config["window_size"][0], self.pg_config["window_size"][1]) self.win.requestProperties(props) logging.warning( "Since your screen is too small ({}, {}), we resize the window to {}.".format( w, h, self.pg_config["window_size"] ) ) main_window_position = ( (w - self.pg_config["window_size"][0]) / 2, (h - self.pg_config["window_size"][1]) / 2 ) self.highway_render = None if self.pg_config["use_topdown"]: self.highway_render = HighwayRender(self.pg_config["use_render"], main_window_position) # screen scale factor self.w_scale = max(self.pg_config["window_size"][0] / self.pg_config["window_size"][1], 1) self.h_scale = max(self.pg_config["window_size"][1] / self.pg_config["window_size"][0], 1) if self.mode == RENDER_MODE_ONSCREEN: self.disableMouse() if not self.pg_config["debug_physics_world"] and (self.mode in [RENDER_MODE_ONSCREEN, RENDER_MODE_OFFSCREEN]): initialize_asset_loader(self) gltf.patch_loader(self.loader) # Display logo if self.mode == RENDER_MODE_ONSCREEN: self._loading_logo = OnscreenImage( image=AssetLoader.file_path("PGDrive-large.png"), pos=(0, 0, 0), scale=(self.w_scale, 1, self.h_scale) ) self._loading_logo.setTransparency(True) for i in range(20): self.graphicsEngine.renderFrame() self.taskMgr.add(self.remove_logo, "remove _loading_logo in first frame") self.closed = False # add element to render and pbr render, if is exists all the time. # these element will not be removed when clear_world() is called self.pbr_render = self.render.attachNewNode("pbrNP") # attach node to this root root whose children nodes will be clear after calling clear_world() self.worldNP = self.render.attachNewNode("world_np") # same as worldNP, but this node is only used for render gltf model with pbr material self.pbr_worldNP = self.pbr_render.attachNewNode("pbrNP") self.debug_node = None # some render attr self.pbrpipe = None self.light = None self.collision_info_np = None # physics world self.physics_world = PGPhysicsWorld() # for real time simulation self.force_fps = ForceFPS(self, start=False) # init terrain self.terrain = Terrain() self.terrain.attach_to_pg_world(self.render, self.physics_world) # init other world elements if self.mode != RENDER_MODE_NONE: from pgdrive.world.our_pbr import OurPipeline self.pbrpipe = OurPipeline( render_node=None, window=None, camera_node=None, msaa_samples=4, max_lights=8, use_normal_maps=False, use_emission_maps=True, exposure=1.0, enable_shadows=False, enable_fog=False, use_occlusion_maps=False ) self.pbrpipe.render_node = self.pbr_render self.pbrpipe.render_node.set_antialias(AntialiasAttrib.M_auto) self.pbrpipe._recompile_pbr() self.pbrpipe.manager.cleanup() # set main cam self.cam.node().setCameraMask(CamMask.MainCam) self.cam.node().getDisplayRegion(0).setClearColorActive(True) self.cam.node().getDisplayRegion(0).setClearColor(ImageBuffer.BKG_COLOR) lens = self.cam.node().getLens() lens.setFov(70) lens.setAspectRatio(1.2) self.sky_box = SkyBox() self.sky_box.attach_to_pg_world(self.render, self.physics_world) self.light = Light(self.pg_config) self.light.attach_to_pg_world(self.render, self.physics_world) self.render.setLight(self.light.direction_np) self.render.setLight(self.light.ambient_np) self.render.setShaderAuto() self.render.setAntialias(AntialiasAttrib.MAuto) # ui and render property if self.pg_config["show_fps"]: self.setFrameRateMeter(True) # onscreen message self.on_screen_message = PGOnScreenMessage( debug=self.DEBUG ) if self.mode == RENDER_MODE_ONSCREEN and self.pg_config["onscreen_message"] else None self._show_help_message = False self._episode_start_time = time.time() self.accept("h", self.toggle_help_message) self.accept("f", self.force_fps.toggle) else: self.on_screen_message = None # task manager self.taskMgr.remove('audioLoop')
def __init__(self, length: int, width: int, view_ground: bool, chassis_np: NodePath, pg_world: PGWorld): """ :param length: Control resolution of this sensor :param width: Control resolution of this sensor :param view_ground: Lane line will be invisible when set to True :param chassis_np: The vehicle chassis to place this sensor :param pg_world: PG-World """ self.view_ground = view_ground self.BUFFER_W = length self.BUFFER_H = width super(DepthCamera, self).__init__(self.BUFFER_W, self.BUFFER_H, Vec3(0.0, 0.8, 1.5), self.BKG_COLOR, pg_world=pg_world, parent_node=chassis_np) self.add_to_display(pg_world, self.default_region) self.cam.lookAt(0, 2.4, 1.3) self.lens.setFov(60) self.lens.setAspectRatio(2.0) # add shader for it if pg_world.world_config["headless_image"]: vert_path = AssetLoader.file_path("shaders", "depth_cam_gles.vert.glsl") frag_path = AssetLoader.file_path("shaders", "depth_cam_gles.frag.glsl") else: if is_mac(): vert_path = AssetLoader.file_path("shaders", "depth_cam_mac.vert.glsl") frag_path = AssetLoader.file_path("shaders", "depth_cam_mac.frag.glsl") else: vert_path = AssetLoader.file_path("shaders", "depth_cam.vert.glsl") frag_path = AssetLoader.file_path("shaders", "depth_cam.frag.glsl") custom_shader = Shader.load(Shader.SL_GLSL, vertex=vert_path, fragment=frag_path) self.cam.node().setInitialState( RenderState.make(ShaderAttrib.make(custom_shader, 1))) if self.view_ground: self.ground = GeoMipTerrain("mySimpleTerrain") self.ground.setHeightfield( AssetLoader.file_path("textures", "height_map.png")) # terrain.setBruteforce(True) # # Since the terrain is a texture, shader will not calculate the depth information, we add a moving terrain # # model to enable the depth information of terrain self.ground_model = self.ground.getRoot() self.ground_model.reparentTo(chassis_np) self.ground_model.setPos(-128, 0, self.GROUND) self.ground_model.hide(BitMask32.allOn()) self.ground_model.show(CamMask.DepthCam) self.ground.generate() pg_world.taskMgr.add(self.renew_pos_of_ground_mode, self.TASK_NAME, extraArgs=[chassis_np], appendTask=True)