Version: Unity 6.0 (6000.0)
言語 : 日本語
URP のレンダーパスでテクスチャを使用する
URP でのレンダーパス間のテクスチャ転送

URP のレンダーグラフシステムでテクスチャを作成する

レンダーグラフシステムでテクスチャを作成するには、UniversalRenderer.CreateRenderGraphTexture API を使用します。

ユニバーサルレンダーパイプライン (URP) がレンダーグラフを最適化する際に、最終フレームでテクスチャを使用しない場合、レンダーパスが使用するメモリと帯域幅を削減するために、テクスチャが作成されないことがあります。URP がレンダーグラフを最適化する方法の詳細については、レンダーグラフシステムの概要を参照してください。

プロジェクトにインポートしたテクスチャアセットなど、複数のフレームまたは複数のカメラでテクスチャを使用する方法の詳細については、レンダーグラフシステムにテクスチャをインポートするを参照してください。

テクスチャの作成

テクスチャを作成するには、ScriptableRenderPass クラスの RecordRenderGraph メソッドで、次のステップに従います。

  1. 必要なテクスチャプロパティを持つ RenderTextureDescriptor オブジェクトを作成します。
  2. 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 という例を参照してください。

追加リソース

URP のレンダーパスでテクスチャを使用する
URP でのレンダーパス間のテクスチャ転送
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961