Ejemplo n.º 1
0
def make_plane(size=(1.0, 1.0)):
    """Make a plane geometry.

    Arguments:
        size {tuple} -- plane size x,y

    Returns:
        Geom -- p3d geometry
    """
    vformat = GeomVertexFormat.get_v3n3t2()
    vdata = GeomVertexData('vdata', vformat, Geom.UHStatic)
    vdata.uncleanSetNumRows(4)

    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    tcoord = GeomVertexWriter(vdata, 'texcoord')

    quad = ((0, 0), (1, 0), (0, 1), (1, 1))

    for u, v in quad:
        vertex.addData3((u - 0.5) * size[0], (v - 0.5) * size[1], 0)
        normal.addData3(0, 0, 1)
        tcoord.addData2(u, v)

    prim = GeomTriangles(Geom.UHStatic)
    prim.addVertices(0, 1, 2)
    prim.addVertices(2, 1, 3)

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    return geom
Ejemplo n.º 2
0
def make_box():
    """Make a uniform box geometry.

    Returns:
        Geom -- p3d geometry
    """
    vformat = GeomVertexFormat.get_v3n3t2()
    vdata = GeomVertexData('vdata', vformat, Geom.UHStatic)
    vdata.uncleanSetNumRows(24)

    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    tcoord = GeomVertexWriter(vdata, 'texcoord')

    axes = itertools.permutations(np.eye(3), r=2)
    quad = ((0, 0), (1, 0), (0, 1), (1, 1))

    for x, y in axes:
        z = np.cross(x, y)
        for u, v in quad:
            vertex.addData3(*(x * (u - 0.5) + y * (v - 0.5) + z * 0.5))
            normal.addData3(*z)
            tcoord.addData2(u, v)

    prim = GeomTriangles(Geom.UHStatic)
    for i in range(0, 24, 4):
        prim.addVertices(i + 0, i + 1, i + 2)
        prim.addVertices(i + 2, i + 1, i + 3)

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    return geom
Ejemplo n.º 3
0
def make_cylinder(num_segments=16, closed=True):
    """Make a uniform cylinder geometry.

    Keyword Arguments:
        num_segments {int} -- segments number (default: {16})
        closed {bool} -- add caps (default: {True})

    Returns:
        Geom -- p3d geometry
    """
    vformat = GeomVertexFormat.get_v3n3t2()
    vdata = GeomVertexData('vdata', vformat, Geom.UHStatic)

    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    tcoord = GeomVertexWriter(vdata, 'texcoord')

    cyl_rows = num_segments * 2
    cap_rows = num_segments + 1
    if closed:
        vdata.uncleanSetNumRows(cyl_rows + 2 * cap_rows)
    else:
        vdata.uncleanSetNumRows(cyl_rows)

    for phi in np.linspace(0, 2 * np.pi, num_segments):
        x, y = np.cos(phi), np.sin(phi)
        for z in (-1, 1):
            vertex.addData3(x, y, z * 0.5)
            normal.addData3(x, y, 0)
            tcoord.addData2(phi / (2 * np.pi), (z + 1) / 2)

    prim = GeomTriangles(Geom.UHStatic)
    for i in range(num_segments - 1):
        prim.addVertices(i * 2, i * 2 + 3, i * 2 + 1)
        prim.addVertices(i * 2, i * 2 + 2, i * 2 + 3)

    if closed:
        for z in (-1, 1):
            vertex.addData3(0, 0, z * 0.5)
            normal.addData3(0, 0, z)
            tcoord.addData2(0, 0)

            for phi in np.linspace(0, 2 * np.pi, num_segments):
                x, y = np.cos(phi), np.sin(phi)
                vertex.addData3(x, y, z * 0.5)
                normal.addData3(0, 0, z)
                tcoord.addData2(x, y)

        for i in range(num_segments):
            r0 = cyl_rows
            r1 = r0 + cap_rows
            prim.addVertices(r0, r0 + i + 1, r0 + i)
            prim.addVertices(r1, r1 + i, r1 + i + 1)

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    return geom
Ejemplo n.º 4
0
def make_capsule(radius, length, num_segments=16, num_rings=16):
    """Make capsule geometry.

    Arguments:
        radius {float} -- capsule radius
        length {float} -- capsule length

    Keyword Arguments:
        num_segments {int} -- segments number (default: {16})
        num_rings {int} -- rings number (default: {16})

    Returns:
        Geom -- p3d geometry
    """
    vformat = GeomVertexFormat.get_v3n3t2()
    vdata = GeomVertexData('vdata', vformat, Geom.UHStatic)
    vdata.uncleanSetNumRows(num_segments * num_rings)

    vertex = GeomVertexWriter(vdata, 'vertex')
    normal = GeomVertexWriter(vdata, 'normal')
    tcoord = GeomVertexWriter(vdata, 'texcoord')

    for u in np.linspace(0, np.pi, num_rings):
        for v in np.linspace(0, 2 * np.pi, num_segments):
            x, y, z = np.cos(v) * np.sin(u), np.sin(v) * np.sin(u), np.cos(u)
            offset = np.sign(z) * 0.5 * length
            vertex.addData3(x * radius, y * radius, z * radius + offset)
            normal.addData3(x, y, z)
            tcoord.addData2(u / np.pi, v / (2 * np.pi))

    prim = GeomTriangles(Geom.UHStatic)
    for i in range(num_rings - 1):
        for j in range(num_segments - 1):
            r0 = i * num_segments + j
            r1 = r0 + num_segments
            if i < num_rings - 2:
                prim.addVertices(r0, r1, r1 + 1)
            if i > 0:
                prim.addVertices(r0, r1 + 1, r0 + 1)

    geom = Geom(vdata)
    geom.addPrimitive(prim)
    return geom
