Version: Unity 6.0 (6000.0)
语言 : 中文
在 URP 中的渲染通道中使用纹理
在 URP 中的渲染通道之间传输纹理

在 URP 中的渲染图形系统中创建纹理

要在渲染图形系统中创建纹理,请使用 UniversalRenderer.CreateRenderGraphTexture API。

通用渲染管线 (URP) 优化渲染图时,如果最终帧不使用纹理,则可能无法创建纹理,从而减少渲染通道使用的内存和带宽。有关 URP 如何优化渲染图的更多信息,请参阅渲染图系统简介

有关在多个帧或多个摄像机上使用纹理的更多信息,例如在项目中导入的纹理资源,请参阅将纹理导入渲染图系统

创建纹理

要创建纹理,请在 ScriptableRenderPass 类的 RecordRenderGraph 方法中执行以下步骤:

  1. 创建具有所需纹理属性的 RenderTextureDescriptor 对象。
  2. 使用 UniversalRenderer.CreateRenderGraphTexture 方法来创建纹理并返回纹理句柄。

例如,以下命令会创建与屏幕大小相同的纹理。

RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
TextureHandle textureHandle = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);

然后,您可以在同一个自定义渲染通道中使用纹理

只有当前摄像机能够访问纹理。要在其他地方(例如从另一个摄像机或自定义渲染代码中)访问纹理,请改用导入纹理

渲染图系统管理使用 CreateRenderGraphTexture 创建的纹理的生命周期,因此在完成这些纹理后,无需手动释放它们使用的内存。

示例

以下可编程渲染器功能包含一个示例渲染通道,用于创建纹理并将其清除为黄色。有关将渲染通道添加到渲染管线的更多信息,请参阅使用可编程渲染器功能注入通道

使用帧调试器检查渲染通道添加的纹理。

using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering;

public class CreateYellowTextureFeature : ScriptableRendererFeature
{
    CreateYellowTexture customPass;

    public override void Create()
    {
        customPass = new CreateYellowTexture();
        customPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(customPass);
    }

    class CreateYellowTexture : ScriptableRenderPass
    {
        class PassData
        {
            internal TextureHandle cameraColorTexture;
        }

        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
        {
            using (var builder = renderGraph.AddRasterRenderPass<PassData>("Create yellow texture", out var passData))
            {
                // Create texture properties that match the screen size
                RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);

                // Create a temporary texture
                TextureHandle texture = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);

                // Set the texture as the render target
                builder.SetRenderAttachment(texture, 0, AccessFlags.Write);
    
                builder.AllowPassCulling(false);

                builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
            }
        }

        static void ExecutePass(PassData data, RasterGraphContext context)
        {          
            // Clear the render target to yellow
            context.cmd.ClearRenderTarget(true, true, Color.yellow);            
        }
    }

}

有关另一个示例,请参阅通用渲染管线 (URP) 包示例中名为 OutputTexture 的示例。

其他资源

在 URP 中的渲染通道中使用纹理
在 URP 中的渲染通道之间传输纹理
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961