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. 이전 텍스처 중 하나를 가져옵니다. 예시:

    // 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 렌더러의 인스펙터 창에서 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. SRP(스크립터블 렌더 파이프라인) Core RequestAccess API를 사용하여 텍스처를 요청합니다.
  2. UniversalAdditionalCameraData.history API를 사용해 텍스처를 가져옵니다.

Unity가 프레임 렌더링을 먼저 완료하도록 LateUpdate 메서드에서 UniversalAdditionalCameraData.history API를 사용합니다.

자세한 내용은 SRP(스크립터블 렌더 파이프라인) Core 패키지의 다음을 참조하십시오.

URP의 현재 프레임에서 데이터 가져오기
카메라 내역에 텍스처 추가
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961