pygfx.resources.Texture

class pygfx.resources.Texture(data=None, *, dim, size=None, format=None, colorspace='srgb', generate_mipmaps=False)

Bases: Resource

Texture object containing structured 1D, 2D or 3D data.

Can be used to represent e.g. image data or colormaps. Can also serve as a render target (for the renderer). Supports texture stacks, cube textures, and mipmapping.

Parameters:
  • data – array, optional Array data of any type that supports the buffer-protocol, (e.g. a bytes or numpy array). If None, nbytes and size must be provided. The dtype must be compatible with the rendering backend.

  • dim – int The dimensionality of the array (1, 2 or 3).

  • size – tuple, [3] The extent (width, height, depth) of the array. If None, it is derived from dim and the shape of the data. The texture can also represent a stack of images by setting dim=2 and depth > 1, or a cube image by setting dim=2 and depth==6.

  • format – str A format string describing the texture layout. If None, this is derived from the data. This must be a pygfx format specifier, e.g. “3xf4”, but can also be a format specific to the render backend (e.g. from wgpu.TextureFormat).

  • colorspace – str If this data is used as color, it is interpreted to be in this colorspace. Can be “srgb” or “physical”. Default “srgb”.

  • generate_mipmaps – bool If True, automatically generates mipmaps when transferring data to the GPU.

property rev

An integer that is increased when update_range() is called.

property dim

The dimensionality of the texture (1, 2, or 3).

property data

The data for this texture. Can be None if the data only exists on the GPU.

Note: the data is the same reference that was given to instantiate this object, but this may change.

property mem

The data for this buffer as a memoryview. Can be None if the data only exists on the GPU.

property nbytes

Get the number of bytes in the texture.

property size

The size of the texture as (width, height, depth). (always a 3-tuple, regardless of the dimension).

property format

The texture format as a string. Usually a pygfx format specifier (e.g. u2 for scalar uint16, or 3xf4 for RGB float32), but can also be a overridden to a backend-specific format.

property colorspace

If this data is used as color, it is interpreted to be in this colorspace. Can be “srgb” or “physical”. Default “srgb”.

property generate_mipmaps

Whether to automatically generate mipmaps when uploading to the GPU.

update_range(offset, size)

Mark a certain range of the data for upload to the GPU. The offset and (sub) size should be (width, height, depth) tuples. Numpy users beware that an arrays shape is (height, width)!

Examples

Offscreen Rendering

Offscreen Rendering

Orbit Camera

Orbit Camera

Use gfx.show to show a Scene

Use gfx.show to show a Scene

Geometry Image

Geometry Image

Show Image

Show Image

Geometry Cubes

Geometry Cubes

Torus knot

Torus knot

Mesh dynamic

Mesh dynamic

Line Segments

Line Segments

Transform Control without Matrix Updating

Transform Control without Matrix Updating

Volume Slice 1

Volume Slice 1

Simulating grass with point sprites

Simulating grass with point sprites

Geometry Plane

Geometry Plane

Bounding Box Coordinates

Bounding Box Coordinates

Volume Slice 2

Volume Slice 2

Image with Points Overlaid

Image with Points Overlaid

Use a Skybox

Use a Skybox

Physical Color

Physical Color

Panzoom Camera

Panzoom Camera

Instancing

Instancing

Synced Video Rendering

Synced Video Rendering

Color Picking

Color Picking

Paint to a texture

Paint to a texture

Colormap Channels

Colormap Channels

Volume Rendering 1

Volume Rendering 1

Image with another image overlaid

Image with another image overlaid

Use a Skybox

Use a Skybox

Nested Scenes

Nested Scenes

PBR Rendering 1

PBR Rendering 1

Mesh and Volume Slicing 2

Mesh and Volume Slicing 2

Mesh Picking

Mesh Picking

Volume and Mesh Slicing 1

Volume and Mesh Slicing 1

Lightmap

Lightmap

Dynamic Environment Map

Dynamic Environment Map

Ambient occlusion

Ambient occlusion

Image Click Events

Image Click Events

Colormap Image

Colormap Image

Subplots Video

Subplots Video

PBR Rendering 2

PBR Rendering 2

Environment Map Effects

Environment Map Effects

Mesh with quads

Mesh with quads

Colormap Mesh

Colormap Mesh

Image Material

Image Material

Simple Colormap

Simple Colormap

Image on Plane Geometry 1

Image on Plane Geometry 1

Validate Skybox

Validate Skybox

Volume and Volume Slice Rendering

Volume and Volume Slice Rendering

Mesh Colormaps

Mesh Colormaps

Mesh slice

Mesh slice