Unity3D LWRP lightweight rendering to build the SRP pipeline

LWRP lightweight rendering

According to the official PPT and the general description,
LWRP is a streamlined and optimized rendering pipeline, mainly focusing on performance, and
pitting LWRP for mobile platforms . As for HDRP, it is mainly used on high-performance hosts and PCs. It is currently not a terminal Travel, not considered for the time being


  • High performance and high consistency PBR
  • New Shader library
  • Render multiple real-time lights at once
  • API for inserting Scriptable Render Passes

Unity Test Version: 2019.3.0.f1


The Scriptable Render Passes is written by C# code to complete the rendering pipeline. In the higher version of Unity (the author Unity2019 3.0.f1) through Editor -> Project Setting -> Graphices [Scriptable Render Pipeline Settings] to set our custom pipeline configuration.

Schematic diagram:

image.png

 

So what file does Scriptable Render Pipeline Settings need to specify?

In Unity, the official configuration is based on the ScriptableObject serializable configuration, let's try to write the code first.

 

[ExecuteInEditMode]
public class BasePipelineAsset_test : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("RenderPipeline/Create BasePipelineAsset")]
    public static void CreateBasePipelineAsset()
    {
        var instances = ScriptableObject.CreateInstance<BasePipelineAsset_test>();
        UnityEditor.AssetDatabase.CreateAsset(instances, "Assets/Pipelines/BasePiplineAsset.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return null;
    }
//这是一份自定义的序列化管线配置。
}

At this point, we can create a pipeline configuration through the menu RenderPipeline => Create BasePipelineAsset, and then drag the created OK configuration into the Scriptable Render Pipeline Settings, the pipeline configuration is created, and then look at the Scene and Game views.

image.png

full black! Don't worry, this is fine, because we haven't started rendering anything yet.

At this point, is our main camera useless? But it is not! Let's look at the next code!

First we abstract a base class:

 

public abstract class BasicRenderPipeline : RenderPipeline
{
    protected CommandBuffer command;

    [SerializeField] protected Color m_ClearColor;
    public BasicRenderPipeline()
    {
        command = new CommandBuffer();
    }
    public BasicRenderPipeline(Color clear) : this()
    {
        this.m_ClearColor = clear;
    }

    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        foreach (Camera c in cameras)
        {
            this.Render(context, c);
        }
    }
    protected abstract void Render(ScriptableRenderContext context, Camera camera);
}

Then we create a pipeline through inheritance:

 

public class MainPipeline : BasicRenderPipeline
{
    public MainPipeline(Color clear) : base(clear){ }

    protected override void Render(ScriptableRenderContext context, Camera camera)
    {
        command.Clear();
        command.ClearRenderTarget(true, true, this.m_ClearColor);
        context.ExecuteCommandBuffer(command);
        context.Submit();
    }
}

In this way, if there are multiple pipelines in the follow-up, we can all complete it through inheritance. Rendering is mainly written in the Render function abstracted from the base class.

Finally, return the pipeline MainPipeline we wrote in the CreatePipeline function in the BasePipelineAsset_test class

 

[ExecuteInEditMode]
public class BasePipelineAsset_test : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("RenderPipeline/Create BasePipelineAsset")]
    public static void CreateBasePipelineAsset()
    {
        var instances = ScriptableObject.CreateInstance<BasePipelineAsset_test>();
        UnityEditor.AssetDatabase.CreateAsset(instances, "Assets/Pipelines/BasePiplineAsset.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new MainPipeline(Color.grey);
    }

}

This pipeline just clears the color (gray as the base color) and returns to Unity, and we can see the effect, but there is a problem, that is, when the screen resolution is not regular, like 16:9, some colors will appear. Cannot be emptied.

Anomaly Figure 1:

 

Figure one

The Scene view that we can see is cleared, but the Game view, the scale is about 1 and the color cleaning is relatively'clean'.

Anomaly Figure 2:

 

Figure II

When we try to zoom the screen, we will find the problem, and the problem is very simple to solve.

Specify the properties of the camera in the pipeline rendering function. If you do not specify it, it is estimated that our camera will become useless (the effect should be there, that is, we can traverse our pipeline =. =)

Modified code:

 

public class MainPipeline : BasicRenderPipeline
{
    public MainPipeline(Color clear) : base(clear){ }

    protected override void Render(ScriptableRenderContext context, Camera camera)
    {
        command.Clear();
        context.SetupCameraProperties(camera);
        command.ClearRenderTarget(true, true, this.m_ClearColor);
        context.ExecuteCommandBuffer(command);
        context.Submit();
    }
}

OK, so no matter how you zoom, the camera's parameters will work

 

Guess you like

Origin blog.csdn.net/mango9126/article/details/108876540