class PbLightNode(): """Pybullet-compatible light node wrapper """ def __init__(self, render: NodePath): self._alight = AmbientLight('pb_alight') self._dlight = DirectionalLight('pb_dlight') self._anode = render.attach_new_node(self._alight) self._dnode = render.attach_new_node(self._dlight) self._render = render self._is_active = False self.set_active(True) def set_active(self, active: bool): if active and not self._is_active: self._render.set_light(self._anode) self._render.set_light(self._dnode) elif not active and self._is_active: self._render.clear_light(self._anode) self._render.clear_light(self._dnode) self._is_active = active def is_active(self): return self._is_active def update(self, pb_light): self._alight.set_color(Vec3(*pb_light.ambient_color)) self._dlight.set_color(Vec3(*pb_light.diffuse_color)) self._dlight.set_specular_color(Vec3(*pb_light.specular_color)) self._dlight.set_shadow_caster(pb_light.shadow_caster) self._dnode.set_pos(Vec3(*pb_light.position)) self._dnode.look_at(0, 0, 0)
def directional_light(colour, direction, specular_colour=None): if specular_colour is None: specular_colour = colour light = DirectionalLight("directionalLight") light.set_color(colour) light.set_specular_color(specular_colour) light.set_direction(direction) return light
def setup_light(game: Game) -> None: ambientLight = AmbientLight("ambient_light") ambientLight.set_color(Vec4(0.3, 0.3, 0.3, 1)) game.render.set_light(game.render.attach_new_node(ambientLight)) directionalLight = DirectionalLight("directional_light") directionalLight.set_direction(Vec3(-5, -5, -5)) directionalLight.set_color(Vec4(1, 1, 1, 1)) directionalLight.set_specular_color(Vec4(1, 1, 1, 1)) game.render.set_light(game.render.attach_new_node(directionalLight))
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()