Version: Unity 6.0 (6000.0)
언어 : 한국어
URP의 호환성 모드
URP의 호환성 모드에서 전체 화면 blit을 수행하는 예시

URP의 호환성 모드에서 스크립터블 렌더 패스 작성

참고: Unity는 더 이상 렌더 그래프 API를 사용하지 않는 렌더링 경로를 개발하거나 개선하지 않습니다. 이제 새로운 그래픽스 기능을 개발할 때는 렌더 그래프 API를 사용하십시오. 이 페이지의 지침을 사용하려면 URP Graphics 설정(Project Settings > Graphics)에서 Compatibility Mode (Render Graph Disabled)를 활성화하십시오.

다음은 아래의 과정을 수행하는 ScriptableRenderPass 인스턴스의 예시입니다.

  1. RenderTextureDescriptor API를 사용하여 임시 렌더 텍스처를 만듭니다.
  2. RTHandleBlit API를 사용하여 카메라 출력에 두 개의 커스텀 셰이더 패스를 적용합니다.

스크립터블 렌더 패스를 작성한 후 다음 방법 중 하나를 사용하여 렌더 패스를 삽입할 수 있습니다.

스크립터블 렌더 패스 생성

이 섹션에서는 스크립터블 렌더 패스를 생성하는 방법을 설명합니다.

  1. 새로운 C# 스크립트를 만들고 이름을 RedTintRenderPass.cs로 지정합니다.

  2. 스크립트에서 Unity가 RedTintRenderPass 클래스에 삽입한 코드를 제거합니다. 다음 using 지시문을 추가합니다.

    using UnityEngine.Rendering;
    using UnityEngine.Rendering.Universal;
    
  3. ScriptableRenderPass 클래스에서 상속하는 RedTintRenderPass 클래스를 만듭니다.

    public class RedTintRenderPass : ScriptableRenderPass
    
  4. 클래스에 Execute 메서드를 추가합니다. Unity는 각 카메라에 대해 한 번씩, 프레임마다 이 메서드를 호출합니다. 이 메서드를 사용하면 스크립터블 렌더 패스의 렌더링 로직을 구현할 수 있습니다.

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    { }
    

아래는 이 섹션의 RedTintRenderPass.cs 파일에 대한 전체 코드입니다.

using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class RedTintRenderPass : ScriptableRenderPass
{
    public override void Execute(ScriptableRenderContext context,
        ref RenderingData renderingData)
    {
        
    }
}

커스텀 렌더 패스 설정 구현

  1. 머티리얼에 대한 필드와 필드를 사용하는 생성자를 추가합니다.

    private Material material;
    
    public RedTintRenderPass(Material material)
    {
        this.material = material;
    }
    
  2. RenderTextureDescriptor 필드를 추가하고 생성자에서 초기화합니다.

    using UnityEngine;
    
    private RenderTextureDescriptor textureDescriptor;
    
    public RedTintRenderPass(Material material)
    {
        this.material = material;
    
        textureDescriptor = new RenderTextureDescriptor(Screen.width,
            Screen.height, RenderTextureFormat.Default, 0);
    }
    
  3. 임시적인 빨간색 틴트 텍스처에 대한 레퍼런스를 저장하는 RTHandle 필드를 선언합니다.

    private RTHandle textureHandle;
    
  4. Configure 메서드를 구현합니다. Unity는 렌더 패스를 실행하기 전에 이 메서드를 호출합니다.

    public override void Configure(CommandBuffer cmd,
        RenderTextureDescriptor cameraTextureDescriptor)
    {
        //Set the red tint texture size to be the same as the camera target size.
        textureDescriptor.width = cameraTextureDescriptor.width;
        textureDescriptor.height = cameraTextureDescriptor.height;
    
        //Check if the descriptor has changed, and reallocate the RTHandle if necessary.
        RenderingUtils.ReAllocateIfNeeded(ref textureHandle, textureDescriptor);
    }
    
  5. Blit 메서드를 사용하여 커스텀 셰이더의 두 렌더 패스를 카메라 출력에 적용합니다.

    public override void Execute(ScriptableRenderContext context,
        ref RenderingData renderingData)
    {
        //Get a CommandBuffer from pool.
        CommandBuffer cmd = CommandBufferPool.Get();
    
        RTHandle cameraTargetHandle =
            renderingData.cameraData.renderer.cameraColorTargetHandle;
    
        // Blit from the camera target to the temporary render texture,
        // using the first shader pass.
        Blit(cmd, cameraTargetHandle, textureHandle, material, 0);
        // Blit from the temporary render texture to the camera target,
        // using the second shader pass.
        Blit(cmd, textureHandle, cameraTargetHandle, material, 1);
    
        //Execute the command buffer and release it back to the pool.
        context.ExecuteCommandBuffer(cmd);
        CommandBufferPool.Release(cmd);
    }
    
  6. 렌더 패스 실행 이후 머티리얼과 임시 렌더 텍스처를 파괴하는 Dispose 메서드를 구현합니다.

    public void Dispose()
    {
        #if UNITY_EDITOR
                if (EditorApplication.isPlaying)
                {
                    Object.Destroy(material);
                }
                else
                {
                    Object.DestroyImmediate(material);
                }
        #else
                Object.Destroy(material);
        #endif
            
        if (textureHandle != null) textureHandle.Release();
    }
    

