Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [KSPAddon(KSPAddon.Startup.Instantly, true)]
- class DebugDrawer : MonoBehaviour
- {
- private static Material mat;
- private static readonly List<Line> lines = new List<Line>();
- 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 void Start()
- {
- DontDestroyOnLoad(this);
- if (mat == null)
- mat = new Material(Shader.Find("Particles/Additive"));
- StartCoroutine("EndOfFrameDrawing");
- }
- public static void DebugLine(Vector3 start, Vector3 end, Color col)
- {
- lines.Add(new Line(start, end, col));
- }
- private IEnumerator EndOfFrameDrawing()
- {
- Debug.Log("DebugDrawer starting");
- while (true)
- {
- yield return new WaitForEndOfFrame();
- Camera cam = GetActiveCam();
- GL.PushMatrix();
- mat.SetPass(0);
- GL.LoadPixelMatrix();
- GL.Begin(GL.LINES);
- for (int i = 0; i < lines.Count; i++)
- {
- Line line = lines[i];
- Vector3 screenPoint1 = cam.WorldToScreenPoint(line.start);
- Vector3 screenPoint2 = cam.WorldToScreenPoint(line.end);
- GL.Color(line.color);
- GL.Vertex3(screenPoint1.x, screenPoint1.y, 0);
- GL.Vertex3(screenPoint2.x, screenPoint2.y, 0);
- }
- GL.End();
- GL.PopMatrix();
- lines.Clear();
- }
- }
- private static Camera GetActiveCam()
- {
- Camera cam;
- if (HighLogic.LoadedSceneIsEditor)
- cam = EditorLogic.fetch.editorCamera;
- else if (HighLogic.LoadedSceneIsFlight)
- cam = FlightCamera.fetch.mainCamera;
- else
- cam = Camera.main;
- return cam;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement