def __init__(self): super().__init__() self.accept('toggle-debug-cam', self.toggle_debug_cam) self.accept('move', self.move_player) self.accept('ability1', self.toggle_range, [0]) self.accept('ability2', self.toggle_range, [1]) self.accept('ability3', self.toggle_range, [2]) self.accept('ability4', self.toggle_range, [3]) self.mapgen = 'static' dungeon = Dungeon(self.mapgen, self.DUNGEON_SX, self.DUNGEON_SY) dungeon.model_root.reparent_to(self.root_node) dlight = p3d.DirectionalLight('sun') dlight.set_color(p3d.LVector3(0.2, 0.2, 0.2)) dlight.set_shadow_caster(True, 4096, 4096) dlnp = self.root_node.attach_new_node(dlight) dlnp.set_z(10) dlnp.set_p(-90) lens = dlight.get_lens() lens.set_film_size(60) lens.set_near(1) lens.set_far(100) self.root_node.set_light(dlnp) dlight2 = p3d.DirectionalLight('ground') dlight2.set_color(p3d.LVector3(0.1, 0.1, 0.1)) dlnp2 = self.root_node.attach_new_node(dlight2) self.root_node.set_light(dlnp2) loader = p3d.Loader.get_global_ptr() player = p3d.NodePath( loader.load_sync('dungeon.bam')).find('**/MonsterSpawn').node() playernp = self.root_node.attach_new_node(player) playernp.set_pos(dungeon.player_start) playernp.set_z(1.5) self.player_ranges = [ RangeIndicator('box', length=5, width=1), RangeIndicator('circle', radius=2), RangeIndicator('circle', radius=3), RangeIndicator('circle', radius=4), ] for rangeindicator in self.player_ranges: rangeindicator.graphics.reparent_to(playernp) rangeindicator.visible = False # self.root_node.ls() # self.root_node.analyze() self.dungeons = [dungeon] self.dungeon_idx = 0 self.dungeon = dungeon self.player = playernp self.last_tele_loc = None self.target = self.player.get_pos() self.debug_cam = False self.reset_camera()
def setupLights(self): # This function sets up some default lighting ambientLight = p3dc.AmbientLight("ambientLight") ambientLight.setColor((.8, .8, .8, 1)) directionalLight = p3dc.DirectionalLight("directionalLight") directionalLight.setDirection(p3dc.LVector3(0, 45, -45)) directionalLight.setColor((0.2, 0.2, 0.2, 1)) self.render.setLight(self.render.attachNewNode(directionalLight)) self.render.setLight(self.render.attachNewNode(ambientLight))
def __init__(self, rendernp, targetpos=None, scene=None, calc_shadow_bounds=True): self.rendernp = rendernp targetpos = targetpos or p3d.LVector3(0, 0, 0) # Lights self.key_light = p3d.DirectionalLight('sun') self.key_light.color_temperature = 6000 self.key_lightnp = rendernp.attach_new_node(self.key_light) self.key_lightnp.set_pos(targetpos + (0, -15, 15)) self.key_lightnp.look_at(targetpos) base.render.set_light(self.key_lightnp) self.fill_light = p3d.DirectionalLight('fill light') self.fill_light.color_temperature = 4800 self.fill_light.color = self.fill_light.color * 0.5 self.fill_lightnp = rendernp.attach_new_node(self.fill_light) self.fill_lightnp.set_pos(targetpos + (-20, 0, 10)) self.fill_lightnp.look_at(targetpos) base.render.set_light(self.fill_lightnp) self.back_light = p3d.DirectionalLight('fill light') self.back_light.color_temperature = 4800 self.back_light.color = self.back_light.color * 0.25 self.back_lightnp = rendernp.attach_new_node(self.back_light) self.back_lightnp.set_pos(20, 20, 0) self.back_lightnp.look_at(targetpos) base.render.set_light(self.back_lightnp) ambient_power = 0.1 self.ambient_light = p3d.AmbientLight('ambient') self.ambient_light.color = (ambient_power, ambient_power, ambient_power, 1.0) self.ambient_lightnp = rendernp.attach_new_node(self.ambient_light) base.render.set_light(self.ambient_lightnp) # Shadows self.key_light.set_shadow_caster(True, 512, 512) if calc_shadow_bounds: self.recalc_bounds(scene)
def setup(self): # setup parameters self.path = os.path.dirname(os.path.abspath( __file__)) + '/objects/' # default path to copy local stimuli self.set_intensity(self.params['intensity']) # store local copy of files if not os.path.isdir(self.path): # create path if necessary os.makedirs(self.path) self.object_files = dict() for cond in self.conditions: for obj_id in cond['obj_id']: object_info = (Objects() & ('obj_id=%d' % obj_id)).fetch1() filename = self.path + object_info['file_name'] self.object_files[obj_id] = filename if not os.path.isfile(filename): print('Saving %s ...' % filename) object_info['object'].tofile(filename) ShowBase.__init__(self, fStartDirect=True, windowType=None) props = WindowProperties() props.setCursorHidden(True) self.win.requestProperties(props) self.set_background_color(0, 0, 0) self.disableMouse() # Create Ambient Light self.ambientLight = core.AmbientLight('ambientLight') self.ambientLightNP = self.render.attachNewNode(self.ambientLight) self.render.setLight(self.ambientLightNP) # Directional light 01 self.directionalLight1 = core.DirectionalLight('directionalLight1') self.directionalLight1NP = self.render.attachNewNode( self.directionalLight1) self.render.setLight(self.directionalLight1NP) # Directional light 02 self.directionalLight2 = core.DirectionalLight('directionalLight2') self.directionalLight2NP = self.render.attachNewNode( self.directionalLight2) self.render.setLight(self.directionalLight2NP)
def __init__(self): ShowBase.__init__(self) dlight = core.DirectionalLight('dlight') dlight.setDirection(core.LVector3(1, 0, 1)) dlnode = self.render.attachNewNode(dlight) self.render.setLight(dlnode) alight = core.AmbientLight('alight') alight.setColor((0.3, 0.3, 0.3, 1)) alnode = self.render.attachNewNode(alight) self.render.setLight(alnode)
def prepare(self, curr_cond, stim_period=''): self.curr_cond = curr_cond if stim_period == '' else curr_cond[ stim_period] self.period = stim_period if not self.curr_cond: self.isrunning = False self.background_color = self.curr_cond['background_color'] # set background color self.set_background_color(*self.curr_cond['background_color']) # Set Ambient Light self.ambientLight.setColor(self.curr_cond['ambient_color']) # Set Directional Light self.lights = dict() self.lightsNP = dict() for idx, light_idx in enumerate(iterable(self.curr_cond['light_idx'])): self.lights[idx] = core.DirectionalLight('directionalLight_%d' % idx) self.lightsNP[idx] = self.render.attachNewNode(self.lights[idx]) self.render.setLight(self.lightsNP[idx]) self.lights[idx].setColor(tuple( self.curr_cond['light_color'][idx])) self.lightsNP[idx].setHpr(*self.curr_cond['light_dir'][idx]) # Set Object tasks self.objects = dict() for idx, obj in enumerate(iterable(self.curr_cond['obj_id'])): self.objects[idx] = Agent(self, self.get_cond('obj_', idx)) if 'movie_name' in self.curr_cond: self.movie = True loader = Loader(self) file_name = self.get_clip_info(self.curr_cond, 'file_name') self.mov_texture = loader.loadTexture(self.movie_path + file_name[0]) cm = CardMaker("card") tx_scale = self.mov_texture.getTexScale() cm.setFrame(-1, 1, -tx_scale[1] / tx_scale[0], tx_scale[1] / tx_scale[0]) self.movie_node = NodePath(cm.generate()) self.movie_node.setTexture(self.mov_texture, 1) self.movie_node.setPos(0, 100, 0) self.movie_node.setTexScale(TextureStage.getDefault(), self.mov_texture.getTexScale()) self.movie_node.setScale(48) self.movie_node.reparentTo(self.render) if not self.isrunning: self.timer.start() self.isrunning = True
def init_entity(self, filter_name, entity): if filter_name == 'sun': sun = entity[Sun] light = core.DirectionalLight(entity._uid.name) light.priority = sun.priority path = base.render.attach_new_node(light) path.set_h(sun.azimuth) self.lights[entity] = path elif filter_name == 'ambient': ambient = entity[AmbientLight] light = core.AmbientLight(entity._uid.name) light.color = core.LVecBase4(*ambient.color, 0) * ambient.intensity path = base.render.attach_new_node(light) self.lights[entity] = path base.render.set_light(path)
def setup_lights(self): '''Adds lights to the scene ''' # ambient self.ambient_light = self.render.attach_new_node(p3d.AmbientLight('ambient')) self.ambient_light.node().set_color((0.1, 0.1, 0.1, 1.0)) self.render.set_light(self.ambient_light) # directional self.dir_light = self.render.attach_new_node(p3d.DirectionalLight('directional')) self.dir_light.node().set_color((0.1, 0.1, 0.25, 1.0)) self.dir_light.node().set_direction(p3d.Vec3(0.2,0.4,-1.0)) self.render.set_light(self.dir_light) # spot self.spot_light = self.render.attach_new_node(p3d.Spotlight('spot')) self.spot_light.node().set_color((1.0, 1.0, 1.0, 1.0)) self.spot_light.node().set_shadow_caster(True, 1024, 1024) self.spot_light.node().get_lens().set_near_far(0.1, 20.0) self.spot_light.node().get_lens().set_fov(25) self.spot_light.node().set_exponent(120.0) self.spot_light.set_pos(-8, 0, 8) self.spot_light.look_at(p3d.Point3(3,-3,0)) self.render.set_light(self.spot_light)
def __init__(self, direction, color=None, color_temperature=None, intensity=None): self.light = core.DirectionalLight("sun") q = core.Quat() core.look_at(q, direction) self.light.set_transform(core.TransformState.make_quat(q)) self.path = None if color is not None: self.light.set_color(color) if color_temperature is not None: self.light.set_color_temperature(color_temperature) if intensity is not None: self.light.set_color(self.light.get_color() * intensity) if base.quality >= 3: self.light.set_shadow_caster(True, 1024, 2048, -1500) elif base.quality >= 2: self.light.set_shadow_caster(True, 256, 1024, -1500)
def __init__(self): """Construct a Scene.""" self._nodes = {} self._seg_node_map = {} self._loader = p3d.Loader.get_global_ptr() self._render = p3d.NodePath('#scene') self._bg_color = (0.7, 0.7, 0.8, 0.0) # setup attributes self._render.set_attrib(p3d.RescaleNormalAttrib.makeDefault(), 1) self._render.set_two_sided(False, 1) self._render.set_antialias(p3d.AntialiasAttrib.MAuto, 1) self._render.set_shader_auto(1) self._render.set_depth_offset(1, 1) # setup default camera camera = p3d.Camera('#camera', p3d.PerspectiveLens()) self._camera_np = self._render.attach_new_node(camera) self._camera_np.set_pos(0.0, -2.0, 3.0) self._camera_np.look_at(0.0, 0.0, 0.0) # setup ambient light alight = p3d.AmbientLight('#alight') alight.set_color((0.7, 0.7, 0.7, 0.0)) self._alight_np = self._render.attach_new_node(alight) self._render.set_light(self._alight_np) # setup directional light dlight = p3d.DirectionalLight('#dlight') dlight.set_color((0.3, 0.3, 0.3, 0.0)) dlight.get_lens().set_film_size(2, 2) dlight.get_lens().set_near_far(0.1, 10) dlight.set_shadow_caster(True, 512, 512) self._dlight_np = self._render.attach_new_node(dlight) self._dlight_np.set_pos(-0.8, -0.2, 2.0) self._dlight_np.look_at(0.0, 0.0, 0.0) self._render.set_light(self._dlight_np)
def start(self): # The main initialization of our class # This creates the on screen title that is in every tutorial self.title = OnscreenText(text="Panda3D: Tutorial - Lighting", style=1, fg=(1, 1, 0, 1), shadow=(0, 0, 0, 0.5), pos=(0.87, -0.95), scale = .07) # Creates labels used for onscreen instructions self.ambientText = self.makeStatusLabel(0) self.directionalText = self.makeStatusLabel(1) self.spotlightText = self.makeStatusLabel(2) self.pointLightText = self.makeStatusLabel(3) self.spinningText = self.makeStatusLabel(4) self.ambientBrightnessText = self.makeStatusLabel(5) self.directionalBrightnessText = self.makeStatusLabel(6) self.spotlightBrightnessText = self.makeStatusLabel(7) self.spotlightExponentText = self.makeStatusLabel(8) self.lightingPerPixelText = self.makeStatusLabel(9) self.lightingShadowsText = self.makeStatusLabel(10) self.disco = self.loader.loadModel("disco_lights_models/disco_hall") self.disco.reparentTo(self.render) self.disco.setPosHpr(0, 50, -4, 90, 0, 0) # First we create an ambient light. All objects are affected by ambient # light equally # Create and name the ambient light self.ambientLight = self.render.attachNewNode(p3dc.AmbientLight("ambientLight")) # Set the color of the ambient light self.ambientLight.node().setColor((.1, .1, .1, 1)) # add the newly created light to the lightAttrib # Now we create a directional light. Directional lights add shading from a # given angle. This is good for far away sources like the sun self.directionalLight = self.render.attachNewNode( p3dc.DirectionalLight("directionalLight")) self.directionalLight.node().setColor((.35, .35, .35, 1)) # The direction of a directional light is set as a 3D vector self.directionalLight.node().setDirection(p3dc.LVector3(1, 1, -2)) # These settings are necessary for shadows to work correctly self.directionalLight.setZ(6) dlens = self.directionalLight.node().getLens() dlens.setFilmSize(41, 21) dlens.setNearFar(50, 75) # self.directionalLight.node().showFrustum() # Now we create a spotlight. Spotlights light objects in a given cone # They are good for simulating things like flashlights self.spotlight = self.camera.attachNewNode(p3dc.Spotlight("spotlight")) self.spotlight.node().setColor((.45, .45, .45, 1)) self.spotlight.node().setSpecularColor((0, 0, 0, 1)) # The cone of a spotlight is controlled by it's lens. This creates the lens self.spotlight.node().setLens(p3dc.PerspectiveLens()) # This sets the Field of View (fov) of the lens, in degrees for width # and height. The lower the numbers, the tighter the spotlight. self.spotlight.node().getLens().setFov(16, 16) # Attenuation controls how the light fades with distance. The three # values represent the three attenuation constants (constant, linear, # and quadratic) in the internal lighting equation. The higher the # numbers the shorter the light goes. self.spotlight.node().setAttenuation(p3dc.LVector3(1, 0.0, 0.0)) # This exponent value sets how soft the edge of the spotlight is. # 0 means a hard edge. 128 means a very soft edge. self.spotlight.node().setExponent(60.0) # Now we create three colored Point lights. Point lights are lights that # radiate from a single point, like a light bulb. Like spotlights, they # are given position by attaching them to NodePaths in the world self.redHelper = self.loader.loadModel('disco_lights_models/sphere') self.redHelper.setColor((1, 0, 0, 1)) self.redHelper.setPos(-6.5, -3.75, 0) self.redHelper.setScale(.25) self.redPointLight = self.redHelper.attachNewNode( p3dc.PointLight("redPointLight")) self.redPointLight.node().setColor((.35, 0, 0, 1)) self.redPointLight.node().setAttenuation(p3dc.LVector3(.1, 0.04, 0.0)) # The green point light and helper self.greenHelper = self.loader.loadModel('disco_lights_models/sphere') self.greenHelper.setColor((0, 1, 0, 1)) self.greenHelper.setPos(0, 7.5, 0) self.greenHelper.setScale(.25) self.greenPointLight = self.greenHelper.attachNewNode( p3dc.PointLight("greenPointLight")) self.greenPointLight.node().setAttenuation(p3dc.LVector3(.1, .04, .0)) self.greenPointLight.node().setColor((0, .35, 0, 1)) # The blue point light and helper self.blueHelper = self.loader.loadModel('disco_lights_models/sphere') self.blueHelper.setColor((0, 0, 1, 1)) self.blueHelper.setPos(6.5, -3.75, 0) self.blueHelper.setScale(.25) self.bluePointLight = self.blueHelper.attachNewNode( p3dc.PointLight("bluePointLight")) self.bluePointLight.node().setAttenuation(p3dc.LVector3(.1, 0.04, 0.0)) self.bluePointLight.node().setColor((0, 0, .35, 1)) self.bluePointLight.node().setSpecularColor((1, 1, 1, 1)) # Create a dummy node so the lights can be spun with one command self.pointLightHelper = self.render.attachNewNode("pointLightHelper") self.pointLightHelper.setPos(0, 50, 11) self.redHelper.reparentTo(self.pointLightHelper) self.greenHelper.reparentTo(self.pointLightHelper) self.blueHelper.reparentTo(self.pointLightHelper) # Finally we store the lights on the root of the scene graph. # This will cause them to affect everything in the scene. self.render.setLight(self.ambientLight) self.render.setLight(self.directionalLight) self.render.setLight(self.spotlight) self.render.setLight(self.redPointLight) self.render.setLight(self.greenPointLight) self.render.setLight(self.bluePointLight) # Create and start interval to spin the lights, and a variable to # manage them. self.pointLightsSpin = self.pointLightHelper.hprInterval( 6, p3dc.LVector3(360, 0, 0)) self.pointLightsSpin.loop() self.arePointLightsSpinning = True # Per-pixel lighting and shadows are initially off self.perPixelEnabled = False self.shadowsEnabled = False # listen to keys for controlling the lights self.accept("escape", sys.exit) self.accept("a", self.toggleLights, [[self.ambientLight]]) self.accept("d", self.toggleLights, [[self.directionalLight]]) self.accept("s", self.toggleLights, [[self.spotlight]]) self.accept("p", self.toggleLights, [[self.redPointLight, self.greenPointLight, self.bluePointLight]]) self.accept("r", self.toggleSpinningPointLights) self.accept("l", self.togglePerPixelLighting) self.accept("e", self.toggleShadows) self.accept("z", self.addBrightness, [self.ambientLight, -.05]) self.accept("x", self.addBrightness, [self.ambientLight, .05]) self.accept("c", self.addBrightness, [self.directionalLight, -.05]) self.accept("v", self.addBrightness, [self.directionalLight, .05]) self.accept("b", self.addBrightness, [self.spotlight, -.05]) self.accept("n", self.addBrightness, [self.spotlight, .05]) self.accept("q", self.adjustSpotlightExponent, [self.spotlight, -1]) self.accept("w", self.adjustSpotlightExponent, [self.spotlight, 1]) # Finally call the function that builds the instruction texts self.updateStatusLabel()
def __init__(self): super().__init__() # Allow panda to synthesize shaders. Make sure hardware-animated-vertices is set # to true in panda config. render.setShaderAuto() # Show FPS self.setFrameRateMeter(True) # Load character self.character = Actor('data/character/character') self.character.reparentTo(self.render) self.joints = [] for name in JOINT_NAMES: j = self.character.controlJoint(None, 'modelRoot', name) j.reparentTo(self.render) self.joints.append(j) # Add lights dlight = core.DirectionalLight('DirectionalLight') dlight.setDirection(core.LVector3(2, 0, -1)) dlight.setColor(core.LColor(1, 1, 1, 1)) dlnp = self.render.attachNewNode(dlight) self.render.setLight(dlnp) alight = core.AmbientLight('AmbientLight') alight.setColor(core.LColor(0.6, 0.6, 0.6, 1)) alnp = self.render.attachNewNode(alight) self.render.setLight(alnp) # Camera angle in xy-plane (degrees) self.camera_angle = 0 # Camera control self.cam_left_pressed, self.cam_right_pressed = 0, 0 self.accept('a', self.set_cam_left_pressed, [1]) self.accept('a-up', self.set_cam_left_pressed, [0]) self.accept('d', self.set_cam_right_pressed, [1]) self.accept('d-up', self.set_cam_right_pressed, [0]) # Pad display self.pad_radius = 60 self.pad_outline = OnscreenImage('project/data/pad/pad_outline.png', (0, 0, 0)) self.pad_outline.setTransparency(core.TransparencyAttrib.MAlpha) self.pad_outline.hide() self.pad_response_circle = OnscreenImage( 'project/data/pad/pad_response_indicator.png', (0, 0, 0)) self.pad_response_circle.setTransparency( core.TransparencyAttrib.MAlpha) self.pad_response_circle.hide() self.accept('window-event', self.handle_window_event) # Pad control self.mouse1_pressed, self.controlling_pad = False, False self.accept('mouse1', self.set_mouse1_pressed, [True]) self.accept('mouse1-up', self.set_mouse1_pressed, [False]) # Load terrain self.heightmaps = {} self.set_heightmap('hmap2', 'project/data/heightmaps/hmap2.npy') # Heightmap choice self.accept('1', self.set_heightmap, ['hmap1', 'project/data/heightmaps/hmap1.npy']) self.accept('2', self.set_heightmap, ['hmap2', 'project/data/heightmaps/hmap2.npy']) self.accept('3', self.set_heightmap, ['hmap3', 'project/data/heightmaps/hmap3.npy']) self.accept('4', self.set_heightmap, ['hmap4', 'project/data/heightmaps/hmap4.npy']) self.accept('5', self.set_heightmap, ['hmap5', 'project/data/heightmaps/hmap5.npy']) # Tasks self.taskMgr.add(self.update_pad, 'UpdatePadTask', sort=1) self.last_update_char_time = 0 self.taskMgr.add(self.update_character, 'UpdateCharacterTask', sort=2) self.last_update_cam_time = 0 self.taskMgr.add(self.update_camera, 'UpdateCameraTask', sort=3)
def start(self): # Set the background color to black self.win.setClearColor((0, 0, 0, 1)) # This is used to store which keys are currently pressed. self.keyMap = { "left": 0, "right": 0, "forward": 0, "cam-left": 0, "cam-right": 0} # Post the instructions self.title = addTitle( "Panda3D Tutorial: Roaming Ralph (Walking on Uneven Terrain)") self.inst1 = addInstructions(0.06, "[ESC]: Quit") self.inst2 = addInstructions(0.12, "[Left Arrow]: Rotate Ralph Left") self.inst3 = addInstructions(0.18, "[Right Arrow]: Rotate Ralph Right") self.inst4 = addInstructions(0.24, "[Up Arrow]: Run Ralph Forward") self.inst6 = addInstructions(0.30, "[A]: Rotate p3dc.Camera Left") self.inst7 = addInstructions(0.36, "[S]: Rotate p3dc.Camera Right") # Set up the environment # # This environment model contains collision meshes. If you look # in the egg file, you will see the following: # # <Collide> { Polyset keep descend } # # This tag causes the following mesh to be converted to a collision # mesh -- a mesh which is optimized for collision, not rendering. # It also keeps the original mesh, so there are now two copies --- # one optimized for rendering, one for collisions. self.environ = self.loader.loadModel("roaming_ralph_models/world") self.environ.reparentTo(self.render) # Create the main character, Ralph ralphStartPos = self.environ.find("**/start_point").getPos() self.ralph = Actor("roaming_ralph_models/ralph", {"run": "roaming_ralph_models/ralph-run", "walk": "roaming_ralph_models/ralph-walk"}) self.ralph.reparentTo(self.render) self.ralph.setScale(.2) self.ralph.setPos(ralphStartPos + (0, 0, 0.5)) # Create a floater object, which floats 2 units above ralph. We # use this as a target for the camera to look at. self.floater = p3dc.NodePath(p3dc.PandaNode("floater")) self.floater.reparentTo(self.ralph) self.floater.setZ(2.0) # Accept the control keys for movement and rotation self.accept("escape", sys.exit) self.accept("arrow_left", self.setKey, ["left", True]) self.accept("arrow_right", self.setKey, ["right", True]) self.accept("arrow_up", self.setKey, ["forward", True]) self.accept("a", self.setKey, ["cam-left", True]) self.accept("s", self.setKey, ["cam-right", True]) self.accept("arrow_left-up", self.setKey, ["left", False]) self.accept("arrow_right-up", self.setKey, ["right", False]) self.accept("arrow_up-up", self.setKey, ["forward", False]) self.accept("a-up", self.setKey, ["cam-left", False]) self.accept("s-up", self.setKey, ["cam-right", False]) self.taskMgr.add(self.move, "moveTask") # Game state variables self.isMoving = False # Set up the camera self.disableMouse() self.camera.setPos(self.ralph.getX(), self.ralph.getY() + 10, 2) # We will detect the height of the terrain by creating a collision # ray and casting it downward toward the terrain. One ray will # start above ralph's head, and the other will start above the camera. # A ray may hit the terrain, or it may hit a rock or a tree. If it # hits the terrain, we can detect the height. If it hits anything # else, we rule that the move is illegal. self.cTrav = p3dc.CollisionTraverser() self.ralphGroundRay = p3dc.CollisionRay() self.ralphGroundRay.setOrigin(0, 0, 9) self.ralphGroundRay.setDirection(0, 0, -1) self.ralphGroundCol = p3dc.CollisionNode('ralphRay') self.ralphGroundCol.addSolid(self.ralphGroundRay) self.ralphGroundCol.setFromCollideMask(p3dc.CollideMask.bit(0)) self.ralphGroundCol.setIntoCollideMask(p3dc.CollideMask.allOff()) self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol) self.ralphGroundHandler = p3dc.CollisionHandlerQueue() self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler) self.camGroundRay = p3dc.CollisionRay() self.camGroundRay.setOrigin(0, 0, 9) self.camGroundRay.setDirection(0, 0, -1) self.camGroundCol = p3dc.CollisionNode('camRay') self.camGroundCol.addSolid(self.camGroundRay) self.camGroundCol.setFromCollideMask(p3dc.CollideMask.bit(0)) self.camGroundCol.setIntoCollideMask(p3dc.CollideMask.allOff()) self.camGroundColNp = self.camera.attachNewNode(self.camGroundCol) self.camGroundHandler = p3dc.CollisionHandlerQueue() self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler) # Uncomment this line to see the collision rays #self.ralphGroundColNp.show() #self.camGroundColNp.show() # Uncomment this line to show a visual representation of the # collisions occuring #self.cTrav.showCollisions(render) # Create some lighting ambientLight = p3dc.AmbientLight("ambientLight") ambientLight.setColor((.3, .3, .3, 1)) directionalLight = p3dc.DirectionalLight("directionalLight") directionalLight.setDirection((-5, -5, -5)) directionalLight.setColor((1, 1, 1, 1)) directionalLight.setSpecularColor((1, 1, 1, 1)) self.render.setLight(self.render.attachNewNode(ambientLight)) self.render.setLight(self.render.attachNewNode(directionalLight))
def __init__(self): ShowBase.__init__(self) self.render.set_shader_auto() light = p3d.DirectionalLight('sun') light.set_color(p3d.VBase4(1.0, 0.94, 0.84, 1.0)) light_np = self.render.attach_new_node(light) light_np.set_hpr(p3d.VBase3(0, -45, 0)) self.render.set_light(light_np) light = p3d.DirectionalLight('indirect') light.set_color(p3d.VBase4(0.15, 0.15, 0.15, 1.0)) light_np = self.render.attach_new_node(light) light_np.set_hpr(p3d.VBase3(0, 45, 0)) self.render.set_light(light_np) if base.win: wp = p3d.WindowProperties() wp.set_cursor_hidden(True) wp.set_mouse_mode(p3d.WindowProperties.MRelative) base.win.requestProperties(wp) self.disableMouse() self.inputmapper = inputmapper.InputMapper('input.conf') self.ecsmanager = ecs.ECSManager() self.ecsmanager.add_system(CharacterSystem()) self.ecsmanager.add_system(PhysicsSystem()) self.ecsmanager.add_system(EffectSystem()) self.ecsmanager.add_system(AiSystem()) port = int(sys.argv[2]) if len(sys.argv) > 2 else 9999 host = sys.argv[3] if len(sys.argv) > 3 else 'localhost' if len(sys.argv) == 1 or sys.argv[1] == 'stand-alone': is_server = False proc = subprocess.Popen([sys.argv[0], 'server', str(port), str(host)]) def kill_server(): if proc: print('Terminating stand-alone server') proc.terminate() atexit.register(kill_server) time.sleep(1) elif sys.argv[1] == 'server': is_server = True # No need to run at full speed globalClock.set_mode(p3d.ClockObject.MLimited) globalClock.set_frame_rate(60) elif sys.argv[1] == 'client': is_server = False else: raise RuntimeError('Unrecognized mode: {}'.format(sys.argv[1])) self.network_manager = network.NetworkManager(self.ecsmanager, network.PandaTransportLayer, is_server) if is_server: self.network_manager.start_server(port) else: self.network_manager.start_client(host, port) self.game_mode = game_modes.ClassicGameMode() def run_ecs(task): self.ecsmanager.update(globalClock.get_dt()) if self.game_mode.is_game_over(): print("Game over, restarting") messenger.send('restart-game') return task.cont self.taskMgr.add(run_ecs, 'ECS') def run_net(task): self.network_manager.update(globalClock.get_dt()) return task.cont self.taskMgr.add(run_net, 'Network') def run_gamemode(task): self.game_mode.update(globalClock.get_dt()) return task.cont self.taskMgr.add(run_gamemode, 'Game Mode') def restart_game(): self.game_mode.end_game() self.game_mode.start_game() restart_game() self.accept('restart-game', restart_game) self.accept('quit-up', sys.exit) self.accept('aspectRatioChanged', self.cb_resize)
def __init__(self): ''' init the base class, this will give us access to all the ShowBase functionality If you are using python 2.x you could use: ShowBase.__init__(self) but a better solution is to use Python 3.x, Here's a link, go ahead, download it, I'm not going anywhere... https://www.python.org/downloads/ ''' super().__init__() ''' By default the background is a dull grey, let's make it black colors in Panda3D usually are red, green, blue and alpha values (in a 0.0-1.0 range, where 0.0 is black and 1.1 is white ) but in this case we only have red, green and blue''' self.set_background_color(0.0, 0.0, 0.0) # Load a model of a empty room self.room = self.loader.load_model('models/room_industrial') ''' loader returns a node (NodePath), to make it visible we need to parent it to 'render' -the root node of the scene graph we could use just 'render' but apparently that's un-pythonic''' self.room.reparent_to(self.render) ''' Now we need to add some crates can't have games without crates, I'm almost sure there's a law for that we'll load a model and copy it a few times''' crate_model = self.loader.load_model('models/crate') ''' The crate should be a 1x1x1 cube, but *someone* who knows nothing about Blender made the model (it's all wezu's fault!) and it's just a wee bit too small- wee need to fix that ''' # First resize it, just a bit crate_model.set_scale(1.033) ''' We don't want this extra scale to persist, so we flatten the model flatten_light() will just apply the transformations (movement scale, rotation) to the vertex of the model flatten_strong() will try to merge meshes into as few batches as it can flatten_medium() will do something in between ''' crate_model.flatten_light() '''We'll make a list of crates so it will be easy to access them later copy_to() makes a copy of a node, parents it to a given node and returns the NodePath with that copied node - we'll use that list comprehension version for the python savvy...''' self.crates = [crate_model.copy_to(self.render) for _ in range(5)] '''...for the non-savvy: the above code is equivalent to: self.crates = [] for i in range(5): self.crates.append(crate_model.copy_to(self.render))''' ''' Now we have the crates all in one place, overlapping - that's no good let's put crate #1 on top of create #0, and twist it a bit the crates are 1x1x1 cubes with the pivot cantered at the bottom, so placing them is easy Panda3D uses a Z-up coordinate system (by default) Z Y | / | / |/____ X X is right ---> /\ / Y is forward / ^ | Z is up | ''' # First move it up self.crates[1].set_pos(0, 0, 1) ''' HPR stands for Heading, Pitch, Roll Heading is the rotation around a vertical axis, eg. the way a top spins, the way Earth spins, looking left and right Pitch is the rotation around a horizontal axis, eg. the way your jaw moves, the way a bike throttle in the handle works, looking up and down Roll is the rotation in the.. em.. the other axis eg. the way clock hands move''' # Rotate it a bit, 10 degrees should be fine self.crates[1].set_hpr(10, 0, 0) # Let's move crate #2 to the side and a bit back self.crates[2].set_pos(1, -0.3, 0) # Let's make the #3 and #4 crates bigger, and also move them self.crates[3].set_pos(3, -3, 0) self.crates[4].set_pos(3.1, -1.35, 0) self.crates[3].set_scale(1.6) self.crates[4].set_scale(1.3) ''' Still looks lame. Why don't we change the textures on the small crates? I think the crate uses 2 textures - a diffuse(color) texture and a normal (bump) texture let's just see what textures the crates have and change the textures if we find anything worth changing we will be using some enums defined near the top you can use p3d.SamplerState.FT_linear_mipmap_linear in place of FT_MIPMAP but first - load new textures''' new_diffuse_tex = self.loader.load_texture( 'models/texture/crate_2.png') # loader uses some reasonable defaults, but let's play with the filter type a new_diffuse_tex.set_minfilter(FT_MIPMAP) new_diffuse_tex.set_magfilter(FT_LINEAR) # Same for normal map, but pass the filter types as arguments # both give the same result, use what you prefer new_normal_tex = self.loader.load_texture( 'models/texture/crate_2_ng.png', minfilter=FT_MIPMAP, magfilter=FT_LINEAR) # Change the textures only on the last two crates for crate in self.crates[-2:]: # Model have textures in texture stages # so we iterate over all the texture stages of the model for tex_stage in crate.find_all_texture_stages(): if crate.find_texture( tex_stage): #test if there is any texture to override # texture stages have modes if tex_stage.get_mode() in (TS_NORMAL, TS_NORMAL_GLOSS): # we found ourself a normal map - replace! crate.set_texture(tex_stage, new_normal_tex, 1) if tex_stage.get_mode() == TS_MODULATE: # we found ourself a diffuse map - replace! crate.set_texture(tex_stage, new_diffuse_tex, 1) ''' Now it looks... wait, what? The texture we loaded is grey! Someone overrode it, and now it's broken! No panic - we'll just add some color to the models. make crate #3 a nice yellowish-brown color''' self.crates[3].set_color((0.8, 0.696, 0.496, 1.0), 1) # Make crate #4 a nice brown color self.crates[4].set_color((0.66, 0.55, 0.46, 1.0), 1) # That still looks bad, but at least you now know how to change colors '''ShowBase creates a camera for us, so we can use it without any extra setup ...well almost, for this scene we don't want the player to move the camera by default the camera can be moved and rotated using the mouse, we'll disable that mouse-look, using the worst named function ever:''' self.disable_mouse( ) # <- this disables the camera mouse control not the mouse! # Now we can place the camera in a good spot self.camera.set_pos(-7.0, -4.5, 4.5) # We could use self.camera.set_hpr() to orient the camera, but it's # simpler to just point it at one of the crates, (or a point in space) self.camera.look_at(self.crates[4]) ''' Lets also change the field of view (FOV) for the camera ShowBase already has a reference to the default lens used by the default camera that lens is available under the name base.camLens or self.camLens that name uses the old camelCase naming convention and we want to avoid that Let's get the lens from the default camera here's the catch - the *actual* camera is *not* self.camera (or base.camera) self.camera is a extra node (NodePath class) that has the camera (Camera class) ...well, kind of, the camera is also wrapped in a NodePath, hence the .node() call ShowBase keeps a this under the name self.cam''' self.cam_lens = self.cam.node().get_lens() # The fov may change with the size of the window, # we'll just increase it to 125% of what it was (both horizontal and vertical) fov = self.cam_lens.get_fov() self.cam_lens.set_fov(fov * 1.25) #Lights # First we need an ambient light, else everything not illuminated will be black self.ambient_light = self.render.attach_new_node( p3d.AmbientLight('ambient')) # Remember the use of .node() from earlier? self.ambient_light.node().set_color((0.1, 0.1, 0.1, 1.0)) # Tell Panda3D to actually use this light for anything parented to render self.render.set_light(self.ambient_light) # Next a directional light self.dir_light = self.render.attach_new_node( p3d.DirectionalLight('directional')) self.dir_light.node().set_color((0.1, 0.1, 0.25, 1.0)) ''' You can think of the light direction vector as: 'how much is the light coming from the left, back, and bottom?' on a -1.0 - 1.0 scale''' light_from_left = 0.2 # the light is a bit from the left light_from_back = 0.4 # the light is a bit from the back light_from_bottom = -1.0 # the light is coming from the top light_vec = p3d.Vec3(light_from_left, light_from_back, light_from_bottom) light_vec.normalize() # <- not needed, but it won't hurt self.dir_light.node().set_direction(light_vec) self.render.set_light(self.dir_light) # Last we add a spotlight, just for shadows self.spot_light = self.render.attach_new_node(p3d.Spotlight('spot')) self.spot_light.node().set_color((1.0, 1.0, 1.0, 1.0)) # Make a light cast shadows, and set the shadow map resolution to 1024x1024 self.spot_light.node().set_shadow_caster(True, 1024, 1024) ''' To get a good resolution for the shadow depth map you need to fit the near and far planes of the light (lense) the more precise the bounds, the better the shadow quality''' self.spot_light.node().get_lens().set_near_far(0.1, 20.0) # Let's set the light cone to be narrower self.spot_light.node().get_lens().set_fov(25) # and also to have a smoother falloff self.spot_light.node().set_exponent(120.0) # spotlights unlike ambient and directional lights need to be # placed and oriented in the scene self.spot_light.set_pos(-8, 0, 8) self.spot_light.look_at(self.crates[3]) self.render.set_light(self.spot_light) # Enable the shader generator, # it creates Cg shaders for per pixel lights and shadows # It's an older code but it checks out self.render.set_shader_auto() # Enable MSAA self.render.set_antialias(p3d.AntialiasAttrib.M_multisample)
def start(self): # Set time of day if self.render_pipeline: self.render_pipeline.daytime_mgr.time = "7:40" # Use a special effect for rendering the scene, this is because the # roaming ralph model has no normals or valid materials if self.render_pipeline: self.render_pipeline.set_effect( ape.render(), "roaming_ralph_pipeline_scene-effect.yaml", {}, sort=250) self.keyMap = { "left": 0, "right": 0, "forward": 0, "backward": 0, "cam-left": 0, "cam-right": 0 } self.speed = 1.0 ape.base().win.setClearColor(p3dc.Vec4(0, 0, 0, 1)) # Post the instructions self.inst4 = addInstructions(0.90, "[W] Run Ralph Forward") self.inst4 = addInstructions(0.85, "[S] Run Ralph Backward") self.inst2 = addInstructions(0.80, "[A] Rotate Ralph Left") self.inst3 = addInstructions(0.75, "[D] Rotate Ralph Right") self.inst6 = addInstructions(0.70, "[Left Arrow] Rotate Camera Left") self.inst7 = addInstructions(0.65, "[Right Arrow] Rotate Camera Right") # Set up the environment # # This environment model contains collision meshes. If you look # in the egg file, you will see the following: # # <Collide> { Polyset keep descend } # # This tag causes the following mesh to be converted to a collision # mesh -- a mesh which is optimized for collision, not rendering. # It also keeps the original mesh, so there are now two copies --- # one optimized for rendering, one for collisions. self.environ = ape.loader().loadModel( "roaming_ralph_pipeline_resources/world") self.environ.reparentTo(ape.render()) self.environ.setPos(0, 0, 0) # Remove wall nodes self.environ.find("**/wall").remove_node() # Create the main character, Ralph self.ralph = Actor( "roaming_ralph_pipeline_resources/ralph", { "run": "roaming_ralph_pipeline_resources/ralph-run", "walk": "roaming_ralph_pipeline_resources/ralph-walk", "stand": "roaming_ralph_pipeline_resources/ralph" }) self.ralph.reparentTo(ape.render()) self.ralph.setScale(.2) self.ralph.setPos(p3dc.Vec3(-110.9, 29.4, 1.8)) # Create a floater object. We use the "floater" as a temporary # variable in a variety of calculations. self.floater = p3dc.NodePath(p3dc.PandaNode("floater")) self.floater.reparentTo(ape.render()) # Accept the control keys for movement and rotation self.accept("a", self.setKey, ["left", 1]) self.accept("d", self.setKey, ["right", 1]) self.accept("w", self.setKey, ["forward", 1]) self.accept("s", self.setKey, ["backward", 1]) self.accept("arrow_left", self.setKey, ["cam-left", 1]) self.accept("arrow_right", self.setKey, ["cam-right", 1]) self.accept("a-up", self.setKey, ["left", 0]) self.accept("d-up", self.setKey, ["right", 0]) self.accept("w-up", self.setKey, ["forward", 0]) self.accept("s-up", self.setKey, ["backward", 0]) self.accept("arrow_left-up", self.setKey, ["cam-left", 0]) self.accept("arrow_right-up", self.setKey, ["cam-right", 0]) self.accept("=", self.adjustSpeed, [0.25]) self.accept("+", self.adjustSpeed, [0.25]) self.accept("-", self.adjustSpeed, [-0.25]) ape.base().taskMgr.add(self.move, "moveTask") # Game state variables self.isMoving = False # Set up the camera ape.base().disableMouse() ape.base().camera.setPos(self.ralph.getX() + 10, self.ralph.getY() + 10, 2) ape.base().camLens.setFov(80) # We will detect the height of the terrain by creating a collision # ray and casting it downward toward the terrain. One ray will # start above ralph's head, and the other will start above the camera. # A ray may hit the terrain, or it may hit a rock or a tree. If it # hits the terrain, we can detect the height. If it hits anything # else, we rule that the move is illegal. self.cTrav = p3dc.CollisionTraverser() self.ralphGroundRay = p3dc.CollisionRay() self.ralphGroundRay.setOrigin(0, 0, 1000) self.ralphGroundRay.setDirection(0, 0, -1) self.ralphGroundCol = p3dc.CollisionNode('ralphRay') self.ralphGroundCol.addSolid(self.ralphGroundRay) self.ralphGroundCol.setFromCollideMask(p3dc.BitMask32.bit(0)) self.ralphGroundCol.setIntoCollideMask(p3dc.BitMask32.allOff()) self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol) self.ralphGroundHandler = p3dc.CollisionHandlerQueue() self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler) self.camGroundRay = p3dc.CollisionRay() self.camGroundRay.setOrigin(0, 0, 1000) self.camGroundRay.setDirection(0, 0, -1) self.camGroundCol = p3dc.CollisionNode('camRay') self.camGroundCol.addSolid(self.camGroundRay) self.camGroundCol.setFromCollideMask(p3dc.BitMask32.bit(0)) self.camGroundCol.setIntoCollideMask(p3dc.BitMask32.allOff()) self.camGroundColNp = ape.base().camera.attachNewNode( self.camGroundCol) self.camGroundHandler = p3dc.CollisionHandlerQueue() self.cTrav.addCollider(self.camGroundColNp, self.camGroundHandler) # Uncomment this line to see the collision rays #self.ralphGroundColNp.show() #self.camGroundColNp.show() # Uncomment this line to show a visual representation of the # collisions occuring #self.cTrav.showCollisions(ape.render()) # Create some lighting ambientLight = p3dc.AmbientLight("ambientLight") ambientLight.setColor(p3dc.Vec4(.3, .3, .3, 1)) directionalLight = p3dc.DirectionalLight("directionalLight") directionalLight.setDirection(p3dc.Vec3(-5, -5, -5)) directionalLight.setColor(p3dc.Vec4(1, 1, 1, 1)) directionalLight.setSpecularColor(p3dc.Vec4(1, 1, 1, 1)) ape.render().setLight(ape.render().attachNewNode(ambientLight)) ape.render().setLight(ape.render().attachNewNode(directionalLight))