예제 #1
0
        def do_updates(task):
            if not self.connection.running:
                sys.exit()

            latest_scene_update = None
            for update in self.connection.get_updates():
                # print('update: {}'.format(update))
                update_type = update['type']
                if update_type == 'view':
                    self.update_view(
                        update['width'],
                        update['height'],
                        self.load_matrix(update['projection_matrix']),
                        self.load_matrix(update['view_matrix']),
                    )
                elif update_type == 'scene':
                    latest_scene_update = update
                elif update_type == 'background_color':
                    self.bg_color = p3d.LVector4(*update['color'])
                else:
                    raise RuntimeError('Unknown update type: {}'.format(update_type))

            if latest_scene_update is not None:
                self.update_scene(latest_scene_update['path'])

            return task.cont
예제 #2
0
 def process_data(self, data):
     '''Accept converted data to be consumed by the processor'''
     #pprint.pprint(data)
     self.converter.update(data)
     bg = self.converter.background_color
     self.bg = p3d.LVector4(bg[0], bg[1], bg[2], 1)
     self.view_region.set_clear_color(self.bg)
     self.view_camera.reparent_to(self.converter.active_scene)
     self.converter.active_scene.reparent_to(self.render)
예제 #3
0
    def __init__(self, shape, **kwargs):
        sdftex = None
        frame = p3d.LVector4(-1, 1, -1, 1)
        scale = p3d.LVector3(1, 1, 1)
        offset = p3d.LVector3(0, 0, 0)

        if shape == 'circle':
            sdftex = _SDF_CIRCLE
            scale *= kwargs['radius']
        elif shape == 'box':
            sdftex = _SDF_BOX
            scale = p3d.LVector3(kwargs['width'] / 2.0, 1,
                                 kwargs['length'] / 2.0)
            offset = p3d.LVector3(0, kwargs['length'] / 2.0, 0)
        else:
            raise ValueError(
                "Unknown shape for RangeIndicator: {}".format(shape))

        cardmaker = p3d.CardMaker('RI_' + shape)
        cardmaker.set_frame(frame)

        card = p3d.NodePath(cardmaker.generate())
        card.set_p(-90)
        card.set_scale(scale)
        card.set_pos(offset)
        card.set_transparency(p3d.TransparencyAttrib.MAlpha)

        card.set_texture(sdftex)

        card.set_shader(_SHADER)
        card.set_shader_input('sdftex', sdftex)
        card.set_shader_input('ricolor', p3d.LVector4(0.8, 0.0, 0.0, 0.3))
        card.set_shader_input('outline_color',
                              p3d.LVector4(0.0, 0.0, 0.0, 0.8))

        self.graphics = card
예제 #4
0
        def conversion(task):
            while not self.conversion_queue.empty():
                data = self.conversion_queue.get()
                #print(data)
                if 'extras' in data and 'view' in data['extras']:
                    viewd = data['extras']['view']
                    if 'width' in viewd:
                        width = viewd['width']
                        height = viewd['height']
                        self.make_offscreen(width, height)
                    if 'projection_matrix' in viewd:
                        proj_mat = self.converter.load_matrix(
                            viewd['projection_matrix'])
                        self.view_lens.set_user_mat(proj_mat)
                    if 'view_matrix' in viewd:
                        view_mat = self.converter.load_matrix(
                            viewd['view_matrix'])

                        # Panda wants an OpenGL model matrix instead of an OpenGL view matrix
                        view_mat.invert_in_place()
                        self.view_lens.set_view_mat(view_mat)

                self.converter.update(data)
                bg_color = self.converter.background_color
                self.bg_color = p3d.LVector4(bg_color[0], bg_color[1],
                                             bg_color[2], 1)
                self.view_region.set_clear_color(self.bg_color)
                self.converter.active_scene.reparent_to(self.render)
                #self.render.ls()

            if self.texture.has_ram_image():
                #start = time.perf_counter()
                self.server.image_lock.acquire()
                self.image_width = self.texture.get_x_size()
                self.image_height = self.texture.get_y_size()
                self.image_data = memoryview(
                    self.texture.get_ram_image_as("BGR"))
                self.server.image_lock.release()
                #print('Extern: Updated image data in {}ms'.format((time.perf_counter() - start) * 1000))
                #self.texture.write('tex.png')
            return task.cont
