DepthOfFieldEffector.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. using UnityEngine.Rendering.PostProcessing;
  6. public enum ChangeMode
  7. {
  8. Immediate,
  9. Lerp
  10. }
  11. [RequireComponent(typeof(PostProcessVolume))]
  12. public class DepthOfFieldEffector : MonoBehaviour
  13. {
  14. [Tooltip("Control the focal distance with mouse scroll.")]
  15. public bool scrollControl = true;
  16. [Tooltip("Mode of changing depth of field.")]
  17. public ChangeMode changeMode = ChangeMode.Immediate;
  18. [Tooltip("If change mode is lerp, the speed at which to lerp.")]
  19. public float lerpSpeed = 0.5f ;
  20. // dof post effect
  21. DepthOfField depthOfFieldEffect = null;
  22. // values and delta values
  23. float focalLengthValue, dFocalLengthValue;
  24. float apertureValue, dApertureValue;
  25. float focusDistanceValue, dFocusDistanceValue;
  26. void Start()
  27. {
  28. PostProcessVolume volume = gameObject.GetComponent<PostProcessVolume>();
  29. volume.profile.TryGetSettings(out depthOfFieldEffect);
  30. focalLengthValue = dFocalLengthValue = depthOfFieldEffect.focalLength.value ;
  31. apertureValue = dApertureValue = depthOfFieldEffect.aperture.value ;
  32. focusDistanceValue = dFocusDistanceValue = depthOfFieldEffect.focusDistance.value ;
  33. }
  34. void Update()
  35. {
  36. if(depthOfFieldEffect == null)
  37. return;
  38. switch(changeMode)
  39. {
  40. case ChangeMode.Immediate:
  41. return;
  42. case ChangeMode.Lerp:
  43. LerpAndChangeValue(ref focalLengthValue, ref dFocalLengthValue, ref depthOfFieldEffect.focalLength.value);
  44. LerpAndChangeValue(ref apertureValue, ref dApertureValue, ref depthOfFieldEffect.aperture.value);
  45. LerpAndChangeValue(ref focusDistanceValue, ref dFocusDistanceValue, ref depthOfFieldEffect.focusDistance.value);
  46. break;
  47. }
  48. if(scrollControl)
  49. SetFocusDistance(dFocusDistanceValue + Input.mouseScrollDelta.y);
  50. }
  51. void LerpAndChangeValue(ref float currentValue, ref float deltaValue, ref float destinationValue)
  52. {
  53. // exit early if the values match
  54. if(currentValue == deltaValue)
  55. return;
  56. currentValue = Mathf.Lerp(currentValue, deltaValue, Time.deltaTime*lerpSpeed);
  57. // clamp if the value is close
  58. if(Mathf.Approximately(currentValue, deltaValue))
  59. currentValue = deltaValue;
  60. destinationValue = currentValue;
  61. }
  62. public void SetFocalLength(float value)
  63. {
  64. switch(changeMode)
  65. {
  66. case ChangeMode.Immediate:
  67. depthOfFieldEffect.focalLength.value = value;
  68. focalLengthValue = dFocalLengthValue = value;
  69. break;
  70. case ChangeMode.Lerp:
  71. dFocalLengthValue = value;
  72. break;
  73. }
  74. }
  75. public void SetAperture(float value)
  76. {
  77. switch(changeMode)
  78. {
  79. case ChangeMode.Immediate:
  80. depthOfFieldEffect.aperture.value = value;
  81. apertureValue = dApertureValue = value;
  82. break;
  83. case ChangeMode.Lerp:
  84. dApertureValue = value;
  85. break;
  86. }
  87. }
  88. public void SetFocusDistance(float value)
  89. {
  90. switch(changeMode)
  91. {
  92. case ChangeMode.Immediate:
  93. depthOfFieldEffect.focusDistance.value = value;
  94. focusDistanceValue = dFocusDistanceValue = value;
  95. break;
  96. case ChangeMode.Lerp:
  97. dFocusDistanceValue = value;
  98. break;
  99. }
  100. }
  101. }