/
LightShifter.cs
175 lines (152 loc) · 8.46 KB
/
LightShifter.cs
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
/*
* LightShifter class provided courtesy of NovaSilisko, from Alternis Kerbol, used with permission.
* Modified by Justin Bengtson.
*/
using System.Collections.Generic;
using System.Collections;
using System;
using System.IO;
using UnityEngine;
namespace RealSolarSystem {
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class LightShifter : MonoBehaviour {
void Start() {
if (!CompatibilityChecker.IsCompatible())
return;
//The lights are instantiated on each scene startup, unlike planets which instantiate at the beginning of the game
//so a more specific check has to be performed
if(HighLogic.LoadedScene == GameScenes.TRACKSTATION || HighLogic.LoadedScene == GameScenes.SPACECENTER || HighLogic.LoadedScene == GameScenes.FLIGHT) {
ConfigNode RSSSettings = null;
foreach(ConfigNode node in GameDatabase.Instance.GetConfigNodes("REALSOLARSYSTEMSETTINGS")) {
RSSSettings = node;
}
if(RSSSettings == null) {
print("*RSS* REALSOLARSYSTEMSETTINGS node not found, could not load light settings!");
return;
}
GameObject sunLight = GameObject.Find("SunLight");
GameObject scaledSunLight = GameObject.Find("Scaledspace SunLight");
if(sunLight != null) {
print("*RSS* LightShifter: Found sunlight and scaled sunlight, shifting...");
if(RSSSettings.HasValue("sunlightColor")) {
try {
Vector4 col = KSPUtil.ParseVector4(RSSSettings.GetValue("sunlightColor"));
Color c = new Color(col.x, col.y, col.z, col.w);
sunLight.light.color = c;
} catch(Exception e) {
print("*RSS* Error parsing as color4: original text: " + RSSSettings.GetValue("sunlightColor") + " --- exception " + e.Message);
}
}
if(RSSSettings.HasValue("sunlightIntensity")) {
try {
float f = float.Parse(RSSSettings.GetValue("sunlightIntensity"));
sunLight.light.intensity = f;
} catch(Exception e) {
print("*RSS* Error parsing as float: original text: " + RSSSettings.GetValue("sunlightIntensity") + " --- exception " + e.Message);
}
}
if(RSSSettings.HasValue("sunlightShadowStrength")) {
try {
float f = float.Parse(RSSSettings.GetValue("sunlightShadowStrength"));
sunLight.light.shadowStrength = f;
} catch(Exception e) {
print("*RSS* Error parsing as float: original text: " + RSSSettings.GetValue("sunlightShadowStrength") + " --- exception " + e.Message);
}
}
if(RSSSettings.HasValue("scaledSunlightColor")) {
try {
Vector4 col = KSPUtil.ParseVector4(RSSSettings.GetValue("scaledSunlightColor"));
Color c = new Color(col.x, col.y, col.z, col.w);
scaledSunLight.light.color = c;
} catch(Exception e) {
print("*RSS* Error parsing as color4: original text: " + RSSSettings.GetValue("scaledSunlightColor") + " --- exception " + e.Message);
}
}
if(RSSSettings.HasValue("scaledSunlightIntensity")) {
try {
float f = float.Parse(RSSSettings.GetValue("scaledSunlightIntensity"));
scaledSunLight.light.intensity = f;
} catch(Exception e) {
print("*RSS* Error parsing as float: original text: " + RSSSettings.GetValue("scaledSunlightIntensity") + " --- exception " + e.Message);
}
}
} else {
print("*RSS* LightShifter: Couldn't find either sunlight or scaled sunlight");
}
if(HighLogic.LoadedScene == GameScenes.FLIGHT) {
GameObject IVASun = GameObject.Find("IVASun");
if(IVASun != null) {
print("LightShifter: Found IVA sun, shifting...");
if(RSSSettings.HasValue("IVASunColor")) {
try {
Vector4 col = KSPUtil.ParseVector4(RSSSettings.GetValue("IVASunColor"));
Color c = new Color(col.x, col.y, col.z, col.w);
IVASun.light.color = c;
} catch(Exception e) {
print("*RSS* Error parsing as color4: original text: " + RSSSettings.GetValue("IVASunColor") + " --- exception " + e.Message);
}
}
if(RSSSettings.HasValue("IVASunIntensity")) {
try {
float f = float.Parse(RSSSettings.GetValue("IVASunIntensity"));
IVASun.light.intensity = f;
} catch(Exception e) {
print("*RSS* Error parsing as float: original text: " + RSSSettings.GetValue("IVASunIntensity") + " --- exception " + e.Message);
}
}
} else {
print("*RSS* LightShifter: No IVA sun found.");
}
}
LensFlare sunLightFlare = sunLight.gameObject.GetComponent<LensFlare>();
if(sunLightFlare != null) {
print("*RSS* LightShifter: Shifting LensFlare");
if(RSSSettings.HasValue("sunlightLensFlareColor")) {
try {
Vector4 col = KSPUtil.ParseVector4(RSSSettings.GetValue("sunlightLensFlareColor"));
Color c = new Color(col.x, col.y, col.z, col.w);
sunLightFlare.color = c;
} catch(Exception e) {
print("*RSS* Error parsing as color4: original text: " + RSSSettings.GetValue("sunlightLensFlareColor") + " --- exception " + e.Message);
}
}
}
DynamicAmbientLight ambientLight = FindObjectOfType(typeof(DynamicAmbientLight)) as DynamicAmbientLight;
//Funny story behind locating this one. When I was typing "Light l" in the foreach function earlier, one of the suggestions
//from the autocomplete was DynamicAmbientLight. Saved me a lot of trial and error looking for it to be sure.
if(ambientLight != null) {
print("*RSS* LightShifter: Found DynamicAmbientLight. Shifting...");
if(RSSSettings.HasValue("ambientLightColor")) {
try {
Vector4 col = KSPUtil.ParseVector4(RSSSettings.GetValue("ambientLightColor"));
Color c = new Color(col.x, col.y, col.z, col.w);
ambientLight.vacuumAmbientColor = c;
} catch(Exception e) {
print("*RSS* Error parsing as color4: original text: " + RSSSettings.GetValue("ambientLightColor") + " --- exception " + e.Message);
}
}
} else {
print("*RSS* LightShifter: Couldn't find DynamicAmbientLight");
}
Sun sun = Sun.Instance;
if (sun != null)
{
if (RSSSettings.HasValue("dumpBrightnessCurve"))
{
print("**DUMP** Sun AU: " + sun.AU);
for (int i = 0; i < sun.brightnessCurve.keys.Length; i++)
{
Keyframe k = sun.brightnessCurve.keys[i];
print("Key " + k.time + ", " + k.value + ", " + k.inTangent + "," + k.outTangent + " : mode " + k.tangentMode);
}
}
if (RSSSettings.HasNode("sunBrightnessCurve"))
{
AnimationCurve newCurve = Utils.LoadAnimationCurve(RSSSettings.GetNode("sunBrightnessCurve"));
}
RSSSettings.TryGetValue("sunAU", ref sun.AU);
}
}
}
}
}