pyglet.graphics.texture
- exception TextureArrayDepthExceeded
Exception occurs when depth has hit the maximum supported of the array.
- exception TextureArraySizeExceeded
Exception occurs ImageData dimensions are larger than the array supports.
- class CompressedTexture(
- width: int,
- height: int,
- handle: Any,
- compression_format: CompressionFormat,
- tex_type: TextureType = TextureType.TYPE_2D,
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- *,
- key: TextureKey | None = None,
Base class for backend textures that store compressed image data on the GPU.
Concrete backends provide texture creation and compressed data uploads. Pixel readback and region views are not implemented by this base class.
Initialize texture metadata for an existing backend resource.
- Parameters:
width (
int) – Width of the texture in pixels.height (
int) – Height of the texture in pixels.handle (
Any) – Backend handle for the texture resource.compression_format (
CompressionFormat) – Compression format of the stored image data.tex_type (
TextureType) – Type of texture storage.filters (
TextureFilter|tuple[TextureFilter,TextureFilter] |None) – Minification and magnification filters. A single filter applies to both; None usesdefault_filters.address_mode (
AddressMode) – Addressing mode for texture coordinates.anisotropic_level (
int) – Requested anisotropic filtering level.key (
TextureKey|None) – Shared resource identity, or None to create a new identity.
- key_type
alias of
TextureKey
- classmethod create_from_image(
- image_data: CompressedImageData,
- tex_type: TextureType = TextureType.TYPE_2D,
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- context: SurfaceContextT | None = None,
Create a compressed texture using a concrete backend implementation.
- Return type:
- classmethod supports_format(
- fmt: CompressionFormat,
- context: SurfaceContextT | None = None,
Return whether the backend can upload this format in the given context.
- Return type:
- get_image_data() ImageData
Get an ImageData view of this image.
Changes to the returned instance may or may not be reflected in this image.
- Return type:
- get_region( ) _AbstractImage
Retrieve a rectangular region of this image.
- Return type:
_AbstractImage
- get_texture() CompressedTexture
Return this texture.
- Return type:
- default_filters: TextureFilter | tuple[TextureFilter, TextureFilter] = (TextureFilter.LINEAR, TextureFilter.LINEAR)
Default minification and magnification filters, respectively.
- images = 1
- tex_coords = (0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0)
12-tuple of float, named (u1, v1, r1, u2, v2, r2, …).
u, v, rgive the 3D texture coordinates for vertices 1-4. The vertices are specified in the order bottom-left, bottom-right, top-right and top-left.
- class PixelData(
- width: int,
- height: int,
- format: ComponentFormat,
- data_type: Literal['f', 'i', 'I', 'h', 'H', 'b', 'B', 'q', 'Q', '?', 'd'],
- data: bytes | Array,
Typed pixel data read from GPU memory.
Unlike
ImageData, this class does not perform image format conversions. Its data is stored as tightly packed native values ofdata_type.- to_image_data() ImageData
Return an
ImageDataview of this pixel data.No pixel conversion is performed.
ImageDataretains this object’s underlying buffer until it needs to convert or repack it.- Return type:
- format: ComponentFormat
- class PixelReadback(context: SurfaceContextT) None
Base interface for backend-owned texture pixel readback resources.
Retain the backend context that owns the readback resources.
- class Texture(
- width: int,
- height: int,
- handle: Any,
- tex_type: TextureType = TextureType.TYPE_2D,
- internal_format: ComponentFormat = ComponentFormat.RGBA,
- internal_format_size: int = 8,
- internal_format_type: str = 'B',
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- *,
- key: TextureKey | None = None,
- context: SurfaceContextT | None = None,
An image loaded into GPU memory.
Typically, you will get an instance of Texture by accessing calling the
get_texture()method of any AbstractImage class (such as ImageData).Initialized in subclass.
- key_type
alias of
TextureKey
- region_class
The class to use when constructing regions of this texture. The class should be a subclass of TextureRegion.
alias of
TextureRegion
- classmethod create(
- width: int,
- height: int,
- tex_type: TextureType = TextureType.TYPE_2D,
- internal_format: ComponentFormat = ComponentFormat.RGBA,
- internal_format_size: int = 8,
- internal_format_type: str = 'B',
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- blank_data: bool = True,
- immutable: bool = False,
- mipmap_levels: int = 1,
- context: SurfaceContextT | None = None,
Create a Texture.
Create a Texture with the specified dimensions, target and format. On return, the texture will be bound.
- Parameters:
width (
int) – Width of texture in pixels.height (
int) – Height of texture in pixels.tex_type (
TextureType) – The type of texture.internal_format (
ComponentFormat) – The components of the internal format.internal_format_size (
int) – Bit size of each component in the internal format.internal_format_type (
str) – Internal format type, as a struct string value.filters (
TextureFilter|tuple[TextureFilter,TextureFilter] |None) – The texture filter for the min and mag filters. If a single value is passed, both values will be used as the filter.address_mode (
AddressMode) – The wrapping address mode of the texture.anisotropic_level (
int) – The anisotropic level of the texture.blank_data (
bool) – If True, initialize the texture data with all zeros. If False, do not pass initial data.immutable (
bool) – If True, allocate immutable-format texture storage.mipmap_levels (
int) – Number of mipmap levels to allocate. Immutable textures cannot add levels later.context (
TypeVar(SurfaceContextT, bound=SurfaceContext) |None) – If multiple contexts are being used, a specified context the texture is tied to.
- Return type:
- classmethod create_from_image(
- image_data: ImageData | ImageDataRegion,
- tex_type: TextureType = TextureType.TYPE_2D,
- internal_format_size: int = 8,
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- context: SurfaceContextT | None = None,
Create a Texture from image data.
On return, the texture will be bound.
- Parameters:
image_data (
ImageData|ImageDataRegion) – The image instance.tex_type (
TextureType) – The type of texture.internal_format_size (
int) – The bit size of the internal format.filters (
TextureFilter|tuple[TextureFilter,TextureFilter] |None) – The texture filter for the min and mag filters. If a single value is passed, both values will be used as the filter.address_mode (
AddressMode) – The wrapping address mode of the texture.anisotropic_level (
int) – The anisotropic level of the texture.context (
TypeVar(SurfaceContextT, bound=SurfaceContext) |None) – If multiple contexts are being used, a specified context the texture will be tied to.
- Return type:
- blit( ) None
Blit the texture to the screen.
Removed as of 3.0. Instead, consider creating a
Spritewith the Texture, and drawing it as part of a largerBatch.- Return type:
- delete() None
Delete this texture and the memory it occupies.
Textures are invalid after deletion, and may no longer be used.
- Return type:
- fetch(z: int = 0, level: int = 0) ImageData
Fetch an image-oriented copy of this texture from the GPU.
Reading data back from the GPU can be a costly operation.
Modifying the returned ImageData object has no effect on the texture itself. Uploading ImageData back to the GPU/texture can be done with the
upload()method.The returned
ImageDatauses unsigned-byte image components. Floating-point and integer textures are unsupported here; useread_pixels()to retrieve their typed pixel values instead.
- get_region( ) TextureRegion[Any]
Create a region sharing this texture’s storage.
- Return type:
- get_transform( ) TextureRegion[Any]
Create a texture region applying a simple transformation.
The returned region implements the Texture interface and shares this texture’s storage. The transformation is applied to the texture coordinates only;
get_image_data()will fetch the untransformed data from the GPU.- Parameters:
- Return type:
- init_mipmaps(levels: int | None = None, blank_data: bool = True) int
Initialize mipmap levels for this texture.
- Parameters:
- Return type:
- read_pixels(z: int = 0, level: int = 0) PixelData
Read typed pixel values from this texture.
Floating-point and integer textures retain their numeric data type; normalized textures use unsigned-byte components. Unlike
fetch(), this method does not provide general-purpose image conversion.
- upload( ) None
Upload image data into the Texture at specific coordinates.
You must have this texture bound before uploading data.
The given
xandycoordinates specify the lower-left pixel of the upload. If this texture is a 3D texture, thezparameter gives the image slice to upload into. Thelevelparameter specifies the mipmap level to upload to.- Return type:
- default_filters: TextureFilter | tuple[TextureFilter, TextureFilter] = (TextureFilter.LINEAR, TextureFilter.LINEAR)
The default minification and magnification filters, as a tuple. Both default to LINEAR. If a texture is created without specifying a filter, these defaults will be used.
- property filters: tuple[TextureFilter, TextureFilter]
The current Texture filters.
Providing a single TextureFilter will adjust both minification and magnification filters. Otherwise, a tuple can be provided to adjust each individually.
- images = 1
- owner: Texture
The root texture that owns this texture’s storage.
A texture owns itself. Texture regions reference the texture that owns their underlying storage.
- tex_coords: tuple[float, float, float, float, float, float, float, float, float, float, float, float] = (0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0)
12-tuple of float, named (u1, v1, r1, u2, v2, r2, …).
u, v, rgive the 3D texture coordinates for vertices 1-4. The vertices are specified in the order bottom-left, bottom-right, top-right and top-left.
- tex_coords_order: tuple[int, int, int, int] = (0, 1, 2, 3)
The default vertex winding order for a quad. This defaults to counter-clockwise, starting at the bottom-left.
- property uv: tuple[float, float, float, float]
Tuple containing the left, bottom, right, top 2D texture coordinates.
- class Texture3D(
- width: int,
- height: int,
- handle: Any,
- tex_type: TextureType = TextureType.TYPE_2D,
- internal_format: ComponentFormat = ComponentFormat.RGBA,
- internal_format_size: int = 8,
- internal_format_type: str = 'B',
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- *,
- key: TextureKey | None = None,
- context: SurfaceContextT | None = None,
A texture with more than one image slice.
Use the
create_for_images()orcreate_for_image_grid()classmethod to construct a Texture3D.- region_class
alias of
TextureRegion
- items: list[TextureRegion] | tuple[TextureRegion, ...]
- class TextureArray(
- width: int,
- height: int,
- tex_id: int,
- max_depth: int,
- internal_format: ComponentFormat = ComponentFormat.RGBA,
- internal_format_size: int = 8,
- internal_format_type: str = 'B',
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- *,
- context: SurfaceContextT | None = None,
A container of image layers, sampled with an array sampler.
Indexing selects a
TextureArrayRegionsuitable for a sprite. Regions of this texture remain array regions after cropping or transforming.- region_class
alias of
TextureArrayRegion
- classmethod create(
- width: int,
- height: int,
- tex_type: TextureType = TextureType.TYPE_2D_ARRAY,
- internal_format: ComponentFormat = ComponentFormat.RGBA,
- internal_format_size: int = 8,
- internal_format_type: str = 'B',
- filters: TextureFilter | tuple[TextureFilter, TextureFilter] | None = None,
- address_mode: AddressMode = AddressMode.REPEAT,
- anisotropic_level: int = 0,
- blank_data: bool = False,
- immutable: bool = False,
- mipmap_levels: int = 1,
- context: SurfaceContextT | None = None,
- *,
- max_depth: int = 256,
Create an empty texture array with capacity for
max_depthlayers.- Return type:
TextureArray[TypeVar(SurfaceContextT, bound=SurfaceContext)]
- get_region( ) TextureArrayRegion[SurfaceContextT]
Create a region sharing this texture’s storage.
- Return type:
TextureArrayRegion[TypeVar(SurfaceContextT, bound=SurfaceContext)]
- get_transform( ) TextureArrayRegion[SurfaceContextT]
Create a texture region applying a simple transformation.
The returned region implements the Texture interface and shares this texture’s storage. The transformation is applied to the texture coordinates only;
get_image_data()will fetch the untransformed data from the GPU.- Parameters:
- Return type:
TextureArrayRegion[TypeVar(SurfaceContextT, bound=SurfaceContext)]
- class TextureArrayRegion( ) None
A view of one array layer, sampled with an array sampler and its Z coordinate.
Initialized in subclass.
- class TextureGrid(
- texture: Texture | TextureRegion,
- rows: int,
- columns: int,
- item_width: int | None = None,
- item_height: int | None = None,
- row_padding: int = 0,
- column_padding: int = 0,
A texture containing a regular grid of texture regions.
To construct, create an
ImageGridfirst:image_grid = ImageGrid(...) texture_grid = TextureGrid(image_grid)
The texture grid can be accessed as a single texture, or as a sequence of
TextureRegion. When accessing as a sequence, you can specify integer indexes, in which the images are arranged in rows from the bottom-left to the top-right:# assume the texture_grid is 3x3: current_texture = texture_grid[3] # get the middle-left image
You can also specify tuples in the sequence methods, which are addressed as
row, column:# equivalent to the previous example: current_texture = texture_grid[1, 0]
When using tuples in a slice, the returned sequence is over the rectangular region defined by the slice:
# returns center, center-right, center-top, top-right images in that # order: images = texture_grid[(1,1):] # equivalent to images = texture_grid[(1,1):(3,3)]
Construct a grid for the given image.
You can specify parameters for the grid, for example setting the padding between cells. Grids are always aligned to the bottom-left corner of the image.
- Parameters:
texture (
Texture|TextureRegion) – A texture or region over which to construct the grid.rows (
int) – Number of rows in the grid.columns (
int) – Number of columns in the grid.item_width (
int|None) – Width of each column. If unspecified, is calculated such that the entire texture width is used.item_height (
int|None) – Height of each row. If unspecified, is calculated such that the entire texture height is used.row_padding (
int) – Pixels separating adjacent rows. The padding is only inserted between rows, not at the edges of the grid.column_padding (
int) – Pixels separating adjacent columns. The padding is only inserted between columns, not at the edges of the grid.
- classmethod from_image_grid(
- image_grid: ImageGrid,
- Return type:
- class TextureRegion( ) None
Common base for backend texture views sharing their owner’s storage.
Cropping and transforming a region preserve its concrete class, including the array-layer distinction represented by
TextureArrayRegion.Initialized in subclass.
- delete() None
Deleting a TextureRegion has no effect. Operate on the owning texture instead.
- Return type:
- fetch(z: int = 0, level: int = 0) ImageDataRegion
Fetch the region. Its layer is fixed;
zis ignored.- Return type:
- get_image_data(z: int = 0) ImageDataRegion
Get the image data of this texture.
- Return type:
- get_region(x: int, y: int, width: int, height: int) Self
Create a region sharing this texture’s storage.
- Return type:
Self
- get_transform( ) Self
Create a texture region applying a simple transformation.
The returned region implements the Texture interface and shares this texture’s storage. The transformation is applied to the texture coordinates only;
get_image_data()will fetch the untransformed data from the GPU.- Parameters:
- Return type:
Self
- init_mipmaps(levels: int | None = None, blank_data: bool = True) int
Initialize mipmap levels for this texture.
- Parameters:
- Return type:
- read_pixels( ) PixelData
Read typed pixels. The region’s layer is fixed;
zis ignored.- Return type:
- upload( ) None
Upload image data into the Texture at specific coordinates.
You must have this texture bound before uploading data.
The given
xandycoordinates specify the lower-left pixel of the upload. If this texture is a 3D texture, thezparameter gives the image slice to upload into. Thelevelparameter specifies the mipmap level to upload to.- Return type:
- class TextureSequence
Interface for a sequence of textures.
Typical implementations store multiple
Textureto minimise state changes.- get_texture_sequence() TextureSequence[TTexture]
Get a TextureSequence.
Added in version 1.1.
- Return type:
TextureSequence[TypeVar(TTexture, bound= Texture)]
- class UniformTextureSequence
Interface for a sequence of textures, each with the same dimensions.