2024-09-23 15:05:34 +08:00
|
|
|
using AOT.Common;
|
2023-08-22 19:08:45 +08:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Experimental.Rendering;
|
|
|
|
|
using UnityEngine.Rendering;
|
2024-09-26 19:54:57 +08:00
|
|
|
using UnityEngine.Rendering.RendererUtils;
|
2023-08-22 19:08:45 +08:00
|
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
|
|
|
|
|
|
public class PostOutlineRPF : ScriptableRendererFeature
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class Settings
|
|
|
|
|
{
|
|
|
|
|
//渲染发生的阶段,具体信息可以在枚举类RenderPassEvent中查看
|
|
|
|
|
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingSkybox;
|
2024-09-26 19:54:57 +08:00
|
|
|
|
|
|
|
|
public LayerMask teamSelfLayer;
|
|
|
|
|
public LayerMask teamEnemyLayer;
|
2025-06-30 18:39:34 +08:00
|
|
|
public LayerMask teamNeutralLayer;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CustomRenderPass : ScriptableRenderPass
|
|
|
|
|
{
|
|
|
|
|
|
2024-09-26 19:54:57 +08:00
|
|
|
private Settings _settings;
|
2025-06-30 18:39:34 +08:00
|
|
|
private Material _matSelf;
|
|
|
|
|
private Material _matEnemy;
|
|
|
|
|
private Material _matNeutral;
|
2023-10-19 15:00:19 +08:00
|
|
|
private Material _mat2;
|
|
|
|
|
private Material _mat3;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
private int _SSOutlineTexureID;
|
|
|
|
|
private int _EdgeTexureID;
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
private bool _canEnable = false;
|
2024-09-23 13:37:39 +08:00
|
|
|
|
2025-03-19 19:36:39 +08:00
|
|
|
private readonly ShaderTagId _shaderTagId = new ShaderTagId("UniversalForward");
|
2024-09-26 19:54:57 +08:00
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
public CustomRenderPass(Settings setting)
|
|
|
|
|
{
|
2024-09-26 19:54:57 +08:00
|
|
|
_settings = setting;
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
public override void OnCameraSetup(CommandBuffer cmd,
|
2023-08-22 19:08:45 +08:00
|
|
|
ref RenderingData renderingData)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
base.OnCameraSetup(cmd, ref renderingData);
|
|
|
|
|
_canEnable = false;
|
2024-09-23 15:05:34 +08:00
|
|
|
|
|
|
|
|
if (OutlineManager.instance == null || !OutlineManager.instance.m_EnableOutline)
|
2024-09-23 13:37:39 +08:00
|
|
|
{
|
2024-09-23 15:05:34 +08:00
|
|
|
if (AOTCodeDefine.ENABLE_OUTLINE_DEBUG)
|
2024-09-23 13:37:39 +08:00
|
|
|
{
|
2024-09-23 15:05:34 +08:00
|
|
|
Debug.Log("OutlineManager.instance == null || !OutlineManager.instance.m_EnableOutline");
|
2024-09-23 13:37:39 +08:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-09-23 15:05:34 +08:00
|
|
|
|
|
|
|
|
if (renderingData.cameraData.camera != OutlineManager.instance.targetCamera)
|
2024-09-23 13:37:39 +08:00
|
|
|
{
|
2024-09-23 15:05:34 +08:00
|
|
|
if (AOTCodeDefine.ENABLE_OUTLINE_DEBUG)
|
2024-09-23 13:37:39 +08:00
|
|
|
{
|
2024-09-23 15:05:34 +08:00
|
|
|
Debug.Log("不是主相机, 当前相机名字: " + renderingData.cameraData.camera.name + " 主相机名字:" + OutlineManager.instance.targetCamera.name);
|
2024-09-23 13:37:39 +08:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
|
2025-06-30 18:39:34 +08:00
|
|
|
if (_matSelf == null)
|
2023-10-19 15:00:19 +08:00
|
|
|
{
|
2025-06-30 18:39:34 +08:00
|
|
|
_matSelf = OutlineManager.instance.mat1;
|
|
|
|
|
_matSelf.SetColor(PureColor, OutlineManager.instance.m_OutlineColor);
|
2024-09-26 19:54:57 +08:00
|
|
|
|
2025-06-30 18:39:34 +08:00
|
|
|
_matEnemy = OutlineManager.instance.mat1_1;
|
|
|
|
|
_matEnemy.SetColor(PureColor, OutlineManager.instance.m_OutlineColorEnemy);
|
|
|
|
|
|
|
|
|
|
_matNeutral = OutlineManager.instance.mat1_2;
|
|
|
|
|
_matNeutral.SetColor(PureColor, OutlineManager.instance.m_OutlineColorNeutral);
|
2024-09-26 19:54:57 +08:00
|
|
|
|
2024-09-23 15:05:34 +08:00
|
|
|
_mat2 = OutlineManager.instance.mat2;
|
|
|
|
|
_mat3 = OutlineManager.instance.mat3;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
|
|
|
|
_SSOutlineTexureID = Shader.PropertyToID("_SSOutlineTexure");
|
|
|
|
|
_EdgeTexureID = Shader.PropertyToID("_EdgeTex");
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
2025-06-30 18:39:34 +08:00
|
|
|
var isSupported = _matSelf.shader.isSupported && _mat2.shader.isSupported && _mat3.shader.isSupported;
|
2024-09-23 13:05:59 +08:00
|
|
|
|
2024-09-23 13:37:39 +08:00
|
|
|
if (!isSupported)
|
2024-09-23 13:05:59 +08:00
|
|
|
{
|
2024-09-23 15:05:34 +08:00
|
|
|
Debug.LogError("Shader not supported on platform");
|
2024-09-23 13:37:39 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_canEnable = true;
|
|
|
|
|
|
2024-09-23 15:05:34 +08:00
|
|
|
if (AOTCodeDefine.ENABLE_OUTLINE_DEBUG)
|
2024-09-23 13:37:39 +08:00
|
|
|
{
|
2024-09-23 15:05:34 +08:00
|
|
|
Debug.Log("可以进行描边渲染");
|
2024-09-23 13:05:59 +08:00
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
public override void OnCameraCleanup(CommandBuffer cmd)
|
|
|
|
|
{
|
|
|
|
|
base.OnCameraCleanup(cmd);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
if (!_canEnable) return;
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
public override void Execute(ScriptableRenderContext context,
|
|
|
|
|
ref RenderingData renderingData)
|
|
|
|
|
{
|
|
|
|
|
if (!_canEnable) return;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
var cmd = CommandBufferPool.Get("Draw Outline");
|
2023-10-19 15:00:19 +08:00
|
|
|
|
|
|
|
|
_DrawPureColor(cmd, context, ref renderingData);
|
|
|
|
|
|
|
|
|
|
_DoEdgeDetect(cmd, context, ref renderingData);
|
|
|
|
|
|
|
|
|
|
_DoMerge(cmd, context, ref renderingData);
|
|
|
|
|
|
|
|
|
|
context.ExecuteCommandBuffer(cmd);
|
2023-08-22 19:08:45 +08:00
|
|
|
cmd.ReleaseTemporaryRT(_EdgeTexureID);
|
|
|
|
|
cmd.ReleaseTemporaryRT(_SSOutlineTexureID);
|
|
|
|
|
CommandBufferPool.Release(cmd);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
private void _DrawPureColor(CommandBuffer cmd,
|
|
|
|
|
ScriptableRenderContext context,
|
2023-08-22 19:08:45 +08:00
|
|
|
ref RenderingData renderingData)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
var desc = renderingData.cameraData.cameraTargetDescriptor;
|
|
|
|
|
//using (new ProfilingScope(cmd, new ProfilingSampler("Draw PureColor")))
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
// 绘制己方
|
2025-03-19 19:36:39 +08:00
|
|
|
cmd.GetTemporaryRT(_SSOutlineTexureID, desc.width, desc.height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
|
2023-08-22 19:08:45 +08:00
|
|
|
cmd.SetRenderTarget(_SSOutlineTexureID);
|
|
|
|
|
cmd.ClearRenderTarget(true, true, new Color(0, 0, 0, 0));
|
2024-09-26 19:54:57 +08:00
|
|
|
var renderListDesc = new RendererListDesc(_shaderTagId,
|
|
|
|
|
renderingData.cullResults,
|
|
|
|
|
renderingData.cameraData.camera);
|
|
|
|
|
renderListDesc.renderQueueRange = RenderQueueRange.all;
|
|
|
|
|
renderListDesc.layerMask = _settings.teamSelfLayer;
|
2025-06-30 18:39:34 +08:00
|
|
|
renderListDesc.overrideMaterial = _matSelf;
|
2024-09-26 19:54:57 +08:00
|
|
|
var renderList = context.CreateRendererList(renderListDesc);
|
|
|
|
|
cmd.DrawRendererList(renderList);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
// using (new ProfilingScope(cmd, new ProfilingSampler("Draw PureColor")))
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
// 绘制敌方
|
2024-09-26 19:54:57 +08:00
|
|
|
var renderListDesc = new RendererListDesc(_shaderTagId,
|
|
|
|
|
renderingData.cullResults,
|
|
|
|
|
renderingData.cameraData.camera);
|
|
|
|
|
renderListDesc.renderQueueRange = RenderQueueRange.all;
|
|
|
|
|
renderListDesc.layerMask = _settings.teamEnemyLayer;
|
2025-06-30 18:39:34 +08:00
|
|
|
renderListDesc.overrideMaterial = _matEnemy;
|
2024-09-26 19:54:57 +08:00
|
|
|
var renderList = context.CreateRendererList(renderListDesc);
|
|
|
|
|
cmd.DrawRendererList(renderList);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 18:39:34 +08:00
|
|
|
{
|
|
|
|
|
// 绘制中立
|
|
|
|
|
var renderListDesc = new RendererListDesc(_shaderTagId,
|
|
|
|
|
renderingData.cullResults,
|
|
|
|
|
renderingData.cameraData.camera);
|
|
|
|
|
renderListDesc.renderQueueRange = RenderQueueRange.all;
|
|
|
|
|
renderListDesc.layerMask = _settings.teamNeutralLayer;
|
|
|
|
|
renderListDesc.overrideMaterial = _matNeutral;
|
|
|
|
|
var renderList = context.CreateRendererList(renderListDesc);
|
|
|
|
|
cmd.DrawRendererList(renderList);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
// context.ExecuteCommandBuffer(cmd);
|
|
|
|
|
// CommandBufferPool.Release(cmd);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
private void _DoEdgeDetect(CommandBuffer cmd,
|
|
|
|
|
ScriptableRenderContext context,
|
2023-08-22 19:08:45 +08:00
|
|
|
ref RenderingData renderingData)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
// var cmd = CommandBufferPool.Get("Draw Outline");
|
|
|
|
|
// using (new ProfilingScope(cmd, new ProfilingSampler("Final SSOutline")))
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
|
|
|
|
var desc = renderingData.cameraData.cameraTargetDescriptor;
|
|
|
|
|
cmd.GetTemporaryRT(_EdgeTexureID, desc.width, desc.height, 0, FilterMode.Bilinear, RenderTextureFormat.ARGB32);
|
2023-10-24 12:57:09 +08:00
|
|
|
var sampleDistance = OutlineManager.instance.m_SampleDistance;
|
2024-09-23 15:05:34 +08:00
|
|
|
var scaleProgress = OutlineManager.instance.scaleProgress;
|
2024-08-06 19:43:11 +08:00
|
|
|
_mat2.SetFloat(SampleDistance, scaleProgress * sampleDistance);
|
2023-10-19 15:00:19 +08:00
|
|
|
_mat2.SetColor(OutlineColor, OutlineManager.instance.m_OutlineColor);
|
|
|
|
|
cmd.Blit(_SSOutlineTexureID, _EdgeTexureID, _mat2);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
// context.ExecuteCommandBuffer(cmd);
|
|
|
|
|
// CommandBufferPool.Release(cmd);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
private void _DoMerge(CommandBuffer cmd,
|
|
|
|
|
ScriptableRenderContext context,
|
2023-08-22 19:08:45 +08:00
|
|
|
ref RenderingData renderingData)
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
// using (new ProfilingScope(cmd, new ProfilingSampler("SSOutline Merge")))
|
2023-08-22 19:08:45 +08:00
|
|
|
{
|
2024-10-10 16:11:08 +08:00
|
|
|
var source = renderingData.cameraData.renderer.cameraColorTargetHandle.nameID;
|
2023-10-19 15:00:19 +08:00
|
|
|
cmd.SetGlobalTexture("_EdgeTex", _EdgeTexureID);
|
2024-10-10 16:59:05 +08:00
|
|
|
cmd.Blit(source, _SSOutlineTexureID, _mat3, 0);
|
|
|
|
|
cmd.Blit(_SSOutlineTexureID, source, _mat3, 0);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
2023-10-19 15:00:19 +08:00
|
|
|
// context.ExecuteCommandBuffer(cmd);
|
2023-08-22 19:08:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Settings settings;
|
|
|
|
|
private CustomRenderPass _pass;
|
2023-10-19 15:00:19 +08:00
|
|
|
private static readonly int SampleDistance = Shader.PropertyToID("_SampleDistance");
|
|
|
|
|
private static readonly int OutlineColor = Shader.PropertyToID("_OutlineColor");
|
|
|
|
|
private static readonly int PureColor = Shader.PropertyToID("_PureColor");
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
public override void AddRenderPasses(ScriptableRenderer renderer,
|
|
|
|
|
ref RenderingData renderingData)
|
|
|
|
|
{
|
|
|
|
|
renderer.EnqueuePass(_pass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Create()
|
|
|
|
|
{
|
|
|
|
|
_pass = new CustomRenderPass(settings);
|
|
|
|
|
_pass.renderPassEvent = settings.renderPassEvent;
|
|
|
|
|
}
|
|
|
|
|
}
|