Version: Unity 6.0 (6000.0)
言語 : 日本語
Create a texture as a global texture in URP
URP のレンダーグラフシステムのフレームデータ

URP でレンダーグラフシステムにテクスチャをインポートする

レンダーパス内で レンダーグラフシステムを使用してテクスチャを作成する と、レンダーグラフシステムがテクスチャの作成と破棄を処理します。この処理により、次のフレームにそのテクスチャが存在しない場合や他のカメラがテクスチャを使用できない場合があります。

複数のフレームおよびカメラをまたいでテクスチャを使用できるようにするには、ImportTexture API を使用してレンダーグラフシステムにインポートします。

レンダーグラフシステムの外部で作成されたテクスチャを使用する場合、テクスチャをインポートできます。例えば、テクスチャアセット など、プロジェクト内のテクスチャを指すレンダーテクスチャを作成し、レンダーパスへの入力として使用できます。

レンダーグラフシステムは、インポートしたテクスチャの生存期間を管理しません。そのため、以下の内容が適用されます。

  • 使用が終わったら インポートしたレンダーテクスチャを破棄 して、使用メモリを解放する必要があります。
  • URP は、インポートしたテクスチャを使用するレンダーパスを削除できません。その結果、レンダリングが遅くなる場合があります。

RTHandle API の詳細については、RTHandle システムの使用 を参照してください。

テクスチャのインポート

テクスチャをインポートするには、ScriptableRenderPass クラスの RecordRenderGraph メソッドで、以下のステップに従います。

  1. RTHandle API を使用してレンダーテクスチャハンドルを作成します。

    例:

    private RTHandle renderTextureHandle;
    
  2. 必要なテクスチャプロパティを持つ RenderTextureDescriptor オブジェクトを作成します。

    例:

    RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
    
  3. ReAllocateIfNeeded メソッドを使用し、レンダーテクスチャを作成してレンダーテクスチャハンドルにアタッチします。このメソッドは、レンダーテクスチャハンドルが null である場合、またはレンダーテクスチャがレンダーテクスチャ記述子と異なるプロパティを持つ場合にのみレンダーテクスチャを作成します。

    例:

    RenderingUtils.ReAllocateIfNeeded(ref renderTextureHandle, textureProperties, FilterMode.Bilinear, TextureWrapMode.Clamp, name: "My render texture" );
    
  4. テクスチャをインポートして、RTHandle オブジェクトをレンダーグラフシステムが使用できる TextureHandle オブジェクトに変換します。

    例:

    TextureHandle texture = renderGraph.ImportTexture(renderTextureHandle);
    

その後、TextureHandle オブジェクトを使用して、レンダーテクスチャの読み取り/書き込み を行うことができます。

プロジェクトからテクスチャをインポート

プロジェクトからテクスチャ (マテリアルに設定されているインポートされたテクスチャなど) をインポートするには、以下のステップに従います。

  1. RTHandles.Alloc API を使用して、外部テクスチャからレンダーテクスチャハンドルを作成します。

    例:

    RTHandle renderTexture = RTHandles.Alloc(texture);
    
  2. テクスチャをインポートして、RTHandle オブジェクトをレンダーグラフシステムが使用できる TextureHandle オブジェクトに変換します。

    例:

    TextureHandle textureHandle = renderGraph.ImportTexture(renderTexture);
    

その後、TextureHandle オブジェクトを使用して、レンダーテクスチャの読み取り/書き込み を行うことができます。

レンダーテクスチャの破棄

Dispose メソッドを使用して、レンダーパス終了時にレンダーテクスチャが使用するメモリを解放する必要があります。

public void Dispose()
{
    renderTexture.Release();
}

以下の Scriptable Renderer Feature には、テクスチャアセットを一時的なテクスチャにコピーするレンダーパスの例が含まれています。この例を使用するには、以下のステップに従います。

  1. この Scriptable Renderer Feature を URP アセットに追加します。Scriptable Renderer Feature を使用したパスの挿入 を参照してください。
  2. URP アセットの Inspector ウィンドウで、Texture To Use プロパティにテクスチャを追加します。
  3. Frame Debugger を使用して、レンダーパスが追加するテクスチャを確認します。
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering;

public class BlitFromExternalTexture : ScriptableRendererFeature
{
    // The texture to use as input 
    public Texture2D textureToUse;

    BlitFromTexture customPass;

    public override void Create()
    {
        // Create an instance of the render pass, and pass in the input texture 
        customPass = new BlitFromTexture(textureToUse);

        customPass.renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderer.EnqueuePass(customPass);
    }

    class BlitFromTexture : ScriptableRenderPass
    {
        class PassData
        {
            internal TextureHandle textureToRead;
        }

        private Texture2D texturePassedIn;

        public BlitFromTexture(Texture2D textureIn)
        {
            // In the render pass's constructor, set the input texture
            texturePassedIn = textureIn;
        }

        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
        {
            using (var builder = renderGraph.AddRasterRenderPass<PassData>("Copy texture", out var passData))
            {
                // Create a temporary texture and set it as the render target
                RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
                TextureHandle texture = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
                builder.SetRenderAttachment(texture, 0, AccessFlags.Write);

                // Create a render texture from the input texture
                RTHandle rtHandle = RTHandles.Alloc(texturePassedIn);

                // Create a texture handle that the shader graph system can use
                TextureHandle textureToRead = renderGraph.ImportTexture(rtHandle);

                // Add the texture to the pass data
                passData.textureToRead = textureToRead;

                // Set the texture as readable
                builder.UseTexture(passData.textureToRead, AccessFlags.Read);

                builder.AllowPassCulling(false);

                builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
            }
        }

        static void ExecutePass(PassData data, RasterGraphContext context)
        {          
            // Copy the imported texture to the render target
            Blitter.BlitTexture(context.cmd, data.textureToRead, new Vector4(0.8f,0.6f,0,0), 0, false);
        }
    }
}

追加リソース

Create a texture as a global texture in URP
URP のレンダーグラフシステムのフレームデータ
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961