Draw objects in the render graph system in URP
Draw objects in the render graph system using the RendererList API.
阅读时间4 分钟最后更新于 1 个月前
To draw objects in a custom render pass that uses the render graph system, use the API to create a list of objects to draw.
RendererListHandleCreate a list of objects to draw
Follow these steps:
-
In yourclass, in the class you use for pass data, create a
ScriptableRenderPassfield.RendererListHandleFor example:class PassData{ public RendererListHandle objectsToDraw;} -
Create a RendererListParams object that contains the objects to draw, drawing settings, and culling data. For more information about theobject, refer to Creating a simple render loop in a custom render pipeline.
RenderListParamsFor a detailed example, refer to Example. -
In themethod, use the
RecordRenderGraphAPI to convert theCreateRendererListobject to a handle that the render graph system can use.RendererListParamsFor example:RendererListHandle rendererListHandle = renderGraph.CreateRendererList(rendererListParameters); -
Set thefield in the pass data.
RendererListHandleFor example:passData.objectsToDraw = rendererListHandle;
Draw the objects
After you set a in the pass data, you can draw the objects in the list.
RendererListHandleFollow these steps:
-
In themethod, tell the render graph system to use the list of objects, using the
RecordRenderGraphAPI.UseRendererListFor example:builder.UseRendererList(passData.rendererListHandle); -
Set the texture to draw the objects onto. Set both the color texture and the depth texture so URP renders the objects correctly.For example, the following tells URP to draw to the color texture and depth texture of the active camera texture.UniversalResourceData frameData = frameContext.Get<UniversalResourceData>();builder.SetRenderAttachment(frameData.activeColorTexture, 0);builder.SetRenderAttachmentDepth(frameData.activeDepthTexture, AccessFlags.Write);
-
In yourmethod, draw the renderers using the
SetRenderFuncAPI.DrawRendererListFor example:context.cmd.DrawRendererList(passData.rendererListHandle);
Example
The following Scriptable Renderer Feature redraws the objects in the scene that have their tag set to , using an override material.
LightmodeUniversalForwardAfter you add this Scriptable Reader Feature to the renderer, set the Material To Use parameter to any material.
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; } 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(static (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); } } }
For another example, refer to the example called RendererList in the render graph system URP package samples.