TextureArray

class moderngl.TextureArray

An Array Texture is a Texture where each mipmap level contains an array of images of the same size. Array textures may have Mipmaps, but each mipmap in the texture has the same number of levels.

A TextureArray object cannot be instantiated directly, it requires a context. Use Context.texture_array() to create one.

Create

Context.texture_array(size, components, data=None, alignment=1, dtype='f1') → TextureArray

Create a TextureArray object.

Parameters:
  • size (tuple) – The (width, height, layers) of the texture.
  • components (int) – The number of components 1, 2, 3 or 4.
  • data (bytes) – Content of the texture. The size must be (width, height * layers) so each layer is stacked vertically.
Keyword Arguments:
 
  • alignment (int) – The byte alignment 1, 2, 4 or 8.
  • dtype (str) – Data type.
Returns:

Texture3D object

Methods

TextureArray.read(alignment=1) → bytes

Read the content of the texture array into a buffer.

Keyword Arguments:
 alignment (int) – The byte alignment of the pixels.
Returns:bytes
TextureArray.read_into(buffer, alignment=1, write_offset=0)

Read the content of the texture array into a buffer.

Parameters:

buffer (bytearray) – The buffer that will receive the pixels.

Keyword Arguments:
 
  • alignment (int) – The byte alignment of the pixels.
  • write_offset (int) – The write offset.
TextureArray.write(data, viewport=None, alignment=1)

Update the content of the texture array.

Parameters:
  • data (bytes) – The pixel data.
  • viewport (tuple) – The viewport.
Keyword Arguments:
 

alignment (int) – The byte alignment of the pixels.

TextureArray.build_mipmaps(base=0, max_level=1000)

Generate mipmaps.

This also changes the texture filter to LINEAR_MIPMAP_LINEAR, LINEAR (Will be removed in 6.x)

Keyword Arguments:
 
  • base (int) – The base level
  • max_level (int) – The maximum levels to generate
TextureArray.use(location=0)

Bind the texture array.

Parameters:location (int) – The texture location. Same as the integer value that is used for sampler2D uniforms in the shaders. The value 0 will bind the texture to the GL_TEXTURE0 binding point.

Attributes

TextureArray.repeat_x

bool – The x repeat flag for the texture (Default True)

Example:

# Enable texture repeat (GL_REPEAT)
texture.repeat_x = True

# Disable texture repeat (GL_CLAMP_TO_EDGE)
texture.repeat_x = False
TextureArray.repeat_y

bool – The y repeat flag for the texture (Default True)

Example:

# Enable texture repeat (GL_REPEAT)
texture.repeat_y = True

# Disable texture repeat (GL_CLAMP_TO_EDGE)
texture.repeat_y = False
TextureArray.filter

tuple – The minification and magnification filter for the texture. (Default (moderngl.LINEAR. moderngl.LINEAR))

Example:

texture.filter == (monderngl.NEAREST, moderngl.NEAREST)
texture.filter == (monderngl.LINEAR_MIPMAP_LINEAR, moderngl.LINEAR)
texture.filter == (monderngl.NEAREST_MIPMAP_LINEAR, moderngl.NEAREST)
texture.filter == (monderngl.LINEAR_MIPMAP_NEAREST, moderngl.NEAREST)
TextureArray.swizzle

str – The swizzle mask of the texture (Default 'RGBA').

The swizzle mask change/reorder the vec4 value returned by the texture() function in a GLSL shaders. This is represented by a 4 character string were each character can be:

'R' GL_RED
'G' GL_GREEN
'B' GL_BLUE
'A' GL_ALPHA
'0' GL_ZERO
'1' GL_ONE

Example:

# Alpha channel will always return 1.0
texture.swizzle = 'RGB1'

# Only return the red component. The rest is masked to 0.0
texture.swizzle = 'R000'

# Reverse the components
texture.swizzle = 'ABGR'
TextureArray.anisotropy

float – Number of samples for anisotropic filtering (Default 1.0). The value will be clamped in range 1.0 and ctx.max_anisotropy.

Any value greater than 1.0 counts as a use of anisotropic filtering:

# Disable anisotropic filtering
texture.anisotropy = 1.0

# Enable anisotropic filtering suggesting 16 samples as a maximum
texture.anisotropy = 16.0
TextureArray.width

int – The width of the texture array.

TextureArray.height

int – The height of the texture array.

TextureArray.layers

int – The number of layers of the texture array.

TextureArray.size

tuple – The size of the texture array.

TextureArray.dtype

str – Data type.

TextureArray.components

int – The number of components of the texture array.

TextureArray.glo

int – The internal OpenGL object. This values is provided for debug purposes only.