纹理#

class arcade.gl.Texture2D(ctx: Context, size: Tuple[int, int], *, components: int = 4, dtype: str = 'f1', data: BufferProtocol | None = None, filter: Tuple[PyGLuint, PyGLuint] | None = None, wrap_x: PyGLuint | None = None, wrap_y: PyGLuint | None = None, target=3553, depth=False, samples: int = 0, immutable: bool = False, internal_format: PyGLuint | None = None, compressed: bool = False, compressed_data: bool = False)[源代码]#

基类:

OpenGL 2D纹理。我们可以创建空的黑色纹理或从字节数据创建纹理。也可以使用不同的数据类型创建纹理,例如浮点型、整型或无符号整型。

创建纹理实例的最佳方法是通过 arcade.gl.Context.texture()

支撑点 dtype 值为:

# Float formats
'f1': UNSIGNED_BYTE
'f2': HALF_FLOAT
'f4': FLOAT
# int formats
'i1': BYTE
'i2': SHORT
'i4': INT
# uint formats
'u1': UNSIGNED_BYTE
'u2': UNSIGNED_SHORT
'u4': UNSIGNED_INT
参数:
  • ctx -- 对象所属的上下文

  • size (Tuple[int, int]) -- 纹理的大小

  • components -- 组件数量(1:R、2:RG、3:RGB、4:RGBA)

  • dtype -- 各组件的数据类型:f1、f2、f4/i1、i2、i4/u1、u2、u4

  • data -- 纹理数据(可选)。可以是字节,也可以是支持缓冲区协议的任何对象。

  • filter -- 纹理的缩小/放大滤镜

  • wrap_x -- 换行模式x

  • wrap_y -- 换行模式y

  • target -- 纹理类型(忽略。传统)

  • depth -- 在以下情况下创建深度纹理 True

  • samples -- 为大于0的值创建多采样纹理。该值将被限制在0和驱动程序报告的最大样本容量之间。

  • immutable -- 使存储(而不是内容)不可变。将纹理与计算着色器一起使用时,有时可能需要此选项。

  • internal_format -- 纹理的内部格式

resize(size: Tuple[int, int])[源代码]#

调整纹理大小。这将重新分配内部内存,所有像素数据都将丢失。

ctx#

此纹理所属的上下文

类型:

Context

glo#

OpenGL纹理ID

类型:

GLuint

compressed#

这是使用压缩格式吗?

类型:

布尔尔

width#

