# Unity 6 URP RenderGraph Custom Screen-Space Pass

> Custom Unity 6 URP 17.6 ScriptableRendererFeature overriding RecordRenderGraph with transient TextureHandle creation and AddBlitPass.

- **Canonical page:** https://gamecrew.ai/techniques/unity/urp-rendergraph-custom-pass
- **Techniques hub:** https://gamecrew.ai/techniques
- **Engine:** Unity 6
- **Category:** rendering
- **Tags:** `unity6`, `urp`, `rendergraph`, `postprocessing`, `blit`, `pass`, `shaders`
- **Verification:** Compile-verified pattern

## When to Use & Field Best Practices

Unity 6 removed legacy `Execute(ScriptableRenderContext, ref RenderingData)` execution paths when RenderGraph Compatibility Mode is disabled. Writing custom screen-space grading, distance-fog, or edge-accent effects against URP 17.6's native `RecordRenderGraph` keeps memory aliasing intact and avoids per-frame render-target allocations:

1. **Override `RecordRenderGraph(RenderGraph, ContextContainer)` Only**: Pull `UniversalResourceData` and `UniversalCameraData` directly from `frameData`; never access deprecated `ScriptableRenderer.cameraColorTargetHandle`.
2. **Guard Scene/Preview & Backbuffer Targets**: Skip when `resourcesData.isActiveTargetBackBuffer`, `!source.IsValid()`, or unsupported preview cameras so editor/capture passes never throw texture-access errors.
3. **Declare Transient Targets via `renderGraph.CreateTexture(desc)`**: Clone `renderGraph.GetTextureDesc(source)` with `clearBuffer = false`, run `AddBlitPass` from `source -> dest` with your shader material, and copy back cleanly so URP manages lifetime and memory aliasing across passes.

## Mathematical & Algorithmic Core

- **Two-Stage Ping-Pong Blit**:
  $$T_{\text{tmp}} \leftarrow \mathcal{F}_{\text{mat}}(T_{\text{color}}), \qquad T_{\text{color}} \leftarrow T_{\text{tmp}}$$
- **Transient Allocation Cost**: URP RenderGraph reuses physical GPU textures across non-overlapping pass intervals, keeping transient screen-space passes at zero net VRAM growth across frames.

## Engine Implementation

### URP 17.6 `ScriptableRendererFeature` & Pass (`Assets/Game/Runtime/Rendering/TintBlitRendererFeature.cs`)

```csharp
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering.RenderGraphModule.Util;

namespace __NS__
{
    /// <summary>
    /// URP 17.6 RenderGraph fullscreen pass that applies a configurable material cleanly via transient textures.
    /// </summary>
    public sealed class TintBlitRendererFeature : ScriptableRendererFeature
    {
        [System.Serializable]
        public sealed class Settings
        {
            public RenderPassEvent passEvent = RenderPassEvent.BeforeRenderingPostProcessing;
            public Material material;
            public int passIndex = 0;
        }

        [SerializeField] Settings settings = new Settings();
        TintBlitPass pass;

        public override void Create()
        {
            pass = new TintBlitPass(settings)
            {
                renderPassEvent = settings.passEvent,
            };
        }

        public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
        {
            if (settings.material == null) return;
            var camType = renderingData.cameraData.cameraType;
            if (camType == CameraType.Preview || camType == CameraType.Reflection) return;
            renderer.EnqueuePass(pass);
        }

        sealed class TintBlitPass : ScriptableRenderPass
        {
            readonly Settings settings;

            public TintBlitPass(Settings settings)
            {
                this.settings = settings;
                requiresIntermediateTexture = true;
            }

            public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
            {
                if (settings.material == null) return;

                var resources = frameData.Get<UniversalResourceData>();
                var cameraData = frameData.Get<UniversalCameraData>();
                if (resources.isActiveTargetBackBuffer || cameraData.isPreviewCamera) return;

                TextureHandle source = resources.activeColorTexture;
                if (!source.IsValid()) return;

                TextureDesc desc = renderGraph.GetTextureDesc(source);
                desc.name = "_CustomScreenTemp";
                desc.clearBuffer = false;
                TextureHandle temp = renderGraph.CreateTexture(desc);

                var blitParams = new RenderGraphUtils.BlitMaterialParameters(
                    source,
                    temp,
                    settings.material,
                    settings.passIndex
                );
                renderGraph.AddBlitPass(blitParams, passName: "CustomScreenTint");
                renderGraph.AddCopyPass(temp, source, passName: "CustomScreenResolve");
            }
        }
    }
}
```

## WebGL & Mobile Guardrails

- **Set `requiresIntermediateTexture = true`**: Ensures URP provides a readable `activeColorTexture` rather than trying to read and write the swapchain backbuffer in a single WebGL frame.
- **Avoid MSAA Resolve Surprise**: Set `desc.msaaSamples = MSAASamples.None` on fullscreen post effects if your shader reads `_BlitTexture` without `TEXTURE2D_X_MSAA`.

## Verification Checklist

1. Assign the `TintBlitRendererFeature` on the URP Renderer asset and verify WebGL builds compile and render without RenderGraph attachment errors.
2. Confirm `RecordRenderGraph` skips cleanly when `resources.isActiveTargetBackBuffer` is true during overlay camera draws.

## Official references

- Unity 6 URP 17.6 RenderGraph Documentation
- ScriptableRendererFeature Best Practices