예제 #5
0
    def reset(self, workingdir):
        self.bg = p3d.LVector4(0.0, 0.0, 0.0, 1.0)

        p3d.get_model_path().clear()
        pman_conf = None

        if workingdir:
            p3d.get_model_path().prepend_directory(workingdir)
            try:
                pman_conf = pman.get_config(workingdir)
            except pman.NoConfigError:
                pass

        if self.converter is not None:

            self.converter.active_scene.remove_node()
        self.converter = converter.Converter()

        if self.render is not None:
            self.render.remove_node
        self.render = p3d.NodePath('render')

        self.render_manager = rendermanager.create_render_manager(self, pman_conf)
예제 #6
0
    def __init__(self):
        # Preliminary capabilities check.

        if not ape.base().win.getGsg().getSupportsBasicShaders():
            self.t = addTitle(
                "Shadow Demo: Video driver reports that shaders are not supported."
            )
            return
        if not ape.base().win.getGsg().getSupportsDepthTexture():
            self.t = addTitle(
                "Shadow Demo: Video driver reports that depth textures are not supported."
            )
            return

        self.inst_p = addInstructions(0.06,
                                      'P : stop/start the Panda Rotation')
        self.inst_w = addInstructions(0.12, 'W : stop/start the Walk Cycle')
        self.inst_t = addInstructions(0.18, 'T : stop/start the Teapot')
        self.inst_l = addInstructions(0.24,
                                      'L : move light source far or close')
        self.inst_v = addInstructions(0.30,
                                      'V : View the Depth-Texture results')
        self.inst_u = addInstructions(0.36,
                                      'U : toggle updating the shadow map')
        self.inst_x = addInstructions(
            0.42, 'Left/Right Arrow : switch camera angles')

        ape.base().setBackgroundColor(0, 0, 0.2, 1)

        ape.base().camLens.setNearFar(1.0, 10000)
        ape.base().camLens.setFov(75)
        ape.base().disableMouse()

        # Load the scene.
        floorTex = ape.loader().loadTexture('maps/envir-ground.jpg')

        cm = p3dc.CardMaker('')
        cm.setFrame(-2, 2, -2, 2)
        floor = ape.render().attachNewNode(p3dc.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()

        self.pandaAxis = ape.render().attachNewNode('panda axis')
        self.pandaModel = Actor('panda-model', {'walk': 'panda-walk4'})
        self.pandaModel.reparentTo(self.pandaAxis)
        self.pandaModel.setPos(9, 0, 0)
        self.pandaModel.setScale(0.01)
        self.pandaWalk = self.pandaModel.actorInterval('walk', playRate=1.8)
        self.pandaWalk.loop()
        self.pandaMovement = self.pandaAxis.hprInterval(
            20.0, p3dc.LPoint3(-360, 0, 0), startHpr=p3dc.LPoint3(0, 0, 0))
        self.pandaMovement.loop()

        self.teapot = ape.loader().loadModel('teapot')
        self.teapot.reparentTo(ape.render())
        self.teapot.setPos(0, -20, 10)
        self.teapotMovement = self.teapot.hprInterval(
            50, p3dc.LPoint3(0, 360, 360))
        self.teapotMovement.loop()

        self.accept('escape', sys.exit)

        self.accept("arrow_left", self.incrementCameraPosition, [-1])
        self.accept("arrow_right", self.incrementCameraPosition, [1])
        self.accept("p", self.toggleInterval, [self.pandaMovement])
        self.accept("t", self.toggleInterval, [self.teapotMovement])
        self.accept("w", self.toggleInterval, [self.pandaWalk])
        self.accept("v", ape.base().bufferViewer.toggleEnable)
        self.accept("u", self.toggleUpdateShadowMap)
        self.accept("l", self.incrementLightPosition, [1])
        self.accept("o", ape.base().oobe)

        self.light = ape.render().attachNewNode(p3dc.Spotlight("Spot"))
        self.light.node().setScene(ape.render())
        self.light.node().setShadowCaster(True)
        self.light.node().showFrustum()
        self.light.node().getLens().setFov(40)
        self.light.node().getLens().setNearFar(10, 100)
        ape.render().setLight(self.light)

        self.alight = ape.render().attachNewNode(p3dc.AmbientLight("Ambient"))
        self.alight.node().setColor(p3dc.LVector4(0.2, 0.2, 0.2, 1))
        ape.render().setLight(self.alight)

        # Important! Enable the shader generator.
        ape.render().setShaderAuto()

        # default values
        self.cameraSelection = 0
        self.lightSelection = 0

        self.incrementCameraPosition(0)
        self.incrementLightPosition(0)
예제 #7
0
    def setupLights(
        self
    ):  # This function sets up some default lighting with simple shadows
        #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))
        #render().setLight(render().attachNewNode(directionalLight))
        #render().setLight(render().attachNewNode(ambientLight))

        # Shadows
        self.light = p3dc.Spotlight("Spot")
        self.light_node = render().attachNewNode(
            self.light)  # generates instance of p3dc.NodePath
        self.light.setScene(render())
        self.light.setShadowCaster(True, 1024 * 4,
                                   1024 * 4)  #.setShadowCaster(True)
        #self.light.showFrustum()
        # This exponent value sets how soft the edge of the spotlight is.
        # 0 means a hard edge. 128 means a very soft edge.
        #self.light.setExponent(60.0)
        # 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.light.setAttenuation(p3dc.LVector3(0.3, 0.0, 0.0))
        # The cone of a spotlight is controlled by it's lens. This creates the lens
        self.light.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.light.getLens().setFov(40, 40)
        #self.light.getLens().setFov(40)
        self.light.getLens().setNearFar(0.5, 500)
        self.light.setColor((0.6, 0.6, 0.8, 1))
        ############################################################
        ####\/########\/########\/########\/########\/########\/####
        #self.light_node.setPosHpr(0, -10, 15, 0, -50, 0) #This does not light the tiles... (similar to https://discourse.panda3d.org/t/shadows-with-directional-light-source-strange-behaviour/10025 )
        #self.light_node.setPosHpr(0, 10, 15, 180, -50, 0) #This works as intended
        #self.light_node.setPosHpr(0, 0, 8, 0, -80, 0) #This lights half the tiles but I don't know why this works to a degree but the first one doesn't at all
        self.light_node.setPosHpr(0, 8, 12, 180, -50, 0)  #This is now used
        ####/\########/\########/\########/\########/\########/\####
        ############################################################
        render().setLight(self.light_node)
        #
        #
        #
        #self.light2 = p3dc.Spotlight("Spot")
        #self.light2_node = render().attachNewNode(self.light2) # generates instance of p3dc.NodePath
        #self.light2.setScene(render())
        #self.light2.setShadowCaster(True, 1024, 1024)#.setShadowCaster(True)
        ##self.light.showFrustum()
        ## This exponent value sets how soft the edge of the spotlight is.
        ## 0 means a hard edge. 128 means a very soft edge.
        #self.light2.setExponent(60.0)
        ## 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.light2.setAttenuation(p3dc.LVector3(0.3, 0.0, 0.0))
        ## The cone of a spotlight is controlled by it's lens. This creates the lens
        #self.light2.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.light2.getLens().setFov(40, 40)
        ##self.light2.getLens().setFov(40)
        #self.light2.getLens().setNearFar(0.5, 50)
        #self.light2.setColor((0.6, 0.6, 0.8, 1))
        #self.light2_node.setPosHpr(0, -10, 15, 0, -50, 0)
        #render().setLight(self.light2_node)

        self.alight = render().attachNewNode(p3dc.AmbientLight("Ambient"))
        self.alight.node().setColor(p3dc.LVector4(0.1, 0.1, 0.1, 1))
        render().setLight(self.alight)

        # Important! Enable the shader generator.
        render().setShaderAuto()