BloomEffectManager.cs 4.7 KB

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