Advertisement
Sigma88Mods

debug drawer

Nov 25th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1.  
  2. [KSPAddon(KSPAddon.Startup.Instantly, true)]
  3. class DebugDrawer : MonoBehaviour
  4. {
  5. private static Material mat;
  6.  
  7. private static readonly List<Line> lines = new List<Line>();
  8.  
  9. private struct Line
  10. {
  11. public readonly Vector3 start;
  12. public readonly Vector3 end;
  13. public readonly Color color;
  14.  
  15. public Line(Vector3 start, Vector3 end, Color color)
  16. {
  17. this.start = start;
  18. this.end = end;
  19. this.color = color;
  20. }
  21. }
  22.  
  23. private void Start()
  24. {
  25. DontDestroyOnLoad(this);
  26. if (mat == null)
  27. mat = new Material(Shader.Find("Particles/Additive"));
  28.  
  29. StartCoroutine("EndOfFrameDrawing");
  30. }
  31.  
  32. public static void DebugLine(Vector3 start, Vector3 end, Color col)
  33. {
  34. lines.Add(new Line(start, end, col));
  35. }
  36.  
  37. private IEnumerator EndOfFrameDrawing()
  38. {
  39. Debug.Log("DebugDrawer starting");
  40. while (true)
  41. {
  42. yield return new WaitForEndOfFrame();
  43.  
  44. Camera cam = GetActiveCam();
  45.  
  46. GL.PushMatrix();
  47. mat.SetPass(0);
  48. GL.LoadPixelMatrix();
  49. GL.Begin(GL.LINES);
  50.  
  51. for (int i = 0; i < lines.Count; i++)
  52. {
  53. Line line = lines[i];
  54.  
  55. Vector3 screenPoint1 = cam.WorldToScreenPoint(line.start);
  56. Vector3 screenPoint2 = cam.WorldToScreenPoint(line.end);
  57.  
  58. GL.Color(line.color);
  59. GL.Vertex3(screenPoint1.x, screenPoint1.y, 0);
  60. GL.Vertex3(screenPoint2.x, screenPoint2.y, 0);
  61. }
  62. GL.End();
  63. GL.PopMatrix();
  64. lines.Clear();
  65. }
  66. }
  67.  
  68. private static Camera GetActiveCam()
  69. {
  70. Camera cam;
  71.  
  72. if (HighLogic.LoadedSceneIsEditor)
  73. cam = EditorLogic.fetch.editorCamera;
  74. else if (HighLogic.LoadedSceneIsFlight)
  75. cam = FlightCamera.fetch.mainCamera;
  76. else
  77. cam = Camera.main;
  78. return cam;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement