Esempio n. 1
0
 def __init__(self, names, width=1.0, height=1.0, bgcolor=None):
     super(BlockTextureGroup, self).__init__()
     self.atlas = None
     self.texture_data = []
     self.block_texture = {}
     for name in names:
         if name == 'missing':
             self.block_texture[name] = resource_pack.get_resource(
                 'textures/misc/missing_texture')
         else:
             self.block_texture[name] = resource_pack.get_resource(
                 'textures/block/%s' % name)
         size = self.block_texture[name].width
         if bgcolor is not None:
             data = bytearray(
                 self.block_texture[name].get_image_data().get_data(
                     'RGBA', size * 4))
             for i in range(len(data)):
                 if data[i] == bgcolor:
                     data[i] = 0
             else:
                 self.block_texture[name].get_image_data().set_data(
                     'RGBA', size * 4, bytes(data))
         if self.atlas == None:
             self.atlas = TextureAtlas(size * len(names), size)
             self.texture = self.atlas.texture
         subtex = self.atlas.add(self.block_texture[name])
         i = 0
         for value in subtex.tex_coords:
             i += 1
             if i % 3 != 0:
                 self.texture_data.append(value)
     if self.atlas == None:
         self.atlas = TextureAtlas(1, 1)
     self.texture = self.atlas.texture
     self.texture_data += self.texture_data[-8:] * (6 - len(names))
     # 调整贴图大小
     if height != 1.0 or width != 1.0:
         if len(self.texture_data) == 0:
             return
         else:
             tex_width = tex_height = self.texture_data[
                 2] - self.texture_data[0]
             w_margin = tex_height * (1.0 - width) / 2
             h_margin = tex_height * (1.0 - height)
             # 顶部和底部
             for i in (0, 1):
                 for j in (0, 1, 3, 6):
                     self.texture_data[i * 8 + j] += w_margin
                 for j in (2, 4, 5, 7):
                     self.texture_data[i * 8 + j] -= w_margin
             # 四边
             for i in range(2, 6):
                 for j in (0, 6):
                     self.texture_data[i * 8 + j] += w_margin
                 for j in (2, 4):
                     self.texture_data[i * 8 + j] -= w_margin
                 for j in (5, 7):
                     self.texture_data[i * 8 + j] -= h_margin
Esempio n. 2
0
    def __init__(self, names, height=1.0, width=1.0):
        super(TextureGroupIndividual, self).__init__()
        atlas = None
        # self.texture = atlas.texture
        self.texture_data = []
        i=0
        texture_pack = G.texture_pack_list.selected_texture_pack
        for name in names:
            
            if not name in BLOCK_TEXTURE_DIR:
                BLOCK_TEXTURE_DIR[name] = texture_pack.load_texture(['textures', 'blocks', name + '.png'])

            if not BLOCK_TEXTURE_DIR[name]:
                continue

            texture_size = BLOCK_TEXTURE_DIR[name].width

            if atlas == None:
                atlas = TextureAtlas(texture_size * len(names), texture_size)
                self.texture = atlas.texture

            subtex = atlas.add(BLOCK_TEXTURE_DIR[name].get_region(0,0,texture_size,texture_size))
            for val in subtex.tex_coords:
                i += 1
                if i % 3 != 0: self.texture_data.append(val) #tex_coords has a z component we don't utilize
        if atlas == None:
            atlas = TextureAtlas(1, 1)
        self.texture = atlas.texture
        #Repeat the last texture for the remaining sides
        # (top, bottom, side, side, side, side)
        # ie: ("dirt",) ("grass_top","dirt","grass_side")
        # Becomes ("dirt","dirt","dirt","dirt","dirt","dirt") ("grass_top","dirt","grass_side","grass_side","grass_side","grass_side")
        self.texture_data += self.texture_data[-8:]*(6-len(names))

        # resize the texture
        if height != 1.0 or width != 1.0 :
            if len(self.texture_data) == 0:
                return

            tex_width = tex_height = self.texture_data[2] - self.texture_data[0]
            h_margin = tex_height * (1.0 - height)
            w_margin = tex_width * (1.0 - width) / 2
            # top and bottom
            for i in (0, 1):
                for j in (0, 1, 3, 6): self.texture_data[i * 8 + j] += w_margin
                for j in (2, 4, 5, 7): self.texture_data[i * 8 + j] -= w_margin

            # side
            for i in range(2, 6):
                for j in (0, 6): self.texture_data[i * 8 + j] += w_margin
                for j in (2, 4): self.texture_data[i * 8 + j] -= w_margin
                for j in (5, 7): self.texture_data[i * 8 + j] -= h_margin
Esempio n. 3
0
    def __init__(self, path):
        self.atlas = TextureAtlas()
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        self.group = TextureGroup(self.atlas.texture)

        for image in os.listdir(path):
            setattr(self, image[:-4], self.add(os.path.join(path, image)))