BloomEffectManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Reflection;
  2. using UnityEngine;
  3. namespace COM3D2.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 Serialize(System.IO.BinaryWriter binaryWriter)
  84. {
  85. binaryWriter.Write(header);
  86. binaryWriter.Write(BloomValue);
  87. binaryWriter.Write(BlurIterations);
  88. binaryWriter.WriteColour(BloomThresholdColour);
  89. binaryWriter.Write(BloomHDR);
  90. binaryWriter.Write(Active);
  91. }
  92. public void Deserialize(System.IO.BinaryReader binaryReader)
  93. {
  94. BloomValue = binaryReader.ReadSingle();
  95. BlurIterations = binaryReader.ReadInt32();
  96. BloomThresholdColour = binaryReader.ReadColour();
  97. BloomHDR = binaryReader.ReadBoolean();
  98. SetEffectActive(binaryReader.ReadBoolean());
  99. }
  100. public void Activate()
  101. {
  102. if (Bloom == null)
  103. {
  104. Ready = true;
  105. Bloom = GameMain.Instance.MainCamera.GetComponent<Bloom>();
  106. initialIntensity = bloomValue = 50f;
  107. initialBlurIterations = blurIterations = Bloom.bloomBlurIterations;
  108. initialThresholdColour = bloomThresholdColour = Bloom.bloomThreshholdColor;
  109. initialHDRBloomMode = Bloom.hdr;
  110. bloomHdr = Bloom.hdr == Bloom.HDRBloomMode.On;
  111. backupBloomValue = GameMain.Instance.CMSystem.BloomValue;
  112. }
  113. SetEffectActive(false);
  114. }
  115. public void Deactivate()
  116. {
  117. BloomValue = initialIntensity;
  118. BlurIterations = initialBlurIterations;
  119. BloomThresholdColour = initialThresholdColour;
  120. BloomHDR = initialHDRBloomMode == Bloom.HDRBloomMode.On;
  121. BloomHDR = false;
  122. Active = false;
  123. BloomDefIntensity = backup_m_fBloomDefIntensity;
  124. GameMain.Instance.CMSystem.BloomValue = backupBloomValue;
  125. }
  126. public void Reset()
  127. {
  128. GameMain.Instance.CMSystem.BloomValue = backupBloomValue;
  129. Bloom.bloomBlurIterations = initialBlurIterations;
  130. Bloom.bloomThreshholdColor = initialThresholdColour;
  131. Bloom.hdr = initialHDRBloomMode;
  132. BloomDefIntensity = backup_m_fBloomDefIntensity;
  133. }
  134. public void SetEffectActive(bool active)
  135. {
  136. if (Active = active)
  137. {
  138. backupBloomValue = GameMain.Instance.CMSystem.BloomValue;
  139. GameMain.Instance.CMSystem.BloomValue = (int)BloomValue;
  140. Bloom.bloomBlurIterations = BlurIterations;
  141. Bloom.bloomThreshholdColor = BloomThresholdColour;
  142. Bloom.hdr = BloomHDR ? Bloom.HDRBloomMode.On : Bloom.HDRBloomMode.Auto;
  143. BloomDefIntensity = bloomDefIntensity;
  144. }
  145. else Reset();
  146. }
  147. public void Update() { }
  148. }
  149. }