DepthOfFieldManager.cs 3.1 KB

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