-
Notifications
You must be signed in to change notification settings - Fork 15
/
DebugDrawer.cs
197 lines (167 loc) · 6.55 KB
/
DebugDrawer.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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DebugStuff
{
[KSPAddon(KSPAddon.Startup.Instantly, true)]
class DebugDrawer : MonoBehaviour
{
private static readonly List<Line> lines = new List<Line>();
private static readonly List<Point> points = new List<Point>();
private static readonly List<Trans> transforms = new List<Trans>();
public Material lineMaterial;
private struct Line
{
public readonly Vector3 start;
public readonly Vector3 end;
public readonly Color color;
public Line(Vector3 start, Vector3 end, Color color)
{
this.start = start;
this.end = end;
this.color = color;
}
}
private struct Point
{
public readonly Vector3 pos;
public readonly Color color;
public Point(Vector3 pos, Color color)
{
this.pos = pos;
this.color = color;
}
}
private struct Trans
{
public readonly Vector3 pos;
public readonly Vector3 up;
public readonly Vector3 right;
public readonly Vector3 forward;
public Trans(Vector3 pos, Vector3 up, Vector3 right, Vector3 forward)
{
this.pos = pos;
this.up = up;
this.right = right;
this.forward = forward;
}
}
public static void DebugLine(Vector3 start, Vector3 end, Color col)
{
lines.Add(new Line(start, end, col));
}
public static void DebugPoint(Vector3 start, Color col)
{
points.Add(new Point(start, col));
}
public static void DebugTransforms(Transform t)
{
transforms.Add(new Trans(t.position, t.up, t.right, t.forward));
}
private void Start()
{
DontDestroyOnLoad(this);
if (!lineMaterial)
{
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
lineMaterial.SetInt("_ZWrite", 0);
lineMaterial.SetInt("_ZWrite", (int)UnityEngine.Rendering.CompareFunction.Always);
}
StartCoroutine("EndOfFrameDrawing");
}
private IEnumerator EndOfFrameDrawing()
{
Debug.Log("DebugDrawer starting");
while (true)
{
yield return new WaitForEndOfFrame();
Camera cam = GetActiveCam();
if (cam == null) continue;
try
{
transform.position = Vector3.zero;
GL.PushMatrix();
lineMaterial.SetPass(0);
// In a modern Unity we would use cam.projectionMatrix.decomposeProjection to get the decomposed matrix
// and Matrix4x4.Frustum(FrustumPlanes frustumPlanes) to get a new one
// Change the far clip plane of the projection matrix
Matrix4x4 projectionMatrix = Matrix4x4.Perspective(cam.fieldOfView, cam.aspect, cam.nearClipPlane, float.MaxValue);
GL.LoadProjectionMatrix(projectionMatrix);
GL.MultMatrix(cam.worldToCameraMatrix);
//GL.Viewport(new Rect(0, 0, Screen.width, Screen.height));
GL.Begin(GL.LINES);
for (int i = 0; i < lines.Count; i++)
{
Line line = lines[i];
DrawLine(line.start, line.end, line.color);
}
for (int i = 0; i < points.Count; i++)
{
Point point = points[i];
DrawPoint(point.pos, point.color);
}
for (int i = 0; i < transforms.Count; i++)
{
Trans t = transforms[i];
DrawTransform(t.pos, t.up, t.right, t.forward);
}
}
catch (Exception e)
{
Debug.Log("EndOfFrameDrawing Exception" + e);
}
finally
{
GL.End();
GL.PopMatrix();
lines.Clear();
points.Clear();
transforms.Clear();
}
}
}
private static Camera GetActiveCam()
{
Camera cam;
if (!HighLogic.fetch)
return Camera.main;
if (HighLogic.LoadedSceneIsEditor)
cam = EditorLogic.fetch.editorCamera;
else if (HighLogic.LoadedSceneIsFlight)
cam = MapView.MapIsEnabled ? PlanetariumCamera.Camera : FlightCamera.fetch.mainCamera;
else
cam = Camera.main;
return cam;
}
private static void DrawLine(Vector3 origin, Vector3 destination, Color color)
{
GL.Color(color);
GL.Vertex(origin);
GL.Vertex(destination);
}
private static void DrawRay(Vector3 origin, Vector3 direction, Color color)
{
GL.Color(color);
GL.Vertex(origin);
GL.Vertex(origin + direction);
}
private static void DrawTransform(Vector3 position, Vector3 up, Vector3 right, Vector3 forward, float scale = 1.0f)
{
DrawRay(position, up * scale, Color.green);
DrawRay(position, right * scale, Color.red);
DrawRay(position, forward * scale, Color.blue);
}
private static void DrawPoint(Vector3 position, Color color, float scale = 1.0f)
{
DrawRay(position + Vector3.up * (scale * 0.5f), -Vector3.up * scale, color);
DrawRay(position + Vector3.right * (scale * 0.5f), -Vector3.right * scale, color);
DrawRay(position + Vector3.forward * (scale * 0.5f), -Vector3.forward * scale, color);
}
}
}