Version: Unity 6 (6000.0)
Language : English
Get data from the current frame in URP
Add textures to the camera history

Get data from previous frames in URP

To fetch the previous frames that the cameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary
rendered in the Universal Render PipelineA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary
(URP), use the UniversalCameraData.historyManager API. These textures are sometimes called history textures or history buffers.

The frames are the output of the GPU rendering pipeline, so they don’t include any processing that occurs after GPU rendering, such as post-processingA process that improves product visuals by applying filters and effects before the image appears on screen. You can use post-processing effects to simulate physical camera and film properties, for example Bloom and Depth of Field. More info post processing, postprocessing, postprocess
See in Glossary
effects.

To fetch previous frames from outside a scriptable render pass, refer to Get data from previous frames in a script.

Follow these steps:

  1. In the RecordRenderGraph method, get the UniversalCameraData object from the ContextContainer object. For example:

    public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
    {
        UniversalCameraData cameraData = frameContext.Get<UniversalCameraData>();
    }    
    
  2. To request access to either the color textures or the depth textures in the rendering history, use the RequestAccess API. For example:

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

    Use RawDepthHistory instead to request access to the depth textures.

  3. Get one of the previous textures. For example:

    // 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. Convert the texture into a handle the render graph system can use. For example:

    passData.historyTexture = renderGraph.ImportTexture(historyTexture);
    

You can then read the texture in the render pass.

For more information about using the historyManager API, refer to UniversalCameraData.historyManager.

Example

The following is a Scriptable Renderer Feature that creates a material and uses the previous frame as the material’s texture.

To use the example, follow these steps:

  1. Create a URP shaderA program that runs on the GPU. More info
    See in Glossary
    that samples a texture called _BaseMap. For an example, refer to Drawing a texture.
  2. Create a material from the shader.
  3. Create a new C# script called RenderLastFrameInMaterial.cs, paste the following code into it, and save the file.
  4. In the active URP renderer, add the Scriptable Renderer Feature.
  5. In the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
    See in Glossary
    window of the active URP renderer, in the Render Last Frame In Material section of the Scriptable Renderer Feature you added in step 4, assign the material you created in step 2 to the Object Material field.
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);
                });
            }
        }
    }
}

Get data from previous frames in a script

To get data from previous frames in a script, for example a MonoBehaviour, do the following:

  1. Use the Scriptable Render Pipeline (SRP) Core RequestAccess API to request the texture.
  2. Use the UniversalAdditionalCameraData.history API to get the data.

To make sure Unity finishes rendering the frame first, use the UniversalAdditionalCameraData.history API in the LateUpdate method.

For more information, refer to the following in the Scriptable Render Pipeline (SRP) Core package:

Get data from the current frame in URP
Add textures to the camera history