forked from Mihara/RasterPropMonitor
/
JSIExternalCameraSelector.cs
261 lines (232 loc) · 8.58 KB
/
JSIExternalCameraSelector.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
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
/*****************************************************************************
* RasterPropMonitor
* =================
* Plugin for Kerbal Space Program
*
* by Mihara (Eugene Medvedev), MOARdV, and other contributors
*
* RasterPropMonitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, revision
* date 29 June 2007, or (at your option) any later version.
*
* RasterPropMonitor is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with RasterPropMonitor. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
using UnityEngine;
namespace JSI
{
public class JSIExternalCameraSelector : PartModule
{
// Actual configuration parameters.
[KSPField]
public string cameraContainer;
[KSPField]
public string cameraIDPrefix = "ExtCam";
[KSPField]
public int maximum = 8;
[KSPField]
public bool showRay = true;
[KSPField]
public Vector3 rotateCamera = Vector3.zero;
[KSPField]
public Vector3 translateCamera = Vector3.zero;
// Internal data storage.
[KSPField(isPersistant = true)]
public int current = 1;
// Fields to handle right-click GUI.
[KSPField(guiActive = true, guiActiveEditor = true, guiName = "Camera ID: ")]
public string visibleCameraName;
[UI_Toggle(disabledText = "off", enabledText = "on")]
[KSPField(guiActiveEditor = true, guiName = "FOV marker ", isPersistant = true)]
public bool showCones = true;
[KSPField]
public float cameraFoVMax = 60;
[KSPField]
public float cameraFoVMin = 5;
// The rest of it
private GameObject lightCone;
private LineRenderer lightConeRenderer;
private static Material lightConeMaterial;
private Transform actualCamera;
private const float endSpan = 15f;
[KSPEvent(guiActive = true, guiActiveEditor = true, guiName = "ID +")]
public void IdPlus()
{
current++;
if (current > maximum)
{
current = 1;
}
UpdateName();
}
[KSPEvent(guiActive = true, guiActiveEditor = true, guiName = "ID -")]
public void IdMinus()
{
current--;
if (current <= 0)
{
current = maximum;
}
UpdateName();
}
private void UpdateName()
{
visibleCameraName = cameraIDPrefix + current;
if (actualCamera != null)
{
actualCamera.name = visibleCameraName;
}
if (HighLogic.LoadedSceneIsEditor)
{
ColorizeLightCone();
}
}
public override void OnAwake()
{
base.OnAwake();
// initialize the static material if it hasn't been loaded yet. this clearly isn't threadsafe...
if (lightConeMaterial == null)
{
lightConeMaterial = new Material(Shader.Find("Legacy Shaders/Particles/Additive"));
}
}
public override void OnStart(PartModule.StartState state)
{
if (string.IsNullOrEmpty(cameraContainer))
{
JSI.JUtil.AnnoyUser(this);
return;
}
// Create the camera transform
Transform containingTransform = part.FindModelTransform(cameraContainer);
if (containingTransform.childCount > 0)
{
actualCamera = containingTransform.GetChild(0);
}
else
{
actualCamera = new GameObject().transform;
actualCamera.parent = containingTransform;
}
actualCamera.position = containingTransform.position;
actualCamera.rotation = containingTransform.rotation;
if (rotateCamera != Vector3.zero)
{
actualCamera.transform.Rotate(rotateCamera);
}
if (translateCamera != Vector3.zero)
{
actualCamera.transform.localPosition = translateCamera;
}
if (state == StartState.Editor)
{
if (part.parent == null)
{
foreach (Part thatPart in EditorLogic.SortedShipList)
{
if (thatPart != part)
{
foreach (PartModule thatModule in thatPart.Modules)
{
var peerModule = thatModule as JSIExternalCameraSelector;
if (peerModule != null && peerModule.cameraIDPrefix == cameraIDPrefix && peerModule.current == current)
{
IdPlus();
}
}
}
}
}
CreateLightCone();
part.OnEditorAttach += new Callback(HideLightCone);
if (showRay)
{
part.OnEditorDetach += new Callback(ShowLightCone);
}
part.OnEditorDestroy += new Callback(HideLightCone);
}
UpdateName();
}
private void HideLightCone()
{
showCones = false;
}
private void ShowLightCone()
{
showCones = true;
}
private void CreateLightCone()
{
if (lightConeRenderer == null)
{
lightCone = new GameObject();
lightConeRenderer = lightCone.AddComponent<LineRenderer>();
lightConeRenderer.useWorldSpace = true;
lightConeRenderer.material = lightConeMaterial;
lightConeRenderer.startWidth = 0.054f;
lightConeRenderer.endWidth=endSpan;
lightConeRenderer.positionCount =2;
lightConeRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
lightConeRenderer.receiveShadows = false;
lightConeRenderer.SetPosition(0, Vector3.zero);
lightConeRenderer.SetPosition(1, Vector3.zero);
ColorizeLightCone();
}
}
public void OnDestroy()
{
if (lightConeRenderer != null)
{
Destroy(lightConeRenderer);
lightConeRenderer = null;
Destroy(lightCone);
lightCone = null;
}
}
private void ColorizeLightCone()
{
if (lightConeRenderer != null)
{
var newStart = Color32.Lerp(new Color32(0, 0, 255, 178), new Color32(255, 0, 0, 178), 1f / (maximum) * (current - 1));
lightConeRenderer.startColor = newStart;
lightConeRenderer.endColor = new Color32(newStart.r, newStart.g, newStart.b, 0);
}
}
private void OnGUI()
{
if (HighLogic.LoadedSceneIsEditor)
{
if (showCones && lightConeRenderer != null && actualCamera != null)
{
Vector3 origin = actualCamera.transform.TransformPoint(Vector3.zero);
Vector3 direction = actualCamera.transform.TransformDirection(Vector3.forward);
lightConeRenderer.SetPosition(0, origin);
lightConeRenderer.SetPosition(1, origin + direction * (endSpan / 2 / Mathf.Tan(Mathf.Deg2Rad * cameraFoVMax / 2)));
}
}
}
public void Update()
{
if (!HighLogic.LoadedSceneIsEditor)
{
return;
}
showCones |= GameSettings.HEADLIGHT_TOGGLE.GetKeyDown();
showCones &= !GameSettings.HEADLIGHT_TOGGLE.GetKeyUp();
if (lightConeRenderer != null)
{
lightConeRenderer.enabled = showCones;
}
}
public override string GetInfo()
{
return "Hold down '" + GameSettings.HEADLIGHT_TOGGLE.primary + "' to display all the camera fields of view at once.";
}
}
}