レンダーグラフシステムでテクスチャを作成するには、UniversalRenderer.CreateRenderGraphTexture API を使用します。
ユニバーサルレンダーパイプライン (URP) がレンダーグラフを最適化する際に、最終フレームでテクスチャを使用しない場合、レンダーパスが使用するメモリと帯域幅を削減するために、テクスチャが作成されないことがあります。URP がレンダーグラフを最適化する方法の詳細については、レンダーグラフシステムの概要を参照してください。
プロジェクトにインポートしたテクスチャアセットなど、複数のフレームまたは複数のカメラでテクスチャを使用する方法の詳細については、レンダーグラフシステムにテクスチャをインポートするを参照してください。
テクスチャを作成するには、ScriptableRenderPass クラスの RecordRenderGraph メソッドで、次のステップに従います。
RenderTextureDescriptor オブジェクトを作成します。UniversalRenderer.CreateRenderGraphTexture メソッドを使用してテクスチャを作成し、テクスチャハンドルを返します。例えば、以下の場合は画面と同じサイズのテクスチャが作成されます。
RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
TextureHandle textureHandle = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
その後、同じカスタムレンダーパス内でテクスチャを使用できます。
現在のカメラのみがテクスチャにアクセスできます。別の場所、例えば別のカメラから、もしくはカスタムレンダリングコードでテクスチャにアクセスするには、代わりにテクスチャをインポートします。
レンダーグラフシステムは、CreateRenderGraphTexture で作成されたテクスチャの生存期間を管理します。そのため、テクスチャを使い終わったときに、テクスチャが使用しているメモリを手動でリリースする必要はありません。
以下のスクリプタブルレンダラー機能には、テクスチャを作成して黄色でクリアするレンダーパスの例が含まれています。レンダーパイプラインにレンダーパスを追加する方法の詳細については、スクリプタブルレンダラー機能を使用したパスの挿入を参照してください。
フレームデバッガーを使用して、レンダーパスが追加するテクスチャを確認します。
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering;
public class CreateYellowTextureFeature : ScriptableRendererFeature
{
CreateYellowTexture customPass;
public override void Create()
{
customPass = new CreateYellowTexture();
customPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
renderer.EnqueuePass(customPass);
}
class CreateYellowTexture : ScriptableRenderPass
{
class PassData
{
internal TextureHandle cameraColorTexture;
}
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
{
using (var builder = renderGraph.AddRasterRenderPass<PassData>("Create yellow texture", out var passData))
{
// Create texture properties that match the screen size
RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
// Create a temporary texture
TextureHandle texture = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
// Set the texture as the render target
builder.SetRenderAttachment(texture, 0, AccessFlags.Write);
builder.AllowPassCulling(false);
builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
}
}
static void ExecutePass(PassData data, RasterGraphContext context)
{
// Clear the render target to yellow
context.cmd.ClearRenderTarget(true, true, Color.yellow);
}
}
}
別の例については、ユニバーサルレンダーパイプライン (URP) パッケージサンプルで OutputTexture という例を参照してください。