ノート:Unity は、今後は、レンダーグラフ API を使用しないレンダリングパスの開発や改善を行うことはありません。新しいグラフィックス機能を開発する場合は、代わりにレンダーグラフ API を使用してください。このページの手順を使用するには、URP の Graphics 設定 (Project Settings > Graphics) で Compatibility Mode (Render Graph Disabled) を有効にしてください。
以下の例は、以下の手順を実行する ScriptableRenderPass インスタンスです。
RenderTextureDescriptor API を使用して一時的なレンダーテクスチャを作成します。RTHandle と Blit API を使用して カスタムシェーダー の 2 つのパスをカメラ出力に適用します。スクリプタブルレンダーパスを記述したら、以下のいずれかの方法でレンダーパスを挿入できます。
このセクションでは、スクリプタブルレンダーパスの作成方法について説明します。
新しい C# スクリプトを作成し、名前を RedTintRenderPass.cs に設定します。
スクリプトで、Unity が RedTintRenderPass クラスに挿入したコードを削除します。以下の using ディレクティブを追加します。
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
ScriptableRenderPass クラスから継承する RedTintRenderPass クラスを作成します。
public class RedTintRenderPass : ScriptableRenderPass
クラスに 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)
{
}
}
マテリアルのフィールドと、そのフィールドを使用するコンストラクターを追加します。
private Material material;
public RedTintRenderPass(Material material)
{
this.material = material;
}
RenderTextureDescriptor フィールドを追加し、コンストラクターで初期化します。
using UnityEngine;
private RenderTextureDescriptor textureDescriptor;
public RedTintRenderPass(Material material)
{
this.material = material;
textureDescriptor = new RenderTextureDescriptor(Screen.width,
Screen.height, RenderTextureFormat.Default, 0);
}
赤い色合いの一時テクスチャへの参照を格納する RTHandle フィールドを宣言します 。
private RTHandle textureHandle;
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);
}
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);
}
レンダーパスの実行後に、マテリアルと一時レンダーテクスチャを破棄する 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
}
}
}