-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRemoteView.cs
186 lines (168 loc) · 6.16 KB
/
RemoteView.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
/*
* Scientific Committee on Advanced Navigation S.C.A.N. Satellite
* RemoveView: A camera looking at a GameObject, rendering to a texture.
*
* Copyright (c)2013 damny; see LICENSE.txt for licensing details.
*/
using System;
using System.Collections.Generic;
using UnityEngine;
namespace SCANsat
{
public class RemoteView {
private static Camera cam;
private static GameObject camgo;
private static Shader edgeDetectShader, grayscaleShader;
private static Material edgeDetectMaterial, grayscaleMaterial;
private static AudioClip tick;
private RenderTexture rt;
private Rect camRect;
private int updateFrame, width, height;
private List<CrashObjectName> cons;
private Bounds bounds;
private int activeCon;
private double switchTime;
public GameObject lookat;
public CrashObjectName lookdetail;
public void setup(int w, int h, GameObject focus) {
if(edgeDetectShader == null) {
// simple colour based edge detection that comes with Unity Pro
edgeDetectShader = Shader.Find("Hidden/Edge Detect X");
edgeDetectMaterial = new Material(edgeDetectShader);
edgeDetectMaterial.SetFloat("_Threshold", 0.05f);
// greyscale shader that comes with Unity Pro
grayscaleShader = Shader.Find("Hidden/Grayscale Effect");
grayscaleMaterial = new Material(grayscaleShader);
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, Color.Lerp(Color.black, XKCDColors.Amber, i/1024f));
for(int i=0; i<10; ++i) t.SetPixel(i, 0, XKCDColors.Amber);
t.Apply();
grayscaleMaterial.SetTexture("_RampTex", t);
}
if(lookat != focus) {
switchTime = Time.realtimeSinceStartup;
lookdetail = null;
bounds = new Bounds();
foreach(Collider c in focus.GetComponentsInChildren<Collider>()) {
if(c.bounds != null) bounds.Encapsulate(c.bounds);
}
cons = new List<CrashObjectName>();
foreach(CrashObjectName con in focus.GetComponentsInChildren<CrashObjectName>()) {
cons.Add(con);
}
}
lookat = focus;
width = w;
height = h;
camRect = new Rect(0, 0, width, height);
}
public void free() {
GameObject.Destroy(camgo);
RenderTexture.Destroy(rt);
cam = null;
camgo = null;
rt = null;
}
public void updateCamera() {
if(updateFrame > Time.frameCount - 5) return;
if(rt == null || rt.width != width || rt.height != height) {
rt = new RenderTexture(width, height, 32, RenderTextureFormat.RGB565);
rt.Create();
}
if(camgo == null) {
camgo = new GameObject();
}
if(cam == null) {
cam = camgo.AddComponent<Camera>();
cam.enabled = false; // so we can render on demand
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;
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; // TODO: replace with something useful for given object size
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;
cam.Render();
Graphics.Blit(rt, rt, edgeDetectMaterial);
Graphics.Blit(rt, rt, grayscaleMaterial);
RenderTexture.active = old;
updateFrame = Time.frameCount;
}
private bool ticking;
private double lastTick;
private int tickRate = 10;
public void drawOverlay(Rect where, GUIStyle style, bool identified) {
if(tick == null) {
tick = GameDatabase.Instance.GetAudioClip("Squad/Sounds/sound_click_tick");
}
ticking = false;
int chars = (int)((Time.realtimeSinceStartup - switchTime) * tickRate);
Rect r = new Rect(0, 0, 200, 20);
string info = "no structures found";
if(cons.Count == 1) {
info = "identified 1 structure";
} else if(cons.Count > 1) {
info = "identified " + cons.Count.ToString() + " structures";
}
if(!identified) info = "scanning...";
info = "> " + info;
if(Math.Round(Time.realtimeSinceStartup) % 2 == 0) info += "_";
r.x = where.x + 4;
r.y = where.y + 8;
GUI.Label(r, left(info, chars), style);
if(!identified) return;
r.x = where.x + 4;
r.y = where.y + where.height - 60;
string sname = lookat.name;
Vector3 lookvec = lookat.transform.position;
if(cons.Count > 0) {
if(Event.current.type == EventType.ScrollWheel && where.Contains(Event.current.mousePosition)) {
activeCon += cons.Count + (Event.current.delta.y > 0 ? 1 : -1);
Event.current.Use();
switchTime = Time.realtimeSinceStartup;
}
activeCon = activeCon % cons.Count;
lookdetail = cons[activeCon];
Vector3 pos = cam.WorldToScreenPoint(lookdetail.transform.position);
lookvec = lookdetail.transform.position;
sname = lookdetail.objectName;
}
if(sname != null) {
GUI.Label(r, left(sname, chars), style);
r.y += 30;
GUI.Label(r, left(distanceString((FlightGlobals.ActiveVessel.transform.position - lookvec).magnitude), chars), style);
}
if(ticking) {
if(Time.realtimeSinceStartup - lastTick > 1f/tickRate) {
AudioSource.PlayClipAtPoint(tick, Camera.main.transform.position);
lastTick = Time.realtimeSinceStartup;
}
}
}
public string left(string s, int cnt) {
if(cnt >= s.Length) return s;
ticking = true;
return s.Substring(0, cnt);
}
public string distanceString(double dist) {
if(dist < 5000) return dist.ToString("N1") + "m";
return (dist / 1000d).ToString("N3") + "km";
}
public Texture getTexture() {
updateCamera();
return rt;
}
}
}