【Unity】ShaderGraphで使用できる半透明オブジェクト含む背景テクスチャ_CameraTransparentTextureを作成、表示できるパスを追加する(RenderGraph版)
はじめに
これのRenderGraph版のやり方を紹介します
環境は Unity 6.0.0b12です
コード
using UnityEngine; using UnityEngine.Rendering.Universal; using UnityEngine.Rendering; using UnityEngine.Rendering.RenderGraphModule; using UnityEngine.Experimental.Rendering; public class TransparentRendererFeature : ScriptableRendererFeature { private CopyTransparentPass copyTransparentsPass = null;//パス public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingTransparents;//レンダリングタイミング public Downsampling downsampling;//解像度指定 /// <summary> /// パス生成 /// </summary> public override void Create() { copyTransparentsPass = new CopyTransparentPass(renderPassEvent, downsampling); } /// <summary> /// パス登録する /// </summary> /// <param name="renderer"></param> /// <param name="renderingData"></param> public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { //パスを実行できるようにする renderer.EnqueuePass(copyTransparentsPass); } protected override void Dispose(bool disposing) { copyTransparentsPass?.Dispose(); copyTransparentsPass = null; } } /// <summary> /// 半透明を含むカメラの描画結果をグローバルテクスチャに設定するためのパス /// </summary> public class CopyTransparentPass : ScriptableRenderPass { public CopyTransparentPass(RenderPassEvent passEvent, Downsampling downsampling) { renderPassEvent = passEvent; this.downsampling = downsampling; } private class PassData { public TextureHandle CameraColorHandle; } private readonly Vector4 scaleBias = new Vector4(1f, 1f, 0f, 0f); private readonly int textureID = Shader.PropertyToID("_CameraTransparentTexture"); const string ProfilerTag = "TestPass";//FrameDebuggerで表示される名前 private Downsampling downsampling; private RTHandle outputHandle; //コピー先 public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData) { var cameraData = frameData.Get<UniversalCameraData>(); var resourceData = frameData.Get<UniversalResourceData>(); var desc = cameraData.cameraTargetDescriptor; //解像度を下げる var down = downsampling switch { Downsampling._2xBilinear => 2, Downsampling._4xBilinear => 4, Downsampling._4xBox => 4, _ => 1 }; desc.width /= down; desc.height /= down; //depthとStencilは使用しないので無効にする これをしないと正常に描画がされない desc.depthStencilFormat = GraphicsFormat.None; RenderingUtils.ReAllocateHandleIfNeeded(ref outputHandle, desc, FilterMode.Bilinear, TextureWrapMode.Clamp, name: "ScreenCaptureHandle"); //コピー元 var source = resourceData.activeColorTexture; var destination = renderGraph.ImportTexture(outputHandle); using (var builder = renderGraph.AddRasterRenderPass<PassData>(ProfilerTag, out var passData)) { if (!source.IsValid() || !destination.IsValid()) return; passData.CameraColorHandle = source; builder.UseTexture(source); builder.SetRenderAttachment(destination, 0); builder.SetRenderAttachmentDepth(resourceData.activeDepthTexture); //グローバル変数の使用を許可 builder.AllowGlobalStateModification(true); //グローバル変数設定 builder.SetGlobalTextureAfterPass(destination, textureID); //描画 builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context)); } } private void ExecutePass(PassData data, RasterGraphContext context) { using (new ProfilingScope(context.cmd, profilingSampler)) { bool bilinear = downsampling == Downsampling._2xBilinear || downsampling == Downsampling._4xBilinear; //描画結果をコピーする Blitter.BlitTexture(context.cmd, data.CameraColorHandle, scaleBias, 0, bilinear); } } public void Dispose() { outputHandle?.Release(); } }