Exemple #1
0
    def prepare(self, curr_cond, stim_period=''):
        self.curr_cond = curr_cond if stim_period == '' else curr_cond[
            stim_period]
        self.period = stim_period

        if not self.curr_cond:
            self.isrunning = False
        self.background_color = self.curr_cond['background_color']

        # set background color
        self.set_background_color(*self.curr_cond['background_color'])

        # Set Ambient Light
        self.ambientLight.setColor(self.curr_cond['ambient_color'])

        # Set Directional Light
        self.lights = dict()
        self.lightsNP = dict()
        for idx, light_idx in enumerate(iterable(self.curr_cond['light_idx'])):
            self.lights[idx] = core.DirectionalLight('directionalLight_%d' %
                                                     idx)
            self.lightsNP[idx] = self.render.attachNewNode(self.lights[idx])
            self.render.setLight(self.lightsNP[idx])
            self.lights[idx].setColor(tuple(
                self.curr_cond['light_color'][idx]))
            self.lightsNP[idx].setHpr(*self.curr_cond['light_dir'][idx])

        # Set Object tasks
        self.objects = dict()
        for idx, obj in enumerate(iterable(self.curr_cond['obj_id'])):
            self.objects[idx] = Agent(self, self.get_cond('obj_', idx))

        if 'movie_name' in self.curr_cond:
            self.movie = True
            loader = Loader(self)
            file_name = self.get_clip_info(self.curr_cond, 'file_name')
            self.mov_texture = loader.loadTexture(self.movie_path +
                                                  file_name[0])
            cm = CardMaker("card")
            tx_scale = self.mov_texture.getTexScale()
            cm.setFrame(-1, 1, -tx_scale[1] / tx_scale[0],
                        tx_scale[1] / tx_scale[0])
            self.movie_node = NodePath(cm.generate())
            self.movie_node.setTexture(self.mov_texture, 1)
            self.movie_node.setPos(0, 100, 0)
            self.movie_node.setTexScale(TextureStage.getDefault(),
                                        self.mov_texture.getTexScale())
            self.movie_node.setScale(48)
            self.movie_node.reparentTo(self.render)

        if not self.isrunning:
            self.timer.start()
            self.isrunning = True
