BlurEffectManager.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.IO;
  2. using UnityEngine;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public class BlurEffectManager : IEffectManager
  6. {
  7. public const string header = "EFFECT_BLUR";
  8. private Blur Blur { get; set; }
  9. public bool Ready { get; private set; }
  10. public bool Active { get; private set; }
  11. private float initialBlurSize;
  12. private int initialBlurIterations;
  13. private int initialDownsample;
  14. private float blurSize;
  15. public float BlurSize
  16. {
  17. get => blurSize;
  18. set
  19. {
  20. blurSize = value;
  21. Blur.blurSize = blurSize / 10f;
  22. if (blurSize >= 3f)
  23. {
  24. Blur.blurSize -= 0.3f;
  25. Blur.blurIterations = 1;
  26. Blur.downsample = 1;
  27. }
  28. else
  29. {
  30. Blur.blurIterations = 0;
  31. Blur.downsample = 0;
  32. }
  33. }
  34. }
  35. public void Activate()
  36. {
  37. if (Blur == null)
  38. {
  39. Ready = true;
  40. Blur = GameMain.Instance.MainCamera.GetComponent<Blur>();
  41. initialBlurSize = Blur.blurSize;
  42. initialBlurIterations = Blur.blurIterations;
  43. initialDownsample = Blur.downsample;
  44. }
  45. SetEffectActive(false);
  46. }
  47. public void Deactivate()
  48. {
  49. BlurSize = 0f;
  50. Reset();
  51. Blur.enabled = false;
  52. Active = false;
  53. }
  54. public void Deserialize(BinaryReader binaryReader)
  55. {
  56. BlurSize = binaryReader.ReadSingle();
  57. SetEffectActive(binaryReader.ReadBoolean());
  58. }
  59. public void Serialize(BinaryWriter binaryWriter)
  60. {
  61. binaryWriter.Write(header);
  62. binaryWriter.Write(BlurSize);
  63. binaryWriter.Write(Active);
  64. }
  65. public void SetEffectActive(bool active)
  66. {
  67. if (Blur.enabled = Active = active) BlurSize = BlurSize;
  68. else Reset();
  69. }
  70. public void Reset()
  71. {
  72. Blur.blurSize = initialBlurSize;
  73. Blur.blurIterations = initialBlurIterations;
  74. Blur.downsample = initialDownsample;
  75. }
  76. public void Update() { }
  77. }
  78. }