예제 #1
0
class Application(ShowBase, object):
    '''
	The default Application class which holds the code for
	Panda3D to run the game
	'''
    def __init__(self, quality):
        # Set the model quality, (super-low, low or high)
        self.quality = quality
        print("[>] PoultryGeist:\t      Setting Model Resolution to {}".format(
            self.quality.upper()))

        load_prc_file_data("", "win-size 1920 1080")

        # Run the standard Showbase init if running in super-low resolution mode
        # Do some stuff if the game is running at normal or high resolution
        if self.quality != 'super-low':
            # Construct and create the pipeline
            self.render_pipeline = RenderPipeline()
            self.render_pipeline.pre_showbase_init()
            super(Application, self).__init__()
            self.render_pipeline.create(self)
            # Enable anti-aliasing for the game
            render.setAntialias(AntialiasAttrib.MAuto)
            # Set the time pipeline lighting simulation time
            self.render_pipeline.daytime_mgr.time = "20:15"

        else:
            super(Application, self).__init__()
            # Enable the filter handler
            self.filters = CommonFilters(base.win, base.cam)
            self.filters.setAmbientOcclusion()

        # Enable particles and physics
        self.enableParticles()

        #Modify the Panda3D config on-the-fly
        #In this case, edit the window title
        load_prc_file_data(
            "", """window-title PoultryGeist
								  threading-model /Draw
								  multisamples 2
								  framebuffer-multisample 1
							   """)

        # Set the window size
        self.width, self.height = (800, 600)

        # Initialise the movement controller
        self.controller = None

        # Turn off normal mouse controls
        self.disableMouse()

        # Hide the cursor
        self.props = WindowProperties()
        #
        self.props.setCursorHidden(True)
        # Lower the FOV to make the game more difficult
        self.win.requestProperties(self.props)
        self.camLens.setFov(60)
        # Reduces the distance of which the camera can render objects close to it
        self.camLens.setNear(0.1)
        # Store and empty renderTree for later use
        self.emptyRenderTree = deepcopy(self.render)

        # Register the buttons for movement
        # TODO remove this and overhaul the button handling
        self.w_button = KeyboardButton.ascii_key('w'.encode())
        self.s_button = KeyboardButton.ascii_key('s'.encode())
        self.l_button = KeyboardButton.ascii_key('l'.encode())

        self.switch_button = KeyboardButton.ascii_key('p'.encode())

        # Initialise the SceneManager
        self.sceneMgr = SceneManager(self)

        # Initialise the collision traverser
        self.collisionTraverser = CollisionTraverser('main_traverser')
        base.cTrav = self.collisionTraverser
        base.cTrav.showCollisions(self.render)

        # Add the sceneMgr events to run as a task
        taskMgr.add(self.sceneMgr.runSceneTasks, "scene-tasks")

    def loadSettings(self, options):
        '''
		Iterate a dictionary of settings and apply them to the game
		'''
        # Toggle the audio based on the options
        if options.get('audio', 'on') == 'off':
            base.disableAllAudio()
        windowProperties = WindowProperties()
        # Set the resolution
        if options.get('resolution') == '720p':
            windowProperties.setSize(1280, 720)
            # Set the app width and height variables
            self.width, self.height = (1280, 720)
        else:
            windowProperties.setSize(1920, 1080)
            # Set the app width and height variables
            self.width, self.height = (1920, 1080)
        # Apply the properties to the window
        base.win.requestProperties(windowProperties)
        load_prc_file_data('', 'win-size {} {}'.format(self.width,
                                                       self.height))

    def move(self, forward, dir, elapsed):
        '''
		Move the camera forward or backwards
		For testing ONLY at the moment
		'''
        if forward:
            self.sceneMgr.focus += dir * elapsed * 30
        else:
            self.sceneMgr.focus -= dir * elapsed * 30
        # Set the position of the camera based on the direction
        self.camera.setPos(self.sceneMgr.focus - (dir * 5))

    def dumpTree(self):
        for a in self.render.getChildren():
            print(a)
예제 #2
0
"""

This is an alternative possibility of initializing the RenderPipeline, which enables
you to construct the show base object yourself.

"""

import sys
from direct.showbase.ShowBase import ShowBase

# Insert the pipeline path to the system path, this is required to be
# able to import the pipeline classes. In case you placed the render
# pipeline in a subfolder of your project, you have to adjust this.
sys.path.insert(0, "../../RenderPipeline")
sys.path.insert(0, "../../")

# Import render pipeline classes
from rpcore import RenderPipeline, SpotLight