Exemple #2
0
    def __init__(self):
        ShowBase.__init__(self)
        props = WindowProperties()
        props.setTitle("The Rat Cave")
        self.win.requestProperties(props)
        self.win.setClearColor((0.5, 0.5, 0.9, 1.0))
        self.disableMouse()
        self.locked = False
        lens = PerspectiveLens()
        lens.set_fov(70)
        self.cam.node().setLens(lens)
        self.position = [30, 30, 30]
        self.velocity = [0, 0, 0]
        self.setFrameRateMeter(True)
        self.myFog = Fog("Fog Name")
        self.myFog.setColor(0.5, 0.5, 0.9)
        self.myFog.setExpDensity(0.005)
        render.setFog(self.myFog)
        #cool bg
        #b=OnscreenImage(parent=render2d, image="space.png")
        #base.cam.node().getDisplayRegion(0).setSort(20)

        render.setAntialias(AntialiasAttrib.MMultisample)

        debugNode = BulletDebugNode('Debug')
        debugNode.showWireframe(True)
        debugNode.showConstraints(True)
        debugNode.showBoundingBoxes(False)
        debugNode.showNormals(False)
        debugNP = render.attachNewNode(debugNode)
        debugNP.show()

        self.world = BulletWorld()
        self.world.setGravity(Vec3(0, 0, -80))
        #self.world.setDebugNode(debugNP.node())

        loader = Loader(self)
        myShader = Shader.load(Shader.SL_GLSL,
                               vertex="vertshader.vert",
                               fragment="fragshader.frag")
        floorMesh = BulletTriangleMesh()

        texs = [
            loader.loadTexture("flatstone.png"),
            loader.loadTexture("flatstone2.png"),
            loader.loadTexture("flatgrass.png"),
            loader.loadTexture("flatgrass2.png"),
            loader.loadTexture("flatrock.png"),
            loader.loadTexture("flatrock2.png"),
            loader.loadTexture("flatsnow.png"),
            loader.loadTexture("flatsand.png"),
            loader.loadTexture("flatsand2.png")
        ]
        hmap = generator.generate(200, 200, 50, 0.01, 5)
        groundTypes = [[] for x in range(9)]
        for thing in hmap:
            groundTypes[thing[3]].append(thing[0:3])
        for i in range(len(groundTypes)):
            if len(groundTypes[i]) == 0:
                continue
            format = GeomVertexFormat.get_v3n3t2()
            format = GeomVertexFormat.registerFormat(format)
            vdata = GeomVertexData('name', format, Geom.UHStatic)
            vdata.setNumRows(3)
            vertex = GeomVertexWriter(vdata, 'vertex')
            normal = GeomVertexWriter(vdata, 'normal')
            texcoord = GeomVertexWriter(vdata, 'texcoord')
            prim = GeomTriangles(Geom.UHStatic)
            for grid in groundTypes[i]:
                v0 = (grid[0][0], grid[0][2], grid[0][1])
                vertex.addData3(v0)
                if grid[1][2] < 0:
                    normal.addData3(grid[1][0], grid[1][2], grid[1][1])
                else:
                    normal.addData3(-grid[1][0], -grid[1][2], -grid[1][1])
                texcoord.addData2(grid[2][0], grid[2][1])
                v1 = (grid[0][3], grid[0][5], grid[0][4])
                vertex.addData3(v1)
                if grid[1][5] < 0:
                    normal.addData3(grid[1][3], grid[1][5], grid[1][4])
                else:
                    normal.addData3(-grid[1][3], -grid[1][5], -grid[1][4])
                texcoord.addData2(grid[2][2], grid[2][3])
                v2 = (grid[0][6], grid[0][8], grid[0][7])
                vertex.addData3(v2)
                if grid[1][8] < 0:
                    normal.addData3(grid[1][6], grid[1][8], grid[1][7])
                else:
                    normal.addData3(-grid[1][6], -grid[1][8], -grid[1][7])
                texcoord.addData2(grid[2][4], grid[2][5])
                floorMesh.addTriangle(v0, v1, v2)
                prim.add_next_vertices(3)
            geom = Geom(vdata)
            geom.addPrimitive(prim)
            node = GeomNode('gnode')
            node.addGeom(geom)
            nodePath = render.attachNewNode(node)
            nodePath.setTexture(texs[i])
            nodePath.setShader(myShader)
        vdata2 = GeomVertexData('wata', format, Geom.UHStatic)
        vdata2.setNumRows(3)
        prim2 = GeomTriangles(Geom.UHStatic)
        vertex2 = GeomVertexWriter(vdata2, 'vertex')
        normal2 = GeomVertexWriter(vdata2, 'normal')
        texcoord2 = GeomVertexWriter(vdata2, 'texcoord')
        vertex2.addData3((0, 0, 0))
        vertex2.addData3((200, 0, 0))
        vertex2.addData3((0, 200, 0))
        normal2.addData3((0, 0, 1))
        normal2.addData3((0, 0, 1))
        normal2.addData3((0, 0, 1))
        texcoord2.addData2((0, 0))
        texcoord2.addData2((1, 0))
        texcoord2.addData2((0, 1))
        prim2.addNextVertices(3)
        vertex2.addData3((200, 200, 0))
        vertex2.addData3((0, 200, 0))
        vertex2.addData3((200, 0, 0))
        normal2.addData3((0, 0, 1))
        normal2.addData3((0, 0, 1))
        normal2.addData3((0, 0, 1))
        texcoord2.addData2((1, 1))
        texcoord2.addData2((0, 1))
        texcoord2.addData2((1, 0))
        prim2.addNextVertices(3)
        water = Geom(vdata2)
        water.addPrimitive(prim2)
        waterNode = GeomNode('water')
        waterNode.addGeom(water)
        waterNodePath = render.attachNewNode(waterNode)
        waterNodePath.setTransparency(True)
        waterNodePath.setTexture(loader.loadTexture("water.png"))
        floorMeshShape = BulletTriangleMeshShape(floorMesh, dynamic=False)
        fNode = BulletRigidBodyNode('floor')
        fNode.addShape(floorMeshShape)
        self.floorPhysNode = render.attachNewNode(fNode)
        self.world.attachRigidBody(fNode)
        for i in range(25):
            rat = loader.loadModel("deer.obj")
            rat.setScale((0.003, 0.003, 0.003))
            rat.setHpr((0, 90, 0))
            rat.setPos((0, 0, -0.8))
            rat.setTexture(texs[5])

            shape = BulletSphereShape(1)
            node = BulletRigidBodyNode('ratBox')
            #node.setAngularFactor((0,0,1))
            node.setMass(10.0)
            node.addShape(shape)
            node.setActive(False)
            #node.friction = 1
            np = render.attachNewNode(node)
            np.setPos((i % 5) * 2 + 40, int(i / 5) * 2 + 40, 50)
            self.world.attachRigidBody(node)
            rat.flattenLight()
            rat.reparentTo(np)
            #posInterval1 = rat.hprInterval(0.1,
            #							 Point3(10, 90, 0),
            #							 startHpr=Point3(-10, 90, 0))
            #posInterval2 = rat.hprInterval(0.1,
            #							 Point3(-10, 90, 0),
            #							 startHpr=Point3(10,90,0))
            #pandaPace = Sequence(posInterval1, posInterval2,
            #						  name="pandaPace" + str(i))
            #pandaPace.loop()
            self.ratto = np
        self.deer = loader.loadModel("rat.obj")
        self.deer.setScale((0.15, 0.15, 0.15))
        self.deer.setHpr((0, 90, 0))
        self.deer.setPos((0, 0, -2))
        self.deerShape = BulletBoxShape((3, 1, 3))
        self.deerNode = BulletRigidBodyNode('deerBox')
        self.deerNode.setAngularFactor((0, 0, 1))
        self.deerNode.setMass(10.0)
        self.deerNode.setFriction(1)
        self.deerNode.addShape(self.deerShape)
        self.deerNodePath = render.attachNewNode(self.deerNode)
        self.deerNodePath.setPos((30, 30, 130))
        self.world.attachRigidBody(self.deerNode)
        self.deer.reparentTo(self.deerNodePath)

        self.keyMap = {
            "w": False,
            "s": False,
            "a": False,
            "d": False,
            "space": False,
            "lshift": False,
            "p": False,
            "o": False
        }
        self.accept("w", self.setKey, ["w", True])
        self.accept("s", self.setKey, ["s", True])
        self.accept("a", self.setKey, ["a", True])
        self.accept("d", self.setKey, ["d", True])
        self.accept("space", self.setKey, ["space", True])
        self.accept("lshift", self.setKey, ["lshift", True])
        self.accept("e", self.lockMouse)
        self.accept("p", self.setKey, ["p", True])
        self.accept("o", self.setKey, ["o", True])

        self.accept("w-up", self.setKey, ["w", False])
        self.accept("s-up", self.setKey, ["s", False])
        self.accept("a-up", self.setKey, ["a", False])
        self.accept("d-up", self.setKey, ["d", False])
        self.accept("space-up", self.setKey, ["space", False])
        self.accept("lshift-up", self.setKey, ["lshift", False])
        self.accept("e-up", self.setKey, ["e", False])
        self.accept('escape', sys.exit)
        self.accept("p-up", self.setKey, ["p", False])
        self.accept("o-up", self.setKey, ["o", False])

        # Add the spinCameraTask procedure to the task manager.
        self.taskMgr.add(self.cameraControl, "CameraControl")

        self.camera.setPos(tuple(self.position))
