BloomEffectManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. internal class BloomEffectManager : IEffectManager
  5. {
  6. public const string header = "EFFECT_BLOOM";
  7. private Bloom Bloom { get; set; }
  8. private float initialIntensity;
  9. private int initialBlurIterations;
  10. private Color initialThresholdColour;
  11. private Bloom.HDRBloomMode initialHDRBloomMode;
  12. public bool Ready { get; private set; }
  13. public bool Active { get; private set; }
  14. public float Intensity { get; set; }
  15. private int blurIterations;
  16. public int BlurIterations
  17. {
  18. get => blurIterations;
  19. set => blurIterations = Bloom.bloomBlurIterations = value;
  20. }
  21. public float BloomThresholdColorRed
  22. {
  23. get => BloomThresholdColour.r;
  24. set
  25. {
  26. Color colour = Bloom.bloomThreshholdColor;
  27. BloomThresholdColour = new Color(value, colour.g, colour.b);
  28. }
  29. }
  30. public float BloomThresholdColorGreen
  31. {
  32. get => BloomThresholdColour.g;
  33. set
  34. {
  35. Color colour = Bloom.bloomThreshholdColor;
  36. BloomThresholdColour = new Color(colour.r, value, colour.b);
  37. }
  38. }
  39. public float BloomThresholdColorBlue
  40. {
  41. get => BloomThresholdColour.b;
  42. set
  43. {
  44. Color colour = Bloom.bloomThreshholdColor;
  45. BloomThresholdColour = new Color(colour.r, colour.g, value);
  46. }
  47. }
  48. private Color bloomThresholdColour;
  49. public Color BloomThresholdColour
  50. {
  51. get => bloomThresholdColour;
  52. set => bloomThresholdColour = Bloom.bloomThreshholdColor = value;
  53. }
  54. private bool HDRBloomMode;
  55. public bool BloomHDR
  56. {
  57. get => HDRBloomMode;
  58. set
  59. {
  60. Bloom.hdr = value ? Bloom.HDRBloomMode.On : Bloom.HDRBloomMode.Auto;
  61. HDRBloomMode = value;
  62. }
  63. }
  64. public void Serialize(System.IO.BinaryWriter binaryWriter)
  65. {
  66. binaryWriter.Write(header);
  67. binaryWriter.Write(Intensity);
  68. binaryWriter.Write(BlurIterations);
  69. binaryWriter.WriteColour(BloomThresholdColour);
  70. binaryWriter.Write(BloomHDR);
  71. binaryWriter.Write(Active);
  72. }
  73. public void Deserialize(System.IO.BinaryReader binaryReader)
  74. {
  75. Intensity = binaryReader.ReadSingle();
  76. BlurIterations = binaryReader.ReadInt32();
  77. BloomThresholdColour = binaryReader.ReadColour();
  78. BloomHDR = binaryReader.ReadBoolean();
  79. SetEffectActive(binaryReader.ReadBoolean());
  80. }
  81. public void Activate()
  82. {
  83. if (Bloom == null)
  84. {
  85. Ready = true;
  86. Bloom = Utility.GetFieldValue<CameraMain, Bloom>(GameMain.Instance.MainCamera, "m_gcBloom");
  87. initialIntensity = Intensity = Bloom.bloomIntensity;
  88. initialBlurIterations = BlurIterations = Bloom.bloomBlurIterations;
  89. initialThresholdColour = BloomThresholdColour = Bloom.bloomThreshholdColor;
  90. initialHDRBloomMode = Bloom.hdr;
  91. BloomHDR = initialHDRBloomMode == Bloom.HDRBloomMode.On;
  92. }
  93. }
  94. public void Deactivate()
  95. {
  96. Intensity = initialIntensity;
  97. BlurIterations = initialBlurIterations;
  98. BloomThresholdColour = initialThresholdColour;
  99. BloomHDR = initialHDRBloomMode == Bloom.HDRBloomMode.On;
  100. BloomHDR = false;
  101. Bloom.enabled = true;
  102. Active = false;
  103. }
  104. public void Reset()
  105. {
  106. Bloom.bloomIntensity = initialIntensity;
  107. Bloom.bloomBlurIterations = initialBlurIterations;
  108. Bloom.bloomThreshholdColor = initialThresholdColour;
  109. Bloom.hdr = initialHDRBloomMode;
  110. }
  111. public void SetEffectActive(bool active)
  112. {
  113. Bloom.enabled = active;
  114. if (Active = active)
  115. {
  116. Bloom.bloomIntensity = Intensity;
  117. Bloom.bloomBlurIterations = BlurIterations;
  118. Bloom.bloomThreshholdColor = BloomThresholdColour;
  119. Bloom.hdr = BloomHDR ? Bloom.HDRBloomMode.On : Bloom.HDRBloomMode.Auto;
  120. }
  121. else Reset();
  122. }
  123. public void Update()
  124. {
  125. if (Active)
  126. {
  127. // Fuck this stupid shit
  128. // 2020/08/15 this stupid shit doesn't even work anymore
  129. // TODO: Fix this stupid shit
  130. Bloom.enabled = true;
  131. Bloom.bloomIntensity = Intensity;
  132. }
  133. }
  134. }
  135. }