X

Unity Scriptable Render Pipeline

スクリプタブルレンダーパイプラインの概要

https://blogs.unity3d.com/jp/2018/01/31/srp-overview/

こちらのブログについて Unity 2019.1 以降で CullingResults, DrawRendererSettings の個所などでエラーが発生して動作しなくなりました。

これはこのクラスがExperimentalに指定されて、Unity 2019.1 以降のバージョンでは利用できなくなりました。次のように書き換えると動作しました。

BasicAssetPipe.cs

using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;


[ExecuteInEditMode]
public class BasicAssetPipe : RenderPipelineAsset
{
    public Color clearColor = Color.green;

#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/01 - Create Basic Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<BasicAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/1-BasicAssetPipe/BasicAssetPipe.asset");
    }

#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new BasicPipeInstance(clearColor);
    }
}

public class BasicPipeInstance : RenderPipeline
{
    private Color m_ClearColor = Color.black;

    public BasicPipeInstance(Color clearColor)
    {
        m_ClearColor = clearColor;
    }

    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // does not so much yet :()
        // base.Render(context, cameras);

        // clear buffers to the configured color
        var cmd = new CommandBuffer();
        cmd.ClearRenderTarget(true, true, m_ClearColor);
        context.ExecuteCommandBuffer(cmd);
        cmd.Release();
        context.Submit();
    }
}

OpaqueAssetPipe.cs

using System;
using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;

[ExecuteInEditMode]
public class OpaqueAssetPipe : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/02 - Create Opaque Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<OpaqueAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/2-OpaqueAssetPipe/OpaqueAssetPipe.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new OpaqueAssetPipeInstance();
    }
}

public class OpaqueAssetPipeInstance : RenderPipeline
{
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // base.Render(context, cameras);

        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;

            if (!(camera.TryGetCullingParameters(out cullingParams)))
                 continue;

            var cull = context.Cull(ref cullingParams);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawingSettings(new ShaderTagId("BasicPass"), new SortingSettings(camera));
            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonOpaque };
            var filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.opaque);
            context.DrawRenderers(cull, ref settings ,ref filterSettings);

            // Draw skybox
            context.DrawSkybox(camera);

            context.Submit();
        }
    }
}

TransparentAssetPipe.cs

using System;
using UnityEngine;
using UnityEngine.Rendering;
// using UnityEngine.Experimental.Rendering;

[ExecuteInEditMode]
public class TransparentAssetPipe : RenderPipelineAsset
{
#if UNITY_EDITOR
    [UnityEditor.MenuItem("SRP-Demo/03 - Create Transparent Asset Pipeline")]
    static void CreateBasicAssetPipeline()
    {
        var instance = ScriptableObject.CreateInstance<TransparentAssetPipe>();
        UnityEditor.AssetDatabase.CreateAsset(instance, "Assets/SRP-Demo/3-TransparentAssetPipe/TransparentAssetPipe.asset");
    }
#endif

    protected override RenderPipeline CreatePipeline()
    {
        return new TransparentAssetPipeInstance();
    }
}

public class TransparentAssetPipeInstance : RenderPipeline
{
    protected override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        // base.Render(context, cameras);

        foreach (var camera in cameras)
        {
            // Culling
            ScriptableCullingParameters cullingParams;

            if (!(camera.TryGetCullingParameters(out cullingParams)))
                continue;

            var cull = context.Cull(ref cullingParams);

            // Setup camera for rendering (sets render target, view/projection matrices and other
            // per-camera built-in shader variables).
            context.SetupCameraProperties(camera);

            // clear depth buffer
            var cmd = new CommandBuffer();
            cmd.ClearRenderTarget(true, false, Color.black);
            context.ExecuteCommandBuffer(cmd);
            cmd.Release();

            // Draw opaque objects using BasicPass shader pass
            var settings = new DrawingSettings(new ShaderTagId("BasicPass"), new SortingSettings(camera));
            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonOpaque };
            var filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.opaque);
            context.DrawRenderers(cull, ref settings, ref filterSettings);


            // Draw skybox
            context.DrawSkybox(camera);

            settings.sortingSettings = new SortingSettings() { criteria = SortingCriteria.CommonTransparent };
            filterSettings = new FilteringSettings(renderQueueRange: RenderQueueRange.transparent);
            context.DrawRenderers(cull, ref settings, ref filterSettings);

            context.Submit();
        }
    }
}

こちらのサイトが参考になりました。

LWRP 4-preview を2019.1で動かすように改造した話

https://connect.unity.com/p/lwrp-4-preview-wo2019-1dedong-kasuyounigai-zao-shitahua

Unity Scriptable Render Pipeline(SRP)はパッケージマネージャーからUniversal Render Pipeline(URP, 2019より前はLightweight Render Pipeline(LWRP))、HD レンダーパイプライン(HDRP)をチェックしなくても利用できます。

kawano:
Related Post