forked from tommaier123/UdonSharpDayNightController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleController.cs
More file actions
316 lines (265 loc) · 8.13 KB
/
CycleController.cs
File metadata and controls
316 lines (265 loc) · 8.13 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
using System.Collections.Generic;
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using UnityEngine.UI;
public class CycleController : UdonSharpBehaviour
{
public Light Sun;
public Slider SpeedSlider;
public Slider TimeSlider;
public Toggle LocalToggle;
public Material LowCloud;
public Material HighCloud;
public Material Stars;
public GameObject StarsObject;
public Material Moon;
public GameObject SkyObject;
public GameObject FireFX;
public AudioSource Birds;
public AudioSource Cicadas;
[UdonSynced]
public float SetTime = 0.2f;
[UdonSynced]
public int syncid = 0;
public int lastreceivedid = 0;
[UdonSynced]
public float SetSpeed = 1 / 600f;
[Range(0, 1)]
public float CurrentTimeOfDay = 0.2f;
public float Speed = 1 / 600f;
[HideInInspector]
public float TimeMultiplier = 1f;
public Color SunColor1;
public Color SunColor2;
public float SunPoint1 = 0.25f;
public float SunPoint2 = 0.35f;
public Color AmbientColor1;
public Color AmbientColor2;
public Color AmbientColor3;
public float AmbientPoint1 = 0.2f;
public float AmbientPoint2 = 0.25f;
public float AmbientPoint3 = 0.35f;
public Color CloudColor1;
public Color CloudColor2;
public Color CloudColor3;
public float CloudPoint1 = 0.2f;
public float CloudPoint2 = 0.25f;
public float CloudPoint3 = 0.35f;
public Color StarColor1;
public Color StarColor2;
public float StarPoint1 = 0.2f;
public float StarPoint2 = 0.25f;
public float StarCutoff = 0.3f;
public Color MoonColor1;
public Color MoonColor2;
public float MoonPoint1 = 0.2f;
public float MoonPoint2 = 0.25f;
public float AudioPoint1 = 0.25f;
public float AudioPoint2 = 0.35f;
public float SunIntensityPoint1 = 0.23f;
public float SunIntensityPoint2 = 0.25f;
public float FirePoint = 0.2f;
float SunInitialIntensity;
float BirdsInitialVolume;
float CicadasInitialVolume;
bool local = false;
void Start()
{
SunInitialIntensity = Sun.intensity;
BirdsInitialVolume = Birds.volume;
CicadasInitialVolume = Cicadas.volume;
Random.InitState((int)Time.time);
TimeSlider.value = CurrentTimeOfDay;
SpeedSlider.value = Speed;
}
public void LocalUpdated()
{
local = LocalToggle.isOn;
if (!local)
{
CurrentTimeOfDay = SetTime;
Speed = SetSpeed;
}
}
public void SliderUpdated()
{
if (!local)
{
//Networking.SetOwner(Networking.LocalPlayer, this.gameObject);
SetTime = TimeSlider.value;
SetSpeed = SpeedSlider.value;
syncid = GetID();
}
else
{
CurrentTimeOfDay = TimeSlider.value;
Speed = SpeedSlider.value;
}
}
private int GetID()
{
return Random.Range(-2000000000, 2000000000);
}
void Update()
{
if (syncid != lastreceivedid)
{
lastreceivedid = syncid;
if (!local)
{
CurrentTimeOfDay = SetTime;
Speed = SetSpeed;
TimeSlider.value = CurrentTimeOfDay;
SpeedSlider.value = Speed;
}
}
else if (TimeSlider.gameObject.activeInHierarchy && Time.time % 1 < 0.01f)
{
TimeSlider.value = CurrentTimeOfDay;
SpeedSlider.value = Speed;
}
Sun.transform.localRotation = Quaternion.Euler((CurrentTimeOfDay * 360f) - 90, 140, 30);
SkyObject.transform.localRotation = Quaternion.Euler((CurrentTimeOfDay * 360f) - 90, 140, 30);
Sun.color = TwoPoint(SunPoint1, SunPoint2, SunColor1, SunColor2);
RenderSettings.ambientLight = ThreePoint(AmbientPoint1, AmbientPoint2, AmbientPoint3, AmbientColor1, AmbientColor2, AmbientColor3);
Color c = ThreePoint(CloudPoint1, CloudPoint2, CloudPoint3, CloudColor1, CloudColor2, CloudColor3);
LowCloud.SetColor("_CloudColor", c);
HighCloud.SetColor("_CloudColor", c);
c = TwoPoint(StarPoint1, StarPoint2, StarColor1, StarColor2);
Stars.SetColor("_EmissionColor", c);
if (c.a <= StarCutoff)
{
if (StarsObject.activeInHierarchy)
{
StarsObject.SetActive(false);
}
}
else if (!StarsObject.activeInHierarchy)
{
StarsObject.SetActive(true);
}
if (CurrentTimeOfDay > FirePoint && CurrentTimeOfDay < 1 - FirePoint)
{
if (FireFX.activeSelf)
{
FireFX.SetActive(false);
}
}
else
{
if (!FireFX.activeSelf)
{
FireFX.SetActive(true);
}
}
Moon.color = TwoPoint(MoonPoint1, MoonPoint2, MoonColor1, MoonColor2);
float audiovolume = TwoPointFloat(AudioPoint1, AudioPoint2);
Birds.volume = BirdsInitialVolume * audiovolume;
Cicadas.volume = CicadasInitialVolume * Mathf.Clamp(1f - audiovolume, 0f, 1f);
float sunintensity = TwoPointFloat(SunIntensityPoint1, SunIntensityPoint2);
Sun.intensity = (SunInitialIntensity * sunintensity) + 0.001f;
CurrentTimeOfDay += (Time.deltaTime * Speed) * TimeMultiplier;
if (CurrentTimeOfDay >= 1)
{
CurrentTimeOfDay = 0;
}
}
public float TwoPointFloat(float p1, float p2)
{
float p3 = 1 - p2;
float p4 = 1 - p1;
float ret = 1f;
if (CurrentTimeOfDay < p1)
{
ret = 0f;
}
else if (CurrentTimeOfDay < p2)
{
ret = (CurrentTimeOfDay - p1) / (p2 - p1);
}
else if (CurrentTimeOfDay < p3)
{
ret = 1f;
}
else if (CurrentTimeOfDay < p4)
{
ret = 1 - ((CurrentTimeOfDay - p3) / (p4 - p3));
}
else
{
ret = 0f;
}
return ret;
}
public Color TwoPoint(float p1, float p2, Color c1, Color c2)
{
Color ret = new Color(0f, 0f, 0f);
float p3 = 1 - p2;
float p4 = 1 - p1;
if (CurrentTimeOfDay < p1)
{
ret = c1;
}
else if (CurrentTimeOfDay < p2)
{
float v = (CurrentTimeOfDay - p1) / (p2 - p1);
ret = Color.Lerp(c1, c2, v);
}
else if (CurrentTimeOfDay < p3)
{
ret = c2;
}
else if (CurrentTimeOfDay < p4)
{
float v = (CurrentTimeOfDay - p3) / (p4 - p3);
ret = Color.Lerp(c2, c1, v);
}
else
{
ret = c1;
}
return ret;
}
public Color ThreePoint(float p1, float p2, float p3, Color c1, Color c2, Color c3)
{
Color ret = new Color(1f, 1f, 1f);
float p4 = 1 - p3;
float p5 = 1 - p2;
float p6 = 1 - p1;
if (CurrentTimeOfDay < p1)
{
ret = c1;
}
else if (CurrentTimeOfDay < p2)
{
float v = (CurrentTimeOfDay - p1) / (p2 - p1);
ret = Color.Lerp(c1, c2, v);
}
else if (CurrentTimeOfDay < p3)
{
float v = (CurrentTimeOfDay - p2) / (p3 - p2);
ret = Color.Lerp(c2, c3, v);
}
else if (CurrentTimeOfDay < p4)
{
ret = c3;
}
else if (CurrentTimeOfDay < p5)
{
float v = (CurrentTimeOfDay - p4) / (p5 - p4);
ret = Color.Lerp(c3, c2, v);
}
else if (CurrentTimeOfDay < p6)
{
float v = (CurrentTimeOfDay - p5) / (p6 - p5);
ret = Color.Lerp(c2, c1, v);
}
else
{
ret = c1;
}
return ret;
}
}