Creating and Sampling Textures 创建和采样纹理

本文介绍了在 Metal 中如何创建和采样纹理,包括加载图像数据、创建纹理、映射到几何图元、计算颜色等步骤。通过自定义纹理加载器,将 TGA 文件的图像数据转换为 Metal 理解的像素格式,并使用 MTLTexture 对象进行管理。此外,还探讨了纹理坐标、采样和过滤在渲染过程中的作用。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值