커스텀 렌더 패스 코드

아래는 커스텀 렌더 패스 스크립트의 코드 완성본입니다.

using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class RedTintRenderPass : ScriptableRenderPass
{
    private Material material;

    private RenderTextureDescriptor textureDescriptor;
    private RTHandle textureHandle;

    public RedTintRenderPass(Material material)
    {
        this.material = material;

        textureDescriptor = new RenderTextureDescriptor(Screen.width,
            Screen.height, RenderTextureFormat.Default, 0);
    }

    public override void Configure(CommandBuffer cmd,
        RenderTextureDescriptor cameraTextureDescriptor)
    {
        // Set the texture size to be the same as the camera target size.
        textureDescriptor.width = cameraTextureDescriptor.width;
        textureDescriptor.height = cameraTextureDescriptor.height;

        // Check if the descriptor has changed, and reallocate the RTHandle if necessary
        RenderingUtils.ReAllocateIfNeeded(ref textureHandle, textureDescriptor);
    }

    public override void Execute(ScriptableRenderContext context,
        ref RenderingData renderingData)
    {
        //Get a CommandBuffer from pool.
        CommandBuffer cmd = CommandBufferPool.Get();

        RTHandle cameraTargetHandle =
            renderingData.cameraData.renderer.cameraColorTargetHandle;

        // Blit from the camera target to the temporary render texture,
        // using the first shader pass.
        Blit(cmd, cameraTargetHandle, textureHandle, material, 0);
        // Blit from the temporary render texture to the camera target,
        // using the second shader pass.
        Blit(cmd, textureHandle, cameraTargetHandle, material, 1);

        //Execute the command buffer and release it back to the pool.
        context.ExecuteCommandBuffer(cmd);
        CommandBufferPool.Release(cmd);
    }

    public void Dispose()
    {
    #if UNITY_EDITOR
        if (EditorApplication.isPlaying)
        {
            Object.Destroy(material);
        }
        else
        {
            Object.DestroyImmediate(material);
        }
    #else
            Object.Destroy(material);
    #endif

        if (textureHandle != null) textureHandle.Release();
    }
}

빨간색 틴트 효과를 위한 커스텀 셰이더

이 섹션에는 빨간색 틴트 효과를 구현하는 커스텀 셰이더 코드가 포함되어 있습니다.

Shader "CustomEffects/RedTint"
{
    HLSLINCLUDE
    
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        // The Blit.hlsl file provides the vertex shader (Vert),
        // the input structure (Attributes), and the output structure (Varyings)
        #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"

    
        float4 RedTint (Varyings input) : SV_Target
        {
            float3 color = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord).rgb;
            return float4(1, color.gb, 1);
        }

        float4 SimpleBlit (Varyings input) : SV_Target
        {
            float3 color = SAMPLE_TEXTURE2D(_BlitTexture, sampler_LinearClamp, input.texcoord).rgb;
            return float4(color.rgb, 1);
        }
    
    ENDHLSL
    
    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"}
        LOD 100
        ZTest Always ZWrite Off Cull Off
        Pass
        {
            Name "RedTint"

            HLSLPROGRAM
            
            #pragma vertex Vert
            #pragma fragment RedTint
            
            ENDHLSL
        }
        
        Pass
        {
            Name "SimpleBlit"

            HLSLPROGRAM
            
            #pragma vertex Vert
            #pragma fragment SimpleBlit
            
            ENDHLSL
        }
    }
}
URP의 호환성 모드
URP의 호환성 모드에서 전체 화면 blit을 수행하는 예시
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961