Version: Unity 6.0 (6000.0)
言語 : 日本語
URP の互換性モード
URP の互換性モードでの全画面転送の実行例

URP の互換性モードでスクリプタブルレンダーパスを記述する

ノート:Unity は、今後は、レンダーグラフ API を使用しないレンダリングパスの開発や改善を行うことはありません。新しいグラフィックス機能を開発する場合は、代わりにレンダーグラフ API を使用してください。このページの手順を使用するには、URP の Graphics 設定 (Project Settings > Graphics) で Compatibility Mode (Render Graph Disabled) を有効にしてください。

以下の例は、以下の手順を実行する ScriptableRenderPass インスタンスです。

  1. RenderTextureDescriptor API を使用して一時的なレンダーテクスチャを作成します。
  2. RTHandleBlit API を使用して カスタムシェーダー の 2 つのパスをカメラ出力に適用します。

スクリプタブルレンダーパスを記述したら、以下のいずれかの方法でレンダーパスを挿入できます。

スクリプタブルレンダーパスを作成する

このセクションでは、スクリプタブルレンダーパスの作成方法について説明します。

  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 は、各カメラに対して 1 回、フレームごとにこのメソッドを呼び出します。このメソッドを使用すると、スクリプタブルレンダーパスのレンダリングロジックを実装できます。

    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 メソッドを使用して、カスタムシェーダーからカメラ出力に描画する 2 つのパスを適用します。

    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 の互換性モードでの全画面転送の実行例