Unity UGUI 背景动态模糊的一种处理方案

2024/05 12 23:05

具体实现部分代码

public struct RenderingData
{
    public CullingResults cullResults;
    public CameraData cameraData;
    public LightData lightData;
    public ShadowData shadowData;
    public PostProcessingData postProcessingData;
    public bool supportsDynamicBatching;
    public PerObjectData perObjectData;
    public short customSortingLayerRange;
}

// ScriptableRenderer.cs
if (renderBlocks.GetLength(RenderPassBlock.MainRenderingTransparent) > 0)
{
    if ( mBlurCameraName.Length > 0 && renderingData.cameraData.camera.name == mBlurCameraName)
    {
        renderingData.customSortingLayerRange = mBlurSortingLayer;
    }

    using var profScope = new ProfilingScope(null, Profiling.RenderBlock.mainRenderingOpaque);
    ExecuteBlock(RenderPassBlock.MainRenderingTransparent, in renderBlocks, context, ref renderingData);
    renderingData.customSortingLayerRange = 0;
}

// Render2DLightingPass.cs
Profiler.BeginSample("Render Sprites Unlit");
var customSortingLayerRange = renderingData.customSortingLayerRange;
if (customSortingLayerRange != 0)
{
    filterSettings.sortingLayerRange = new SortingLayerRange(short.MinValue, (short)(customSortingLayerRange-1));
    Render(context, cmd, ref renderingData, ref filterSettings, unlitDrawSettings);

    ScriptableRenderer.ProcessBlurPass(context, renderingData);
    cmd.SetRenderTarget(colorAttachment, RenderBufferLoadAction.Load, storeAction, depthAttachment, RenderBufferLoadAction.Load, storeAction);
    context.ExecuteCommandBuffer(cmd);

    filterSettings.sortingLayerRange = new SortingLayerRange( (short)(customSortingLayerRange), (short)(customSortingLayerRange));
    Render(context, cmd, ref renderingData, ref filterSettings, unlitDrawSettings);
}
else
{
    Render(context, cmd, ref renderingData, ref filterSettings, unlitDrawSettings);
}