DepthOfFieldEffectManager.cs 2.7 KB

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