UnityGame/Assets/Scripts/OutlineRenderFeature.cs

67 lines
2.1 KiB
C#
Raw Normal View History

2024-10-27 10:53:47 +03:00
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class OutlineRenderFeature : ScriptableRendererFeature
{
class OutlineRenderPass : ScriptableRenderPass
{
public Material outlineMaterial;
public LayerMask outlineLayer;
private string profilerTag = "OutlineRenderPass";
private ProfilingSampler profilingSampler;
public OutlineRenderPass(Material material, LayerMask layer)
{
outlineMaterial = material;
outlineLayer = layer;
profilingSampler = new ProfilingSampler(profilerTag);
renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (outlineMaterial == null)
return;
CommandBuffer cmd = CommandBufferPool.Get();
using (new ProfilingScope(cmd, profilingSampler))
{
var drawingSettings = CreateDrawingSettings(new ShaderTagId("UniversalForward"), ref renderingData, SortingCriteria.CommonOpaque);
drawingSettings.overrideMaterial = outlineMaterial;
var filteringSettings = new FilteringSettings(RenderQueueRange.all, outlineLayer);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref filteringSettings);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
public Material outlineMaterial;
public LayerMask outlineLayer;
OutlineRenderPass outlinePass;
public override void Create()
{
outlinePass = new OutlineRenderPass(outlineMaterial, outlineLayer);
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (outlineMaterial == null)
return;
renderer.EnqueuePass(outlinePass);
}
}