XftCameraEffectComp.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Xft
  5. {
  6. [ExecuteInEditMode]
  7. public class XftCameraEffectComp : MonoBehaviour
  8. {
  9. private void Awake()
  10. {
  11. base.enabled = false;
  12. }
  13. public void ResetEvent(CameraEffectEvent e)
  14. {
  15. for (int i = 0; i < this.m_eventList.Count; i++)
  16. {
  17. if (this.m_eventList[i].EffectType == e.EffectType)
  18. {
  19. this.m_eventList.RemoveAt(i);
  20. break;
  21. }
  22. }
  23. if (this.m_eventList.Count == 0)
  24. {
  25. base.enabled = false;
  26. }
  27. }
  28. public void AddEvent(CameraEffectEvent e)
  29. {
  30. for (int i = 0; i < this.m_eventList.Count; i++)
  31. {
  32. if (this.m_eventList[i].EffectType == e.EffectType)
  33. {
  34. UnityEngine.Debug.LogWarning("can't add camera effect duplicated:" + e.EffectType);
  35. return;
  36. }
  37. }
  38. this.m_eventList.Add(e);
  39. this.m_eventList.Sort();
  40. base.enabled = true;
  41. }
  42. private void OnPreRender()
  43. {
  44. for (int i = 0; i < this.m_eventList.Count; i++)
  45. {
  46. this.m_eventList[i].OnPreRender();
  47. }
  48. }
  49. private void OnRenderImage(RenderTexture source, RenderTexture destination)
  50. {
  51. if (this.m_eventList.Count == 0)
  52. {
  53. return;
  54. }
  55. RenderTexture temporary = RenderTexture.GetTemporary(source.width, source.height, 0);
  56. RenderTexture temporary2 = RenderTexture.GetTemporary(source.width, source.height, 0);
  57. this.m_eventList[0].OnRenderImage(source, temporary);
  58. bool flag = true;
  59. for (int i = 1; i < this.m_eventList.Count; i++)
  60. {
  61. if (flag)
  62. {
  63. this.m_eventList[i].OnRenderImage(temporary, temporary2);
  64. temporary.DiscardContents();
  65. }
  66. else
  67. {
  68. this.m_eventList[i].OnRenderImage(temporary2, temporary);
  69. temporary2.DiscardContents();
  70. }
  71. flag = !flag;
  72. }
  73. if (flag)
  74. {
  75. Graphics.Blit(temporary, destination);
  76. }
  77. else
  78. {
  79. Graphics.Blit(temporary2, destination);
  80. }
  81. RenderTexture.ReleaseTemporary(temporary);
  82. RenderTexture.ReleaseTemporary(temporary2);
  83. }
  84. protected List<CameraEffectEvent> m_eventList = new List<CameraEffectEvent>();
  85. }
  86. }