렌더 그래프 시스템 렌더 패스에서 렌더 그래프 AddUnsafePass API를 사용하여 SetRenderTarget과 같은 Compatibility Mode API를 사용할 수 있습니다.
AddUnsafePass API를 사용하는 경우 다음이 적용됩니다.
RecordRenderGraph 메서드에서 SetRenderAttachment 메서드를 사용할 수 없습니다. 대신 SetRenderFunc 메서드에서 SetRenderTarget를 사용하십시오.안전하지 않은 렌더 패스를 생성하려면 다음 단계를 따르십시오.
RecordRenderGraph 메서드에서 AddRasterRenderPass 메서드 대신 AddUnsafePass 메서드를 사용합니다.
예시:
using (var builder = renderGraph.AddUnsafePass<PassData>("My unsafe render pass", out var passData))
SetRenderFunc 메서드를 호출할 때 RasterGraphContext가 아닌 UnsafeGraphContext 유형을 사용합니다.
예시:
builder.SetRenderFunc(
(PassData passData, UnsafeGraphContext context) => ExecutePass(passData, context)
);
렌더 패스가 텍스처에 쓰기를 수행하는 경우, 해당 텍스처를 패스 데이터 클래스에 필드로 추가해야 합니다.
예시:
private class PassData
{
internal TextureHandle textureToWriteTo;
}
렌더 패스가 텍스처에 쓰기를 수행하는 경우, UseTexture 메서드를 사용하여 텍스처를 쓰기 가능으로 설정해야 합니다.
예시:
builder.UseTexture(passData.textureToWriteTo, AccessFlags.Write);
이제 SetRenderFunc 메서드에서 Compatibility Mode API를 사용할 수 있습니다.
다음 예시에서는 Compatibility Mode 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라는 예시를 참조하십시오.