-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather.cs
More file actions
238 lines (195 loc) · 5.95 KB
/
Weather.cs
File metadata and controls
238 lines (195 loc) · 5.95 KB
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
using UnityEngine;
using System.Collections;
public class Weather : MonoBehaviour {
// Objects
private Transform myTransform;
private Transform globalLight;
private MeshRenderer clouds;
private MeshRenderer clouds2;
// Colors
[HideInInspector]
public Color ambientColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
private Color ambientColorMorning = new Color(0.5f, 0.3f, 0.1f, 1.0f);
private Color ambientColorNoon = new Color(1.0f, 1.0f, 0.7f, 1.0f);
private Color ambientColorEvening = new Color(0.5f, 0.15f, 0.1f, 1.0f);
private Color ambientColorNight = new Color(0.0f, 0.1f, 0.5f, 1.0f);
// Time
public bool timeOn = true;
public float timeCurrent = 0.0f;
private float timeOld = -1.0f;
private float dayLength = 12.0f;
// Clouds
private bool cloudsOn = false;
private float cloudsAlpha = 0.0f;
private float cloudsMax = 0.5f;
private float cloudsSpeed = 2.0f;
private float cloudsMove = 0.01f;
private Vector2 cloudsOffset = Vector2.zero;
private float cloudsMove2 = 0.005f;
private Vector2 cloudsOffset2 = Vector2.zero;
// Sea
private Transform sea;
private MeshRenderer seaNormal;
private float seaMove = 0.05f;
private Vector2 seaOffset = Vector2.zero;
private MeshRenderer seaFoam;
private float seaMoveFoam = 0.03f;
private Vector2 seaOffsetFoam = Vector2.zero;
void Start() {
myTransform = transform;
globalLight = myTransform.Find("Weather/Lights/Light");
clouds = myTransform.Find("Weather/Effects/Clouds/Clouds").GetComponent<MeshRenderer>();
clouds2 = myTransform.Find("Weather/Effects/Clouds/Clouds2").GetComponent<MeshRenderer>();
sea = myTransform.Find("Weather/Effects/Sea");
seaNormal = sea.Find("SeaNormal").GetComponent<MeshRenderer>();
seaFoam = sea.Find("SeaFoam").GetComponent<MeshRenderer>();
// Startup setup
Global.system.isDay = false;
timeCurrent = 12;
TimeCycle();
Lights();
}
void Update() {
if(!Global.system.isPaused) {
TimeCycle();
Lights();
CloudsAlpha();
}
CloudsMove();
SeaMove();
}
// TIME
void TimeCycle() {
if(timeOn && Global.system.playerMove) {
timeCurrent = dayLength * (Global.system.timePercentage / 100.0f);
}
else {
timeCurrent = Mathf.Round(timeCurrent);
}
timeCurrent = Mathf.Clamp(timeCurrent, 0.0f, dayLength);
}
public void TimeOn(bool value) {
timeOn = value;
}
// LIGHTS
void Lights() {
// If time has changed (player is moving)
if(timeOld != timeCurrent) {
float rotY = 0.0f;
float rotX = 50.0f;
float rotZ = 0.0f;
float lightIntensity = 0.0f;
float shadowIntensity = 0.0f;
if(!Global.system.isDay) {
// Noon -> Evening
if(timeCurrent < dayLength / 2.0f) {
lightIntensity = 1.0f - (((timeCurrent * 2.0f) / dayLength) / 2.0f);
shadowIntensity = 1.0f - ((timeCurrent * 2.0f) / dayLength);
ambientColor = Color.Lerp(ambientColorNoon, ambientColorEvening, (timeCurrent * 2.0f) / dayLength);
}
// Evening -> Night
else {
lightIntensity = 0.5f;
shadowIntensity = 0.0f;
ambientColor = Color.Lerp(ambientColorNight, ambientColorEvening, 2.0f - ((timeCurrent * 2.0f) / dayLength));
}
// Rotate light
rotY = 0.0f + (180.0f * (timeCurrent / dayLength));
}
else {
// Night -> Morning
if(timeCurrent < dayLength / 2.0f) {
lightIntensity = 0.5f;
shadowIntensity = 0.0f;
ambientColor = Color.Lerp(ambientColorNight, ambientColorMorning, (timeCurrent * 2.0f) / dayLength);
}
// Morning -> Noon
else {
lightIntensity = 0.5f + ((timeCurrent - (dayLength / 2.0f)) / (dayLength / 2.0f) / 2.0f);
shadowIntensity = 0.0f + (timeCurrent - (dayLength / 2.0f)) / (dayLength / 2.0f);
ambientColor = Color.Lerp(ambientColorNoon, ambientColorMorning, 2.0f - ((timeCurrent * 2.0f) / dayLength));
}
// Rotate light
rotY = 180.0f + (180.0f * (timeCurrent / dayLength));
}
// Set lights
globalLight.rotation = Quaternion.Euler(rotX, rotY, rotZ);
globalLight.GetComponent<Light>().intensity = lightIntensity;
globalLight.GetComponent<Light>().shadowStrength = shadowIntensity;
RenderSettings.ambientLight = ambientColor;
// Time has changed
timeOld = timeCurrent;
}
}
// CLOUDS
public void CloudsActivate(bool enable) {
clouds.gameObject.SetActive(enable);
clouds2.gameObject.SetActive(enable);
cloudsOn = enable;
cloudsAlpha = 0.0f;
}
void CloudsAlpha() {
if(cloudsOn) {
if(cloudsAlpha >= cloudsMax) {
cloudsAlpha = cloudsMax;
}
else {
cloudsAlpha += cloudsSpeed * Time.deltaTime;
}
Color cloudsColor = clouds.material.color;
cloudsColor.a = cloudsAlpha;
clouds.material.color = cloudsColor;
clouds2.material.color = cloudsColor;
}
}
void CloudsMove() {
if(cloudsOn) {
// 1
cloudsOffset.x += cloudsMove * Time.deltaTime;
cloudsOffset.y += cloudsMove * Time.deltaTime;
if(cloudsOffset.x > 1.0f) {
cloudsOffset.x = 0.0f;
}
if(cloudsOffset.y > 1.0f) {
cloudsOffset.y = 0.0f;
}
clouds.material.SetTextureOffset("_MainTex", cloudsOffset);
// 2
cloudsOffset2.x += cloudsMove2 * Time.deltaTime;
cloudsOffset2.y += 0.00005f + cloudsMove2 * Time.deltaTime;
if(cloudsOffset2.x > 1.0f) {
cloudsOffset2.x = 0.0f;
}
if(cloudsOffset2.y > 1.0f) {
cloudsOffset2.y = 0.0f;
}
clouds2.material.SetTextureOffset("_MainTex", cloudsOffset2);
}
}
void SeaMove() {
sea.gameObject.SetActive(!Global.system.battle);
if(!Global.system.battle) {
// Normal
seaOffset.x -= seaMove * Time.deltaTime;
seaOffset.y -= seaMove * Time.deltaTime;
if(seaOffset.x < 0.0f) {
seaOffset.x = 1.0f;
}
if(seaOffset.y < 0.0f) {
seaOffset.y = 1.0f;
}
seaNormal.material.SetTextureOffset("_MainTex", seaOffset);
// Foam
seaOffsetFoam.x -= seaMoveFoam * Time.deltaTime;
seaOffsetFoam.y -= seaMoveFoam * Time.deltaTime;
if(seaOffsetFoam.x < 0.0f) {
seaOffsetFoam.x = 1.0f;
}
if(seaOffsetFoam.y < 0.0f) {
seaOffsetFoam.y = 1.0f;
}
seaFoam.material.SetTextureOffset("_MainTex", seaOffsetFoam);
}
}
}