-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWeatherSource.cs
More file actions
407 lines (360 loc) · 14.1 KB
/
WeatherSource.cs
File metadata and controls
407 lines (360 loc) · 14.1 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Xml;
#if DEBUG
using System;
#endif
namespace ProceduralSkyMod
{
public class WeatherState
{
public WeatherState ()
{
this.fileName = "FOO";
this.name = "BAR";
this.cloudClearSky = 0;
this.cloudNoiseScale = 0;
this.cloudChange = 0;
this.cloudSpeed = 0;
this.cloudBrightness = 0;
this.cloudGradient = 0;
this.rainParticleStrength = 0;
}
public WeatherState (string fileName, string name, float cloudClearSky, float cloudNoiseScale, float cloudChange, float cloudSpeed, float cloudBrightness, float cloudGradient, float rainParticleStrength)
{
this.fileName = fileName;
this.name = name;
this.cloudClearSky = cloudClearSky;
this.cloudNoiseScale = cloudNoiseScale;
this.cloudChange = cloudChange;
this.cloudSpeed = cloudSpeed;
this.cloudBrightness = cloudBrightness;
this.cloudGradient = cloudGradient;
this.rainParticleStrength = rainParticleStrength;
}
public WeatherState (string fileName, WeatherState copyState)
{
this.fileName = fileName;
this.name = copyState.name;
this.cloudClearSky = copyState.cloudClearSky;
this.cloudNoiseScale = copyState.cloudNoiseScale;
this.cloudChange = copyState.cloudChange;
this.cloudSpeed = copyState.cloudSpeed;
this.cloudBrightness = copyState.cloudBrightness;
this.cloudGradient = copyState.cloudGradient;
this.rainParticleStrength = copyState.rainParticleStrength;
}
public string fileName;
public string name;
public float cloudClearSky;
public float cloudNoiseScale;
public float cloudChange;
public float cloudSpeed;
public float cloudBrightness;
public float cloudGradient;
public float rainParticleStrength;
public static WeatherState LoadFromXML (string filePath)
{
try
{
WeatherState state = new WeatherState();
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
foreach (XmlNode nodes0 in doc.DocumentElement)
{
foreach (XmlNode nodes1 in nodes0.ChildNodes)
{
switch (nodes0.Name)
{
case "Names":
if (nodes1.Name == "fileName") state.fileName = nodes1.InnerText;
if (nodes1.Name == "name") state.name = nodes1.InnerText;
break;
case "Clouds":
if (nodes1.Name == "cloudClearSky") state.cloudClearSky = float.Parse(nodes1.InnerText);
if (nodes1.Name == "cloudNoiseScale") state.cloudNoiseScale = float.Parse(nodes1.InnerText);
if (nodes1.Name == "cloudChange") state.cloudChange = float.Parse(nodes1.InnerText);
if (nodes1.Name == "cloudSpeed") state.cloudSpeed = float.Parse(nodes1.InnerText);
if (nodes1.Name == "cloudBrightness") state.cloudBrightness = float.Parse(nodes1.InnerText);
if (nodes1.Name == "cloudGradient") state.cloudGradient = float.Parse(nodes1.InnerText);
break;
case "Rain":
if (nodes1.Name == "rainParticleStrength") state.rainParticleStrength = float.Parse(nodes1.InnerText);
break;
default:
Debug.LogWarning("FOO");
break;
}
}
}
return state;
}
catch
{
Debug.LogWarning($"WeatherState.cs: Load From XML Error While Trying To Load\n{filePath}");
return GetFALLBACK();
}
}
private static WeatherState GetFALLBACK ()
{
if (File.Exists(WeatherSource.XMLWeatherStatePath + "PSWS_FALLBACK"))
{
Debug.Log(">>> >>> >>> Get FALLBACK From File");
return LoadFromXML(WeatherSource.XMLWeatherStatePath + "PSWS_FALLBACK");
}
WeatherState fallback = new WeatherState("PSWS_FALLBACK", "FALLBACK", 0, 1, 0.1f, 0.001f, 0, 0, 1);
CreateNewXML(fallback);
Debug.Log(">>> >>> >>> Created New FALLBACK File");
return fallback;
}
#if DEBUG
public static void CreateNewXML (WeatherState state)
#else
private static void CreateNewXML (WeatherState state)
#endif
{
XmlDocument doc = new XmlDocument();
XmlNode rootNode = doc.CreateElement("WeatherState");
doc.AppendChild(rootNode);
// name data
XmlNode namesNode = doc.CreateElement("Names");
rootNode.AppendChild(namesNode);
XmlNode filenameNode = doc.CreateElement("fileName");
filenameNode.InnerText = state.fileName;
namesNode.AppendChild(filenameNode);
XmlNode nameNode = doc.CreateElement("name");
nameNode.InnerText = state.name;
namesNode.AppendChild(nameNode);
// cloud data
XmlNode cloudsNode = doc.CreateElement("Clouds");
rootNode.AppendChild(cloudsNode);
XmlNode cloudClearSky = doc.CreateElement("cloudClearSky");
cloudClearSky.InnerText = state.cloudClearSky.ToString();
cloudsNode.AppendChild(cloudClearSky);
XmlNode cloudNoiseScale = doc.CreateElement("cloudNoiseScale");
cloudNoiseScale.InnerText = state.cloudNoiseScale.ToString();
cloudsNode.AppendChild(cloudNoiseScale);
XmlNode cloudChange = doc.CreateElement("cloudChange");
cloudChange.InnerText = state.cloudChange.ToString();
cloudsNode.AppendChild(cloudChange);
XmlNode cloudSpeed = doc.CreateElement("cloudSpeed");
cloudSpeed.InnerText = state.cloudSpeed.ToString();
cloudsNode.AppendChild(cloudSpeed);
XmlNode cloudBrightness = doc.CreateElement("cloudBrightness");
cloudBrightness.InnerText = state.cloudBrightness.ToString();
cloudsNode.AppendChild(cloudBrightness);
XmlNode cloudGradient = doc.CreateElement("cloudGradient");
cloudGradient.InnerText = state.cloudGradient.ToString();
cloudsNode.AppendChild(cloudGradient);
// rain data
XmlNode rainNode = doc.CreateElement("Rain");
rootNode.AppendChild(rainNode);
XmlNode rainParticleStrength = doc.CreateElement("rainParticleStrength");
rainParticleStrength.InnerText = state.rainParticleStrength.ToString();
rainNode.AppendChild(rainParticleStrength);
doc.Save(WeatherSource.XMLWeatherStatePath + state.fileName);
}
}
public delegate void CloudRenderDelegate ();
public class WeatherSource
{
private static RenderTexture cloudRendTex;
private static RenderTexture sunShadowRendTex;
public static string XMLWeatherStatePath { get => Main.ModPath + "ManagedData" + Path.DirectorySeparatorChar; }
public static Camera CloudRenderTexCam { get; set; }
public static RenderTexture CloudRenderTex
{
get
{
if (cloudRendTex == null) SetupCloudRenderTex();
return cloudRendTex;
}
set => cloudRendTex = value;
}
public static Camera SunShadowRenderTexCam { get; set; }
public static RenderTexture SunShadowRenderTex
{
get
{
if (sunShadowRendTex == null) SetupShadowRenderTex();
return sunShadowRendTex;
}
}
public static Texture2D CloudRenderImage0 { get; private set; }
public static Texture2D CloudRenderImage1 { get; private set; }
public static Texture2D CloudRenderImage2 { get; private set; }
public static Texture2D SunShadowRenderImage { get; private set; }
public static string[] AvailableWeatherStateFilesXML { get; private set; }
public static WeatherState CurrentWeatherState { get; set; }
public static WeatherState NextWeatherState { get; set; }
public static float WeatherStateBlending { get; set; }
public static float WeatherChangeProbability { get; private set; }
#if DEBUG
public static float LastRNDWeatherChange { get; set; }
public static float LastRNDFileSelect { get; set; }
#endif
public static float CloudClearSkyBlend
{ get => (NextWeatherState == null) ? CurrentWeatherState.cloudClearSky : Mathf.Lerp(CurrentWeatherState.cloudClearSky, NextWeatherState.cloudClearSky, WeatherStateBlending); }
public static float CloudNoiseScaleBlend
{ get => (NextWeatherState == null) ? CurrentWeatherState.cloudNoiseScale : Mathf.Lerp(CurrentWeatherState.cloudNoiseScale, NextWeatherState.cloudNoiseScale, WeatherStateBlending); }
public static float CloudChangeBlend
{ get => (NextWeatherState == null) ? CurrentWeatherState.cloudChange : Mathf.Lerp(CurrentWeatherState.cloudChange, NextWeatherState.cloudChange, WeatherStateBlending); }
public static float CloudSpeedBlend
{ get => (NextWeatherState == null) ? CurrentWeatherState.cloudSpeed : Mathf.Lerp(CurrentWeatherState.cloudSpeed, NextWeatherState.cloudSpeed, WeatherStateBlending); }
public static float CloudBrightnessBlend
{ get => (NextWeatherState == null) ? CurrentWeatherState.cloudBrightness : Mathf.Lerp(CurrentWeatherState.cloudBrightness, NextWeatherState.cloudBrightness, WeatherStateBlending); }
public static float CloudGradientBlend
{ get => (NextWeatherState == null) ? CurrentWeatherState.cloudGradient : Mathf.Lerp(CurrentWeatherState.cloudGradient, NextWeatherState.cloudGradient, WeatherStateBlending); }
public static float RainStrengthBlend
{ get => (NextWeatherState == null) ? CurrentWeatherState.rainParticleStrength : Mathf.Lerp(CurrentWeatherState.rainParticleStrength, NextWeatherState.rainParticleStrength, WeatherStateBlending); }
public static event CloudRenderDelegate CloudRenderEvent;
public static void OnCloudRendered () { CloudRenderEvent?.Invoke(); }
public static IEnumerator WeatherStateChanger ()
{
AvailableWeatherStateFilesXML = SearchAvailableWeatherStateFilesXML();
if (CurrentWeatherState.fileName == "PSWS_FALLBACK" && AvailableWeatherStateFilesXML.Length > 0)
CurrentWeatherState = WeatherState.LoadFromXML(AvailableWeatherStateFilesXML[0]);
else
{
Debug.LogError("WeatherSource.cs: Weather State Changer Error");
yield break;
}
WeatherChangeProbability = 0.2f;
float frameRate = 30f;
Debug.Log($"{System.DateTime.Now} ProSkyMod: Weather changer started!");
while (true)
{
for (int i = 0; i < Mathf.Max(Main.settings.DayLengthSecondsRT / 4, 600) * frameRate; i++) // break out of loop 4 times a day but wait a minimum of 10 minutes
{
if (NextWeatherState != null && !DV.AppUtil.IsPaused)
{
WeatherStateBlending += 0.0033334f / frameRate; // it will take just over 5 minutes to change state copletely to target
if (WeatherStateBlending > 1)
{
CurrentWeatherState = NextWeatherState;
NextWeatherState = null;
WeatherStateBlending = 0;
}
}
if (DV.AppUtil.IsPaused) yield return null;
else
{
i++;
yield return new WaitForSeconds(1f / frameRate);
}
}
float rnd = UnityEngine.Random.value;
#if DEBUG
LastRNDWeatherChange = rnd;
#endif
if (WeatherChangeProbability > rnd)
{
NextWeatherState = WeatherState.LoadFromXML(AvailableWeatherStateFilesXML[(int)(UnityEngine.Random.value * AvailableWeatherStateFilesXML.Length)]);
if (NextWeatherState == CurrentWeatherState)
{
NextWeatherState = null;
WeatherChangeProbability += 0.2f;
}
else WeatherChangeProbability = 0.2f;
}
else WeatherChangeProbability += 0.2f;
Debug.Log($"{System.DateTime.Now} ProSkyMod: Try change weather: probability check = {WeatherChangeProbability > rnd}, next state = {NextWeatherState?.name}");
}
}
public static IEnumerator UpdateCloudRenderTex ()
{
RenderTexture current = null;
CloudRenderImage0 = new Texture2D(16, 16);
CloudRenderImage1 = new Texture2D(32, 32);
CloudRenderImage2 = new Texture2D(64, 64);
SunShadowRenderImage = new Texture2D(SunShadowRenderTex.width, SunShadowRenderTex.height);
while (true)
{
current = RenderTexture.active;
RenderTexture.active = CloudRenderTex;
CloudRenderTexCam.Render();
CloudRenderImage0.ReadPixels(new Rect(24, 24, 16, 16), 0, 0);
CloudRenderImage0.Apply();
CloudRenderImage1.ReadPixels(new Rect(16, 16, 32, 32), 0, 0);
CloudRenderImage1.Apply();
CloudRenderImage2.ReadPixels(new Rect(0, 0, 64, 64), 0, 0);
CloudRenderImage2.Apply();
OnCloudRendered();
for (int i = 0; i < 16; i++)
{
if (Main.settings.cloudShadowsEnabled)
{
RenderTexture.active = SunShadowRenderTex;
SunShadowRenderTexCam.Render();
SunShadowRenderImage.ReadPixels(new Rect(0, 0, SunShadowRenderTex.width, SunShadowRenderTex.height), 0, 0);
for (int x = 0; x < SunShadowRenderImage.width; x++)
{
for (int y = 0; y < SunShadowRenderImage.height; y++)
{
SunShadowRenderImage.SetPixel(x, y, new Color(1, 1, 1, 1 - WeatherSource.SunShadowRenderImage.GetPixel(x, y).a));
}
}
SunShadowRenderImage.Apply();
}
RenderTexture.active = current;
yield return new WaitForSeconds(0.03f); // 0.03s * 16 = ~0.5s
current = RenderTexture.active;
}
}
}
private static void SetupCloudRenderTex ()
{
cloudRendTex = new RenderTexture(64, 64, 8, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
cloudRendTex.dimension = UnityEngine.Rendering.TextureDimension.Tex2D;
cloudRendTex.antiAliasing = 1;
cloudRendTex.depth = 0;
cloudRendTex.useMipMap = false;
cloudRendTex.useDynamicScale = false;
cloudRendTex.wrapMode = TextureWrapMode.Clamp;
cloudRendTex.filterMode = FilterMode.Point;
cloudRendTex.anisoLevel = 0;
}
private static void SetupShadowRenderTex ()
{
sunShadowRendTex = new RenderTexture(64, 64, 8, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
sunShadowRendTex.dimension = UnityEngine.Rendering.TextureDimension.Tex2D;
sunShadowRendTex.antiAliasing = 1;
sunShadowRendTex.depth = 0;
sunShadowRendTex.useMipMap = false;
sunShadowRendTex.useDynamicScale = false;
sunShadowRendTex.wrapMode = TextureWrapMode.Clamp;
sunShadowRendTex.filterMode = FilterMode.Bilinear;
sunShadowRendTex.anisoLevel = 0;
}
public static string[] SearchAvailableWeatherStateFilesXML ()
{
#if DEBUG
Debug.Log(">>> >>> >>> Loading Weather State Files...");
#endif
List<string> allFiles = Directory.GetFiles(WeatherSource.XMLWeatherStatePath).ToList();
XmlDocument doc;
for (int i = 0; i < allFiles.Count;)
{
if (Path.GetFileName(allFiles[i]).Contains("PSWS_FALLBACK")) allFiles.RemoveAt(i);
else
{
try
{
doc = new XmlDocument();
doc.Load(allFiles[i]);
if (doc.DocumentElement.Name == "WeatherState") i++;
else allFiles.RemoveAt(i);
}
catch { allFiles.RemoveAt(i); }
}
}
#if DEBUG
Debug.Log($">>> >>> >>> Weather File Loading: {allFiles.Count} Weather Files found!");
#endif
return allFiles.ToArray();
}
}
}