From df1e3842c7571cf6db8f8238b893bde049abf651 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 21 Sep 2021 19:51:10 +0800 Subject: *custom rendering pipeline --- Assets/Scripts/Unit/Components/UnitLensEffect.cs | 43 +++++++++++++++++++----- 1 file changed, 34 insertions(+), 9 deletions(-) (limited to 'Assets/Scripts/Unit/Components/UnitLensEffect.cs') diff --git a/Assets/Scripts/Unit/Components/UnitLensEffect.cs b/Assets/Scripts/Unit/Components/UnitLensEffect.cs index 18328fc8..fee02a7c 100644 --- a/Assets/Scripts/Unit/Components/UnitLensEffect.cs +++ b/Assets/Scripts/Unit/Components/UnitLensEffect.cs @@ -5,8 +5,6 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; -//https://docs.unity3d.com/ScriptReference/Rendering.RenderTargetIdentifier.html - // Unit效果之一,镜头效果,通过command buffer实现 public class UnitLensEffect : UnitComponent @@ -17,6 +15,7 @@ public class UnitLensEffect : UnitComponent private List m_Effects; + // 每个角色维护单独的command buffers,而不是共享command buffer private Dictionary m_InUseCommandBuffers; private Dictionary m_CachedCommandBuffers; @@ -82,24 +81,25 @@ public class UnitLensEffect : UnitComponent { cb.Value.Clear(); ERenderingEvent re = cb.Key; - CameraEvent ce = CustomRenderingPipeline.RenderingEventToCameraEvent[re]; + CameraEvent ce = re.ToCameraEvent(); for (int i = 0; i < m_Effects.Count; ++i) { LensEffectBase eff = m_Effects[i]; if (!eff.renderingEvents.HasFlag(re)) continue; - MethodInfo method = eff.GetType().GetMethod(re.ToString(), BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(LensEffectBase.EStage), typeof(CommandBuffer), typeof(BodyPartRenderer) }, null); + MethodInfo method = eff.GetType().GetMethod(re.ToString(), BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(LensEffectBase.EStage), typeof(CommandBuffer) }, null); if (method == null) continue; - method.Invoke(eff, new object[] { LensEffectBase.EStage.Before, cb.Value, null }); + method.Invoke(eff, new object[] { LensEffectBase.EStage.Before, cb.Value }); for (int j = 0; j < body.renderers.Length; ++j) { var renderer = body.renderers[j]; if (renderer == null) continue; - method.Invoke(eff, new object[] { LensEffectBase.EStage.Iterate, cb.Value, renderer }); + eff.curBodypartRenderer = renderer; + method.Invoke(eff, new object[] { LensEffectBase.EStage.Iterate, cb.Value }); } - method.Invoke(eff, new object[] { LensEffectBase.EStage.After, cb.Value, null }); + method.Invoke(eff, new object[] { LensEffectBase.EStage.After, cb.Value }); } MainCamera.Instance.camera.AddCommandBuffer(ce, cb.Value); } @@ -107,9 +107,34 @@ public class UnitLensEffect : UnitComponent private void OnRenderUnit() { - foreach(var cb in m_InUseCommandBuffers) + // 执行每个event的finish + foreach (var cb in m_InUseCommandBuffers) + { + ERenderingEvent re = cb.Key; + for (int i = 0; i < m_Effects.Count; ++i) + { + LensEffectBase eff = m_Effects[i]; + if (!eff.renderingEvents.HasFlag(re)) + continue; + MethodInfo method = eff.GetType().GetMethod(re.ToString(), BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(LensEffectBase.EStage), typeof(CommandBuffer) }, null); + if (method == null) + continue; + method.Invoke(eff, new object[] { LensEffectBase.EStage.Finished, cb.Value }); + } + } + + for (int i = 0; i < m_Effects.Count; ++i) + { + LensEffectBase eff = m_Effects[i]; + MethodInfo method = eff.GetType().GetMethod("OnRenderFinish", BindingFlags.Instance | BindingFlags.Public); + if (method == null) + continue; + method.Invoke(eff, new object[] { }); + } + + foreach (var cb in m_InUseCommandBuffers) { - CameraEvent ce = CustomRenderingPipeline.RenderingEventToCameraEvent[cb.Key]; + CameraEvent ce = cb.Key.ToCameraEvent(); MainCamera.Instance.camera.RemoveCommandBuffer(ce, cb.Value); } var temp = m_CachedCommandBuffers; -- cgit v1.1-26-g67d0