DepthOfFieldManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using UnityEngine;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. internal class DepthOfFieldEffectManager : IEffectManager
  5. {
  6. private DepthOfFieldScatter DepthOfField { get; set; }
  7. public bool IsReady { get; private set; }
  8. public bool IsActive { get; private set; }
  9. private readonly float initialValue = 0f;
  10. private float focalLength;
  11. public float FocalLength
  12. {
  13. get => focalLength;
  14. set => focalLength = DepthOfField.focalLength = value;
  15. }
  16. private float focalSize;
  17. public float FocalSize
  18. {
  19. get => focalSize;
  20. set => focalSize = DepthOfField.focalSize = value;
  21. }
  22. private float aperture;
  23. public float Aperture
  24. {
  25. get => aperture;
  26. set => aperture = DepthOfField.aperture = value;
  27. }
  28. private float maxBlurSize;
  29. public float MaxBlurSize
  30. {
  31. get => maxBlurSize;
  32. set => maxBlurSize = DepthOfField.maxBlurSize = value;
  33. }
  34. private bool visualizeFocus;
  35. public bool VisualizeFocus
  36. {
  37. get => visualizeFocus;
  38. set => visualizeFocus = DepthOfField.visualizeFocus = value;
  39. }
  40. public void Activate()
  41. {
  42. if (DepthOfField == null)
  43. {
  44. IsReady = true;
  45. DepthOfField = GameMain.Instance.MainCamera.GetOrAddComponent<DepthOfFieldScatter>();
  46. if (DepthOfField.dofHdrShader == null)
  47. {
  48. DepthOfField.dofHdrShader = Shader.Find("Hidden/Dof/DepthOfFieldHdr");
  49. }
  50. if (DepthOfField.dx11BokehShader == null)
  51. {
  52. DepthOfField.dx11BokehShader = Shader.Find("Hidden/Dof/DX11Dof");
  53. }
  54. if (DepthOfField.dx11BokehTexture == null)
  55. {
  56. DepthOfField.dx11BokehTexture = Resources.Load("Textures/hexShape") as Texture2D;
  57. }
  58. }
  59. }
  60. public void Deactivate()
  61. {
  62. FocalLength = initialValue;
  63. FocalSize = initialValue;
  64. Aperture = initialValue;
  65. MaxBlurSize = initialValue;
  66. VisualizeFocus = false;
  67. DepthOfField.enabled = false;
  68. IsActive = false;
  69. }
  70. public void Reset()
  71. {
  72. DepthOfField.focalLength = initialValue;
  73. DepthOfField.focalSize = initialValue;
  74. DepthOfField.aperture = initialValue;
  75. DepthOfField.maxBlurSize = initialValue;
  76. }
  77. public void SetEffectActive(bool active)
  78. {
  79. DepthOfField.enabled = active;
  80. this.IsActive = active;
  81. if (this.IsActive)
  82. {
  83. DepthOfField.focalLength = FocalLength;
  84. DepthOfField.focalSize = FocalSize;
  85. DepthOfField.aperture = Aperture;
  86. DepthOfField.maxBlurSize = MaxBlurSize;
  87. }
  88. else Reset();
  89. }
  90. public void Update() { }
  91. }
  92. }