If you are creating your own Scriptable Render Pipeline (SRP), your Project must contain:
CreatePipeline()
method. This script defines your Render Pipeline Asset.Render()
method. This script defines your Render Pipeline Instance.Because these elements are so closely related, you should create them at the same time.
The following example shows how to create a script for a basic custom Render Pipeline Asset that instantiates the Render Pipeline Instance, a script that defines the Render Pipeline Instance, and the Render Pipeline Asset itself.
Create a C# script called ExampleRenderPipelineAsset.cs.
Copy and paste the following code into the new script:
using UnityEngine;
using UnityEngine.__Rendering__The process of drawing graphics to the screen (or to a render texture). By default, the main camera in Unity renders its view to the screen. [More info](GraphicsOverview.html)<span class="tooltipGlossaryLink">See in [Glossary](Glossary.html#Rendering)</span>;
// The CreateAssetMenu attribute lets you create instances of this class in the Unity Editor.
[CreateAssetMenu(menuName = "Rendering/ExampleRenderPipelineAsset")]
public class ExampleRenderPipelineAsset : RenderPipelineAsset
{
// Unity calls this method before rendering the first frame.
// If a setting on the Render Pipeline Asset changes, Unity destroys the current Render Pipeline Instance and calls this method again before rendering the next frame.
protected override RenderPipeline CreatePipeline() {
// Instantiate the Render Pipeline that this custom SRP uses for rendering.
return new ExampleRenderPipelineInstance();
}
}
Create a C# script called ExampleRenderPipelineInstance.cs.
Copy and paste the following code into the new script:
using UnityEngine;
using UnityEngine.Rendering;
public class ExampleRenderPipelineInstance : RenderPipeline
{
public ExampleRenderPipelineInstance() {
}
// Unity calls this method once per frame for each CameraType that is currently rendering.
protected override void Render (ScriptableRenderContext context, Camera[] cameras) {
// This is where you can write custom rendering code. Customize this method to customize your SRP.
}
}
In the Project view, either click the add (+) button, or open the context menu and navigate to Create, and then choose Rendering > Example Render Pipeline Asset. Unity creates a new Render Pipeline Asset in the Project view.
By default, a Render Pipeline Asset stores information about which Render Pipeline Instance to use for rendering, and the default Materials and Shaders to use in the Editor. In your RenderPipelineAsset
script, you can extend your Render Pipeline Asset so that it stores additional data, and you can have multiple different Render Pipeline Assets with different configurations in your Project. For example, you might use a Render Pipeline Asset to hold configuration data for each different tier of hardware. The High Definition Render Pipeline (HDRP) and the Universal Render Pipeline (URP) include examples of this.
The following example shows how to create a RenderPipelineAsset
script that defines a Render Pipeline Asset with public data that you can set for each instance using the Inspector, and a Render Pipeline Instance that receives a Render Pipeline Asset in its constructor and uses data from that Render Pipeline Asset.
Create a C# script called ExampleRenderPipelineAsset.cs.
Copy and paste the following code into the new script:
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
{
// This data can be defined in the Inspector for each Render Pipeline Asset
public Color exampleColor;
public string exampleString;
// Unity calls this method before rendering the first frame.
// If a setting on the Render Pipeline Asset changes, Unity destroys the current Render Pipeline Instance and calls this method again before rendering the next frame.
protected override RenderPipeline CreatePipeline() {
// Instantiate the Render Pipeline that this custom SRP uses for rendering, and pass a reference to this Render Pipeline Asset.
// The Render Pipeline Instance can then access the configuration data defined above.
return new ExampleRenderPipelineInstance(this);
}
}
Create a C# script called ExampleRenderPipelineInstance.cs.
Copy and paste the following code into the new script:
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.
}
}
In the Project view, either click the add (+) button, or open the context menu and navigate to Create, and then choose Rendering > Example Render Pipeline Asset. Unity creates a new Render Pipeline Asset in the Project view.