ユニバーサルレンダーパイプライン (URP) でカメラがレンダリングした前のフレームを取得するには、UniversalCameraData.historyManager API を使用します。これらのテクスチャは履歴テクスチャまたは履歴バッファと呼ばれることもあります。
フレームは GPU レンダーパイプラインの出力であるため、ポストプロセスエフェクトなど、GPU レンダリング後に発生する処理は含まれません。
スクリプタブルレンダーパス外から前のフレームを取得するには、スクリプトで前のフレームのデータを取得する を参照してください。
以下の手順に従ってください。
RecordRenderGraph メソッドで、ContextContainer オブジェクトから UniversalCameraData オブジェクトを取得します。例:
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
{
UniversalCameraData cameraData = frameContext.Get<UniversalCameraData>();
}
レンダリング履歴のカラーテクスチャまたは深度テクスチャへのアクセスをリクエストするには、RequestAccess API を使用します。例:
// Request access to the color textures
cameraData.historyManager.RequestAccess<RawColorHistory>();
深度テクスチャへのアクセスをリクエストするには、代わりに RawDepthHistory を使用します。
前のテクスチャの 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);
テクスチャをレンダーグラフシステムが使用できるハンドルに変換します。例:
passData.historyTexture = renderGraph.ImportTexture(historyTexture);
その後、レンダーパスでテクスチャを読み取ることができます。
historyManager API の使用方法の詳細については、UniversalCameraData.historyManager を参照してください。
以下は、マテリアルを作成し、前のフレームをマテリアルのテクスチャとして使用するスクリプタブルレンダラー機能です。
この例を使用するには、以下の手順に従います。
_BaseMap というテクスチャをサンプリングする URP シェーダーを作成します。例については、テクスチャの描画 を参照してください。RenderLastFrameInMaterial.cs という新しい C# スクリプトを作成し、以下のコードを貼り付け、ファイルを保存します。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 を取得するには、以下を行います。
RequestAccess API を使用してテクスチャをリクエストします。UniversalAdditionalCameraData.history API を使用してデータを取得します。Unity がフレームのレンダリングを最初に終了するようにするには、LateUpdate メソッドで UniversalAdditionalCameraData.history API を使用します。
詳細については、Scriptable Render Pipeline (SRP) Core パッケージの以下を参照してください。