BloomEffectManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System.Reflection;
  2. using UnityEngine;
  3. namespace MeidoPhotoStudio.Plugin
  4. {
  5. public class BloomEffectManager : IEffectManager
  6. {
  7. public const string header = "EFFECT_BLOOM";
  8. private const float bloomDefIntensity = 5.7f;
  9. private static readonly CameraMain camera = GameMain.Instance.MainCamera;
  10. private Bloom Bloom { get; set; }
  11. // CMSystem's bloomValue;
  12. private static int backupBloomValue;
  13. private static readonly float backup_m_fBloomDefIntensity;
  14. private static readonly FieldInfo m_fBloomDefIntensity
  15. = Utility.GetFieldInfo<CameraMain>("m_fBloomDefIntensity");
  16. private static float BloomDefIntensity
  17. {
  18. set => m_fBloomDefIntensity.SetValue(camera, value);
  19. get => (float)m_fBloomDefIntensity.GetValue(camera);
  20. }
  21. private float initialIntensity;
  22. private int initialBlurIterations;
  23. private Color initialThresholdColour;
  24. private Bloom.HDRBloomMode initialHDRBloomMode;
  25. public bool Ready { get; private set; }
  26. public bool Active { get; private set; }
  27. private float bloomValue;
  28. public float BloomValue
  29. {
  30. get => bloomValue;
  31. set => GameMain.Instance.CMSystem.BloomValue = (int)(bloomValue = value);
  32. }
  33. private int blurIterations;
  34. public int BlurIterations
  35. {
  36. get => blurIterations;
  37. set => blurIterations = Bloom.bloomBlurIterations = value;
  38. }
  39. public float BloomThresholdColorRed
  40. {
  41. get => BloomThresholdColour.r;
  42. set
  43. {
  44. Color colour = Bloom.bloomThreshholdColor;
  45. BloomThresholdColour = new Color(value, colour.g, colour.b);
  46. }
  47. }
  48. public float BloomThresholdColorGreen
  49. {
  50. get => BloomThresholdColour.g;
  51. set
  52. {
  53. Color colour = Bloom.bloomThreshholdColor;
  54. BloomThresholdColour = new Color(colour.r, value, colour.b);
  55. }
  56. }
  57. public float BloomThresholdColorBlue
  58. {
  59. get => BloomThresholdColour.b;
  60. set
  61. {
  62. Color colour = Bloom.bloomThreshholdColor;
  63. BloomThresholdColour = new Color(colour.r, colour.g, value);
  64. }
  65. }
  66. private Color bloomThresholdColour;
  67. public Color BloomThresholdColour
  68. {
  69. get => bloomThresholdColour;
  70. set => bloomThresholdColour = Bloom.bloomThreshholdColor = value;
  71. }
  72. private bool bloomHdr;
  73. public bool BloomHDR
  74. {
  75. get => bloomHdr;
  76. set
  77. {
  78. Bloom.hdr = value ? Bloom.HDRBloomMode.On : Bloom.HDRBloomMode.Auto;
  79. bloomHdr = value;
  80. }
  81. }
  82. static BloomEffectManager() => backup_m_fBloomDefIntensity = BloomDefIntensity;
  83. public void Activate()
  84. {
  85. if (Bloom == null)
  86. {
  87. Ready = true;
  88. Bloom = GameMain.Instance.MainCamera.GetComponent<Bloom>();
  89. initialIntensity = bloomValue = 50f;
  90. initialBlurIterations = blurIterations = Bloom.bloomBlurIterations;
  91. initialThresholdColour = bloomThresholdColour = Bloom.bloomThreshholdColor;
  92. initialHDRBloomMode = Bloom.hdr;
  93. bloomHdr = Bloom.hdr == Bloom.HDRBloomMode.On;
  94. backupBloomValue = GameMain.Instance.CMSystem.BloomValue;
  95. }
  96. SetEffectActive(false);
  97. }
  98. public void Deactivate()
  99. {
  100. BloomValue = initialIntensity;
  101. BlurIterations = initialBlurIterations;
  102. BloomThresholdColour = initialThresholdColour;
  103. BloomHDR = initialHDRBloomMode == Bloom.HDRBloomMode.On;
  104. BloomHDR = false;
  105. Active = false;
  106. BloomDefIntensity = backup_m_fBloomDefIntensity;
  107. GameMain.Instance.CMSystem.BloomValue = backupBloomValue;
  108. }
  109. public void Reset()
  110. {
  111. GameMain.Instance.CMSystem.BloomValue = backupBloomValue;
  112. Bloom.bloomBlurIterations = initialBlurIterations;
  113. Bloom.bloomThreshholdColor = initialThresholdColour;
  114. Bloom.hdr = initialHDRBloomMode;
  115. BloomDefIntensity = backup_m_fBloomDefIntensity;
  116. }
  117. public void SetEffectActive(bool active)
  118. {
  119. if (Active = active)
  120. {
  121. backupBloomValue = GameMain.Instance.CMSystem.BloomValue;
  122. GameMain.Instance.CMSystem.BloomValue = (int)BloomValue;
  123. Bloom.bloomBlurIterations = BlurIterations;
  124. Bloom.bloomThreshholdColor = BloomThresholdColour;
  125. Bloom.hdr = BloomHDR ? Bloom.HDRBloomMode.On : Bloom.HDRBloomMode.Auto;
  126. BloomDefIntensity = bloomDefIntensity;
  127. }
  128. else Reset();
  129. }
  130. public void Update() { }
  131. }
  132. }