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()
Example #2
0
 def add_celestial(self, azimuth, elevation, color, intensity, radius,
                   visible):
     """
     Adds a celestial light source to the scene. If it is a visible celestial, also add a sphere model.
     """
     if not self.camera:
         return
     location = Vec3(
         to_cartesian(azimuth, elevation, 1000.0 * 255.0 / 256.0))
     if intensity:
         dlight = DirectionalLight('celestial')
         dlight.set_color((color[0] * intensity, color[1] * intensity,
                           color[2] * intensity, 1.0))
         node = self.scene.attach_new_node(dlight)
         node.look_at(*(location * -1))
         self.scene.set_light(node)
     if visible:
         if radius <= 2.0:
             samples = 6
         elif radius >= 36.0:
             samples = 40
         else:
             samples = int(round(((1.5 * radius) * (2 / 3.0)) + 3.75))
         celestial = Dome(
             radius * 1.5, samples, 2, color, 0, location,
             ((-(math.degrees(azimuth))), 90 + math.degrees(elevation), 0))
         self.celestials.attach(celestial)
Example #3
0
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)
Example #4
0
    def __init__(self):
        # Base
        base.set_background_color(0.2, 0.2, 0.2, 1)
        base.set_frame_rate_meter(True)
        base.cam.set_pos(0, -20, 4)
        base.cam.look_at(0, 0, 0)

        # Light 1
        light1 = AmbientLight('ambientLight')
        light1.set_color(Vec4(0.5, 0.5, 0.5, 1.0))
        light1_node_path = render.attach_new_node(light1)

        # Light 2
        light2 = DirectionalLight('directionalLight')
        light2.set_direction(Vec3(1, 1, -1))
        light2.set_color(Vec4(0.7, 0.7, 0.7, 1))
        light2_node_path = render.attach_new_node(light2)

        # Clear light
        render.clear_light()
        render.set_light(light1_node_path)
        render.set_light(light2_node_path)

        # Input
        self.accept('escape', self.exit_lab())

        # Input state
        inputState.watchWithModifiers('forward', 'w')

        # Task
        taskMgr.add(self._update, 'updateWorld')

        # Physics
        self.setup()
Example #5
0
 def add_celestial(self, azimuth, elevation, color, intensity, radius):
     location = Vec3(to_cartesian(azimuth, elevation, 1000.0 * 255.0 / 256.0))
     if intensity:
         dlight = DirectionalLight('celestial')
         dlight.set_color((color[0] * intensity, color[1] * intensity, color[2] * intensity, 1.0))
         node = self.node.attach_new_node(dlight)
         node.look_at(*(location * -1))
         self.node.set_light(node)
Example #6
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
Example #7
0
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))
Example #8
0
    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()
Example #9
0
    def light_scene(self):
        sun = DirectionalLight("sun")
        sun.set_color((1, 0.8, 0.8, 1))
        sun_np = render.attachNewNode(sun)
        render.set_light(sun_np)
        sun_np.set_h(185)
        sun_np.set_p(-50)

        moon = DirectionalLight("moon")
        moon.set_color((0.8, 0.8, 1, 1))
        moon_np = render.attachNewNode(moon)
        render.set_light(moon_np)
        moon_np.set_p(-50)
    def createLighting(self):
        light = DirectionalLight('light')
        light.set_color(VBase4(0.2, 0.2, 0.2, 1))

        np = self.render.attach_new_node(light)
        np.setPos(0, -200, 0)
        np.lookAt(0, 0, 0)

        self.render.set_light(np)

        light = AmbientLight('ambient')
        light.set_color(VBase4(0.4, 0.4, 0.4, 1))

        np = self.render.attachNewNode(light)

        self.render.setLight(np)
Example #11
0
    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()
