您可以使用渲染图系统 API 将纹理设置为自定义渲染通道的输入或输出,以便对其进行读取或写入。
不能同时读取和写入渲染通道中的同一纹理。有关更多信息,请参阅在渲染通道期间更改渲染目标。
要将纹理设置为自定义渲染通道的输入,请遵循以下步骤:
在 RecordRenderGraph 方法中,将纹理句柄字段添加到您的通道使用的数据。
例如:
// Create the data your pass uses
public class MyPassData
{
// Add a texture handle
public TextureHandle textureToUse;
}
将纹理句柄设置为您要使用的纹理。
例如:
// Add the texture handle to the data
RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
TextureHandle textureHandle = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
passData.textureToUse = textureHandle;
调用 UseTexture 方法将纹理设置为输入。
例如:
builder.UseTexture(passData.textureToUse, AccessFlags.Read);
在 SetRenderFunc 方法中,现在可以使用通道数据中的 TextureHandle 对象作为 API(如 Blitter.BlitTexture)的输入。
要将纹理设置为 Blit 等命令的输出,请在 RecordRenderGraph 方法中使用 SetRenderAttachment 方法。默认情况下,SetRenderAttachment 方法将纹理设置为只读。
例如,以下命令会创建一个临时纹理并将其设置为渲染通道的渲染目标:
// Create texture properties
RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
// Create the texture
TextureHandle targetTexture = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
// Set the texture as the render target
// The second parameter is the index the shader uses to access the texture
builder.SetRenderAttachment(targetTexture, 0);
在 SetRenderFunc 方法中,现在可以使用 API(例如 Blitter.BlitTexture)写入纹理。
无需将纹理添加到通道数据。渲染图系统在执行渲染通道之前,会自动为您设置纹理。
如果需要将对象绘制到渲染目标,请参阅绘制渲染通道对象以了解更多信息。
在渲染图系统渲染通道期间,无法更改 URP 写入的目标纹理。
您可以改用以下任一方式:
builder.SetRenderAttachment 更改渲染目标。UnsafePass API,以便可以在 SetRenderFunc 方法中使用 SetRenderTarget API。请参阅在渲染图渲染通道中使用兼容模式 API,以了解更多信息和获取示例。您可以使用这些方法来读取和写入同一个纹理,方法是先从纹理复制到您创建的临时纹理,然后再复制回来。
如果在具有不同属性的几个纹理之间执行__ blit__“位块传输 (Bit Block Transfer)”的简写。blit 操作是将数据块从内存中的一个位置传输到另一个位置的过程。
See in Glossary 操作,渲染可能会很慢,因为 URP 无法将 blit 合并到单个原生渲染通道中。请改用 AddUnSafePass API 和 SetRenderTarget() 方法。
请参阅以下内容: