スクリプタブルレンダーパイプライン (SRP) に基づいて独自のレンダーパイプラインを作成する場合、プロジェクトには以下が含まれていなければなりません。
CreatePipeline()
メソッドをオーバーライドするスクリプト。このスクリプトはレンダーパイプラインアセットを定義します。Render()
メソッドをオーバーライドするスクリプト。このスクリプトにレンダーパイプラインインスタンスを定義します。ここに、カスタムのレンダリングコードを記述します。これらの要素は密接に関連しているため、同時に作成する必要があります。
次の例は、レンダー パイプラインインスタンスをインスタンス化する基本的なカスタムのレンダーパイプライン アセットのスクリプト、レンダーパイプラインインスタンスを定義するスクリプト、そしてレンダーパイプラインアセット自体を作成する方法を示しています。
ExampleRenderPipelineAsset.cs という C# スクリプトを作成します。
次のコードをコピーして新しいスクリプトに貼り付けます。
using UnityEngine;
using UnityEngine.Rendering;
// CreateAssetMenu 属性によって Unity Editor にこのクラスのインスタンスの作成が可能になります。
[CreateAssetMenu(menuName = "Rendering/ExampleRenderPipelineAsset")]
public class ExampleRenderPipelineAsset : RenderPipelineAsset
{
// 最初のフレームをレンダリングする前に、Unity によってこのメソッドが呼び出されます。
//レンダーパイプラインアセットの設定が変更された場合は、Unity は現在のレンダーパイプラインインスタンスを破棄し、次のフレームをレンダリングする前に再度、このメソッドを呼び出します。
protected override RenderPipeline CreatePipeline() {
// このカスタムの SRP がレンダリングのために使うレンダーパイプラインをインスタンス化します。
return new ExampleRenderPipelineInstance();
}
}
ExampleRenderPipelineInstance.cs という C# スクリプトを作成します。
次のコードをコピーして新しいスクリプトに貼り付けます。
using UnityEngine;
using UnityEngine.Rendering;
public class ExampleRenderPipelineInstance : RenderPipeline
{
public ExampleRenderPipelineInstance() {
}
// Unity calls this method once per frame for each現在レンダリングを行っている各 CameraType に対し、フレームごとに 1 回このメソッドを呼び出します。
protected override void Render (ScriptableRenderContext context, Camera[] cameras) {
// ここにカスタムのレンダリングコードを記述します。このメソッドをカスタム化して、SRP をカスタム化します。
}
}
Project ウィンドウで追加ボタン (+) をクリックするか、コンテキストメニューを開いて Create に移動し、Rendering > Example Render Pipeline Asset を選択します。Unity は、Project ウィンドウに新しいレンダーパイプラインアセットを作成します。
デフォルトでは、レンダーパイプラインアセットには、レンダリングにどのレンダーパイプラインインスタンスを使用するか、また、エディターで使用するデフォルトのマテリアルとシェーダーに関する情報が保存されています。RenderPipelineAsset
スクリプトでは、レンダーパイプラインアセットを拡張して追加のデータを保存することができ、プロジェクト内に設定の異なる複数のレンダーパイプラインアセットを持つことができます。例えば、レンダーパイプラインアセットを使用して、異なるティアのハードウェアごとに設定データを保存できます。HD レンダーパイプライン (HDRP) とユニバーサルレンダーパイプライン (URP) などにこのような例が含まれます。
以下の例は、パブリックデータ (インスペクターを使用してインスタンスごとに設定できます) でレンダーパイプラインアセットを定義する RenderPipelineAsset
スクリプトの作成方法と、レンダーパイプラインインスタンス (コンストラクターでレンダーパイプラインアセットを受け取り、そのレンダーパイプラインアセットのデータを使用します) の作成方法を示しています。
ExampleRenderPipelineAsset.cs という C# スクリプトを作成します。
次のコードをコピーして新しいスクリプトに貼り付けます。
using UnityEngine;
using UnityEngine.Rendering;
// The CreateAssetMenu attribute lets you create instances of this class in the Unity Editor.
[CreateAssetMenu(menuName = "Rendering/ExampleRenderPipelineAsset")]
public class ExampleRenderPipelineAsset : RenderPipelineAsset
{
// このデータは、レンダーパイプラインアセットごとにインスペクターで定義されます
public Color exampleColor;
public string exampleString;
// Unity calls this method before rendering the first frame.
// レンダーパイプラインアセットの設定が変わると、次のフレームをレンダリングする前に、Unity は現在のレンダーパイプラインインスタンスを破棄しこのメソッドを再度呼び出します。
protected override RenderPipeline CreatePipeline() {
// このカスタム SRP がレンダリングに使用するレンダリングパイプラインをインスタンス化し、このレンダーパイプラインアセットへの参照を渡します。
// レンダーパイプラインインスタンスは、上記で定義された構成データにアクセスできます。
return new ExampleRenderPipelineInstance(this);
}
}
ExampleRenderPipelineInstance.cs という C# スクリプトを作成します。
次のコードをコピーして新しいスクリプトに貼り付けます。
using UnityEngine;
using UnityEngine.Rendering;
public class ExampleRenderPipelineInstance : RenderPipeline
{
// Use this variable to a reference to the Render Pipeline Asset that was passed to the constructor
private ExampleRenderPipelineAsset renderPipelineAsset;
// The constructor has an instance of the ExampleRenderPipelineAsset class as its parameter.
public ExampleRenderPipelineInstance(ExampleRenderPipelineAsset asset) {
renderPipelineAsset = asset;
}
// Unity calls this method once per frame for each CameraType that is currently rendering.
protected override void Render(ScriptableRenderContext context, Camera[] cameras) {
// This is an example of using the data from the Render Pipeline Asset.
Debug.Log(renderPipelineAsset.exampleString);
// This is where you can write custom rendering code. Customize this method to customize your SRP.
}
}
Project ウィンドウで追加ボタン (+) をクリックするか、コンテキストメニューを開いて Create に移動し、Rendering > Example Render Pipeline Asset を選択します。Unity は、Project ウィンドウに新しいレンダーパイプラインアセットを作成します。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.