Texture Atlas#
arcade.TextureAtlas#
- class arcade.TextureAtlas(size: Tuple[int, int], *, border: int = 1, textures: Sequence[Texture] | None = None, auto_resize: bool = True, ctx: ArcadeContext | None = None, capacity: int = 2)[source]#
A texture atlas with a size in a context.
A texture atlas is a large texture containing several textures so OpenGL can easily batch draw thousands or hundreds of thousands of sprites on one draw operation.
This is a fairly simple atlas that stores horizontal strips were the height of the strip is the texture/image with the larges height.
Adding a texture to this atlas generates a texture id. This id is used the sprite list vertex data to reference what texture each sprite is using. The actual texture coordinates are located in a float32 texture this atlas is responsible for keeping up to date.
The atlas deals with image and textures. The image is the actual image data. The texture is the arcade texture object that contains the image and other information about such as transforms. Several textures can share the same image with different transforms applied. The transforms are simply changing the order of the texture coordinates to flip, rotate or mirror the image.
- Parameters:
size (Tuple[int, int]) – The width and height of the atlas in pixels
border (int) – Currently no effect; Should always be 1 to avoid textures bleeding
textures (Sequence[Texture]) – The texture for this atlas
auto_resize (bool) – Automatically resize the atlas when full
ctx (Context) – The context for this atlas (will use window context if left empty)
capacity (int) – The number of textures the atlas keeps track of. This is multiplied by 4096. Meaning capacity=2 is 8192 textures. This value can affect the performance of the atlas.
- add(texture: Texture) Tuple[int, AtlasRegion] [source]#
Add a texture to the atlas.
- Parameters:
texture (Texture) – The texture to add
- Returns:
texture_id, AtlasRegion tuple
- allocate(image_data: ImageData) Tuple[int, int, int, AtlasRegion] [source]#
Attempts to allocate space for an image in the atlas.
This doesn’t write the texture to the atlas texture itself. It only allocates space.
- Returns:
The x, y texture_id, TextureRegion
- property auto_resize: bool#
Get or set the auto resize flag for the atlas. If enabled the atlas will resize itself when full.
- Return type:
- classmethod calculate_minimum_size(textures: Sequence[Texture], border: int = 1)[source]#
Calculate the minimum atlas size needed to store the the provided sequence of textures
- Parameters:
textures (Sequence[Texture]) – Sequence of textures
border – The border around each texture in pixels
- Returns:
An estimated minimum size as a (width, height) tuple
- clear(*, clear_image_ids: bool = True, clear_texture_ids: bool = True, texture: bool = True) None [source]#
Clear and reset the texture atlas. Note that also clearing “texture_ids” makes the atlas lose track of the old texture ids. This means the sprite list must be rebuild from scratch.
- classmethod create_from_texture_sequence(textures: Sequence[Texture], border: int = 1) TextureAtlas [source]#
Create a texture atlas of a reasonable size from a sequence of textures.
- property fbo: Framebuffer#
The framebuffer object for this atlas
- get_image_id(hash: str) int [source]#
Get the uv slot for a image by hash
- Parameters:
hash (str) – The hash of the image
- Returns:
The texture id for the given texture name
- get_image_region_info(hash: str) AtlasRegion [source]#
Get the region info for and image by has
- Parameters:
hash (str) – The hash of the image
- Returns:
The AtlasRegion for the given texture name
- get_texture_id(atlas_name: str) int [source]#
Get the uv slot for a texture by atlas name
- Parameters:
atlas_name (str) – The name of the texture in the atlas
- Returns:
The texture id for the given texture name
- get_texture_region_info(atlas_name: str) AtlasRegion [source]#
Get the region info for a texture by atlas name
- Returns:
The AtlasRegion for the given texture name
- property images: List[ImageData]#
Return a list of all the images in the atlas.
A new list is constructed from the internal weak set of images.
- Return type:
List[ImageData]
- rebuild() None [source]#
Rebuild the underlying atlas texture.
This method also tries to organize the textures more efficiently ordering them by size. The texture ids will persist so the sprite list don’t need to be rebuilt.
- remove(texture: Texture) None [source]#
Remove a texture from the atlas.
This doesn’t erase the pixel data from the atlas texture itself, but leaves the area unclaimed.
- Parameters:
texture (Texture) – The texture to remove
- render_into(texture: Texture, projection: Tuple[float, float, float, float] | None = None)[source]#
Render directly into a sub-section of the atlas. The sub-section is defined by the already allocated space of the texture supplied in this method.
By default the projection will be set to match the texture area size were 0, 0 is the lower left corner and width, height (of texture) is the upper right corner.
This method should should be used with the
with
statement:with atlas.render_into(texture): # Draw commands here # Specify projection with atlas.render_into(texture, projection=(0, 100, 0, 100)) # Draw geometry
- resize(size: Tuple[int, int]) None [source]#
Resize the atlas on the gpu.
This will copy the pixel data from the old to the new atlas retaining the exact same data. This is useful if the atlas was rendered into directly and we don’t have to transfer each texture individually from system memory to graphics memory.
- save(path: str | Path, flip: bool = False, components: int = 4, draw_borders: bool = False, border_color: Tuple[int, int, int] = (255, 0, 0)) None [source]#
Save the texture atlas to a png.
Borders can also be drawn into the image to visualize the regions of the atlas.
- show(flip: bool = False, components: int = 4, draw_borders: bool = False, border_color: Tuple[int, int, int] = (255, 0, 0)) None [source]#
Show the texture atlas using Pillow.
Borders can also be drawn into the image to visualize the regions of the atlas.
- property textures: List[Texture]#
Return a list of all the textures in the atlas.
A new list is constructed from the internal weak set of textures.
- Return type:
Set[Texture]
- to_image(flip: bool = False, components: int = 4, draw_borders: bool = False, border_color: Tuple[int, int, int] = (255, 0, 0)) Image [source]#
Convert the atlas to a Pillow image.
Borders can also be drawn into the image to visualize the regions of the atlas.
- update_texture_image(texture: Texture)[source]#
Updates the internal image of a texture in the atlas texture. The new image needs to be the exact same size as the original one meaning the texture already need to exist in the atlas.
This can be used in cases were the image is manipulated in some way and we need a quick way to sync these changes to graphics memory. This operation is fairly expensive, but still orders of magnitude faster than removing the old texture, adding the new one and re-building the entire atlas.
- Parameters:
texture (Texture) – The texture to update
- use_uv_texture(unit: int = 0) None [source]#
Bind the texture coordinate texture to a channel. In addition this method writes the texture coordinate to the texture if the data is stale. This is to avoid a full update every time a texture is added to the atlas.
- Parameters:
unit (int) – The texture unit to bind the uv texture