コード例 #1
0
    def __init__(self,
                 mapfile,
                 xpos,
                 zpos,
                 emap,
                 width=10.0,
                 depth=10.0,
                 height=10.0,
                 name="building",
                 draw_details=None,
                 yoff=0.0,
                 scheme=None):
        """
    Creates a building at the given location. Each pixel of the image is one cell of the building
    If the cell is white then the cell is open, if it is black then it is wall. If it is grey
    then it is open and has no ceiling.
    The building is centred at xpos, zpos (which gets renamed herin to x,y to match the image coords)
    Each cell is width on the x axis and depth on the z axis, and the walls are height high on the y axis.

    The function returns a merged shape with the entire building in it.
    """
        self.xpos = xpos
        self.zpos = zpos
        self.width = width
        self.depth = depth
        self.height = height
        self.name = name
        self.ceilingthickness = 1.0
        self.walls = []

        if scheme == None:
            self.scheme = Building.baseScheme
        else:
            self.scheme = scheme

        # We don't have to be rigorous here, this should only be a draw_details or an iterable of draw_details.
        if hasattr(draw_details, "__getitem__") or hasattr(
                draw_details, "__iter__"):
            assert (len(draw_details) == self.scheme["#models"])
            self.details = draw_details
        else:
            self.details = [
                draw_details for x in range(self.scheme["#models"])
            ]
        # having a method like this allows draw details to be set later

        self.yoff = yoff

        self.model = [
            MergeShape(name=name + "." + str(x))
            for x in range(self.scheme["#models"])
        ]

        if mapfile[0] != '/':
            mapfile = sys.path[0] + '/' + mapfile
        print("Loading building map ...", mapfile)

        im = Image.open(mapfile)
        im = ImageOps.invert(im)
        ix, iy = im.size

        print("image size", ix, ",", iy)

        startx = xpos - ix / 2 * width
        starty = zpos - ix / 2 * depth

        yoff += emap.calcHeight(-xpos, -zpos)

        if not im.mode == "P":
            im = im.convert('P', palette=Image.ADAPTIVE)
        im = im.transpose(Image.FLIP_TOP_BOTTOM)
        im = im.transpose(Image.FLIP_LEFT_RIGHT)
        pixels = im.load()

        for y in range(1, iy - 1):
            print(".", end='')
            for x in range(1, ix - 1):
                colour = pixels[x, y]

                if x == 1:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty,
                                        (colour, pixels[x - 1, y], "edge"),
                                        wallfunc=self.west_wall,
                                        ceilingedgefunc=self.west_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)
                else:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty, (colour, pixels[x - 1, y]),
                                        wallfunc=self.west_wall,
                                        ceilingedgefunc=self.west_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)

                if x == ix - 2:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty,
                                        (colour, pixels[x + 1, y], "edge"),
                                        wallfunc=self.east_wall,
                                        ceilingedgefunc=self.east_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)
                else:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty, (colour, pixels[x + 1, y]),
                                        wallfunc=self.east_wall,
                                        ceilingedgefunc=self.east_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)

                if y == 1:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty,
                                        (colour, pixels[x, y - 1], "edge"),
                                        wallfunc=self.south_wall,
                                        ceilingedgefunc=self.south_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)
                else:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty, (colour, pixels[x, y - 1]),
                                        wallfunc=self.south_wall,
                                        ceilingedgefunc=self.south_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)

                if y == iy - 2:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty,
                                        (colour, pixels[x, y + 1], "edge"),
                                        wallfunc=self.north_wall,
                                        ceilingedgefunc=self.north_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)
                else:
                    self._executeScheme(x,
                                        y,
                                        startx,
                                        starty, (colour, pixels[x, y + 1]),
                                        wallfunc=self.north_wall,
                                        ceilingedgefunc=self.north_edge,
                                        ceilingfunc=self.ceiling,
                                        rooffunc=self.roof)

                self._executeScheme(x,
                                    y,
                                    startx,
                                    starty, (colour, None),
                                    wallfunc=None,
                                    ceilingedgefunc=None,
                                    ceilingfunc=self.ceiling,
                                    rooffunc=self.roof)

        self.set_draw_details(self.details)  # after models created otherwise
コード例 #2
0
ファイル: Amazing.py プロジェクト: akissu/pi3d
mymap = ElevationMap("textures/maze1.jpg",
                     width=mapwidth,
                     depth=mapdepth,
                     height=mapheight,
                     divx=128,
                     divy=128,
                     name="sub")
mymap.set_draw_details(shader, [rockimg1, rockimg2, shineimg], 128.0, 0.05)

# Create fog for more realistic fade in distance. This can be turned on and off between drawing different object (i.e backgound not foggy)
mymap.set_fog((0.1, 0.1, 0.1, 1.0), 200.0)

#Create tree models
treeplane = Plane(w=4.0, h=5.0)

treemodel1 = MergeShape(name="baretree")
treemodel1.add(treeplane.buf[0], 0, 0, 0)
treemodel1.add(treeplane.buf[0], 0, 0, 0, 0, 90, 0)

#Scatter them on map using Merge shape's cluster function
mytrees1 = MergeShape(name="trees1")
mytrees1.cluster(treemodel1.buf[0], mymap, 0.0, 0.0, 900.0, 900.0, 10, "", 8.0,
                 3.0)
mytrees1.buf[0].set_draw_details(shader, [tree2img, rockimg2], 4.0, 0.0)
mytrees1.set_fog((0.1, 0.1, 0.1, 1.0), 200.0)

raspberry = MergeShape(name="rasp")
raspberry.cluster(treemodel1.buf[0], mymap, -250, +250, 470.0, 470.0, 5, "",
                  8.0, 1.0)
