def __init__(self, config: PGConfig): super(Light, self).__init__() self.node_path = NodePath("Light") self.direction_np = NodePath(DirectionalLight("direction light")) # self.light.node().setScene(self.render) # Too large will cause the graphics card out of memory. # self.direction_np.node().setShadowCaster(True, 8192, 8192) # self.direction_np.node().setShadowCaster(True, 4096, 4096) self.direction_np.node().setShadowCaster(True, 128, 128) # self.direction_np.node().showFrustum() # self.light.node().getLens().setNearFar(10, 100) self.direction_np.node().setColor(LVector4(1, 1, 0.8, 1)) self.direction_np.node().setCameraMask(CamMask.Shadow) dlens = self.direction_np.node().getLens() dlens.setFilmSize(8, 8) # dlens.setFocalLength(1) # dlens.setNear(3) self.direction_np.node().setColorTemperature(4000) self.direction_np.reparentTo(self.node_path) self.ambient_np = NodePath(AmbientLight("Ambient")) self.ambient_np.node().setColor(LVector4(0.8, 0.8, 0.8, 1)) self.ambient_np.reparentTo(self.node_path)
def __init__(self): ShowBase.__init__(self) base.set_background_color(0.1, 0.1, 0.8, 1) base.set_frame_rate_meter(True) base.cam.set_pos(0, -40, 10) base.cam.look_at(0, 0, 0) # Light alight = AmbientLight('ambientLight') alight.set_color(LVector4(0.5, 0.5, 0.5, 1)) alightNP = render.attach_new_node(alight) dlight = DirectionalLight('directionalLight') dlight.set_direction(LVector3(5, 0, -2)) dlight.set_color(LVector4(0.7, 0.7, 0.7, 1)) dlightNP = render.attach_new_node(dlight) render.clear_light() render.set_light(alightNP) render.set_light(dlightNP) # Input self.accept('escape', self.do_exit) self.accept('r', self.do_reset) self.accept('f1', base.toggle_wireframe) self.accept('f2', base.toggle_texture) self.accept('f3', self.toggle_debug) self.accept('f5', self.do_screenshot) # Task taskMgr.add(self.update, 'updateWorld') # Physics self.setup()
def __init__(self): """ Load some configuration variables, it's important for this to happen before the ShowBase is initialized """ load_prc_file_data( "", """ sync-video #t ### add entries below if you are not running from an installation. #model-path /path/to/panda3d """) ShowBase.__init__(self) base.set_background_color(0.1, 0.1, 0.8, 1) base.set_frame_rate_meter(True) base.cam.set_pos(0, -20, 4) base.cam.look_at(0, 0, 0) # Light alight = AmbientLight('ambientLight') alight.set_color(LVector4(0.5, 0.5, 0.5, 1)) alightNP = render.attach_new_node(alight) dlight = DirectionalLight('directionalLight') dlight.set_direction(LVector3(1, 1, -1)) dlight.set_color(LVector4(0.7, 0.7, 0.7, 1)) dlightNP = render.attach_new_node(dlight) render.clear_light() render.set_light(alightNP) render.set_light(dlightNP) # Input self.accept('escape', self.do_exit) self.accept('r', self.do_reset) self.accept('f1', base.toggle_wireframe) self.accept('f2', base.toggle_texture) self.accept('f3', self.toggle_debug) self.accept('f5', self.do_screenshot) self.accept('space', self.do_jump) self.accept('c', self.do_crouch) inputState.watchWithModifiers('forward', 'w') inputState.watchWithModifiers('left', 'a') inputState.watchWithModifiers('reverse', 's') inputState.watchWithModifiers('right', 'd') inputState.watchWithModifiers('turnLeft', 'q') inputState.watchWithModifiers('turnRight', 'e') # Task taskMgr.add(self.update, 'updateWorld') # Physics self.setup()
def __init__(self): ShowBase.__init__(self) ##################### # # Simply testing procedural geometry generation # ##################### prism = Prism(LVector3(0,0,0), 100, 100, 20, LVector4(1,1,1,1), loader.loadTexture("ground.jpg"), 'prism') prism.generate(render) ramp = Ramp(LVector3(100,100,100), 100, 100, 20, LVector4(1,1,1,1), loader.loadTexture("ground.jpg"), 'ramp') ramp.generate(render)
def __init__(self, pos): self.parts = () self.firstRoomParts = () self.secondRoomParts = () self.thirdRoomParts = () self.iceCubes = () self.white = LVector4(1, 1, 1, 1) self.innerwalltex = loader.loadTexture("caverock.jpg") self.snowTex = loader.loadTexture("ground.jpg") ######## # # ENTRANCE OF CAVE # ######## self.entrance = Prism(pos, 200, 500, 50, self.white, self.snowTex, 'entrance') self.parts += (self.entrance, ) self.setFirstRoom() for p in self.firstRoomParts: self.parts += (p, ) for p in self.secondRoomParts: self.parts += (p, ) for p in self.thirdRoomParts: self.parts += (p, ) for p in self.iceCubes: self.parts += (p, )
def __init__(self): ShowBase.__init__(self) self.key = { "left": 0, "right": 0, "forward": 0, "backward": 0, "cam_left": 0, "cam_right": 0, "cam_down": 0, "cam_up": 0, "up": 0, "down": 0 } ##################### # # Simply testing procedural cave generation # ##################### self.cave = Cave(LVector3(0, 0, 0)) self.cave.generateAllParts(render) #################### # # Setting light so that we could see the definition in the walls # #################### render.setShaderAuto() self.dlight = DirectionalLight('dlight') self.dlight.setColor(LVector4(0.3, 0.1, 0.7, 1)) dlnp = render.attachNewNode(self.dlight) dlnp.setHpr(90, 20, 0) render.setLight(dlnp) self.alight = render.attachNewNode(AmbientLight("Ambient")) self.alight.node().setColor(LVector4(0.3, 0.3, 0.5, .1)) render.setLight(self.alight) base.disableMouse() base.camera.setPos(0, -20, 500) base.camera.setP(-50) self.bindKeys()
def __init__(self): ShowBase.__init__(self) base.set_background_color(0.1, 0.1, 0.8, 1) base.set_frame_rate_meter(True) base.cam.set_pos(0, -20, 4) base.cam.look_at(0, 0, 0) # Light alight = AmbientLight('ambientLight') alight.set_color(LVector4(0.5, 0.5, 0.5, 1)) alightNP = render.attach_new_node(alight) dlight = DirectionalLight('directionalLight') dlight.set_direction(LVector3(1, 1, -1)) dlight.set_color(LVector4(0.7, 0.7, 0.7, 1)) dlightNP = render.attach_new_node(dlight) render.clear_light() render.set_light(alightNP) render.set_light(dlightNP) # Input self.accept('escape', self.do_exit) self.accept('r', self.do_reset) self.accept('f1', base.toggle_wireframe) self.accept('f2', base.toggle_texture) self.accept('f3', self.toggle_debug) self.accept('f5', self.do_screenshot) inputState.watchWithModifiers('forward', 'w') inputState.watchWithModifiers('left', 'a') inputState.watchWithModifiers('reverse', 's') inputState.watchWithModifiers('right', 'd') inputState.watchWithModifiers('turnLeft', 'q') inputState.watchWithModifiers('turnRight', 'e') # Task taskMgr.add(self.update, 'updateWorld') # Physics self.setup()
def load_chunk_data(self): chunk_geom = VChunkGeom(self.voxel_unit_size) buffer = BytesIO(self.chunk.data) data = pickle.load(buffer) for x, y, z, v in data: color = LVector4(0, 255, 0, 1) if v == 1 else LVector4( 255, 0, 0, 1) # Right if x == self.chunk_size.x - 1 or not VChunk.is_position_filled( (x + 1, y, z), data): chunk_geom.add_face(CubeFaces.RIGHT, x, z, y, color) # Left if x == 0 or (x - 1, y, z) not in data: chunk_geom.add_face(CubeFaces.LEFT, x, z, y, color) # Top if y == self.chunk_size.y - 1 or not VChunk.is_position_filled( (x, y + 1, z), data): chunk_geom.add_face(CubeFaces.TOP, x, z, y, color) # Bottom if y == 0 or not VChunk.is_position_filled((x, y - 1, z), data): chunk_geom.add_face(CubeFaces.BOTTOM, x, z, y, color) # Front if z == 0 or not VChunk.is_position_filled((x, y, z - 1), data): chunk_geom.add_face(CubeFaces.FRONT, x, z, y, color) # Back if z == self.chunk_size.z - 1 or not VChunk.is_position_filled( (x, y, z + 1), data): chunk_geom.add_face(CubeFaces.BACK, x, z, y, color) self.geom = chunk_geom.get_geom()
def makeFilterBuffer(srcbuffer, name, sort, prog): blurBuffer = base.win.makeTextureBuffer(name, 512, 512) blurBuffer.setSort(sort) blurBuffer.setClearColor(LVector4(1, 0, 0, 1)) blurCamera = base.makeCamera2d(blurBuffer) blurScene = NodePath("new Scene") blurCamera.node().setScene(blurScene) shader = loader.loadShader(prog) card = srcbuffer.getTextureCard() card.reparentTo(blurScene) card.setShader(shader) return blurBuffer
def convert_vector(self, vector): """ Convert a OpenVR vector into a Panda3D vector. No coordinate system conversion is performed. """ if len(vector.v) == 4: result = LVector4(vector.v[0], vector.v[1], vector.v[2], vector.v[3]) elif len(vector.v) == 3: result = LVector3(vector.v[0], vector.v[1], vector.v[2]) elif len(vector.v) == 2: result = LVector2(vector.v[0], vector.v[1]) return result
def __init__(self, config): super(Light, self).__init__(random_seed=0) self.global_light = config["global_light"] self.direction_np = NodePath(DirectionalLight("direction light")) # self.light.node().setScene(self.render) # Too large will cause the graphics card out of memory. # self.direction_np.node().setShadowCaster(True, 8192, 8192) # self.direction_np.node().setShadowCaster(True, 4096, 4096) if self.global_light: self.direction_np.node().setShadowCaster(True, 16384, 16384) self.direction_np.setPos(0, 0, 50) self.direction_np.lookAt(100, -30, 0) else: self.direction_np.node().setShadowCaster(True, 128, 128) # self.direction_np.node().showFrustum() # self.light.node().getLens().setNearFar(10, 100) self.direction_np.node().setColor(LVector4(1, 1, 0.8, 1)) self.direction_np.node().setCameraMask(CamMask.Shadow) dlens = self.direction_np.node().getLens() if self.global_light: dlens.setFilmSize(256, 256) else: dlens.setFilmSize(8, 8) # dlens.setFocalLength(1) # dlens.setNear(3) self.direction_np.node().setColorTemperature(4000) self.direction_np.reparentTo(self.origin) self.ambient_np = NodePath(AmbientLight("Ambient")) self.ambient_np.node().setColor(LVector4(0.8, 0.8, 0.8, 1)) self.ambient_np.reparentTo(self.origin)
def __init__(self, pos): self.parts = () self.iceCubes = () self.trunks = () self.pos = pos self.white = LVector4(1, 1, 1, 1) self.snow = loader.loadTexture('ground.jpg') self.ice = loader.loadTexture('ice.png') self.tree = loader.loadTexture('tree.png') self.deadTree = loader.loadTexture('deadTree.png') self.buildStartingArea() for p in self.iceCubes: self.parts += (p, )
def __init__(self, pos): self.parts = () self.trees = () self.white = LVector4(1, 1, 1, 1) self.snowtex = loader.loadTexture("ground.jpg") self.ice = loader.loadTexture("ice.jpg") piece_1 = Prism(pos, 400, 400, 50, self.white, self.snowtex, 'piece_1') self.parts += (piece_1, ) piece_2 = Prism( LVector3(piece_1.pos.x, piece_1.pos.y + piece_1.wid, piece_1.pos.z), piece_1.len / 1.5, piece_1.wid, piece_1.dep, self.white, self.snowtex, 'piece_2') self.parts += (piece_2, ) piece_3 = Prism( LVector3(piece_1.pos.x + piece_1.wid + 50, piece_1.pos.y, piece_1.pos.z), 1000, 700, 50, self.white, self.snowtex, 'piece_3') self.parts += (piece_3, ) piece_4 = Prism( LVector3(piece_3.pos.x + piece_3.len - 250, piece_3.pos.y + piece_3.wid, piece_3.pos.z), 250, 500, 50, self.white, self.ice, 'piece_4') self.parts += (piece_4, ) piece_5 = Prism( LVector3(piece_3.pos.x, piece_3.pos.y + piece_3.wid, piece_3.pos.z), 50, 200, 10, self.white, self.snowtex, 'piece_5') self.parts += (piece_5, ) piece_6 = Prism( LVector3(piece_5.pos.x + piece_5.len, piece_4.pos.y, piece_4.pos.z), 50, 100, 10, self.white, self.snowtex, 'piece_6') self.parts += (piece_6, ) piece_7 = Prism( LVector3(piece_4.pos.x - 900, piece_4.pos.y + piece_4.wid + 50, piece_4.pos.z), 1000, 1000, 10, self.white, self.snowtex, 'piece_7') self.parts += (piece_7, )
def __init__(self, parent, lod, x, y, density, scale): PatchBase.__init__(self, parent, lod, density) self.x = x self.y = y self.scale = scale self.size = 1.0 / (1 << lod) self.half_size = self.size / 2.0 self.x0 = x - self.half_size self.y0 = y - self.half_size self.x1 = x + self.half_size self.y1 = y + self.half_size self.centre = LPoint3d(x, y, 0.0) self.flat_coord = LVector4(self.x0 * scale, self.y0 * scale, (self.x1 - self.x0) * scale, (self.y1 - self.y0) * scale) self.layers = []
def __init__(self, parent, lod, x, y, density, scale, min_height, max_height): PatchBase.__init__(self, parent, lod, density) self.x = x self.y = y self.scale = scale self.size = 1.0 / (1 << lod) self.half_size = self.size / 2.0 self.x0 = x self.y0 = y self.x1 = x + self.size self.y1 = y + self.size self.centre = LPoint3d(x + self.half_size, y + self.half_size, 0.0) self.flat_coord = LVector4(self.x0 * scale, self.y1 * scale, (self.x1 - self.x0) * scale, (self.y0 - self.y1) * scale) self.local_bounds = geometry.PatchAABB(1.0, min_height, max_height) self.layers = [] self.create_holder_instance() self.bounds = self.local_bounds.make_copy() self.bounds.xform(self.holder.getNetTransform().getMat()) self.bounds_shape = BoundingBoxShape(self.bounds)
def get_bone_transform(self, bone_transform_array, bone_index): """ Returns the transform related to the given bone. The transform is returned as a translation vector and a quaternion rotation. bone_transform_array : Array containing all the bones transformations bone_index : Index of the bone transformation to retrieve. """ if bone_index < len(bone_transform_array): bone_transform = bone_transform_array[bone_index] if bone_transform is not None: position = bone_transform.position orientation = bone_transform.orientation return self.coord_mat.xform( self.convert_vector(position)), self.coord_mat.xform( self.convert_quaternion(orientation)) else: print( "ERROR: No transform data for bone {}".format(bone_index)) else: print("ERROR: Invalid bone index {}".format(bone_index)) return LVector4(), LQuaternion()
def __init__(self, heightfield_fn="heightfield.png"): # Store the heightfield's filename. self.heightfield_fn = heightfield_fn """ Load some configuration variables, it's important for this to happen before ShowBase is initialized """ load_prc_file_data("", """ sync-video #t textures-power-2 none ###gl-coordinate-system default notify-level-gobj warning notify-level-grutil debug notify-level-shader_terrain debug notify-level-bullet debug ### model paths model-path /usr/share/panda3d model-path /home/juzzuj/code/prereq/bullet-samples/bullet-samples """) ShowBase.__init__(self) base.set_background_color(0.1, 0.1, 0.8, 1) base.set_frame_rate_meter(True) # Increase camera Field Of View and set near and far planes base.camLens.set_fov(90) base.camLens.set_near_far(0.1, 50000) # Lights alight = AmbientLight('ambientLight') alight.set_color(LVector4(0.5, 0.5, 0.5, 1)) alightNP = render.attach_new_node(alight) dlight = DirectionalLight('directionalLight') dlight.set_direction(LVector3(1, 1, -1)) dlight.set_color(LVector4(0.7, 0.7, 0.7, 1)) dlightNP = render.attach_new_node(dlight) render.clear_light() render.set_light(alightNP) render.set_light(dlightNP) # Basic game controls self.accept('escape', self.do_exit) self.accept('f1', base.toggle_wireframe) self.accept('f2', base.toggle_texture) self.accept('f3', self.toggle_debug) self.accept('f5', self.do_screenshot) self.accept('r', self.do_reset) # Vehicle controls inputState.watchWithModifiers('forward', 'w') inputState.watchWithModifiers('turnLeft', 'a') inputState.watchWithModifiers('reverse', 's') inputState.watchWithModifiers('turnRight', 'd') inputState.watchWithModifiers('forward', 'arrow_up') inputState.watchWithModifiers('turnLeft', 'arrow_left') inputState.watchWithModifiers('reverse', 'arrow_down') inputState.watchWithModifiers('turnRight', 'arrow_right') self.accept('space', self.reset_vehicle) # Controls to do with the terrain #self.accept('t', self.rise_in_front) self.accept('t', self.deform_terrain, ["raise"]) self.accept('g', self.deform_terrain, ["depress"]) self.accept('b', self.drop_boxes) # Some debugging and miscellaneous controls self.accept('e', self.query_elevation) self.accept('c', self.convert_coordinates) self.accept('p', self.save) self.accept('h', self.hide_terrain) # Task taskMgr.add(self.update, 'updateWorld') self.setup()
def __init__(self): ShowBase.__init__(self) base.set_background_color(0.1, 0.1, 0.8, 1) base.set_frame_rate_meter(True) base.cam.set_pos_hpr(0, 0, 25, 0, -90, 0) base.disable_mouse() # Input self.accept('escape', self.exitGame) self.accept('f1', base.toggle_wireframe) self.accept('f2', base.toggle_texture) self.accept('f3', self.toggle_debug) self.accept('f5', self.do_screenshot) # Setup scene 1: World self.debugNP = render.attach_new_node(BulletDebugNode('Debug')) self.debugNP.node().show_wireframe(True) self.debugNP.node().show_constraints(True) self.debugNP.node().show_bounding_boxes(True) self.debugNP.node().show_normals(True) self.debugNP.show() self.world = BulletWorld() self.world.set_gravity(LVector3(0, 0, -9.81)) self.world.set_debug_node(self.debugNP.node()) # Setup scene 2: Ball #visNP = loader.load_model('models/ball.egg') visNP = loader.load_model('samples/ball-in-maze/models/ball.egg.pz') visNP.clear_model_nodes() bodyNPs = BulletHelper.from_collision_solids(visNP, True) self.ballNP = bodyNPs[0] self.ballNP.reparent_to(render) self.ballNP.node().set_mass(1.0) self.ballNP.set_pos(4, -4, 1) self.ballNP.node().set_deactivation_enabled(False) visNP.reparent_to(self.ballNP) # Setup scene 3: Maze visNP = loader.load_model('models/maze.egg') #visNP = loader.load_model('samples/ball-in-maze/models/maze.egg.pz') visNP.clear_model_nodes() visNP.reparent_to(render) self.holes = [] self.maze = [] self.mazeNP = visNP bodyNPs = BulletHelper.from_collision_solids(visNP, True); for bodyNP in bodyNPs: bodyNP.reparent_to(render) if isinstance(bodyNP.node(), BulletRigidBodyNode): bodyNP.node().set_mass(0.0) bodyNP.node().set_kinematic(True) self.maze.append(bodyNP) elif isinstance(bodyNP.node(), BulletGhostNode): self.holes.append(bodyNP) # Lighting and material for the ball ambientLight = AmbientLight('ambientLight') ambientLight.set_color(LVector4(0.55, 0.55, 0.55, 1)) directionalLight = DirectionalLight('directionalLight') directionalLight.set_direction(LVector3(0, 0, -1)) directionalLight.set_color(LVector4(0.375, 0.375, 0.375, 1)) directionalLight.set_specular_color(LVector4(1, 1, 1, 1)) self.ballNP.set_light(render.attach_new_node(ambientLight)) self.ballNP.set_light(render.attach_new_node(directionalLight)) m = Material() m.set_specular(LVector4(1,1,1,1)) m.set_shininess(96) self.ballNP.set_material(m, 1) # Startup self.start_game()
def initialize(self): self.timer = 0 base.enableParticles() #base.setFrameRateMeter(True) ########## # # SETUP COLLISION HANDLERS AND FLAMIE'S MODEL # ########## #Create the collision handlers in order to build the level. WALL_MASK = BitMask32.bit(2) FLOOR_MASK = BitMask32.bit(1) #Start up the collision system self.cTrav = CollisionTraverser() #Determine how collisions with walls will be handled self.wallHandler = CollisionHandlerPusher() self.bobbing = False #Setup flamie's model self.flamieNP = base.render.attachNewNode(ActorNode('flamieNP')) self.flamieNP.reparentTo(base.render) self.flamie = loader.loadModel('models/Flame/body') self.flamie.setScale(.5) self.flamie.setTransparency(TransparencyAttrib.MAlpha) self.flamie.setAlphaScale(0) self.flamie.reparentTo(self.flamieNP) self.flamie.setCollideMask(BitMask32.allOff()) flameLight = DirectionalLight("flameLight") fl = self.flamie.attachNewNode(flameLight) fl.setColor(255, 255, 255, 1) flameLight.setDirection((-5, -5, -5)) self.flamie.setLight(fl) self.flamie2 = loader.loadModel("models/p.obj") self.flamie2.setTexture(loader.loadTexture("models/flamie2D/f7.png")) self.flamie2.reparentTo(self.flamieNP) self.flamie2.setScale(4) self.flamie2OriZ = 2 self.flamie2.setPos(-6.5, 0, self.flamie2OriZ) #(length, depth, height) self.flamie2.setLight(fl) self.flamie2.setTransparency(TransparencyAttrib.MAlpha) self.flamieFire = PointLight('fire') self.flamieFire.setColor(VBase4(1,1,1,1)) self.flamieFire.setAttenuation((1,0,1)) plnp = render.attachNewNode(self.flamieFire) plnp.setPos(self.flamieNP.getPos()) self.flamielight = AmbientLight('light') self.flamielight.setColor(VBase4(1, 0.5, 0.6, 1)) self.flamielight = self.flamie2.attachNewNode(self.flamielight) self.flamie2.setLight(self.flamielight) self.flamie2.setLight(plnp) self.mayFlamieBob = True #self.flamie2.setAlphaScale(0.5) '''self.tree = loader.loadModel("models/p2.obj") self.tree.setTexture(loader.loadTexture("deadTree.png")) self.tree.reparentTo(render) self.tree.setScale(4) self.tree.setPos(25,25,0) #(length, depth, height) self.tree.setLight(fl) self.tree.setTransparency(TransparencyAttrib.MAlpha) self.treelight = AmbientLight('light') self.treelight.setColor(VBase4(0.9, 0.9, 0.6, 1)) self.treelight = self.tree.attachNewNode(self.treelight) self.tree.setLight(self.treelight)''' x = 150 y = 20 offset1 = 0 treeList2 = [loader.loadModel("models/p2.obj") for i in range(7)] for j in treeList2: k = random.randint(1, 100) if k%5 is 1 or k%5 is 2: j.setTexture(loader.loadTexture("deadTree.png")) else: j.setTexture(loader.loadTexture("tree.png")) j.reparentTo(render) j.setScale(random.randint(4,7)) j.setTransparency(TransparencyAttrib.MAlpha) j.setPos(x + 3*offset1, y + 4*offset1, 0) treelight = AmbientLight('light') treelight = j.attachNewNode(treelight) j.setLight(treelight) offset1 = offset1 + random.randint(4, 10) x = 4 y = 90 offset1 = 0 treeList2 = [loader.loadModel("models/p2.obj") for i in range(6)] for j in treeList2: k = random.randint(1, 100) if k%5 is 1 or k%5 is 2: j.setTexture(loader.loadTexture("deadTree.png")) else: j.setTexture(loader.loadTexture("tree.png")) j.reparentTo(render) j.setScale(random.randint(4,7)) j.setTransparency(TransparencyAttrib.MAlpha) j.setPos(x + 3*offset1, y + 4*offset1, 0) treelight = AmbientLight('light') treelight = j.attachNewNode(treelight) j.setLight(treelight) offset1 = offset1 + random.randint(4, 10) x = 3 y = 120 offset1 = 0 treeList2 = [loader.loadModel("models/p2.obj") for i in range(4)] for j in treeList2: k = random.randint(1, 100) if k%5 is 1 or k%5 is 2: j.setTexture(loader.loadTexture("deadTree.png")) else: j.setTexture(loader.loadTexture("tree.png")) j.reparentTo(render) j.setScale(random.randint(4,7)) j.setTransparency(TransparencyAttrib.MAlpha) j.setPos(x + 3*offset1, y + 4*offset1, 0) treelight = AmbientLight('light') treelight = j.attachNewNode(treelight) j.setLight(treelight) offset1 = offset1 + random.randint(4, 10) x = 200 y = 20 offset1 = 0 treeList2 = [loader.loadModel("models/p2.obj") for i in range(4)] for j in treeList2: k = random.randint(1, 100) if k%5 is 1 or k%5 is 2: j.setTexture(loader.loadTexture("deadTree.png")) else: j.setTexture(loader.loadTexture("tree.png")) j.reparentTo(render) j.setScale(random.randint(4,7)) j.setTransparency(TransparencyAttrib.MAlpha) j.setPos(x + 3*offset1, y + 4*offset1, 0) treelight = AmbientLight('light') treelight = j.attachNewNode(treelight) j.setLight(treelight) offset1 = offset1 + random.randint(4, 10) ### Something that should look like water ### w = loader.loadModel("models/flatP.obj") w.setTexture(loader.loadTexture("ice.png")) w.reparentTo(render) w.setScale(75) w.setTransparency(TransparencyAttrib.MAlpha) w.setAlphaScale(.7) w.setLight(treelight) w.setPos(-200, 0, -10) self.waterOrigiZ = -10 self.waterSecZ = -95 self.waterThirdZ = -120 self.water = w ### Reskying the sky ### w = loader.loadModel("models/biggerFlatP.obj") w.setTexture(loader.loadTexture("models/n2.jpg")) w.reparentTo(self.flamie2) w.setScale(15) w.setLight(treelight) w.setPos(-200, 450, -200) #(length, depth, height) #Give flamie gravity self.floorHandler = CollisionHandlerGravity() self.floorHandler.setGravity(9.81+100) self.floorHandler.setMaxVelocity(100) ########## # # GENERATING LEVEL PARTS # ########## self.ice_reset = () self.start = PlatformSeg(LVector3(0,0,0)) self.start.generateAllParts(render) self.checkpointCreator(70, 90, self.start.pos.z, 10) self.floater = False for p in self.start.parts: if isinstance(p, Prism): self.collisionBoxCreator(p.pos.x, p.pos.y, p.pos.z, p.len, p.wid, p.dep, 'terraincollision', 'wallcollision') if isinstance(p, Square): self.collisionBoxCreator(p.pos.x, p.pos.y, p.pos.z, p.len, p.wid, 3, 'terraincollision', 'wallcollision') if isinstance(p, IceCube): p.model.setCollideMask(BitMask32.allOff()) self.ice_reset += (p,) iceCubefloor= p.model.find("**/iceFloor") iceCubewall = p.model.find("**/iceWall") iceCubefloor.node().setIntoCollideMask(FLOOR_MASK) iceCubewall.node().setIntoCollideMask(WALL_MASK) self.lostWood = LostWood(LVector3(self.start.pos.x + 750, self.start.parts[0].pos.y + self.start.parts[0].wid, self.start.pos.z)) self.lostWood.generateAllParts(render) self.checkpointCreator(self.lostWood.pos.x+120, self.lostWood.pos.y+150, self.lostWood.pos.z,20) self.checkpointCreator(self.lostWood.parts[6].pos.x + self.lostWood.parts[6].len/2, self.lostWood.parts[6].pos.y + self.lostWood.parts[6].wid/2, self.lostWood.pos.z, 40) for p in self.lostWood.parts: if isinstance(p, Prism): self.collisionBoxCreator(p.pos.x, p.pos.y, p.pos.z, p.len, p.wid, p.dep, 'terraincollision', 'wallcollision') if isinstance(p, Square): self.collisionBoxCreator(p.pos.x, p.pos.y, p.pos.z, p.len, p.wid, 3, 'terraincollision', 'wallcollision') if isinstance(p, IceCube): p.model.setCollideMask(BitMask32.allOff()) self.ice_reset += (p,) iceCubefloor= p.model.find("**/iceFloor") iceCubewall = p.model.find("**/iceWall") iceCubefloor.node().setIntoCollideMask(FLOOR_MASK) iceCubewall.node().setIntoCollideMask(WALL_MASK) self.cave = Cave(LVector3(self.lostWood.pos.x + 1100, self.lostWood.pos.y + 2000, self.lostWood.pos.z - 50)) self.cave.generateAllParts(render) self.checkpointCreator(self.cave.thirdRoomParts[5].pos.x + self.cave.thirdRoomParts[5].len/2, self.cave.thirdRoomParts[5].pos.y + self.cave.thirdRoomParts[5].wid/2, self.cave.thirdRoomParts[5].pos.z, 30) for p in self.cave.parts: if isinstance(p, Prism): self.collisionBoxCreator(p.pos.x, p.pos.y, p.pos.z, p.len, p.wid, p.dep, 'terraincollision', 'wallcollision') if isinstance(p, Square): self.collisionBoxCreator(p.pos.x, p.pos.y, p.pos.z, p.len, p.wid, 3, 'terraincollision', 'wallcollision') if isinstance(p, IceCube): p.model.setCollideMask(BitMask32.allOff()) self.ice_reset += (p,) iceCubefloor= p.model.find("**/iceFloor") iceCubewall = p.model.find("**/iceWall") iceCubefloor.node().setIntoCollideMask(FLOOR_MASK) iceCubewall.node().setIntoCollideMask(WALL_MASK) self.end = End(LVector3(self.cave.thirdRoomParts[8].pos.x - 200, self.cave.thirdRoomParts[8].pos.y + self.cave.thirdRoomParts[8].wid, self.cave.thirdRoomParts[8].pos.z)) self.end.generate(render) self.collisionBoxCreator(self.end.floor.pos.x, self.end.floor.pos.y, self.end.floor.pos.z, self.end.floor.len, self.end.floor.wid, self.end.floor.dep, 'terraincollision', 'wallcollision') ######### # DRAWING THE CABIN AND FINAL CAMPFIRE ######### self.checkpointCreator(self.end.floor.pos.x + self.end.floor.len/2, self.end.floor.pos.y + self.end.floor.wid/2, self.end.floor.pos.z, 30) self.cabin = loader.loadModel("models/p2.obj") self.cabin.setTexture(loader.loadTexture("models/cabin.png")) self.cabin.setScale(50) self.cabin.reparentTo(render) self.cabin.setPos(self.end.floor.pos.x + self.end.floor.len/2, self.end.floor.pos.y + self.end.floor.wid/1.1, self.end.floor.pos.z) self.cabin.setTransparency(TransparencyAttrib.MAlpha) #Manually creating starting position. Copy and paste the first three parameters of the checkpoint you want to start at. self.startPos = LVector3(70, 90, self.start.pos.z) self.flamieNP.setPos(self.startPos) '''#Testing the tree model self.tree = loader.loadModel('models/Tree/log') self.tree.reparentTo(render) self.tree.setPos(-50,0,100) self.tree.setScale(2)''' '''#Add sky background self.sky = loader.loadModel('models/sphere.obj') self.sky.reparentTo(self.camera) self.sky.set_two_sided(True) self.skyTexture = loader.loadTexture("models/n2.jpg") self.sky.setTexture(self.skyTexture) self.sky.set_bin('background', 0) self.sky.set_depth_write(False) self.sky.set_compass()''' ########## # # CREATE FLAMIE'S COLLISION GEOMETRY # ########## #Give flamie a collision sphere in order to collide with walls flamieCollider = self.flamie.attachNewNode(CollisionNode('flamiecnode')) flamieCollider.node().addSolid(CollisionSphere(0,0,0,5)) flamieCollider.node().setFromCollideMask(WALL_MASK) flamieCollider.node().setIntoCollideMask(BitMask32.allOff()) self.wallHandler.addCollider(flamieCollider, self.flamieNP) self.cTrav.addCollider(flamieCollider, self.wallHandler) #Give flamie a collision ray to collide with the floor flamieRay = self.flamie.attachNewNode(CollisionNode('flamieRay')) flamieRay.node().addSolid(CollisionRay(0,0,8,0,0,-1)) flamieRay.node().setFromCollideMask(FLOOR_MASK) flamieRay.node().setIntoCollideMask(BitMask32.allOff()) self.floorHandler.addCollider(flamieRay, self.flamieNP) self.cTrav.addCollider(flamieRay, self.floorHandler) #Add a sensor that lets us melt ice cubes without standing on the cube. meltSensor = self.flamie.attachNewNode(CollisionNode('meltSensor')) cs = CollisionSphere(-2,0,10, 50) meltSensor.node().addSolid(cs) meltSensor.node().setFromCollideMask(WALL_MASK) meltSensor.node().setIntoCollideMask(BitMask32.allOff()) cs.setTangible(0) self.wallHandler.addCollider(meltSensor, self.flamieNP) self.cTrav.addCollider(meltSensor, self.wallHandler) self.wallHandler.addInPattern('%fn-into-%in') self.wallHandler.addAgainPattern('%fn-again-%in') self.accept('meltSensor-into-iceWall', self.melt) self.accept('meltSensor-again-iceWall', self.melt) self.accept('meltSensor-into-checkpointCol', self.newstart) #Add in an event handle to prevent the jumping glitch found on the bobbing ice cubes. self.floorHandler.addInPattern('%fn-into-%in') self.floorHandler.addAgainPattern('%fn-again-%in') self.floorHandler.addOutPattern('%fn-out-%in') self.accept('flamieRay-into-iceFloor', self.jittercancel) self.accept('flamieRay-again-iceFloor', self.jittercancel) self.accept('flamieRay-out-iceFloor', self.jittercanceloff) #Uncomment these lines to see flamie's collision geometry #flamieCollider.show() #flamieRay.show() #meltSensor.show() #Uncomment this line to see the actual collisions. #self.cTrav.showCollisions(render) #This plane is found at the very bottom of the level and adds global gravity. killfloor = CollisionPlane(Plane(Vec3(0,0,1), Point3(0,0,-1000))) killfloorCol = CollisionNode('kfcollision') killfloorCol.addSolid(killfloor) killfloorCol.setIntoCollideMask(BitMask32.bit(1)) killfloorColNp = self.render.attachNewNode(killfloorCol) #################### # # Setting light so that we could see the definition in the walls # #################### render.setShaderAuto() self.dlight = DirectionalLight('dlight') self.dlight.setColor(LVector4(0.3, 0.1, 0.7, 1)) dlnp = render.attachNewNode(self.dlight) dlnp.setHpr(90, 20, 0) render.setLight(dlnp) self.alight = render.attachNewNode(AmbientLight("Ambient")) self.alight.node().setColor(LVector4(0.5, 0.5, 1, .1)) render.setLight(self.alight) self.snow = loader.loadTexture("models/ground.jpg") #Create a floater object and have it float 2 units above fireball. #And use this as a target for the camera to focus on. #This idea is taken from the roaming ralph sample that came with the #Panda3D SDK. self.camFocus = NodePath(PandaNode("floater")) self.camFocus.reparentTo(render) self.camFocusCurrZ = self.flamie.getZ() + 10 #The camera is locked to the avatar so it always follows regardless of movement. self.camera.reparentTo(render) self.cameraTargetHeight = 8.0 self.cameraDistance = 100 self.cameraHeightModes = (self.start.parts[0].pos.z + 45, self.start.parts[0].pos.z + 125, self.camFocus.getZ() + 10, self.camFocus.getZ() + 150, self.end.floor.pos.z + 10) self.cameraHeight = self.cameraHeightModes[0]
def __init__(self): ShowBase.__init__(self) self.key = { "left": 0, "right": 0, "forward": 0, "backward": 0, "cam_left": 0, "cam_right": 0, "cam_down": 0, "cam_up": 0, "up": 0, "down": 0 } ##################### # # Simply testing procedural cave generation # ##################### self.start = PlatformSeg(LVector3(0, 0, 0)) self.start.generateAllParts(render) self.lostWood = LostWood( LVector3(self.start.pos.x + 750, self.start.parts[0].pos.y + self.start.parts[0].wid, self.start.pos.z)) self.lostWood.generateAllParts(render) self.cave = Cave( LVector3(self.lostWood.pos.x + 1100, self.lostWood.parts[6].pos.y + self.lostWood.parts[6].wid, self.lostWood.pos.z)) self.cave.generateAllParts(render) self.end = End( LVector3( self.cave.thirdRoomParts[8].pos.x - 200, self.cave.thirdRoomParts[8].pos.y + self.cave.thirdRoomParts[8].wid, self.cave.thirdRoomParts[8].pos.z)) self.end.generate(render) #################### # # Setting light so that we could see the definition in the walls # #################### render.setShaderAuto() self.dlight = DirectionalLight('dlight') self.dlight.setColor(LVector4(0.3, 0.1, 0.7, 1)) dlnp = render.attachNewNode(self.dlight) dlnp.setHpr(90, 20, 0) render.setLight(dlnp) self.alight = render.attachNewNode(AmbientLight("Ambient")) self.alight.node().setColor(LVector4(0.5, 0.5, 1, .1)) render.setLight(self.alight) base.disableMouse() self.floater = NodePath(PandaNode("floater")) self.floater.reparentTo(render) base.camera.reparentTo(self.floater) base.camera.setPos(0, -20, 500) base.camera.setP(-50) self.bindKeys()
def __init__(self, pos): self.parts = () self.firstRoomParts = () self.secondRoomParts = () self.thirdRoomParts = () self.iceCubesSecondRoom = () self.iceCubesThirdRoom = () self.bigCube = 0 self.white = LVector4(1, 1, 1, 1) self.innerwalltex = loader.loadTexture("caverock.jpg") self.snowTex = loader.loadTexture("ground.jpg") #Creates an array of ice cube velocities for the cube spinning that will be exhibited in room three. self.iceCubeVel = () cube1Vel = LVector2(1, 0) self.iceCubeVel += (cube1Vel, ) cube2Vel = LVector2(1, 0) self.iceCubeVel += (cube2Vel, ) cube3Vel = LVector2(0, 1) self.iceCubeVel += (cube3Vel, ) cube4Vel = LVector2(0, 1) self.iceCubeVel += (cube4Vel, ) cube5Vel = LVector2(-1, 0) self.iceCubeVel += (cube5Vel, ) cube6Vel = LVector2(0, -1) self.iceCubeVel += (cube6Vel, ) cube7Vel = LVector2(0, -1) self.iceCubeVel += (cube7Vel, ) cube8Vel = LVector2(-1, 0) self.iceCubeVel += (cube8Vel, ) self.corn1 = 0 self.corn2 = 0 self.corn3 = 0 self.corn4 = 0 ######## # # ENTRANCE OF CAVE # ######## self.entrance = Prism(pos, 200, 500, 50, self.white, self.snowTex, 'entrance') self.parts += (self.entrance, ) self.setFirstRoom() ''' Add all parts of the first room of the cave into the cave part queue ''' for p in self.firstRoomParts: self.parts += (p, ) ''' Add all parts of the second room of the cave into the cave part queue ''' for p in self.secondRoomParts: self.parts += (p, ) ''' Add all parts of the third room of the cave into the cave part queue ''' for p in self.thirdRoomParts: self.parts += (p, ) ''' Add all ice cubes of second room to the cave part queue ''' for p in self.iceCubesSecondRoom: self.parts += (p, ) ''' Add all ice cubes of third room to the cave part queue ''' for p in self.iceCubesThirdRoom: self.parts += (p, ) self.parts += (self.bigCube, )
def deform_terrain(self, mode="raise", duration=1.0): self.deform_duration = duration """ Calculate distance to the spot at which we want to raise the terrain. At its current speed the vehicle would reach it in 2.5 seconds. [km/h] / 3.6 = [m/s] * [s] = [m] """ distance = self.vehicle.get_current_speed_km_hour() / 3.6 * 2.5 spot_pos_world = (self.vehicleNP.get_pos() + self.vehicle.get_forward_vector() * distance) spot_pos_heightfield = self.terrain_node.world_to_heightfield( spot_pos_world) xcenter = spot_pos_heightfield.x ycenter = spot_pos_heightfield.y """ To create a smooth hill/depression we call PfmFile.pull_spot to create a sort of gradient. You can use self.cardmaker_debug to view it. From the Panda3D API documentation of PfmFile.pull_spot: Applies delta * t to the point values within radius (xr, yr) distance of (xc, yc). The t value is scaled from 1.0 at the center to 0.0 at radius (xr, yr), and this scale follows the specified exponent. Returns the number of points affected. """ # Delta to apply to the point values in DynamicHeightfield array. delta = LVector4(0.001) # Make the raised spot elliptical. xradius = 10.0 # meters yradius = 6.0 # meters # Choose an exponent exponent = 0.6 # Counter-clockwise angle between Y-axis angle = self.vehicle.get_forward_vector().signed_angle_deg( LVector3.forward(), LVector3.down()) # Define all we need to repeatedly deform the terrain using a task. self.spot_params = [delta, xcenter, ycenter, xradius, yradius, exponent, mode] # Clear staging area to a size that fits our raised region. self.StagingPFM.clear(int(xradius)*2, int(yradius)*2, num_channels=1) """ There are two options: (1) Rotate our hill/depression to be perpendicular to the vehicle's trajectory. This is the default. (2) Just put it in the terrain unrotated. """ # Option (1) self.StagingPFM.pull_spot(delta, xradius-0.5, yradius-0.5, xradius, yradius, exponent) # Rotate wider side so it's perpendicular to vehicle's trajectory. self.RotorPFM.rotate_from(self.StagingPFM, angle) self.raise_start_time = globalClock.get_real_time() taskMgr.do_method_later(0.03, self.deform_perpendicular, "DeformPerpendicularSpot")
def __init__(self): # Initialize the ShowBase class from which we inherit, which will # create a window and set up everything we need for rendering into it. ShowBase.__init__(self) base.disableMouse() base.setBackgroundColor(0, 0, 0) camera.setPos(0, -50, 0) # Check video card capabilities. if not base.win.getGsg().getSupportsBasicShaders(): addTitle( "Glow Filter: Video driver reports that Cg shaders are not supported.") return # Post the instructions self.title = addTitle("Panda3D: Tutorial - Glow Filter") self.inst1 = addInstructions(0.06, "ESC: Quit") self.inst2 = addInstructions(0.12, "Space: Toggle Glow Filter On/Off") self.inst3 = addInstructions(0.18, "Enter: Toggle Running/Spinning") self.inst4 = addInstructions(0.24, "V: View the render-to-texture results") # Create the shader that will determime what parts of the scene will # glow glowShader = loader.loadShader("shaders/glowShader.sha") # load our model self.tron = Actor() self.tron.loadModel("models/tron") self.tron.loadAnims({"running": "models/tron_anim"}) self.tron.reparentTo(render) self.interval = self.tron.hprInterval(60, LPoint3(360, 0, 0)) self.interval.loop() self.isRunning = False # put some lighting on the tron model dlight = DirectionalLight('dlight') alight = AmbientLight('alight') dlnp = render.attachNewNode(dlight) alnp = render.attachNewNode(alight) dlight.setColor(LVector4(1.0, 0.7, 0.2, 1)) alight.setColor(LVector4(0.2, 0.2, 0.2, 1)) dlnp.setHpr(0, -60, 0) render.setLight(dlnp) render.setLight(alnp) # create the glow buffer. This buffer renders like a normal scene, # except that only the glowing materials should show up nonblack. glowBuffer = base.win.makeTextureBuffer("Glow scene", 512, 512) glowBuffer.setSort(-3) glowBuffer.setClearColor(LVector4(0, 0, 0, 1)) # We have to attach a camera to the glow buffer. The glow camera # must have the same frustum as the main camera. As long as the aspect # ratios match, the rest will take care of itself. glowCamera = base.makeCamera( glowBuffer, lens=base.cam.node().getLens()) # Tell the glow camera to use the glow shader tempnode = NodePath(PandaNode("temp node")) tempnode.setShader(glowShader) glowCamera.node().setInitialState(tempnode.getState()) # set up the pipeline: from glow scene to blur x to blur y to main # window. blurXBuffer = makeFilterBuffer( glowBuffer, "Blur X", -2, "shaders/XBlurShader.sha") blurYBuffer = makeFilterBuffer( blurXBuffer, "Blur Y", -1, "shaders/YBlurShader.sha") self.finalcard = blurYBuffer.getTextureCard() self.finalcard.reparentTo(render2d) # This attribute is used to add the results of the post-processing # effects to the existing framebuffer image, rather than replace it. # This is mainly useful for glow effects like ours. self.finalcard.setAttrib(ColorBlendAttrib.make(ColorBlendAttrib.MAdd)) # Panda contains a built-in viewer that lets you view the results of # your render-to-texture operations. This code configures the viewer. self.accept("v", base.bufferViewer.toggleEnable) self.accept("V", base.bufferViewer.toggleEnable) base.bufferViewer.setPosition("llcorner") base.bufferViewer.setLayout("hline") base.bufferViewer.setCardSize(0.652, 0) # event handling self.accept("space", self.toggleGlow) self.accept("enter", self.toggleDisplay) self.accept("escape", sys.exit, [0]) self.glowOn = True
def __init__(self, pos): self.bigCube = 0 self.white = LVector4(1, 1, 1, 1) self.snowTex = loader.loadTexture("ground.jpg") self.floor = Prism(pos, 500, 500, 50, self.white, self.snowTex, 'endFloor')