FogVolume.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace VolumetricFogAndMist {
  4. public class FogVolume : MonoBehaviour {
  5. const float GRAY = 227f / 255f;
  6. [Tooltip ("Enables transition to a given profile.")]
  7. public bool enableProfileTransition;
  8. [Tooltip ("Assign the transition profile.")]
  9. public VolumetricFogProfile targetProfile;
  10. [Tooltip ("Enables alpha transition.")]
  11. public bool enableAlphaTransition;
  12. [Tooltip ("Target alpha for fog when camera enters this fog volume")]
  13. [Range (0, 1)]
  14. public float targetFogAlpha = 0.5f;
  15. [Tooltip ("Target alpha for sky haze when camera enters this fog volume")]
  16. [Range (0, 1)]
  17. public float targetSkyHazeAlpha = 0.5f;
  18. [Tooltip ("Enables fog color transition.")]
  19. public bool enableFogColorTransition;
  20. [Tooltip ("Target fog color 1 when gamera enters this fog folume")]
  21. public Color targetFogColor = new Color (GRAY, GRAY, GRAY);
  22. [Tooltip ("Enables fog specular color transition.")]
  23. public bool enableFogSpecularColorTransition;
  24. [Tooltip ("Target fog color 2 when gamera enters this fog folume")]
  25. public Color targetFogSpecularColor = new Color (GRAY, GRAY, GRAY);
  26. [Tooltip ("Enables light color transition.")]
  27. public bool enableLightColorTransition;
  28. [Tooltip ("Target light color when gamera enters this fog folume")]
  29. public Color targetLightColor = Color.white;
  30. [Tooltip ("Set this to zero for changing fog alpha immediately upon enter/exit fog volume.")]
  31. public float transitionDuration = 3.0f;
  32. [Tooltip ("Set collider that will trigger this fog volume. If not set, this fog volume will react to any collider which has the main camera. If you use a third person controller, assign the character collider here.")]
  33. public Collider targetCollider;
  34. [Tooltip("When enabled, a console message will be printed whenever this fog volume is entered or exited.")]
  35. public bool debugMode;
  36. [Tooltip("Assign target Volumetric Fog component that will be affected by this volume.")]
  37. public VolumetricFog targetFog;
  38. bool cameraInside;
  39. void Start () {
  40. if (targetFog==null) targetFog = VolumetricFog.instance;
  41. if (targetFog!=null) targetFog.useFogVolumes = true;
  42. }
  43. void OnTriggerEnter (Collider other) {
  44. if (cameraInside || targetFog==null)
  45. return;
  46. // Check if other collider has the main camera attached
  47. if (other == targetCollider || other.gameObject.transform.GetComponentInChildren<Camera> () == targetFog.fogCamera) {
  48. cameraInside = true;
  49. if (enableProfileTransition && targetProfile != null) {
  50. targetFog.SetTargetProfile (targetProfile, transitionDuration);
  51. }
  52. if (enableAlphaTransition) {
  53. targetFog.SetTargetAlpha (targetFogAlpha, targetSkyHazeAlpha, transitionDuration);
  54. }
  55. if (enableFogColorTransition) {
  56. targetFog.SetTargetColor (targetFogColor, transitionDuration);
  57. }
  58. if (enableFogSpecularColorTransition) {
  59. targetFog.SetTargetSpecularColor (targetFogSpecularColor, transitionDuration);
  60. }
  61. if (enableLightColorTransition) {
  62. targetFog.SetTargetLightColor (targetLightColor, transitionDuration);
  63. }
  64. if (debugMode) {
  65. Debug.Log("Fog Volume entered by " + other.name);
  66. }
  67. }
  68. }
  69. void OnTriggerExit (Collider other) {
  70. if (!cameraInside || targetFog==null)
  71. return;
  72. if (other == targetCollider || other.gameObject.transform.GetComponentInChildren<Camera> () == targetFog.fogCamera) {
  73. cameraInside = false;
  74. if (enableProfileTransition && targetProfile != null) {
  75. targetFog.ClearTargetProfile (transitionDuration);
  76. }
  77. if (enableAlphaTransition)
  78. targetFog.ClearTargetAlpha (transitionDuration);
  79. if (enableFogColorTransition)
  80. targetFog.ClearTargetColor (transitionDuration);
  81. if (enableFogSpecularColorTransition)
  82. targetFog.ClearTargetSpecularColor (transitionDuration);
  83. if (enableLightColorTransition)
  84. targetFog.ClearTargetLightColor (transitionDuration);
  85. if (debugMode) {
  86. Debug.Log("Fog Volume exited by " + other.name);
  87. }
  88. }
  89. }
  90. }
  91. }