BloomEffectManager.cs 4.9 KB

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