Exemple #3
0
    def _get_node_plate(cls,
                        loader: Loader,
                        build_plate_size: LVector2d,
                        name: str = "") -> NodePath:
        """Generate the textured build plate NodePath.

        This NodePath's only purpose is to display the app's logo.

        Parameters
        ----------
        loader : Loader
            Panda3D asset loader.
        build_plate_size : LVector2d
            Builder plate size.
        name : str
            Name for the generated NodePath.
        """
        # Geom data
        geom_data = cls._get_geom_data_plate(build_plate_size)

        # Primitive
        primitive = GeomTristrips(Geom.UHStatic)
        primitive.addVertices(0, 1, 3, 2)
        primitive.closePrimitive()

        # Geom, GeomNode
        geom = Geom(geom_data)
        geom.addPrimitive(primitive)
        geom_node = GeomNode("")
        geom_node.addGeom(geom)

        # NodePath
        node_path = NodePath(top_node_name=name)
        node_path.attachNewNode(geom_node)

        # Texture
        tex = loader.loadTexture(cls._PATH_TEXTURE)
        ts = TextureStage("ts")
        node_path.setTexture(ts, tex)
        tex.setBorderColor((0, 0, 0, 0))
        tex.setWrapU(Texture.WMBorderColor)
        tex.setWrapV(Texture.WMBorderColor)

        node_path.setTransparency(TransparencyAttrib.MAlpha)
        node_path.setAlphaScale(cls._TEXTURE_OPACITY)

        texture_scale = cls._TEXTURE_SCALE

        width, height = build_plate_size
        ratio = width / height
        if ratio >= 1:  # Landscape or square
            scale_v = 1 / texture_scale
            scale_u = scale_v * ratio
        else:  # Portrait
            scale_u = 1 / texture_scale
            scale_v = scale_u / ratio

        node_path.setTexScale(ts, scale_u, scale_v)
        node_path.setTexOffset(ts, -0.5 * (scale_u - 1), -0.5 * (scale_v - 1))

        return node_path