diff options
Diffstat (limited to 'Assets/Scripts/Unit/Components')
-rw-r--r-- | Assets/Scripts/Unit/Components/UnitLensEffect.cs | 129 |
1 files changed, 88 insertions, 41 deletions
diff --git a/Assets/Scripts/Unit/Components/UnitLensEffect.cs b/Assets/Scripts/Unit/Components/UnitLensEffect.cs index 5a6a309e..18328fc8 100644 --- a/Assets/Scripts/Unit/Components/UnitLensEffect.cs +++ b/Assets/Scripts/Unit/Components/UnitLensEffect.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -14,11 +15,12 @@ public class UnitLensEffect : UnitComponent private static ObjectPool<CommandBuffer> m_CommandBufferPool; - private List<LensEffect> m_Effects; + private List<LensEffectBase> m_Effects; - private CommandBuffer m_CommandBuffer; - - static UnitLensEffect() + private Dictionary<ERenderingEvent, CommandBuffer> m_InUseCommandBuffers;
+ private Dictionary<ERenderingEvent, CommandBuffer> m_CachedCommandBuffers;
+
+ static UnitLensEffect() { m_CommandBufferPool = new ObjectPool<CommandBuffer>(null, null); } @@ -28,8 +30,10 @@ public class UnitLensEffect : UnitComponent base.Initialize(); renderers = new List<RendererProxy>(); - m_Effects = new List<LensEffect>(); - } + m_Effects = new List<LensEffectBase>(); + m_InUseCommandBuffers = new Dictionary<ERenderingEvent, CommandBuffer>(); + m_CachedCommandBuffers = new Dictionary<ERenderingEvent, CommandBuffer>(); + } public override void OnPostInitialize() { @@ -50,58 +54,68 @@ public class UnitLensEffect : UnitComponent renderers.Add(proxy); } - m_CommandBuffer = ClaimCommandBuffer(); - m_CommandBuffer.name = "Unit Render"; - - MainCamera.Instance.lensEffectHandler.onPreCull += OnWillRenderUnit; - MainCamera.Instance.lensEffectHandler.onPostRender += OnRenderUnit; + MainCamera.Instance.customRenderingPipeline.onPreCull += OnWillRenderUnit; + MainCamera.Instance.customRenderingPipeline.onPostRender += OnRenderUnit; ///// - m_Effects.Add(LensEffect.MotionBlur()); + m_Effects.Add(new LensEffect_MotionBlur()); } public override void Release() { base.Release(); - MainCamera.Instance.lensEffectHandler.onPreRender -= OnWillRenderUnit; - MainCamera.Instance.lensEffectHandler.onPostRender -= OnRenderUnit; + MainCamera.Instance.customRenderingPipeline.onPreRender -= OnWillRenderUnit; + MainCamera.Instance.customRenderingPipeline.onPostRender -= OnRenderUnit; } private void OnWillRenderUnit() { - IBodyRendererAgent body = owner.unitRender.body; + PrepareCommandBuffers(); +
+ IBodyRendererAgent body = owner.unitRender.body; if (body == null || body.renderers == null) return; if (m_Effects == null || m_Effects.Count == 0) - return; - - m_CommandBuffer.Clear(); - - for (int i = 0; i < m_Effects.Count; ++i) - { - m_Effects[i].beforeIterate(m_CommandBuffer); - for (int j = 0; j < body.renderers.Length; ++j) - { - var renderer = body.renderers[j]; - if (renderer == null) - continue; - m_Effects[i].iterate(m_CommandBuffer,renderer); - } - m_Effects[i].afterIterate(m_CommandBuffer); - } - - MainCamera.Instance.camera.AddCommandBuffer(CameraEvent.AfterImageEffectsOpaque, m_CommandBuffer); + return;
+ foreach (var cb in m_InUseCommandBuffers)
+ {
+ cb.Value.Clear();
+ ERenderingEvent re = cb.Key;
+ CameraEvent ce = CustomRenderingPipeline.RenderingEventToCameraEvent[re];
+ 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);
+ if (method == null)
+ continue;
+ method.Invoke(eff, new object[] { LensEffectBase.EStage.Before, cb.Value, null });
+ 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 });
+ }
+ method.Invoke(eff, new object[] { LensEffectBase.EStage.After, cb.Value, null });
+ } + MainCamera.Instance.camera.AddCommandBuffer(ce, cb.Value); + }
} private void OnRenderUnit() - { - MainCamera.Instance.camera.RemoveCommandBuffer(CameraEvent.AfterImageEffectsOpaque, m_CommandBuffer); - for(int i = 0; i < m_Effects.Count; ++i) - { - m_Effects[i].finishRender?.Invoke(); - } - } + {
+ foreach(var cb in m_InUseCommandBuffers)
+ {
+ CameraEvent ce = CustomRenderingPipeline.RenderingEventToCameraEvent[cb.Key];
+ MainCamera.Instance.camera.RemoveCommandBuffer(ce, cb.Value); + }
+ var temp = m_CachedCommandBuffers; + m_CachedCommandBuffers = m_InUseCommandBuffers;
+ m_InUseCommandBuffers = temp;
+ } private void OnWillRenderObj(BodyPartRenderer renderer) { @@ -111,7 +125,40 @@ public class UnitLensEffect : UnitComponent { } - static CommandBuffer ClaimCommandBuffer() + void PrepareCommandBuffers()
+ {
+ ERenderingEvent usedEvent = ERenderingEvent.None; + for (int i = 0; i < m_Effects.Count; ++i)
+ {
+ usedEvent |= m_Effects[i].renderingEvents;
+ }
+ foreach(ERenderingEvent evt in Enum.GetValues(typeof(ERenderingEvent)))
+ {
+ if (evt == ERenderingEvent.None)
+ continue;
+ if(usedEvent.HasFlag(evt))
+ {
+ CommandBuffer cb;
+ if(m_CachedCommandBuffers.TryGetValue(evt, out cb))
+ {
+ m_CachedCommandBuffers.Remove(evt);
+ }
+ else
+ {
+ cb = m_CommandBufferPool.Get();
+ cb.name = evt.ToString();
+ }
+ m_InUseCommandBuffers.Add(evt, cb);
+ }
+ }
+ foreach(var cb in m_CachedCommandBuffers)
+ {
+ m_CommandBufferPool.Release(cb.Value);
+ }
+ m_CachedCommandBuffers.Clear();
+ }
+
+ static CommandBuffer ClaimCommandBuffer() { CommandBuffer cb = m_CommandBufferPool.Get(); cb.Clear(); |