Ejemplo n.º 5
0
from tsim.core.geometry import Vector
from tsim.core.network.intersection import ConflictPointType
from tsim.core.network.lane import LANE_WIDTH
from tsim.core.network.node import Node
from tsim.ui import textures
from tsim.ui.objects.way import LEVEL_HEIGHT
from tsim.ui.textures import create_texture

CARD_MAKER = CardMaker('lane_connections_card_maker')
CARD_MAKER.set_frame((-16, 16, -16, 16))
COLORS = ('crimson', 'orange', 'gold', 'limegreen', 'turquoise', 'deepskyblue',
          'blueviolet', 'hotpink')
RESOLUTION = 2048
MIDDLE = Vector(RESOLUTION // 2, -RESOLUTION // 2)
PPM = RESOLUTION // 32
VERTEX_FORMAT = GeomVertexFormat.get_v3n3t2()
FONT = aggdraw.Font('black',
                    resource_filename('tsim', 'data/fonts/caladea-tsim.otf'),
                    15)


def create(parent: NodePath, node: Node) -> NodePath:
    """Create node for given node and attach it to the parent."""
    geom = _generate_mesh(node)
    node_ = GeomNode(str(node.id))
    node_.add_geom(geom)
    node_.adjust_draw_mask(0x00000000, 0x00010000, 0xfffeffff)
    node_path = parent.attach_new_node(node_)
    node_path.set_texture(textures.get('intersection'))
    return node_path
Ejemplo n.º 6
0
    def setup(self):
        self.worldNP = render.attach_new_node('World')

        # World
        self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
        self.debugNP.show()

        self.world = BulletWorld()
        self.world.set_gravity(LVector3(0, 0, -9.81))
        self.world.set_debug_node(self.debugNP.node())

        # Ground
        p0 = LPoint3(-20, -20, 0)
        p1 = LPoint3(-20, 20, 0)
        p2 = LPoint3(20, -20, 0)
        p3 = LPoint3(20, 20, 0)
        mesh = BulletTriangleMesh()
        mesh.add_triangle(p0, p1, p2)
        mesh.add_triangle(p1, p2, p3)
        shape = BulletTriangleMeshShape(mesh, dynamic=False)

        np = self.worldNP.attach_new_node(BulletRigidBodyNode('Mesh'))
        np.node().add_shape(shape)
        np.set_pos(0, 0, -2)
        np.set_collide_mask(BitMask32.all_on())

        self.world.attach(np.node())

        # Soft body world information
        info = self.world.get_world_info()
        info.set_air_density(1.2)
        info.set_water_density(0)
        info.set_water_offset(0)
        info.set_water_normal(LVector3(0, 0, 0))

        # Softbody
        for i in range(50):
            p00 = LPoint3(-2, -2, 0)
            p10 = LPoint3(2, -2, 0)
            p01 = LPoint3(-2, 2, 0)
            p11 = LPoint3(2, 2, 0)
            node = BulletSoftBodyNode.make_patch(info, p00, p10, p01, p11, 6,
                                                 6, 0, True)
            node.generate_bending_constraints(2)
            node.get_cfg().set_lift_coefficient(0.004)
            node.get_cfg().set_dynamic_friction_coefficient(0.0003)
            node.get_cfg().set_aero_model(
                BulletSoftBodyConfig.AM_vertex_two_sided)
            node.set_total_mass(0.1)
            node.add_force(LVector3(0, 2, 0), 0)

            np = self.worldNP.attach_new_node(node)
            np.set_pos(self.LVector3_rand() * 10 + LVector3(0, 0, 20))
            np.set_hpr(self.LVector3_rand() * 16)
            self.world.attach(node)

            fmt = GeomVertexFormat.get_v3n3t2()
            geom = BulletHelper.make_geom_from_faces(node, fmt, True)
            node.link_geom(geom)
            nodeV = GeomNode('')
            nodeV.add_geom(geom)
            npV = np.attach_new_node(nodeV)

            tex = loader.load_texture('models/panda.jpg')
            npV.set_texture(tex)
            BulletHelper.make_texcoords_for_patch(geom, 6, 6)
Ejemplo n.º 7
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))