DepthOfFieldManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Serialize(System.IO.BinaryWriter binaryWriter)
  42. {
  43. binaryWriter.Write(header);
  44. binaryWriter.Write(FocalLength);
  45. binaryWriter.Write(FocalSize);
  46. binaryWriter.Write(Aperture);
  47. binaryWriter.Write(MaxBlurSize);
  48. binaryWriter.Write(VisualizeFocus);
  49. binaryWriter.Write(Active);
  50. }
  51. public void Deserialize(System.IO.BinaryReader binaryReader)
  52. {
  53. FocalLength = binaryReader.ReadSingle();
  54. FocalSize = binaryReader.ReadSingle();
  55. Aperture = binaryReader.ReadSingle();
  56. MaxBlurSize = binaryReader.ReadSingle();
  57. VisualizeFocus = binaryReader.ReadBoolean();
  58. SetEffectActive(binaryReader.ReadBoolean());
  59. }
  60. public void Activate()
  61. {
  62. if (DepthOfField == null)
  63. {
  64. Ready = true;
  65. DepthOfField = GameMain.Instance.MainCamera.GetOrAddComponent<DepthOfFieldScatter>();
  66. if (DepthOfField.dofHdrShader == null)
  67. {
  68. DepthOfField.dofHdrShader = Shader.Find("Hidden/Dof/DepthOfFieldHdr");
  69. }
  70. if (DepthOfField.dx11BokehShader == null)
  71. {
  72. DepthOfField.dx11BokehShader = Shader.Find("Hidden/Dof/DX11Dof");
  73. }
  74. if (DepthOfField.dx11BokehTexture == null)
  75. {
  76. DepthOfField.dx11BokehTexture = Resources.Load("Textures/hexShape") as Texture2D;
  77. }
  78. }
  79. SetEffectActive(false);
  80. }
  81. public void Deactivate()
  82. {
  83. FocalLength = initialValue;
  84. FocalSize = initialValue;
  85. Aperture = initialValue;
  86. MaxBlurSize = initialValue;
  87. VisualizeFocus = false;
  88. DepthOfField.enabled = false;
  89. Active = false;
  90. }
  91. public void Reset()
  92. {
  93. DepthOfField.focalLength = initialValue;
  94. DepthOfField.focalSize = initialValue;
  95. DepthOfField.aperture = initialValue;
  96. DepthOfField.maxBlurSize = initialValue;
  97. }
  98. public void SetEffectActive(bool active)
  99. {
  100. DepthOfField.enabled = active;
  101. if (Active = active)
  102. {
  103. DepthOfField.focalLength = FocalLength;
  104. DepthOfField.focalSize = FocalSize;
  105. DepthOfField.aperture = Aperture;
  106. DepthOfField.maxBlurSize = MaxBlurSize;
  107. }
  108. else Reset();
  109. }
  110. public void Update() { }
  111. }
  112. }