raspberry.buf[0].set_draw_details(shader, [raspimg, raspimg], 1.0, 0.0)
raspberry.set_fog((0.1, 0.1, 0.1, 1.0), 200.0)
コード例 #3
0
mountimg1 = Texture("textures/mountains3_512.jpg")
mymap = ElevationMap("textures/mountainsHgt.jpg",
                     name="map",
                     width=mapwidth,
                     depth=mapdepth,
                     height=mapheight,
                     divx=32,
                     divy=32)  #testislands.jpg
mymap.buf[0].set_draw_details(shader, [mountimg1, bumpimg, reflimg], 128.0,
                              0.0)
mymap.set_fog(*FOG)

#Create tree models
treeplane = Plane(w=4.0, h=5.0)

treemodel1 = MergeShape(name="baretree")
treemodel1.add(treeplane.buf[0], 0, 0, 0)
treemodel1.add(treeplane.buf[0], 0, 0, 0, 0, 90, 0)

treemodel2 = MergeShape(name="bushytree")
treemodel2.add(treeplane.buf[0], 0, 0, 0)
treemodel2.add(treeplane.buf[0], 0, 0, 0, 0, 60, 0)
treemodel2.add(treeplane.buf[0], 0, 0, 0, 0, 120, 0)

#Scatter them on map using Merge shape's cluster function
mytrees1 = MergeShape(name="trees1")
mytrees1.cluster(treemodel1.buf[0], mymap, 0.0, 0.0, 120.0, 120.0, 30, "", 8.0,
                 3.0)
mytrees1.set_draw_details(flatsh, [tree2img], 0.0, 0.0)
mytrees1.set_fog(*TFOG)
コード例 #4
0
matsh = Shader("shaders/mat_reflect")
flatsh = Shader("shaders/uv_flat")

#Create textures
shapeimg = Texture("textures/straw1.jpg")
shapebump = Texture("textures/mudnormal.jpg")
waterbump = []
iFiles = glob.glob("textures/water/n_norm???.png")
iFiles.sort()  # order is vital to animation!
for f in iFiles:
    waterbump.append(Texture(f))
num_n = len(waterbump)
shapeshine = Texture("textures/stars.jpg")

#Create shape
myshape = MergeShape()
num = (2, 2)
asphere = Sphere(sides=32)
for i in range(num[0]):
    for j in range(num[1]):
        myshape.add(asphere, -num[0] * 0.9 + 1.8 * i, -num[1] * 0.9 + 1.8 * j,
                    0.0)

myshape.position(0.0, 0.0, 5)
myshape.set_draw_details(shader, [shapeimg, shapebump, shapeshine], 1.0, 0.1)
myshape.set_material((1.0, 0.5, 0.2, 0.5))

mywater = Plane(w=130.0, h=130.0)
mywater.set_draw_details(matsh, [waterbump[0], shapeshine], 12.0, 0.6)
mywater.set_material((0.0, 0.05, 0.1))
mywater.set_fog((0.4, 0.6, 0.8, 0.0), 150)
コード例 #5
0
ortho_cam = Camera(is_3d=False)  # 2d orthographic view camera

#setup textures, light position and initial model position
Light((0, 5, 0))
#create shaders
shader = Shader("shaders/uv_reflect")
flatsh = Shader("shaders/uv_flat")
defocus = Defocus()

#Create textures
shapeimg = Texture("textures/straw1.jpg")
shapebump = Texture("textures/floor_nm.jpg", True)
shapeshine = Texture("textures/pong3.png")

#Create shape
myshape = MergeShape(camera=persp_cam)  #specify perspective view
asphere = Sphere(sides=16, slices=16)
myshape.radialCopy(asphere, step=72)
myshape.position(0.0, 0.0, 5.0)
myshape.set_draw_details(shader, [shapeimg, shapebump, shapeshine], 8.0, 0.1)

mysprite = Sprite(w=10.0, h=10.0, camera=persp_cam)
mysprite.position(0.0, 0.0, 15.0)
mysprite.set_draw_details(flatsh, [shapebump])

tick = 0
next_time = time.time() + 2.0

#load ttf font and set the font colour to 'raspberry'
arialFont = Ttffont("fonts/FreeMonoBoldOblique.ttf", "#dd00aa")
mystring = String(font=arialFont,
コード例 #6
0
mymap = ElevationMap(mapfile="textures/mars_height.png",
                     width=mapwidth,
                     depth=mapdepth,
                     height=mapheight,
                     divx=128,
                     divy=128)
mymap.set_draw_details(shader, [mountimg1, bumpimg], 128.0, 0.0)
mymap.set_fog((0.3, 0.15, 0.1, 0.1), 300.0)

#create robot
metalimg = Texture("textures/metalhull.jpg")
robot_head = Sphere(radius=1.0)
robot_body = Cylinder(radius=1.0, height=2.0, sides=12)
robot_leg = Cuboid(w=0.35, h=2.0)

robot = MergeShape()
robot.add(robot_head.buf[0], 0.0, 1.6)
robot.add(robot_body.buf[0], 0.0, 0.5)
robot.add(robot_leg.buf[0], -1.04, 0, 0)
robot.add(robot_leg.buf[0], 1.05, 0, 0)
robot.set_draw_details(shader, [metalimg, metalimg, reflcn], 0.0, 0.5)

#create space station
ssphere = Sphere(radius=10, slices=16, sides=16)
scorrid = Cylinder(radius=4, height=22)

station = MergeShape(y=mymap.calcHeight(0, 0), rx=4, ry=4, rz=4)
station.add(ssphere.buf[0], -20, 0, 20)
station.add(ssphere.buf[0], 20, 0, 20)
station.add(ssphere.buf[0], 20, 0, -20)
station.add(ssphere.buf[0], -20, 0, -20)