-
Notifications
You must be signed in to change notification settings - Fork 95
/
SCANremoteView.cs
321 lines (247 loc) · 8.95 KB
/
SCANremoteView.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
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
#region license
/*
* [Scientific Committee on Advanced Navigation]
* S.C.A.N. Satellite
*
* RemoteView - A camera looking at a GameObject, rendering to a texture.
*
* Copyright (c)2013 damny;
* Copyright (c)2014 technogeeky <technogeeky@gmail.com>;
* Copyright (c)2014 (Your Name Here) <your email here>; see LICENSE.txt for licensing details.
*/
#endregion
using SCANsat.SCAN_Data;
using System.Collections.Generic;
using SCANsat.SCAN_Unity;
using UnityEngine;
using palette = SCANsat.SCAN_UI.UI_Framework.SCANcolorUtil;
namespace SCANsat.SCAN_UI.UI_Framework
{
public class SCANremoteView
{
private const float MAX_DISTANCE = 250;
private const float MIN_DISTANCE = 15;
private static Camera cam;
private static GameObject camgo;
private static Material edgeDetectMaterial, grayscaleMaterial;
private RenderTexture rt;
private int updateFrame, width, height;
private List<CrashObjectName> cons;
private List<SCANROC> rocs;
private Bounds bounds;
private int activeCon;
private int activeROC;
public GameObject lookat;
public CrashObjectName lookdetail;
public SCANROC rocDetail;
public bool rocsMode;
public void Initialize(int w, int h)
{
edgeDetectMaterial = new Material(SCAN_UI_Loader.EdgeDetectShader);
edgeDetectMaterial.SetFloat("_Threshold", 0.05f);
grayscaleMaterial = new Material(SCAN_UI_Loader.GreyScaleShader);
Texture2D t = new Texture2D(256, 1, TextureFormat.RGB24, false);
// ramp texture to render everything in dark shades of Amber,
// except originally dark lines, which become bright Amber
for (int i = 0; i < 256; ++i)
t.SetPixel(i, 0, palette.lerp(palette.black, palette.xkcd_Amber, i / 1024f));
for (int i = 0; i < 10; ++i)
t.SetPixel(i, 0, palette.xkcd_Amber);
t.Apply();
grayscaleMaterial.SetTexture("_RampTex", t);
width = w;
height = h;
}
public void setup(GameObject focus)
{
if (lookat != focus)
{
lookdetail = null;
bounds = new Bounds(focus.transform.position, Vector3.zero);
foreach (MeshRenderer c in focus.GetComponentsInChildren<MeshRenderer>())
bounds.Encapsulate(c.bounds);
cons = new List<CrashObjectName>();
foreach (CrashObjectName con in focus.GetComponentsInChildren<CrashObjectName>())
{
cons.Add(con);
}
}
lookat = focus;
rocDetail = null;
rocsMode = false;
}
public void setup(List<SCANROC> rocList, Vessel v)
{
double nearest = -1;
rocs = new List<SCANROC>();
for (int i = rocList.Count - 1; i >= 0; i--)
{
SCANROC roc = rocList[i];
if (!roc.Known)
continue;
rocs.Add(roc);
double d = (roc.Roc.transform.position - v.transform.position).sqrMagnitude;
if (d < nearest || nearest < 0)
{
lookat = roc.Roc.gameObject;
rocDetail = roc;
nearest = d;
}
}
if (lookat != null)
{
bounds = new Bounds(lookat.transform.position, Vector3.zero);
foreach (MeshRenderer c in lookat.GetComponentsInChildren<MeshRenderer>())
bounds.Encapsulate(c.bounds);
}
else
bounds = new Bounds();
lookdetail = null;
rocsMode = true;
}
public bool valid(GameObject focus)
{
if (focus == null)
return false;
if (focus != lookat)
return false;
return true;
}
public bool validROC()
{
if (!rocsMode)
return false;
if (rocDetail.Roc == null)
return false;
if (lookat != rocDetail.Roc.gameObject)
return false;
return true;
}
public void free()
{
GameObject.Destroy(camgo);
RenderTexture.Destroy(rt);
cam = null;
camgo = null;
rt = null;
}
private void updateCamera()
{
if (updateFrame > Time.frameCount - 5)
return;
if (rt == null || rt.width != width || rt.height != height)
{
RenderTextureFormat format = RenderTextureFormat.RGB565;
if (!SystemInfo.SupportsRenderTextureFormat(format))
format = RenderTextureFormat.Default;
rt = new RenderTexture(width, height, 32, format);
rt.Create();
}
if (camgo == null)
camgo = new GameObject();
if (cam == null)
{
cam = camgo.AddComponent<Camera>();
cam.enabled = false;
cam.targetTexture = rt;
cam.aspect = width * 1f / height;
cam.fieldOfView = 90;
}
Vector3 pos_target = lookat.transform.position;
if (lookdetail != null)
pos_target = lookdetail.transform.position;
if (rocDetail != null)
pos_target = rocDetail.Roc.transform.position;
Vector3 pos_cam = FlightGlobals.ActiveVessel.transform.position;
Vector3 target_up = (pos_target - FlightGlobals.currentMainBody.transform.position).normalized;
Vector3 dir = (pos_target - pos_cam).normalized;
float dist = 100;
float bound = Mathf.Max(bounds.size.x, Mathf.Max(bounds.size.y, bounds.size.z));
dist = Mathf.Clamp(bound * 1.5f, MIN_DISTANCE, MAX_DISTANCE);
pos_cam = pos_target - dir * dist / 2 + target_up * dist / 3;
Vector3 cam_up = (pos_cam - FlightGlobals.currentMainBody.transform.position).normalized;
cam.transform.position = pos_cam;
cam.transform.LookAt(pos_target, cam_up);
cam.farClipPlane = dist * 3;
RenderTexture old = RenderTexture.active;
RenderTexture.active = rt;
float shadows = QualitySettings.shadowDistance;
QualitySettings.shadowDistance = 0;
cam.Render();
QualitySettings.shadowDistance = shadows;
Graphics.Blit(rt, rt, edgeDetectMaterial);
Graphics.Blit(rt, rt, grayscaleMaterial);
RenderTexture.active = old;
updateFrame = Time.frameCount;
}
public Texture getTexture()
{
updateCamera();
return rt;
}
public string getInfoString()
{
if (!rocsMode && cons.Count > 0)
return string.Format("> Identified {0} structure{1}", cons.Count.ToString(), cons.Count > 1 ? "s" : "");
else if (rocsMode)
return string.Format("> Identified {0} surface object{1}", rocs.Count.ToString(), rocs.Count > 1 ? "s" : "");
else
return "";
}
public string getAnomalyDataString(bool mouse, bool anomalyKnown)
{
string sname = lookat.name;
Vector3 lookvec = lookat.transform.position;
bool distance = false;
if (!rocsMode && cons.Count > 0)
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
lookdetail = cons[activeCon];
if (mouse && scroll != 0)
{
activeCon += (scroll > 0 ? 1 : -1);
if (activeCon >= cons.Count)
activeCon = 0;
else if (activeCon < 0)
activeCon = cons.Count - 1;
lookdetail = cons[activeCon];
bounds = new Bounds(lookdetail.transform.position, Vector3.zero);
foreach (MeshRenderer c in lookdetail.GetComponentsInChildren<MeshRenderer>())
bounds.Encapsulate(c.bounds);
}
lookvec = lookdetail.transform.position;
sname = lookdetail.objectName;
rocDetail = null;
distance = anomalyKnown;
}
else if (rocsMode && rocs.Count > 0)
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
if (mouse && scroll != 0)
{
activeROC += (scroll > 0 ? 1 : -1);
if (activeROC >= rocs.Count)
activeROC = 0;
else if (activeROC < 0)
activeROC = rocs.Count - 1;
rocDetail = rocs[activeROC];
lookat = rocDetail.Roc.gameObject;
bounds = new Bounds(lookat.transform.position, Vector3.zero);
foreach (MeshRenderer c in lookat.GetComponentsInChildren<MeshRenderer>())
bounds.Encapsulate(c.bounds);
}
lookvec = rocDetail.Roc.transform.position;
sname = rocDetail.Scanned ? rocDetail.Name : "Unknown";
lookdetail = null;
distance = true;
}
if (distance)
{
string dist = SCANuiUtil.distanceString((FlightGlobals.ActiveVessel.transform.position - lookvec).magnitude, 2000);
return string.Format("{0}\n{1}", sname, dist);
}
else
return string.Format("\n{0}", sname);
}
}
}