def resize_button_image(image, old_width, new_width): if new_width == old_width: return image new_width = int(image.width * new_width // old_width) atlas = TextureAtlas(new_width, image.height) atlas.add(image.get_region(0, 0, new_width // 2, image.height).image_data) atlas.add(image.get_region(image.width - new_width // 2, 0, new_width // 2, image.height).image_data) return atlas.texture.image_data
class Graphics(object): def __init__(self): self.atlas = TextureAtlas(width=1024, height=512) blank = SolidColorImagePattern((0, 0, 0, 0)).create_image(1024, 1) self.atlas.add(blank) def _split_filename(self, filename): r""" Splits filename 'blah\file-X.ext' into tuple ('file', X), where X is any integer. Returns ('file', None) if filename does not end in hyphen followed by integer. """ basename = filename[len(IMAGES_DIR) + 1 : -4] name = basename number = None hyphen = basename.rfind("-") if hyphen != -1: try: number = int(basename[hyphen + 1 :]) name = basename[:hyphen] except ValueError: pass return name, number def _split_image(self, image, num_frames): """ Expects an image to contain 1 or more frames, as a horizontal row of equally sized regions. Splits the given image into its constituent frames, returning them as a list of texture regions. """ frames = [] frame_width = image.width / num_frames for i in xrange(0, num_frames): region = image.get_region(frame_width * i, 0, frame_width, image.height) set_anchor(region) frames.append(region) return frames def load(self): """ Loads all files in images directory, making them available as self.images[filename]. Each entry in that dictionary is a list of X frames, where X, if not 1, is indicated by the filename ending in '-X.png' """ images = {} for filename in glob("%s/*.png" % (IMAGES_DIR)): self.atlas.add(SPACER) region = self.atlas.add(load(filename)) name, num_frames = self._split_filename(filename) if num_frames: images[name] = self._split_image(region, num_frames) else: images[name] = [region] return images
def __init__(self, names): super(TextureGroupIndividual, self).__init__() atlas = TextureAtlas(64*len(names), 64) self.texture = atlas.texture self.texture_data = [] i=0 for name in names: if not name in BLOCK_TEXTURE_DIR: if G.TEXTURE_PACK != 'default': BLOCK_TEXTURE_DIR[name] = load_image('resources', 'texturepacks', G.TEXTURE_PACK, 'textures', 'blocks', name + '.png') else: BLOCK_TEXTURE_DIR[name] = load_image('resources', 'texturepacks', 'textures', 'blocks', name + '.png') if not BLOCK_TEXTURE_DIR[name]: return None subtex = atlas.add(BLOCK_TEXTURE_DIR[name].get_region(0,0,64,64)) 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 #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))
def __init__(self, names, height=1.0, width=1.0, background_color=None): 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 # remove background color for crack textures if background_color is not None: data = bytearray(BLOCK_TEXTURE_DIR[name].get_image_data().get_data('RGBA', texture_size * 4)) for i in range(len(data)): if data [i] == background_color: data[i] = 0 BLOCK_TEXTURE_DIR[name].get_image_data().set_data('RGBA', texture_size * 4, bytes(data)) 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
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
class Textures(object): 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))) def add(self, path): image = pyglet.image.load(path) region = self.atlas.add(image) return region.tex_coords
class BlockTextureGroup(Group): 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 def set_state(self): if self.texture: glBindTexture(self.texture.target, self.texture.id) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glEnable(self.texture.target) def unset_state(self): if self.texture: glDisable(self.texture.target)