版本:Unity 6 (6000.0)
语言:English
URP 的帧数据纹理参考
URP 中渲染图系统的计算着色器

在 URP 的渲染图系统中绘制对象

要在使用渲染图系统的自定义渲染通道中绘制对象,请使用RendererListHandle API 创建要绘制的对象列表。

创建要绘制的对象列表

请按照以下步骤操作

  1. 在您的ScriptableRenderPass类中,在您用于传递数据的类中,创建一个RendererListHandle字段。

    例如

    private class PassData
    {
        public RendererListHandle objectsToDraw;
    }
    
  2. 创建一个包含要绘制的对象、绘制设置和剔除数据的RendererListParams对象。有关RenderListParams对象的更多信息,请参阅在自定义渲染管线中创建简单的渲染循环

    有关详细示例,请参阅示例

  3. RecordRenderGraph方法中,使用CreateRendererList APIRendererListParams对象转换为渲染图系统可以使用的句柄。

    例如

    RenderListHandle rendererListHandle = renderGraph.CreateRendererList(rendererListParameters);
    
  4. 设置传递数据中的RendererListHandle字段。

    例如

    passData.objectsToDraw = rendererListHandle;
    

绘制对象

在您设置了传递数据中的RendererListHandle之后,您可以在列表中绘制对象。

请按照以下步骤操作

  1. RecordRenderGraph方法中,使用UseRendererList API 告诉渲染图系统使用对象列表。

    例如

    builder.UseRendererList(passData.rendererListHandle);
    
  2. 设置要绘制对象的纹理。设置颜色纹理和深度纹理,以便 URP 正确渲染对象。

    例如,以下内容告诉 URP 绘制到活动摄像机一个组件,用于创建场景中特定视点的图像。输出要么绘制到屏幕上,要么捕获为纹理。 更多信息
    参见词汇表
    纹理的颜色纹理和深度纹理。

    UniversalResourceData frameData = frameContext.Get<UniversalResourceData>();
    builder.SetRenderAttachment(frameData.activeColorTexture, 0);
    builder.SetRenderAttachmentDepth(frameData.activeDepthTexture, AccessFlags.Write);
    
  3. 在您的SetRenderFunc方法中,使用DrawRendererList API 绘制渲染器。

    例如

    context.cmd.DrawRendererList(passData.rendererListHandle);
    

示例

以下 Scriptable Renderer Feature 使用覆盖材质重新绘制场景中其Lightmode标记设置为UniversalForward的对象。

将此 Scriptable Reader Feature 添加到渲染器后,将**要使用的材质**参数设置为任何材质。

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
 
public class DrawObjectsWithOverrideMaterial : ScriptableRendererFeature
{

    DrawObjectsPass drawObjectsPass;
    public Material overrideMaterial;
 
    public override void Create()
    {
        // Create the render pass that draws the objects, and pass in the override material
        drawObjectsPass = new DrawObjectsPass(overrideMaterial);

        // Insert render passes after URP's post-processing render pass
        drawObjectsPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
    }
 
    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        // Add the render pass to the URP rendering loop
        renderer.EnqueuePass(drawObjectsPass);
    }

    class DrawObjectsPass : ScriptableRenderPass
    {
        private Material materialToUse;

        public DrawObjectsPass(Material overrideMaterial)
        {
            // Set the pass's local copy of the override material 
            materialToUse = overrideMaterial;
        }
       
        private class PassData
        {
            // Create a field to store the list of objects to draw
            public RendererListHandle rendererListHandle;
        }
 
        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
        {
            using (var builder = renderGraph.AddRasterRenderPass<PassData>("Redraw objects", out var passData))
            {
                // Get the data needed to create the list of objects to draw
                UniversalRenderingData renderingData = frameContext.Get<UniversalRenderingData>();
                UniversalCameraData cameraData = frameContext.Get<UniversalCameraData>();
                UniversalLightData lightData = frameContext.Get<UniversalLightData>();
                SortingCriteria sortFlags = cameraData.defaultOpaqueSortFlags;
                RenderQueueRange renderQueueRange = RenderQueueRange.opaque;
                FilteringSettings filterSettings = new FilteringSettings(renderQueueRange, ~0);

                // Redraw only objects that have their LightMode tag set to UniversalForward 
                ShaderTagId shadersToOverride = new ShaderTagId("UniversalForward");

                // Create drawing settings
                DrawingSettings drawSettings = RenderingUtils.CreateDrawingSettings(shadersToOverride, renderingData, cameraData, lightData, sortFlags);

                // Add the override material to the drawing settings
                drawSettings.overrideMaterial = materialToUse;

                // Create the list of objects to draw
                var rendererListParameters = new RendererListParams(renderingData.cullResults, drawSettings, filterSettings);

                // Convert the list to a list handle that the render graph system can use
                passData.rendererListHandle = renderGraph.CreateRendererList(rendererListParameters);
                
                // Set the render target as the color and depth textures of the active camera texture
                UniversalResourceData resourceData = frameContext.Get<UniversalResourceData>();
                builder.UseRendererList(passData.rendererListHandle);
                builder.SetRenderAttachment(resourceData.activeColorTexture, 0);
                builder.SetRenderAttachmentDepth(resourceData.activeDepthTexture, AccessFlags.Write);

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

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

            // Draw the objects in the list
            context.cmd.DrawRendererList(data.rendererListHandle);
        }

    }
 
}

有关另一个示例,请参阅渲染图系统 URP 包示例中名为**RendererList**的示例。

其他资源

URP 的帧数据纹理参考
URP 中渲染图系统的计算着色器