您可以使用渲染图的 AddUnsafePass API 在渲染图系统渲染通道中使用兼容模式 API(例如 SetRenderTarget)。
如果您使用 AddUnsafePass API,则以下内容适用
RecordRenderGraph 方法中使用 SetRenderAttachment 方法。请改用 SetRenderFunc 方法中的 SetRenderTarget。要创建不安全的渲染通道,请按照以下步骤操作
在您的 RecordRenderGraph 方法中,使用 AddUnsafePass 方法而不是 AddRasterRenderPass 方法。
例如
using (var builder = renderGraph.AddUnsafePass<PassData>("My unsafe render pass", out var passData))
调用 SetRenderFunc 方法时,请使用 UnsafeGraphContext 类型而不是 RasterGraphContext。
例如
builder.SetRenderFunc(
    (PassData passData, UnsafeGraphContext context) => ExecutePass(passData, context)
);
如果您的渲染通道写入纹理,则必须将纹理作为字段添加到您的通道数据类中。
例如
private class PassData
{
    internal TextureHandle textureToWriteTo;
}
如果您的渲染通道写入纹理,则还必须使用 UseTexture 方法将纹理设置为可写。
例如
builder.UseTexture(passData.textureToWriteTo, AccessFlags.Write);
您现在可以在 SetRenderFunc 方法中使用兼容模式 API。
以下示例使用兼容模式 SetRenderTarget API 将渲染目标设置为渲染通道期间的活动颜色缓冲区,然后使用其表面法线作为颜色绘制对象。
using UnityEngine;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class DrawNormalsToActiveColorTexture : ScriptableRendererFeature
{
    DrawNormalsPass unsafePass;
    public override void Create()
    {
        unsafePass = new DrawNormalsPass();
        unsafePass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
    }
    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(unsafePass);
    }
    class DrawNormalsPass : ScriptableRenderPass
    {
        private class PassData
        {
            internal TextureHandle activeColorBuffer;
            internal TextureHandle cameraNormalsTexture;
        }
        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
        {
            using (var builder = renderGraph.AddUnsafePass<PassData>("Draw normals", out var passData))
            {
                // Make sure URP generates the normals texture
                ConfigureInput(ScriptableRenderPassInput.Normal);
                // Get the frame data
                UniversalResourceData resourceData = frameContext.Get<UniversalResourceData>();
                // Add the active color buffer to our pass data, and set it as writeable 
                passData.activeColorBuffer = resourceData.activeColorTexture;
                builder.UseTexture(passData.activeColorBuffer, AccessFlags.Write);                
                // Add the camera normals texture to our pass data 
                passData.cameraNormalsTexture = resourceData.cameraNormalsTexture;
                builder.UseTexture(passData.cameraNormalsTexture);
                builder.AllowPassCulling(false);
                builder.SetRenderFunc((PassData data, UnsafeGraphContext context) => ExecutePass(data, context));
            }
        }
        static void ExecutePass(PassData passData, UnsafeGraphContext context)
        {
            // Create a command buffer for a list of rendering methods
            CommandBuffer unsafeCommandBuffer = CommandBufferHelpers.GetNativeCommandBuffer(context.cmd);
            // Add a command to set the render target to the active color buffer so URP draws to it
            context.cmd.SetRenderTarget(passData.activeColorBuffer);
            // Add a command to copy the camera normals texture to the render target
            Blitter.BlitTexture(unsafeCommandBuffer, passData.cameraNormalsTexture, new Vector4(1, 1, 0, 0), 0, false);
        }
    }
}
有关另一个示例,请参阅通用渲染管线 (URP) 软件包示例中名为 UnsafePass 的示例。