BaseRainScript.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //
  2. // Rain Maker (c) 2015 Digital Ruby, LLC
  3. // http://www.digitalruby.com
  4. //
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.Audio;
  10. namespace DigitalRuby.RainMaker
  11. {
  12. public class BaseRainScript : MonoBehaviour
  13. {
  14. [Tooltip("Camera the rain should hover over, defaults to main camera")]
  15. public Camera Camera;
  16. [Tooltip("Whether rain should follow the camera. If false, rain must be moved manually and will not follow the camera.")]
  17. public bool FollowCamera = true;
  18. [Tooltip("Light rain looping clip")]
  19. public AudioClip RainSoundLight;
  20. [Tooltip("Medium rain looping clip")]
  21. public AudioClip RainSoundMedium;
  22. [Tooltip("Heavy rain looping clip")]
  23. public AudioClip RainSoundHeavy;
  24. [Tooltip("AudoMixer used for the rain sound")]
  25. public AudioMixerGroup RainSoundAudioMixer;
  26. [Tooltip("Intensity of rain (0-1)")]
  27. [Range(0.0f, 1.0f)]
  28. public float RainIntensity;
  29. [Tooltip("Rain particle system")]
  30. public ParticleSystem RainFallParticleSystem;
  31. [Tooltip("Particles system for when rain hits something")]
  32. public ParticleSystem RainExplosionParticleSystem;
  33. [Tooltip("Particle system to use for rain mist")]
  34. public ParticleSystem RainMistParticleSystem;
  35. [Tooltip("The threshold for intensity (0 - 1) at which mist starts to appear")]
  36. [Range(0.0f, 1.0f)]
  37. public float RainMistThreshold = 0.5f;
  38. [Tooltip("Wind looping clip")]
  39. public AudioClip WindSound;
  40. [Tooltip("Wind sound volume modifier, use this to lower your sound if it's too loud.")]
  41. public float WindSoundVolumeModifier = 0.5f;
  42. [Tooltip("Wind zone that will affect and follow the rain")]
  43. public WindZone WindZone;
  44. [Tooltip("X = minimum wind speed. Y = maximum wind speed. Z = sound multiplier. Wind speed is divided by Z to get sound multiplier value. Set Z to lower than Y to increase wind sound volume, or higher to decrease wind sound volume.")]
  45. public Vector3 WindSpeedRange = new Vector3(50.0f, 500.0f, 500.0f);
  46. [Tooltip("How often the wind speed and direction changes (minimum and maximum change interval in seconds)")]
  47. public Vector2 WindChangeInterval = new Vector2(5.0f, 30.0f);
  48. [Tooltip("Whether wind should be enabled.")]
  49. public bool EnableWind = true;
  50. protected LoopingAudioSource audioSourceRainLight;
  51. protected LoopingAudioSource audioSourceRainMedium;
  52. protected LoopingAudioSource audioSourceRainHeavy;
  53. protected LoopingAudioSource audioSourceRainCurrent;
  54. protected LoopingAudioSource audioSourceWind;
  55. protected Material rainMaterial;
  56. protected Material rainExplosionMaterial;
  57. protected Material rainMistMaterial;
  58. private float lastRainIntensityValue = -1.0f;
  59. private float nextWindTime;
  60. private void UpdateWind()
  61. {
  62. if (EnableWind && WindZone != null && WindSpeedRange.y > 1.0f)
  63. {
  64. WindZone.gameObject.SetActive(true);
  65. if (FollowCamera)
  66. {
  67. WindZone.transform.position = Camera.transform.position;
  68. }
  69. if (!Camera.orthographic)
  70. {
  71. WindZone.transform.Translate(0.0f, WindZone.radius, 0.0f);
  72. }
  73. if (nextWindTime < Time.time)
  74. {
  75. WindZone.windMain = UnityEngine.Random.Range(WindSpeedRange.x, WindSpeedRange.y);
  76. WindZone.windTurbulence = UnityEngine.Random.Range(WindSpeedRange.x, WindSpeedRange.y);
  77. if (Camera.orthographic)
  78. {
  79. int val = UnityEngine.Random.Range(0, 2);
  80. WindZone.transform.rotation = Quaternion.Euler(0.0f, (val == 0 ? 90.0f : -90.0f), 0.0f);
  81. }
  82. else
  83. {
  84. WindZone.transform.rotation = Quaternion.Euler(UnityEngine.Random.Range(-30.0f, 30.0f), UnityEngine.Random.Range(0.0f, 360.0f), 0.0f);
  85. }
  86. nextWindTime = Time.time + UnityEngine.Random.Range(WindChangeInterval.x, WindChangeInterval.y);
  87. if (audioSourceWind != default)
  88. {
  89. audioSourceWind.Play((WindZone.windMain / WindSpeedRange.z) * WindSoundVolumeModifier);
  90. }
  91. }
  92. }
  93. else
  94. {
  95. if (WindZone != null)
  96. {
  97. WindZone.gameObject.SetActive(false);
  98. }
  99. audioSourceWind.Stop();
  100. }
  101. if (audioSourceWind != default)
  102. {
  103. audioSourceWind.Update();
  104. }
  105. }
  106. private void CheckForRainChange()
  107. {
  108. if (lastRainIntensityValue != RainIntensity)
  109. {
  110. lastRainIntensityValue = RainIntensity;
  111. if (RainIntensity <= 0.01f)
  112. {
  113. if (audioSourceRainCurrent != null)
  114. {
  115. audioSourceRainCurrent.Stop();
  116. audioSourceRainCurrent = null;
  117. }
  118. if (RainFallParticleSystem != null)
  119. {
  120. ParticleSystem.EmissionModule e = RainFallParticleSystem.emission;
  121. e.enabled = false;
  122. RainFallParticleSystem.Stop();
  123. }
  124. if (RainMistParticleSystem != null)
  125. {
  126. ParticleSystem.EmissionModule e = RainMistParticleSystem.emission;
  127. e.enabled = false;
  128. RainMistParticleSystem.Stop();
  129. }
  130. }
  131. else
  132. {
  133. LoopingAudioSource newSource;
  134. if (RainIntensity >= 0.67f)
  135. {
  136. newSource = audioSourceRainHeavy;
  137. }
  138. else if (RainIntensity >= 0.33f)
  139. {
  140. newSource = audioSourceRainMedium;
  141. }
  142. else
  143. {
  144. newSource = audioSourceRainLight;
  145. }
  146. if (audioSourceRainCurrent != newSource)
  147. {
  148. if (audioSourceRainCurrent != null)
  149. {
  150. audioSourceRainCurrent.Stop();
  151. }
  152. audioSourceRainCurrent = newSource;
  153. audioSourceRainCurrent.Play(1.0f);
  154. }
  155. if (RainFallParticleSystem != null)
  156. {
  157. ParticleSystem.EmissionModule e = RainFallParticleSystem.emission;
  158. e.enabled = RainFallParticleSystem.GetComponent<Renderer>().enabled = true;
  159. if (!RainFallParticleSystem.isPlaying)
  160. {
  161. RainFallParticleSystem.Play();
  162. }
  163. ParticleSystem.MinMaxCurve rate = e.rateOverTime;
  164. rate.mode = ParticleSystemCurveMode.Constant;
  165. rate.constantMin = rate.constantMax = RainFallEmissionRate();
  166. e.rateOverTime = rate;
  167. }
  168. if (RainMistParticleSystem != null)
  169. {
  170. ParticleSystem.EmissionModule e = RainMistParticleSystem.emission;
  171. e.enabled = RainMistParticleSystem.GetComponent<Renderer>().enabled = true;
  172. if (!RainMistParticleSystem.isPlaying)
  173. {
  174. RainMistParticleSystem.Play();
  175. }
  176. float emissionRate;
  177. if (RainIntensity < RainMistThreshold)
  178. {
  179. emissionRate = 0.0f;
  180. }
  181. else
  182. {
  183. // must have RainMistThreshold or higher rain intensity to start seeing mist
  184. emissionRate = MistEmissionRate();
  185. }
  186. ParticleSystem.MinMaxCurve rate = e.rateOverTime;
  187. rate.mode = ParticleSystemCurveMode.Constant;
  188. rate.constantMin = rate.constantMax = emissionRate;
  189. e.rateOverTime = rate;
  190. }
  191. }
  192. }
  193. }
  194. protected virtual void Start()
  195. {
  196. #if DEBUG
  197. if (RainFallParticleSystem == null)
  198. {
  199. Debug.LogError("Rain fall particle system must be set to a particle system");
  200. return;
  201. }
  202. #endif
  203. if (Camera == null)
  204. {
  205. Camera = Camera.main;
  206. }
  207. audioSourceRainLight = new LoopingAudioSource(this, RainSoundLight, RainSoundAudioMixer);
  208. audioSourceRainMedium = new LoopingAudioSource(this, RainSoundMedium, RainSoundAudioMixer);
  209. audioSourceRainHeavy = new LoopingAudioSource(this, RainSoundHeavy, RainSoundAudioMixer);
  210. audioSourceWind = new LoopingAudioSource(this, WindSound, RainSoundAudioMixer);
  211. if (RainFallParticleSystem != null)
  212. {
  213. ParticleSystem.EmissionModule e = RainFallParticleSystem.emission;
  214. e.enabled = false;
  215. Renderer rainRenderer = RainFallParticleSystem.GetComponent<Renderer>();
  216. rainRenderer.enabled = false;
  217. rainMaterial = new Material(rainRenderer.material);
  218. rainMaterial.EnableKeyword("SOFTPARTICLES_OFF");
  219. rainRenderer.material = rainMaterial;
  220. }
  221. if (RainExplosionParticleSystem != null)
  222. {
  223. ParticleSystem.EmissionModule e = RainExplosionParticleSystem.emission;
  224. e.enabled = false;
  225. Renderer rainRenderer = RainExplosionParticleSystem.GetComponent<Renderer>();
  226. rainExplosionMaterial = new Material(rainRenderer.material);
  227. rainExplosionMaterial.EnableKeyword("SOFTPARTICLES_OFF");
  228. rainRenderer.material = rainExplosionMaterial;
  229. }
  230. if (RainMistParticleSystem != null)
  231. {
  232. ParticleSystem.EmissionModule e = RainMistParticleSystem.emission;
  233. e.enabled = false;
  234. Renderer rainRenderer = RainMistParticleSystem.GetComponent<Renderer>();
  235. rainRenderer.enabled = false;
  236. rainMistMaterial = new Material(rainRenderer.material);
  237. if (UseRainMistSoftParticles)
  238. {
  239. rainMistMaterial.EnableKeyword("SOFTPARTICLES_ON");
  240. }
  241. else
  242. {
  243. rainMistMaterial.EnableKeyword("SOFTPARTICLES_OFF");
  244. }
  245. rainRenderer.material = rainMistMaterial;
  246. }
  247. }
  248. protected virtual void Update()
  249. {
  250. #if DEBUG
  251. if (RainFallParticleSystem == null)
  252. {
  253. Debug.LogError("Rain fall particle system must be set to a particle system");
  254. return;
  255. }
  256. #endif
  257. CheckForRainChange();
  258. UpdateWind();
  259. if (audioSourceRainLight != default)
  260. {
  261. audioSourceRainLight.Update();
  262. audioSourceRainMedium.Update();
  263. audioSourceRainHeavy.Update();
  264. }
  265. }
  266. protected virtual float RainFallEmissionRate()
  267. {
  268. return (RainFallParticleSystem.main.maxParticles / RainFallParticleSystem.main.startLifetime.constant) * RainIntensity;
  269. }
  270. protected virtual float MistEmissionRate()
  271. {
  272. return (RainMistParticleSystem.main.maxParticles / RainMistParticleSystem.main.startLifetime.constant) * RainIntensity * RainIntensity;
  273. }
  274. protected virtual bool UseRainMistSoftParticles
  275. {
  276. get
  277. {
  278. return true;
  279. }
  280. }
  281. }
  282. /// <summary>
  283. /// Provides an easy wrapper to looping audio sources with nice transitions for volume when starting and stopping
  284. /// </summary>
  285. public class LoopingAudioSource
  286. {
  287. public AudioSource AudioSource { get; private set; }
  288. public float TargetVolume { get; private set; }
  289. public LoopingAudioSource(MonoBehaviour script, AudioClip clip, AudioMixerGroup mixer)
  290. {
  291. AudioSource = script.gameObject.AddComponent<AudioSource>();
  292. if (mixer != null)
  293. {
  294. AudioSource.outputAudioMixerGroup = mixer;
  295. }
  296. AudioSource.loop = true;
  297. AudioSource.clip = clip;
  298. AudioSource.playOnAwake = false;
  299. AudioSource.volume = 0.0f;
  300. AudioSource.Stop();
  301. TargetVolume = 1.0f;
  302. }
  303. public void Play(float targetVolume)
  304. {
  305. if (!AudioSource.isPlaying)
  306. {
  307. AudioSource.volume = 0.0f;
  308. AudioSource.Play();
  309. }
  310. TargetVolume = targetVolume;
  311. }
  312. public void Stop()
  313. {
  314. TargetVolume = 0.0f;
  315. }
  316. public void Update()
  317. {
  318. if (AudioSource.isPlaying && (AudioSource.volume = Mathf.Lerp(AudioSource.volume, TargetVolume, Time.deltaTime)) == 0.0f)
  319. {
  320. AudioSource.Stop();
  321. }
  322. }
  323. }
  324. }