Version: Unity 6.0 (6000.0)
言語 : 日本語
URP の現在のフレームからデータを取得する
カメラ履歴にテクスチャを追加する

URP で前のフレームのデータを取得する

ユニバーサルレンダーパイプライン (URP) でカメラがレンダリングした前のフレームを取得するには、UniversalCameraData.historyManager API を使用します。これらのテクスチャは履歴テクスチャまたは履歴バッファと呼ばれることもあります。

フレームは GPU レンダーパイプラインの出力であるため、ポストプロセスエフェクトなど、GPU レンダリング後に発生する処理は含まれません。

スクリプタブルレンダーパス外から前のフレームを取得するには、スクリプトで前のフレームのデータを取得する を参照してください。

以下の手順に従ってください。

  1. RecordRenderGraph メソッドで、ContextContainer オブジェクトから UniversalCameraData オブジェクトを取得します。例:

    public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
    {
        UniversalCameraData cameraData = frameContext.Get<UniversalCameraData>();
    }    
    
  2. レンダリング履歴のカラーテクスチャまたは深度テクスチャへのアクセスをリクエストするには、RequestAccess API を使用します。例:

    // Request access to the color textures
    cameraData.historyManager.RequestAccess<RawColorHistory>();
    

    深度テクスチャへのアクセスをリクエストするには、代わりに RawDepthHistory を使用します。

  3. 前のテクスチャの 1 つを取得します。例:

    // Get the previous textures 
    RawColorHistory history = cameraData.historyManager.GetHistoryForRead<RawColorHistory>();
    
    // Get the first texture, which the camera rendered in the previous frame
    RTHandle historyTexture = history?.GetPreviousTexture(0);
    
  4. テクスチャをレンダーグラフシステムが使用できるハンドルに変換します。例:

    passData.historyTexture = renderGraph.ImportTexture(historyTexture);
    

その後、レンダーパスでテクスチャを読み取ることができます。

historyManager API の使用方法の詳細については、UniversalCameraData.historyManager を参照してください。

以下は、マテリアルを作成し、前のフレームをマテリアルのテクスチャとして使用するスクリプタブルレンダラー機能です。

この例を使用するには、以下の手順に従います。

  1. _BaseMap というテクスチャをサンプリングする URP シェーダーを作成します。例については、テクスチャの描画 を参照してください。
  2. シェーダーからマテリアルを作成します。
  3. RenderLastFrameInMaterial.cs という新しい C# スクリプトを作成し、以下のコードを貼り付け、ファイルを保存します。
  4. アクティブな URP レンダラーで、スクリプタブルレンダラー機能を追加 します。
  5. アクティブな URP レンダラーの Inspector ウィンドウで、ステップ 4 で追加したスクリプタブルレンダラー機能の Render Last Frame In Material セクションで、ステップ 2 で作成したマテリアルを Object Material フィールドに割り当てます。
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering.RenderGraphModule;
public class RenderLastFrameInMaterial : ScriptableRendererFeature
{
    public Material objectMaterial;
    CustomRenderPass renderLastFrame;

    public override void Create()
    {
        renderLastFrame = new CustomRenderPass();
        renderLastFrame.renderPassEvent = RenderPassEvent.BeforeRenderingOpaques;
    }

    public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
    {
        renderLastFrame.passMaterial = objectMaterial;
        renderer.EnqueuePass(renderLastFrame);
    }

    class CustomRenderPass : ScriptableRenderPass
    {
        public Material passMaterial;

        public class PassData
        {
            internal Material material;
            internal TextureHandle historyTexture;
        }

        public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer contextData)
        {
            UniversalCameraData cameraData = contextData.Get<UniversalCameraData>();

            // Return if the history manager isn't available
            // For example, there are no history textures during the first frame
            if (cameraData.historyManager == null) { return; }
  
            // Request access to the color and depth textures
            cameraData.historyManager.RequestAccess<RawColorHistory>();

            using (var builder = renderGraph.AddRasterRenderPass<PassData>("Get last frame", out var passData))
            {
                UniversalResourceData resourceData = contextData.Get<UniversalResourceData>();

                // Set the render graph to render to the active color texture
                builder.SetRenderAttachment(resourceData.activeColorTexture, 0, AccessFlags.Write);

                // Add the material to the pass data
                passData.material = passMaterial;
                
                // Get the color texture the camera rendered to in the previous frame
                RawColorHistory history = cameraData.historyManager.GetHistoryForRead<RawColorHistory>();
                RTHandle historyTexture = history?.GetPreviousTexture(0);
                passData.historyTexture = renderGraph.ImportTexture(historyTexture);

                builder.SetRenderFunc(static (PassData data, RasterGraphContext context) =>
                {
                    // Set the material to use the texture
                    data.material.SetTexture("_BaseMap", data.historyTexture);
                });
            }
        }
    }
}

スクリプトで前のフレームのデータを取得する

スクリプトで前のフレームのデータ、例えば MonoBehaviour を取得するには、以下を行います。

  1. Scriptable Render Pipeline (SRP) Core RequestAccess API を使用してテクスチャをリクエストします。
  2. UniversalAdditionalCameraData.history API を使用してデータを取得します。

Unity がフレームのレンダリングを最初に終了するようにするには、LateUpdate メソッドで UniversalAdditionalCameraData.history API を使用します。

詳細については、Scriptable Render Pipeline (SRP) Core パッケージの以下を参照してください。

URP の現在のフレームからデータを取得する
カメラ履歴にテクスチャを追加する
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961