def __init__(self, aCol, dDir, dCol): render.setAttrib(LightRampAttrib.makeHdr1()) ambientLight = AmbientLight("ambientLight") ambientLight.setColor(aCol) directionalLight = DirectionalLight("directionalLight") directionalLight.setDirection(dDir) directionalLight.setColor(dCol) directionalLight.setSpecularColor(Vec4(2.0, 2.0, 2.0, 0)) render.setLight(render.attachNewNode(ambientLight)) render.setLight(render.attachNewNode(directionalLight))
def __init__(self, aCol, dDir, dCol): render.setAttrib(LightRampAttrib.makeHdr1()) ambientLight = AmbientLight("ambientLight") ambientLight.setColor(aCol) directionalLight = DirectionalLight("directionalLight") directionalLight.setDirection(dDir) directionalLight.setColor(dCol) directionalLight.setSpecularColor(Vec4(2.0, 2.0, 2.0, 0)) render.setLight(render.attachNewNode(ambientLight)) render.setLight(render.attachNewNode(directionalLight))
def toggle_hdr(self): self.hdr += 1 if self.hdr > 3: self.hdr = 0 print("HDR:", self.hdr) if self.hdr == 0: self.world.clearAttrib(LightRampAttrib.getClassType()) elif self.hdr == 1: self.world.setAttrib(LightRampAttrib.makeHdr0()) elif self.hdr == 2: self.world.setAttrib(LightRampAttrib.makeHdr1()) elif self.hdr == 3: self.world.setAttrib(LightRampAttrib.makeHdr2())
def __init__(self): self.keyMap = {"left":0, "right":0, "forward":0, "backward":0} self.angles = {"left":-90, "right":90, "forward":0, "backward":180} self.title = addTitle("Moving like nowaday's action game") # load the scene base.setBackgroundColor(0,0,0.2,1) floorTex = loader.loadTexture("maps/envir-ground.jpg") cm = CardMaker("") cm.setFrame(-2,2,-2,2) floor = render.attachNewNode(PandaNode("floor")) for y in range(12): for x in range(12): nn = floor.attachNewNode(cm.generate()) nn.setP(-90) nn.setPos((x-6)*4, (y-6)*4, 0) floor.setTexture(floorTex) floor.flattenStrong() # create main character self.eve = Actor("models/eve", {"run":"models/eve_run", "walk":"models/eve_walk"}) self.eve.reparentTo(render) self.eve.setScale(.4) self.eve.setPos(0, 0, 0) # accept the control keys for movement and rotation self.accept("escape", sys.exit) 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("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.pre_move = "forward" taskMgr.add(self.move, "moveTask") # game start variable self.isMoving = False # set up the camera base.camera.reparentTo(self.eve) #look over eve self.cameraTargetHeight = 6.0 self.cameraDistance = 30 self.cameraPitch = 10 # disable the default camera control by mouse base.disableMouse() # hide mouse props = WindowProperties() props.setCursorHidden(True) base.win.requestProperties(props) # create some lighting render.setShaderAuto() render.setAttrib(LightRampAttrib.makeHdr1()) ambientLight = AmbientLight("ambientLight") ambientLight.setColor(Vec4(.4, .4, .3, 1)) directionalLight = DirectionalLight("directionalLight") directionalLight.setDirection(Vec3(0, 8, -2.5)) directionalLight.setColor(Vec4(2.0, 2.0, 2.0, 1)) directionalLight.setSpecularColor(Vec4(2.0, 2.0, 2.0, 1)) render.setLight(render.attachNewNode(ambientLight)) render.setLight(render.attachNewNode(directionalLight))
def __init__(self): self.controlMap = {"left":0, "right":0, "forward":0, "backward":0, "zoom-in":0, "zoom-out":0, "wheel-in":0, "wheel-out":0} self.mousebtn = [0,0,0] base.win.setClearColor(Vec4(0,0,0,1)) # Post the instructions self.title = addTitle("Panda3D Tutorial: Better Ralph (Walking on Uneven Terrain)") self.inst1 = addInstructions(0.95, "[ESC]: Quit") self.inst2 = addInstructions(0.90, "W A S D keys move Ralph forward, left, back, and right, respectively.") self.inst3 = addInstructions(0.85, "Use the mouse to look around and steer Ralph.") self.inst4 = addInstructions(0.80, "Zoom in and out using the mouse wheel, or page up and page down keys.") # 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 = loader.loadModel("models/world") self.environ.reparentTo(render) self.environ.setPos(0,0,0) # Create the main character, Ralph ralphStartPos = self.environ.find("**/start_point").getPos() self.ralph = Actor("models/ralph", {"run":"models/ralph-run", "walk":"models/ralph-walk"}) self.ralph.reparentTo(render) self.ralph.setScale(.2) self.ralph.setPos(ralphStartPos) # Accept the control keys for movement and rotation self.accept("escape", sys.exit) self.accept("w", self.setControl, ["forward",1]) self.accept("a", self.setControl, ["left",1]) self.accept("s", self.setControl, ["backward",1]) self.accept("d", self.setControl, ["right",1]) self.accept("w-up", self.setControl, ["forward",0]) self.accept("a-up", self.setControl, ["left",0]) self.accept("s-up", self.setControl, ["backward",0]) self.accept("d-up", self.setControl, ["right",0]) # self.accept("mouse1", self.setControl, ["zoom-in", 1]) # self.accept("mouse1-up", self.setControl, ["zoom-in", 0]) # self.accept("mouse3", self.setControl, ["zoom-out", 1]) # self.accept("mouse3-up", self.setControl, ["zoom-out", 0]) self.accept("wheel_up", self.setControl, ["wheel-in", 1]) self.accept("wheel_down", self.setControl, ["wheel-out", 1]) self.accept("page_up", self.setControl, ["zoom-in", 1]) self.accept("page_up-up", self.setControl, ["zoom-in", 0]) self.accept("page_down", self.setControl, ["zoom-out", 1]) self.accept("page_down-up", self.setControl, ["zoom-out", 0]) taskMgr.add(self.move,"moveTask") # Game state variables self.isMoving = False # Set up the camera # Adding the camera to Ralph is a simple way to keep the camera locked # in behind Ralph regardless of ralph's movement. base.camera.reparentTo(self.ralph) # We don't actually want to point the camera at Ralph's feet. # This value will serve as a vertical offset so we can look over Ralph self.cameraTargetHeight = 6.0 # How far should the camera be from Ralph self.cameraDistance = 30 # Initialize the pitch of the camera self.cameraPitch = 10 # This just disables the built in camera controls; we're using our own. base.disableMouse() # The mouse moves rotates the camera so lets get rid of the cursor props = WindowProperties() props.setCursorHidden(True) base.win.requestProperties(props) # 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. # 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 = CollisionTraverser() self.ralphGroundRay = CollisionRay() self.ralphGroundRay.setOrigin(0,0,1000) self.ralphGroundRay.setDirection(0,0,-1) self.ralphGroundCol = CollisionNode('ralphRay') self.ralphGroundCol.addSolid(self.ralphGroundRay) self.ralphGroundCol.setFromCollideMask(BitMask32.bit(0)) self.ralphGroundCol.setIntoCollideMask(BitMask32.allOff()) self.ralphGroundColNp = self.ralph.attachNewNode(self.ralphGroundCol) self.ralphGroundHandler = CollisionHandlerQueue() self.cTrav.addCollider(self.ralphGroundColNp, self.ralphGroundHandler) # We will detect anything obstructing the camera's view of the player self.cameraRay = CollisionSegment((0,0,self.cameraTargetHeight),(0,5,5)) self.cameraCol = CollisionNode('cameraRay') self.cameraCol.addSolid(self.cameraRay) self.cameraCol.setFromCollideMask(BitMask32.bit(0)) self.cameraCol.setIntoCollideMask(BitMask32.allOff()) self.cameraColNp = self.ralph.attachNewNode(self.cameraCol) self.cameraColHandler = CollisionHandlerQueue() self.cTrav.addCollider(self.cameraColNp, self.cameraColHandler) ############## CollisionTube doesn't seem to be working # self.cameraRay = CollisionTube( (0,0,self.cameraTargetHeight), # (0,25,25), # (self.cameraTargetHeight/2)) # self.cameraCol = CollisionNode('cameraRay') # self.cameraCol.addSolid(self.cameraRay) # self.cameraCol.setFromCollideMask(BitMask32.bit(0)) # self.cameraCol.setIntoCollideMask(BitMask32.allOff()) # self.cameraColNp = self.ralph.attachNewNode(self.cameraCol) # self.cameraColHandler = CollisionHandlerQueue() # self.cTrav.addCollider(self.cameraColNp, self.cameraColHandler) ############ # Uncomment this line to see the collision rays #self.ralphGroundColNp.show() #self.camGroundColNp.show() #self.cameraColNp.show() # Uncomment this line to show a visual representation of the # collisions occuring #self.cTrav.showCollisions(render) # Create some lighting # lets add hdr lighting for fun render.setShaderAuto() render.setAttrib(LightRampAttrib.makeHdr1()) ambientLight = AmbientLight("ambientLight") # existing lighting is effectively darkened so boost ambient a bit ambientLight.setColor(Vec4(.4, .4, .4, 1)) directionalLight = DirectionalLight("directionalLight") directionalLight.setDirection(Vec3(-5, -5, -5)) # hdr can handle any amount of lighting # lets make things nice and sunny directionalLight.setColor(Vec4(2.0, 2.0, 2.0, 1.0)) directionalLight.setSpecularColor(Vec4(2.0, 2.0, 2.0, 1)) render.setLight(render.attachNewNode(ambientLight)) render.setLight(render.attachNewNode(directionalLight))