纹理的宽度(以像素为单位

类型:

集成

height#

纹理的高度,以像素为单位

类型:

集成

dtype#

每个组件的数据类型

类型:

应力

size#

元组形式的纹理大小

类型:

元组(宽、高)

samples#

启用多重采样时的样本数(只读)

类型:

集成

byte_size#

纹理的字节大小。

类型:

集成

components#

纹理中的组件数量

类型:

集成

component_size#

每个组件的大小(以字节为单位

类型:

集成

depth#

如果这是深度纹理。

类型:

布尔尔

immutable#

这个纹理有不变的存储空间吗?

类型:

布尔尔

swizzle#

纹理的旋转蒙版(默认 'RGBA' )。

Swizzle遮罩更改/重新排序 vec4 方法返回的值 texture() 函数在GLSL着色器中。这由4个字符的字符串表示,其中每个字符可以是::

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

示例::

# 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'
类型:

应力

filter#

获取或设置 (min, mag) 此纹理的过滤器。这些是纹理如何进行内插的规则。滤镜是为缩小和放大指定的。

默认值为 LINEAR, LINEAR 。可以设置为 NEAREST, NEAREST 用于像素化图形。

使用mipmap时,最小筛选器需要是 MIPMAP 变种。

接受的值::

# Enums can be accessed on the context or arcade.gl
NEAREST                # Nearest pixel
LINEAR                 # Linear interpolate
NEAREST_MIPMAP_NEAREST # Minification filter for mipmaps
LINEAR_MIPMAP_NEAREST  # Minification filter for mipmaps
NEAREST_MIPMAP_LINEAR  # Minification filter for mipmaps
LINEAR_MIPMAP_LINEAR   # Minification filter for mipmaps

另请参阅

类型:

元组(最小过滤器、最大过滤器)

wrap_x#

获取或设置纹理的水平换行。这决定了当纹理坐标位于 [0.0, 1.0] 区域。默认值为 REPEAT

有效选项包括:

# Note: Enums can also be accessed in arcade.gl
# Repeat pixels on the y axis
texture.wrap_x = ctx.REPEAT
# Repeat pixels on the y axis mirrored
texture.wrap_x = ctx.MIRRORED_REPEAT
# Repeat the edge pixels when reading outside the texture
texture.wrap_x = ctx.CLAMP_TO_EDGE
# Use the border color (black by default) when reading outside the texture
texture.wrap_x = ctx.CLAMP_TO_BORDER
类型:

集成

wrap_y#

获取或设置纹理的水平换行。这决定了当纹理坐标位于 [0.0, 1.0] 区域。默认值为 REPEAT

有效选项包括:

# Note: Enums can also be accessed in arcade.gl
# Repeat pixels on the x axis
texture.wrap_x = ctx.REPEAT
# Repeat pixels on the x axis mirrored
texture.wrap_x = ctx.MIRRORED_REPEAT
# Repeat the edge pixels when reading outside the texture
texture.wrap_x = ctx.CLAMP_TO_EDGE
# Use the border color (black by default) when reading outside the texture
texture.wrap_x = ctx.CLAMP_TO_BORDER
类型:

集成

anisotropy#

获取或设置此纹理的各向异性。

compare_func#

获取或设置深度纹理的比较函数::

texture.compare_func = None  # Disable depth comparison completely
texture.compare_func = '<='  # GL_LEQUAL
texture.compare_func = '<'   # GL_LESS
texture.compare_func = '>='  # GL_GEQUAL
texture.compare_func = '>'   # GL_GREATER
texture.compare_func = '=='  # GL_EQUAL
texture.compare_func = '!='  # GL_NOTEQUAL
texture.compare_func = '0'   # GL_NEVER
texture.compare_func = '1'   # GL_ALWAYS
类型:

应力

read(level: int = 0, alignment: int = 1) bytes[源代码]#

阅读纹理的内容。

参数:
  • level -- 要读取的纹理级别

  • alignment -- 内存中每行起始位置的对齐方式(以字节数表示)。可能的值:1、2、4

write(data: Buffer | Buffer, level: int = 0, viewport=None) None[源代码]#

将字节数据从传递的源写入纹理。

这个 data 值可以是一个 arcade.gl.Buffer 或任何实现 Buffer Protocol

后一类包括 bytesbytearrayarray.array ,以及更多。对于非内置类型,您可能需要使用类型化解决方法。看见 将原始字节写入GL缓冲区和纹理 以获取更多信息。

参数:
  • data -- Buffer 或缓冲具有要写入的数据的协议对象。

  • level -- 要写入的纹理级别

  • viewport (Union[Tuple[int, int], Tuple[int, int, int, int]]) -- 要写入的纹理区域。2个或4个组件元组

build_mipmaps(base: int = 0, max_level: int = 1000) None[源代码]#

为该纹理生成mipmap。

缺省值通常运行良好。

Mipmap是应用了特殊过滤的原始纹理的连续较小版本。使用mipmap允许OpenGL使用较少的缩放瑕疵来渲染原始纹理的缩放版本。

可以为任何大小的纹理制作Mipmap。每个mipmap版本的宽度和高度都是前一个版本的一半(例如256 x 256、128 x 128、64 x 64等),最小为1 x 1。

备注

仅当纹理的滤镜配置了mipmap类型的缩小时,才会使用Mipmap::

# Set up linear interpolating minification filter
texture.filter = ctx.LINEAR_MIPMAP_LINEAR, ctx.LINEAR
参数:
  • base -- Mipmap的级别开始于(通常为0)

  • max_level -- 要生成的最大级别数

另请参阅:https://www.khronos.org/opengl/wiki/Texture#Mip_maps

delete()[源代码]#

销毁底层OpenGL资源。除非你清楚地知道自己在做什么,否则不要使用这个。

static delete_glo(ctx: Context, glo: gl.GLuint)[源代码]#

破坏纹理。当对对象进行垃圾回收时,会自动调用此方法。

参数:
  • ctx -- OpenGL上下文

  • glo -- OpenGL纹理ID

use(unit: int = 0) None[源代码]#

将纹理绑定到通道,

参数:

unit -- 绑定纹理的纹理单元。

bind_to_image(unit: int, read: bool = True, write: bool = True, level: int = 0)[源代码]#

将纹理绑定到图像单位。

请注意,其中一个或两个都是 readwrite 需要是 True 。支持的模式有:只读、只写、读写

参数:
  • unit -- 图像单元

  • read -- 计算着色器打算从该图像中读取

  • write -- 计算着色器打算写入此图像

  • level --

get_handle(resident: bool = True) int[源代码]#

获取无绑定纹理访问的句柄。

一旦创建了句柄,就不能更改其参数。试图这样做将不会有任何效果。(滤镜、包裹等)。这种不变性是无法改变的。

在驻留之前,着色器不能使用控制柄。可以多次调用此方法来将纹理移入和移出驻留::

>> texture.get_handle(resident=False)
4294969856
>> texture.get_handle(resident=True)
4294969856

如果句柄已经存在,则返回相同的句柄。

备注

OpenGL维基的限制

可用于驻留图像/纹理的存储量可能小于可用于纹理的总存储量。因此,应尝试最大限度地减少纹理驻留的时间。不要试图采取像使纹理驻留/非驻留每一帧之类的步骤。但是,如果您已经使用了一段时间的纹理,请将其设置为非驻留纹理。

关键字参数:

resident (bool) -- 使纹理驻留。