DepthOfFieldManager.cs 3.0 KB

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