FogVolume.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace DynamicFogAndMist {
  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 DynamicFogProfile 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 targetFogColor1 = new Color (GRAY, GRAY, GRAY);
  22. [Tooltip ("Target fog color 2 when gamera enters this fog folume")]
  23. public Color targetFogColor2 = new Color (GRAY, GRAY, GRAY);
  24. [Tooltip ("Set this to zero for changing fog alpha immediately upon enter/exit fog volume.")]
  25. public float transitionDuration = 3.0f;
  26. [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.")]
  27. public Collider targetCollider;
  28. [Tooltip ("When enabled, a console message will be printed whenever this fog volume is entered or exited.")]
  29. public bool debugMode;
  30. [Tooltip ("Assign target Dynamic Fog component that will be affected by this volume.")]
  31. public DynamicFog targetFog;
  32. bool cameraInside;
  33. void Start () {
  34. if (targetFog == null)
  35. targetFog = DynamicFog.instance;
  36. if (targetFog != null)
  37. targetFog.useFogVolumes = true;
  38. }
  39. void OnTriggerEnter (Collider other) {
  40. if (cameraInside || targetFog == null)
  41. return;
  42. // Check if other collider has the main camera attached
  43. if (other == targetCollider || other.gameObject.transform.GetComponentInChildren<Camera> () == targetFog.fogCamera) {
  44. cameraInside = true;
  45. if (enableProfileTransition && targetProfile != null) {
  46. targetFog.SetTargetProfile (targetProfile, transitionDuration);
  47. }
  48. if (enableAlphaTransition) {
  49. targetFog.SetTargetAlpha (targetFogAlpha, targetSkyHazeAlpha, transitionDuration);
  50. }
  51. if (enableFogColorTransition) {
  52. targetFog.SetTargetColors (targetFogColor1, targetFogColor2, transitionDuration);
  53. }
  54. if (debugMode) {
  55. Debug.Log ("Fog Volume entered by " + other.name);
  56. }
  57. }
  58. }
  59. void OnTriggerExit (Collider other) {
  60. if (!cameraInside || targetFog == null)
  61. return;
  62. if (other == targetCollider || other.gameObject.transform.GetComponentInChildren<Camera> () == targetFog.fogCamera) {
  63. cameraInside = false;
  64. if (enableProfileTransition && targetProfile != null) {
  65. targetFog.ClearTargetProfile (transitionDuration);
  66. }
  67. if (enableAlphaTransition) {
  68. targetFog.ClearTargetAlpha (transitionDuration);
  69. }
  70. if (enableFogColorTransition) {
  71. targetFog.ClearTargetColors (transitionDuration);
  72. }
  73. if (debugMode) {
  74. Debug.Log ("Fog Volume exited by " + other.name);
  75. }
  76. }
  77. }
  78. }
  79. }