[Unity]使用SRP调整编辑器透明和不透明的显示次序

2019/10 23 16:10

项目中,地表片是半透明的,需要优先渲染;不透明的物件,是不透明的,需要后渲染; 在Scene视图中,无法被正确显示,通过以下代码,显示在Scene视图中编辑的正常显示:

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using UnityEditor;

public class ArtPreviewBasicPipeInstance : RenderPipeline
{
    public ArtPreviewBasicPipeInstance(Color clearColor)
    {
    }

    private CommandBuffer _cb;

    public override void Render(ScriptableRenderContext context, Camera[] cameras)
    {
        base.Render(context, cameras);

        if (_cb == null)
        {
            _cb = new CommandBuffer();
        }

        var _LightDir = Shader.PropertyToID("_LightDir");
        var _LightColor = Shader.PropertyToID("_LightColor");

        foreach(var camera in cameras)
        {
            ScriptableCullingParameters cullingParams;
            if (!CullResults.GetCullingParameters(camera, out cullingParams))
                continue;

            CullResults cull = CullResults.Cull(ref cullingParams, context);
            context.SetupCameraProperties(camera);

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

            context.DrawSkybox(camera);
            
            var settings = new DrawRendererSettings(camera, new ShaderPassName("ForwardBase"));
            settings.sorting.flags = SortFlags.CommonTransparent;

            var lights = cull.visibleLights;
            foreach(var light in lights)
            {
                if (light.lightType != LightType.Directional) continue;
                Vector4 pos = light.localToWorld.GetColumn(0);
                Vector4 lightDir = new Vector4(pos.x,pos.y,pos.z,0);
                Color lightColor = light.finalColor;
                _cb.SetGlobalVector(_LightDir,lightDir);
                _cb.SetGlobalColor(_LightColor,lightColor);
                context.ExecuteCommandBuffer(_cb);
                _cb.Clear();              
            }

            // 先渲染透明片
            var filterSettings = new FilterRenderersSettings(true) { renderQueueRange = RenderQueueRange.transparent };
            context.DrawRenderers(cull.visibleRenderers, ref settings, filterSettings);

            // 再渲染不透明片
            settings.sorting.flags = SortFlags.CommonOpaque;
            filterSettings.renderQueueRange = RenderQueueRange.opaque;
            context.DrawRenderers(cull.visibleRenderers, ref settings, filterSettings);

            context.Submit();
        }
    }
}

[ExecuteInEditMode]
//定义渲染管线Asset
public class ArtPreviewScenePipeline : RenderPipelineAsset
{
    public Color clearColor = Color.blue;

    protected override IRenderPipeline InternalCreatePipeline()
    {
        return new ArtPreviewBasicPipeInstance(clearColor);
    }
}

public class ArtPreviewSceneMenu
{
    [MenuItem("Tool/地表Prefab编辑-切换至编辑模式", false, 800)]
    public static void UserSRP()
    {
        var obj = AssetDatabase.LoadAssetAtPath<ArtPreviewScenePipeline>("Assets/SceneRes/ArtPreviewScene.asset");
        if (obj != null)
        {
            GraphicsSettings.renderPipelineAsset = obj;
        }
    }

    
    [MenuItem("Tool/地表Prefab编辑-切换至游戏模式", false, 800)]
    public static void NoUserSRP()
    {
        GraphicsSettings.renderPipelineAsset = null;
    }
}