Example #12
0
 def add_celestial(self, azimuth, elevation, color, intensity, radius, visible):
     """
     Adds a celestial light source to the scene. If it is a visible celestial, also add a sphere model.
     """
     if not self.camera:
         return
     location = Vec3(to_cartesian(azimuth, elevation, 1000.0 * 255.0 / 256.0))
     if intensity:
         dlight = DirectionalLight('celestial')
         dlight.set_color((color[0] * intensity, color[1] * intensity,
             color[2] * intensity, 1.0))
         node = self.scene.attach_new_node(dlight)
         node.look_at(*(location * -1))
         self.scene.set_light(node)
     if visible:
         if radius <= 2.0:
             samples = 6
         elif radius >= 36.0:
             samples = 40
         else:
             samples = int(round(((1.5 * radius) * (2 / 3.0)) + 3.75))
         celestial = Dome(radius * 1.5, samples, 2, color, 0, location,
             ((-(math.degrees(azimuth))), 90 + math.degrees(elevation), 0))
         self.celestials.attach(celestial)
    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()
Example #15
0
    def __init__(self,
                 cad_file=None,
                 output_size=(512, 512),
                 light_on=True,
                 cast_shadow=True):
        # acquire lock since showbase cannot be created twice
        Panda3DRenderer.__lock.acquire()

        # set output size and init the base
        loadPrcFileData('', f'win-size {output_size[0]} {output_size[1]}')
        base = ShowBase(windowType='offscreen')

        # coordinate for normalized and centered object
        obj_node = base.render.attach_new_node('normalized_obj')

        # ambient
        alight = AmbientLight('alight')
        alight.set_color(VBase4(0.5, 0.5, 0.5, 1.0))
        alnp = base.render.attachNewNode(alight)
        base.render.setLight(alnp)

        # directional light for ambient
        dlight1 = DirectionalLight('dlight1')
        dlight1.set_color(VBase4(0.235, 0.235, 0.235, 1.0))
        dlnp1 = base.render.attach_new_node(dlight1)
        dlnp1.set_pos(-2, 3, 1)
        dlnp1.look_at(obj_node)
        base.render.set_light(dlnp1)

        # point light for ambient
        plight1 = PointLight('plight1')
        plight1.set_color(VBase4(1.75, 1.75, 1.75, 1.0))
        plight1.setAttenuation((1, 1, 1))
        plnp1 = base.render.attach_new_node(plight1)
        plnp1.set_pos(0, 0, 3)
        plnp1.look_at(obj_node)
        base.render.set_light(plnp1)

        plight2 = PointLight('plight2')
        plight2.set_color(VBase4(1.5, 1.5, 1.5, 1.0))
        plight2.setAttenuation((1, 0, 1))
        plnp2 = base.render.attach_new_node(plight2)
        plnp2.set_pos(0, -3, 0)
        plnp2.look_at(obj_node)
        base.render.set_light(plnp2)

        dlight2 = DirectionalLight('dlight2')
        dlight2.set_color(VBase4(0.325, 0.325, 0.325, 1.0))
        dlnp2 = base.render.attach_new_node(dlight2)
        dlnp2.set_pos(-1, 1, -1.65)
        dlnp2.look_at(obj_node)
        base.render.set_light(dlnp2)

        dlight3 = DirectionalLight('dlight3')
        dlight3.set_color(VBase4(0.15, 0.15, 0.15, 1.0))
        dlnp3 = base.render.attach_new_node(dlight3)
        dlnp3.set_pos(-2.5, 2.5, 2.0)
        dlnp3.look_at(obj_node)
        base.render.set_light(dlnp3)
        if cast_shadow:
            lens = PerspectiveLens()
            dlight3.set_lens(lens)
            dlight3.set_shadow_caster(True, 1024, 1024)

        dlight4 = DirectionalLight('dlight4')
        dlight4.set_color(VBase4(0.17, 0.17, 0.17, 1.0))
        dlnp4 = base.render.attach_new_node(dlight4)
        dlnp4.set_pos(1.2, -2.0, 2.5)
        dlnp4.look_at(obj_node)
        base.render.set_light(dlnp4)
        if cast_shadow:
            lens = PerspectiveLens()
            dlight4.set_lens(lens)
            dlight4.set_shadow_caster(True, 1024, 1024)

        self.direct_node = direct_node = base.render.attach_new_node(
            'direct_light')
        dlnp2.reparent_to(direct_node)
        dlnp3.reparent_to(direct_node)
        dlnp4.reparent_to(direct_node)

        # auto shader for shadow
        if cast_shadow:
            base.render.setShaderAuto()
        # no culling
        base.render.set_two_sided(True)
        # anti-alias
        base.render.setAntialias(AntialiasAttrib.MMultisample, 8)
        # init camera position
        self.coverage = 0.5
        # the default clear color
        self.clear_color = (0.0, 0.0, 0.0, 0.0)
        # translate in rendered image
        self.obj_translate = (0, 0)
        # light rotation
        self.light_hpr = (0, 0, 0)
        # object rotation
        self.obj_hpr = (0, 0, 0)

        self.base = base
        self.obj = None
        self.obj_node = obj_node
        self.cast_shadow = cast_shadow
        self.camera = base.camera
        if cad_file is not None:
            self.set_obj(cad_file)

        if not light_on:
            base.render.set_light_off()
