纹理#
引言#
这个 arcade.Texture
类型是Arade通常与从磁盘加载或手动创建的图像进行交互的方式。这基本上是PIL/Pillow图像的包装器,包括根据所选的Hit Box算法,使用Pirmick检测Hit Box数据。换句话说,这些纹理对象负责向OpenGL提供原始RGBA像素数据,并向精灵引擎提供点击框几何体。
在较低级别的OpenGL API中,Arade中还有另一种纹理类型: arcade.gl.Texture
。这表示实际的OpenGL纹理,应该仅在处理低级渲染API时使用 arcade.gl
。
可以在创建窗口之前或之后创建/加载纹理,因为它们不直接与OpenGL交互。
纹理唯一性#
当创建纹理时, name
是必需的。这应该是唯一的字符串。如果再有两个纹理具有相同的名称,我们就会遇到麻烦。加载纹理时,文件的绝对路径用作名称的一部分,包括垂直/水平/对角线、大小和其他参数,以获得真正唯一的名称。
当通过arcade加载纹理时,纹理的名称将是图像的绝对路径和各种参数,如大小、翻转、XY位置等。
还请记住,纹理类通过查看原始像素数据来检测金丝雀的黑匣子。这意味着,例如,具有不同翻转的纹理将被多次加载(或从缓存中提取),因为我们依赖于变换的像素数据来获得命中框。
纹理缓存#
Arade正在基于 name
属性可以显著加快加载速度。
# The texture will only be loaded during the first sprite creation
tex_name = "path/to/sprite.png"
sprite_1 = arcade.Sprite(tex_name)
sprite_2 = arcade.Sprite(tex_name)
sprite_3 = arcade.Sprite(tex_name)
# Will be loaded and cached because we need fresh pixel data for hit box detection
sprite_4 = arcade.Sprite(tex_name, flipped_vertically=True)
# Fetched from cache
sprite_5 = arcade.Sprite(tex_name, flipped_vertically=True)
上述情况也适用于使用 arcade.load_texture()
或其他纹理加载功能。
可以使用以下命令清除Arade的纹理缓存 arcade.cleanup_texture_cache()
。
自定义纹理#
我们可以通过创建PIL/Pillow图像来手动创建纹理。如何做到这一点完全取决于你。使用Pillow的绘图功能或简单地将另一个库/源中的原始像素数据提供到Pillow图像中。一个随机的例子是从matplotlib获取原始像素数据。
# Create a image from raw pixel data from some source
image = PIL.Image.frombuffer(raw_data)
# NOTE: Also make sure you use a sane hit_box_algorithm
texture = arcade.Texture("unique_name", image, hit_box_algorithm=...)
同样,如何创建图像取决于您。枕头有很多种可能性。