pyglet.image.atlas

将多个小图像分组为更大的纹理。

该模块提供了可以有效地将小图像打包到更大纹理中的类。在处理大量图像时,这可以带来重大的性能优势。

TextureAtlas 保持一种纹理; TextureBin 管理给定大小的地图集。 TextureArrayBin 除了 TextureArray s而不是另类音乐。

该模块由内部使用 pyglet.resource 模块。

示例用法::

# Load images from disk
car_image = pyglet.image.load('car.png')
boat_image = pyglet.image.load('boat.png')

# Pack these images into one or more textures
bin = TextureBin()
car_texture = bin.add(car_image)
boat_texture = bin.add(boat_image)

其结果是 TextureBin.add() 是一种 TextureRegion 包含该图像的。图像一旦添加,就不能从图库(或地图集)中删除;也不能从给定的图库或图集中获得图像列表--应用程序负责跟踪 add 方法:研究方法。

Added in version 1.1.

exception AllocatorException

分配器没有足够的可用空间来容纳所请求的图像大小。

class Allocator

矩形面积分配算法。

一个 Allocator 使用指定的初始化 widthheight 。这个 alloc() 然后可以调用方法来检索该区域的空闲区域(并保护它们免受未来分配的影响)。

Allocator 使用相当简单的基于条带的算法。当矩形以相同大小或高度顺序分配时,它的性能最好。

__init__(width: int, height: int) None

创建一个 Allocator 给定大小的。

alloc(width: int, height: int) tuple[int, int]

获取给定大小的分配器中自由区域的位置

如果可以找到适合请求大小的位置,该位置将被标记为正在使用并返回。(The同一位置永远不会被退回两次)。如果没有足够的空间来容纳给定区域, AllocatorException 都被养大了。

返回类型:

tuple[int, int]

get_fragmentation() float

根据当前的分配行为,获取不太可能使用的那部分区域。

此方法仅对调试和分析有用。

返回类型:

float

get_usage() float

获取已分配的部分区域。

此方法仅对调试和分析有用。

返回类型:

float

height
strips
used_area
width
class TextureArrayBin

纹理数组的集合。

TextureArrayBin 维护纹理阵列的集合,并在超过深度时根据需要创建新的纹理阵列。其工作原理与TextureBin类似,但它管理TextureArrays而不是TextureCases。

__init__(
texture_width: int = 2048,
texture_height: int = 2048,
max_depth: int | None = None,
) None
add(img: ImageData) TextureArrayRegion

将图像添加到此纹理数组框中。

此方法调用 add() 用于具有图像空间的第一个数组。

TextureArraySizeExceeded 如果图像的尺寸超过 texture_widthtexture_height

返回类型:

TextureArrayRegion

class TextureAtlas

由多个较小图像组成的大纹理。

TextureAtlas是一个最大尺寸的纹理,由多个较小的图像组成。这可以通过允许使用单个纹理绑定将所有图像绘制在一起,而不是每次绘制多个微小的纹理绑定来提高渲染性能。

创建TextureAtlas实例时,会出现新的 Texture 将以请求的大小创建对象。如果GL驱动程序支持的最大纹理尺寸小于请求的尺寸,则将使用两者中较小的一个。

__init__(width: int = 2048, height: int = 2048) None

创建给定大小的纹理地图集。

add(
img: ImageData,
border: int = 0,
) TextureRegion

将Image Data添加到地图集。

给定 ImageData ,将其添加到阿特拉斯并返回新的 TextureRegion object.可选的 border 可以传递参数,这将在添加的Image Data周围留下指定数量的空白像素。这在某些情况下和混合模式下可能很有用,因为相邻像素数据可能会“渗透”到边缘。

如果给定图像无法直接传输到纹理(例如,如果它不是IMAData的实例,例如另一个纹理),则此方法将失败。 AllocatorException 如果地图集中没有空间放置图像,则将引发。

返回类型:

TextureRegion

class TextureBin

TextureThomases集合。

TextureBin 维护TextureFinder集合,并根据需要创建新的集合,以适应添加未绑定数量的图像。当一个TextureAltas已满时,系统会自动创建一个新的TextureAltas以适应下一个Image Data。

__init__(texture_width: int = 2048, texture_height: int = 2048) None

创建一个纹理箱来保存给定大小的地图集。

add(
img: ImageData | AbstractImage,
border: int = 0,
) TextureRegion

将图像添加到此纹理框中。

此方法调用 add() 为第一个有空间放置图像的地图集。

AllocatorException 如果图像的尺寸超过 texture_widthtexture_height

返回类型:

TextureRegion