Example #16
0
class Sky:
    """Encapsulates global illumination and fog."""

    ambient: AmbientLight
    ambient_np: NodePath
    sun: DirectionalLight
    sun_np: NodePath
    fog: Fog

    def __init__(self, parent: NodePath, camera: Camera):
        self._camera = camera
        self._parent = parent
        self.hours = 0.0
        self._init_sun()
        self._init_ambient()
        self._init_fog()

    def update(self):
        """Update lights."""
        self.sun_np.set_pos(self._camera.focus, 0.0, 0.0, 0.0)
        film = p3d.camera.get_z() + self._camera.rotator.get_p() ** 2.0 * 0.01
        self.sun.get_lens().set_film_size(film, film)
        self.sun.get_lens().set_near_far(-film, film)

    def set_time(self, hours: float):
        """Update lighting according to time in range 0.0 <= t < 24.0."""
        hours %= 24
        self.hours = hours
        self.sun_np.set_p(360.0 * (hours + 6.0) / 24.0)
        color = calculate_sky_color(hours)
        self.sun.set_color((5.0 * color[0], 5.0 * color[1],
                            5.0 * color[2], color[3]))
        self.fog.set_color(color)
        p3d.win.set_clear_color(color)

    def _init_sun(self):
        """Initialize sun light."""
        self.sun = DirectionalLight('sun')
        # self.sun.set_shadow_caster(True, 2048, 2048)
        self.sun.get_lens().set_film_size(1024, 1024)
        self.sun.get_lens().set_near_far(-1024, 1024)
        self.sun.set_camera_mask(0x00010000)
        self.sun.set_color((1.0, 1.0, 1.0, 1.0))
        # self.sun.show_frustum()
        self.sun_np = self._parent.attach_new_node(self.sun)
        self.sun_np.set_hpr(270.0, 0.0, 0.0)
        self.sun_np.set_pos(self._camera.focus, 0.0, 0.0, 512.0)
        self._parent.set_light(self.sun_np)

    def _init_ambient(self):
        """Initialize ambient light."""
        self.ambient = AmbientLight('ambient')
        self.ambient.set_color((0.4, 0.4, 0.6, 1.0))
        self.ambient_np = self._parent.attach_new_node(self.ambient)
        self._parent.set_light(self.ambient_np)

    def _init_fog(self):
        """Initialize distance fog."""
        self.fog = Fog('fog')
        self.fog.set_linear_range(2000.0, 7000.0)
        if not ConfigVariableBool('use-shaders', False):
            self._parent.set_fog(self.fog)