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