# Construct and create the pipeline
render_pipeline = RenderPipeline()
render_pipeline.pre_showbase_init()
base = ShowBase()
render_pipeline.create(base)

base.run()
예제 #3
0
class PandaVis(ShowBase):
    """Base class for all visualizations with panda3d"""
    def __init__(self, rendering: bool):
        """
        Constructor

        :param rendering: boolean indicating whether to use RenderPipeline or default Panda3d as visualization-module.
        """
        super().__init__(self)
        self.dir = Filename.fromOsSpecific(
            pyrado.PANDA_ASSETS_DIR).getFullpath()

        # Initialize RenderPipeline
        if rendering:
            sys.path.insert(0, pyrado.RENDER_PIPELINE_DIR)
            from rpcore import RenderPipeline

            self.render_pipeline = RenderPipeline()
            self.render_pipeline.pre_showbase_init()
            self.render_pipeline.set_loading_screen_image(
                osp.join(self.dir, "logo.png"))
            self.render_pipeline.settings["pipeline.display_debugger"] = False
            self.render_pipeline.create(self)
            self.render_pipeline.daytime_mgr.time = "17:00"
        else:
            self.render_pipeline = None

        # Activate antialiasing
        self.render.setAntialias(AntialiasAttrib.MAuto)

        # Set window properties
        self.windowProperties = WindowProperties()
        self.windowProperties.setForeground(True)
        self.windowProperties.setTitle("Experiment")

        # Set background color
        self.setBackgroundColor(1, 1, 1)

        # Configuration of the lighting
        self.directionalLight1 = DirectionalLight("directionalLight")
        self.directionalLightNP1 = self.render.attachNewNode(
            self.directionalLight1)
        self.directionalLightNP1.setHpr(0, -8, 0)
        self.render.setLight(self.directionalLightNP1)

        self.directionalLight2 = DirectionalLight("directionalLight")
        self.directionalLightNP2 = self.render.attachNewNode(
            self.directionalLight2)
        self.directionalLightNP2.setHpr(180, -20, 0)
        self.render.setLight(self.directionalLightNP2)

        self.ambientLight = AmbientLight("ambientLight")
        self.ambientLightNP = self.render.attachNewNode(self.ambientLight)
        self.ambientLight.setColor((0.1, 0.1, 0.1, 1))
        self.render.setLight(self.ambientLightNP)

        # Create a text node displaying the physic parameters on the top left of the screen
        self.text = TextNode("parameters")
        self.textNodePath = aspect2d.attachNewNode(self.text)
        self.text.setTextColor(0, 0, 0, 1)  # black
        self.textNodePath.setScale(0.07)
        self.textNodePath.setPos(-1.9, 0, 0.9)

        # Configure trace
        self.trace = LineSegs()
        self.trace.setThickness(3)
        self.trace.setColor(0.8, 0.8, 0.8)  # light grey
        self.lines = self.render.attachNewNode("Lines")
        self.last_pos = None

        # Adds one instance of the update function to the task-manager, thus initializes the animation
        self.taskMgr.add(self.update, "update")

    def update(self, task: Task):
        """
        Updates the visualization with every call.

        :param task: Needed by panda3d task manager.
        :return Task.cont: indicates that task should be called again next frame.
        """
        return Task.cont

    def reset(self):
        """
        Resets the the visualization to a certain state, so that in can be run again. Removes the trace.
        """
        self.lines.getChildren().detach()
        self.last_pos = None

    def draw_trace(self, point):
        """
        Draws a line from the last point to the current point

        :param point: Current position of pen. Needs 3 value vector.
        """
        # Check if trace initialized
        if self.last_pos:
            # Set starting point of new line
            self.trace.moveTo(self.last_pos)

        # Draw line to that point
        self.trace.drawTo(point)

        # Save last position of pen
        self.last_pos = point

        # Show drawing
        self.trace_np = NodePath(self.trace.create())
        self.trace_np.reparentTo(self.lines)
"""

This is an alternative possibility of initializing the RenderPipeline, which
uses the (deprecated!) DirectStart interface. This should not be used anymore,
except for fast prototyping.

"""

import sys

# Insert the pipeline path to the system path, this is required to be
# able to import the pipeline classes. In case you placed the render
# pipeline in a subfolder of your project, you have to adjust this.
sys.path.insert(0, "../../")
sys.path.insert(0, "../../RenderPipeline")

# Import render pipeline classes
from rpcore import RenderPipeline

# Construct and create the pipeline
render_pipeline = RenderPipeline()
render_pipeline.pre_showbase_init()

# Import (deprecated!) DirectStart interface
import direct.directbase.DirectStart
render_pipeline.create(base)

base.run()