Creating and Sampling Textures
创建和采样纹理
【说明:】
本文将尽量采用英-中对照方式,来看看效果。
Load image data into a texture and apply it to a quadrangle.
将图像数据加载到纹理中并将其应用于四边形。
Overview 概述
You use textures to draw and process images in Metal.
A texture is a structured collection of texture elements, often called texels or pixels.
The exact configuration of these texture elements depends on the type of texture.
This sample uses a texture structured as a 2D array of elements, each of which contains color data, to hold an image.
您可以使用纹理在 Metal 中绘制和处理图像。
纹理是纹理元素的结构化集合,通常称为纹素或像素。这些纹理元素的确切配置取决于纹理的类型。此示例使用结构为 2D 元素数组的纹理来保存图像,每个元素都包含颜色数据。
The texture is drawn onto geometric primitives through a process called texture mapping. The fragment function generates colors for each fragment by sampling the texture.
纹理通过称为“纹理映射”的过程绘制到几何图元上。片段函数通过对纹理进行采样来为每个片段生成颜色。
Textures are managed by MTLTexture
objects.
A MTLTexture
object defines the texture’s format, including the size and layout of elements, the number of elements in the texture, and how those elements are organized.
Once created, a texture’s format and organization never change.
However, you can change the contents of the texture, either by rendering to it or copying data into it.
纹理由MTLTexture对象管理。
一个MTLTexture对象定义了纹理的格式,包括元素的大小和布局、纹理中元素的数量以及这些元素的组织方式。
一旦创建,纹理的格式和组织永远不会改变。但是,您可以通过渲染或将数据复制到其中来更改纹理的内容。
The Metal framework doesn’t provide an API to directly load image data from a file to a texture.
Metal itself only allocates texture resources and provides methods that copy data to and from the texture.
Metal apps rely on custom code or other frameworks, like MetalKit, Image I/O, UIKit, or AppKit, to handle image files. For example, you can use MTKTextureLoader to perform simple texture loading. This sample shows how to write a custom texture loader.
Metal 框架不提供 API 来直接将图像数据从文件加载到纹理。
Metal 本身只分配纹理资源,并提供将数据复制到纹理和从纹理复制数据的方法。
Metal 应用程序依赖自定义代码或其他框架(如 MetalKit、Image I/O、UIKit 或 AppKit)来处理图像文件。例如,您可以使用MTKTextureLoader来执行简单的纹理加载。此示例展示了如何编写自定义纹理加载器。
- Note: The Xcode project contains schemes for running the sample on macOS, iOS, or tvOS devices. The default scheme is macOS, which runs the sample as is on your Mac.
笔记: Xcode 项目包含在 macOS、iOS 或 tvOS 设备上运行示例的方案。默认方案是 macOS,它按原样在 Mac 上运行示例。
Load and Format Image Data 加载和格式化图像数据
You can create a texture or update its contents manually, a process that’s covered in the next few sections.
You might do this for multiple reasons:
您可以手动创建纹理或更新其内容,此过程将在接下来的几节中介绍。您可能出于多种原因这样做:
- You have image data stored in a custom format.您以自定义格式存储了图像数据。
- You have textures whose contents need to be generated at runtime.您有其内容需要在运行时生成的纹理。
- You are streaming texture data from a server or otherwise need to dynamically update a texture’s contents.您正在从服务器流式传输纹理数据,或者需要动态更新纹理的内容。
In the sample, the AAPLImage
class loads and parses image data from TGA files.
The class converts pixel data from the TGA file into a pixel format that Metal understands.
The sample uses the image’s metadata to create a new Metal texture and copies the pixel data into the texture.
在示例中,AAPLImage该类从 TGA 文件加载和解析图像数据。该类将 TGA 文件中的像素数据转换为 Metal 能够理解的像素格式。该示例使用图像的元数据创建新的 Metal 纹理并将像素数据复制到纹理中。
- Note: The
AAPLImage
clas