artemisart

Unity GLDebug +doc

Aug 6th, 2013
7,798
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. public class GLDebug : MonoBehaviour
  7. {
  8.     private struct Line
  9.     {
  10.         public Vector3 start;
  11.         public Vector3 end;
  12.         public Color color;
  13.         public float startTime;
  14.         public float duration;
  15.        
  16.         public Line (Vector3 start, Vector3 end, Color color, float startTime, float duration)
  17.         {
  18.             this.start = start;
  19.             this.end = end;
  20.             this.color = color;
  21.             this.startTime = startTime;
  22.             this.duration = duration;
  23.         }
  24.        
  25.         public bool DurationElapsed (bool drawLine)
  26.         {
  27.             if (drawLine)
  28.             {
  29.                 GL.Color (color);
  30.                 GL.Vertex (start);
  31.                 GL.Vertex (end);
  32.             }
  33.             return Time.time - startTime >= duration;
  34.         }
  35.     }
  36.    
  37.     private static GLDebug instance;
  38.     private static Material matZOn;
  39.     private static Material matZOff;
  40.    
  41.     public KeyCode toggleKey;
  42.     public bool displayLines = true;
  43. #if UNITY_EDITOR
  44.     public bool displayGizmos = true;
  45. #endif
  46.     //public ScreenRect rect = new ScreenRect (0, 0, 150, 20);
  47.    
  48.     private List<Line> linesZOn;
  49.     private List<Line> linesZOff;
  50.     private float milliseconds;
  51.    
  52.     void Awake ()
  53.     {
  54.         if (instance)
  55.         {
  56.             DestroyImmediate (this);
  57.             return;
  58.         }
  59.         instance = this;
  60.         SetMaterial ();
  61.         linesZOn = new List<Line> ();
  62.         linesZOff = new List<Line> ();
  63.     }
  64.    
  65.     void SetMaterial ()
  66.     {
  67.         matZOn = new Material (
  68. @"Shader ""GLlineZOn"" {
  69.     SubShader {
  70.         Pass {
  71.             Blend SrcAlpha OneMinusSrcAlpha
  72.             ZWrite Off
  73.             Cull Off
  74.             BindChannels {
  75.                 Bind ""vertex"", vertex
  76.                 Bind ""color"", color
  77.             }
  78.         }
  79.     }
  80. }
  81. ");
  82.         matZOn.hideFlags = HideFlags.HideAndDontSave;
  83.         matZOn.shader.hideFlags = HideFlags.HideAndDontSave;
  84.         matZOff = new Material (
  85. @"Shader ""GLlineZOff"" {
  86.     SubShader {
  87.         Pass {
  88.             Blend SrcAlpha OneMinusSrcAlpha
  89.             ZWrite Off
  90.             ZTest Always
  91.             Cull Off
  92.             BindChannels {
  93.                 Bind ""vertex"", vertex
  94.                 Bind ""color"", color
  95.             }
  96.         }
  97.     }
  98. }
  99. ");
  100.         matZOff.hideFlags = HideFlags.HideAndDontSave;
  101.         matZOff.shader.hideFlags = HideFlags.HideAndDontSave;
  102.     }
  103.    
  104.     void Update ()
  105.     {
  106.         if (Input.GetKeyDown (toggleKey))
  107.             displayLines = !displayLines;
  108.        
  109.         if (!displayLines)
  110.         {
  111.             Stopwatch timer = Stopwatch.StartNew ();
  112.            
  113.             linesZOn = linesZOn.Where (l => !l.DurationElapsed (false)).ToList ();
  114.             linesZOff = linesZOff.Where (l => !l.DurationElapsed (false)).ToList ();
  115.            
  116.             timer.Stop ();
  117.             milliseconds = timer.Elapsed.Ticks / 10000f;
  118.         }
  119.     }
  120.    
  121.     /*void OnGUI ()
  122.     {
  123.         GUI.Label (rect, "GLDebug : " + milliseconds.ToString ("f") + " ms");
  124.     }*/
  125.    
  126. #if UNITY_EDITOR
  127.     void OnDrawGizmos ()
  128.     {
  129.         if (!displayGizmos || !Application.isPlaying)
  130.             return;
  131.         for (int i = 0; i < linesZOn.Count; i++)
  132.         {
  133.             Gizmos.color = linesZOn[i].color;
  134.             Gizmos.DrawLine (linesZOn[i].start, linesZOn[i].end);
  135.         }
  136.         for (int i = 0; i < linesZOff.Count; i++)
  137.         {
  138.             Gizmos.color = linesZOff[i].color;
  139.             Gizmos.DrawLine (linesZOff[i].start, linesZOff[i].end);
  140.         }
  141.     }
  142. #endif
  143.    
  144.     void OnPostRender ()
  145.     {
  146.         if (!displayLines) return;
  147.        
  148.         Stopwatch timer = Stopwatch.StartNew ();
  149.        
  150.         matZOn.SetPass (0);
  151.         GL.Begin (GL.LINES);
  152.         linesZOn = linesZOn.Where (l => !l.DurationElapsed (true)).ToList ();
  153.         GL.End ();
  154.        
  155.         matZOff.SetPass (0);
  156.         GL.Begin (GL.LINES);
  157.         linesZOff = linesZOff.Where (l => !l.DurationElapsed (true)).ToList ();
  158.         GL.End ();
  159.        
  160.         timer.Stop ();
  161.         milliseconds = timer.Elapsed.Ticks / 10000f;
  162.     }
  163.  
  164.     private static void DrawLine (Vector3 start, Vector3 end, Color color, float duration = 0, bool depthTest = false)
  165.     {
  166.         if (duration == 0 && !instance.displayLines)
  167.             return;
  168.         if (start == end)
  169.             return;
  170.         if (depthTest)
  171.             instance.linesZOn.Add (new Line (start, end, color, Time.time, duration));
  172.         else
  173.             instance.linesZOff.Add (new Line (start, end, color, Time.time, duration));
  174.     }
  175.  
  176.     /// <summary>
  177.     /// Draw a line from start to end with color for a duration of time and with or without depth testing.
  178.     /// If duration is 0 then the line is rendered 1 frame.
  179.     /// </summary>
  180.     /// <param name="start">Point in world space where the line should start.</param>
  181.     /// <param name="end">Point in world space where the line should end.</param>
  182.     /// <param name="color">Color of the line.</param>
  183.     /// <param name="duration">How long the line should be visible for.</param>
  184.     /// <param name="depthTest">Should the line be obscured by objects closer to the camera ?</param>
  185.     public static void DrawLine (Vector3 start, Vector3 end, Color? color = null, float duration = 0, bool depthTest = false)
  186.     {
  187.         DrawLine (start, end, color ?? Color.white, duration, depthTest);
  188.     }
  189.  
  190.     /// <summary>
  191.     /// Draw a line from start to start + dir with color for a duration of time and with or without depth testing.
  192.     /// If duration is 0 then the ray is rendered 1 frame.
  193.     /// </summary>
  194.     /// <param name="start">Point in world space where the ray should start.</param>
  195.     /// <param name="dir">Direction and length of the ray.</param>
  196.     /// <param name="color">Color of the ray.</param>
  197.     /// <param name="duration">How long the ray should be visible for.</param>
  198.     /// <param name="depthTest">Should the ray be obscured by objects closer to the camera ?</param>
  199.     public static void DrawRay (Vector3 start, Vector3 dir, Color? color = null, float duration = 0, bool depthTest = false)
  200.     {
  201.         if (dir == Vector3.zero)
  202.             return;
  203.         DrawLine (start, start + dir, color, duration, depthTest);
  204.     }
  205.    
  206.     /// <summary>
  207.     /// Draw an arrow from start to end with color for a duration of time and with or without depth testing.
  208.     /// If duration is 0 then the arrow is rendered 1 frame.
  209.     /// </summary>
  210.     /// <param name="start">Point in world space where the arrow should start.</param>
  211.     /// <param name="end">Point in world space where the arrow should end.</param>
  212.     /// <param name="arrowHeadLength">Length of the 2 lines of the head.</param>
  213.     /// <param name="arrowHeadAngle">Angle between the main line and each of the 2 smaller lines of the head.</param>
  214.     /// <param name="color">Color of the arrow.</param>
  215.     /// <param name="duration">How long the arrow should be visible for.</param>
  216.     /// <param name="depthTest">Should the arrow be obscured by objects closer to the camera ?</param>
  217.     public static void DrawLineArrow (Vector3 start, Vector3 end, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20, Color? color = null, float duration = 0, bool depthTest = false)
  218.     {
  219.         DrawArrow (start, end - start, arrowHeadLength, arrowHeadAngle, color, duration, depthTest);
  220.     }
  221.    
  222.     /// <summary>
  223.     /// Draw an arrow from start to start + dir with color for a duration of time and with or without depth testing.
  224.     /// If duration is 0 then the arrow is rendered 1 frame.
  225.     /// </summary>
  226.     /// <param name="start">Point in world space where the arrow should start.</param>
  227.     /// <param name="dir">Direction and length of the arrow.</param>
  228.     /// <param name="arrowHeadLength">Length of the 2 lines of the head.</param>
  229.     /// <param name="arrowHeadAngle">Angle between the main line and each of the 2 smaller lines of the head.</param>
  230.     /// <param name="color">Color of the arrow.</param>
  231.     /// <param name="duration">How long the arrow should be visible for.</param>
  232.     /// <param name="depthTest">Should the arrow be obscured by objects closer to the camera ?</param>
  233.     public static void DrawArrow (Vector3 start, Vector3 dir, float arrowHeadLength = 0.25f, float arrowHeadAngle = 20, Color? color = null, float duration = 0, bool depthTest = false)
  234.     {
  235.         if (dir == Vector3.zero)
  236.             return;
  237.         DrawRay (start, dir, color, duration, depthTest);
  238.         Vector3 right = Quaternion.LookRotation (dir) * Quaternion.Euler (0, 180 + arrowHeadAngle, 0) * Vector3.forward;
  239.         Vector3 left  = Quaternion.LookRotation (dir) * Quaternion.Euler (0, 180 - arrowHeadAngle, 0) * Vector3.forward;
  240.         DrawRay (start + dir, right * arrowHeadLength, color, duration, depthTest);
  241.         DrawRay (start + dir, left  * arrowHeadLength, color, duration, depthTest);
  242.     }
  243.    
  244.     /// <summary>
  245.     /// Draw a square with color for a duration of time and with or without depth testing.
  246.     /// If duration is 0 then the square is renderer 1 frame.
  247.     /// </summary>
  248.     /// <param name="pos">Center of the square in world space.</param>
  249.     /// <param name="rot">Rotation of the square in euler angles in world space.</param>
  250.     /// <param name="scale">Size of the square.</param>
  251.     /// <param name="color">Color of the square.</param>
  252.     /// <param name="duration">How long the square should be visible for.</param>
  253.     /// <param name="depthTest">Should the square be obscured by objects closer to the camera ?</param>
  254.     public static void DrawSquare (Vector3 pos, Vector3? rot = null, Vector3? scale = null, Color? color = null, float duration = 0, bool depthTest = false)
  255.     {
  256.         DrawSquare (Matrix4x4.TRS (pos, Quaternion.Euler (rot ?? Vector3.zero), scale ?? Vector3.one), color, duration, depthTest);
  257.     }
  258.     /// <summary>
  259.     /// Draw a square with color for a duration of time and with or without depth testing.
  260.     /// If duration is 0 then the square is renderer 1 frame.
  261.     /// </summary>
  262.     /// <param name="pos">Center of the square in world space.</param>
  263.     /// <param name="rot">Rotation of the square in world space.</param>
  264.     /// <param name="scale">Size of the square.</param>
  265.     /// <param name="color">Color of the square.</param>
  266.     /// <param name="duration">How long the square should be visible for.</param>
  267.     /// <param name="depthTest">Should the square be obscured by objects closer to the camera ?</param>
  268.     public static void DrawSquare (Vector3 pos, Quaternion? rot = null, Vector3? scale = null, Color? color = null, float duration = 0, bool depthTest = false)
  269.     {
  270.         DrawSquare (Matrix4x4.TRS (pos, rot ?? Quaternion.identity, scale ?? Vector3.one), color, duration, depthTest);
  271.     }
  272.     /// <summary>
  273.     /// Draw a square with color for a duration of time and with or without depth testing.
  274.     /// If duration is 0 then the square is renderer 1 frame.
  275.     /// </summary>
  276.     /// <param name="matrix">Transformation matrix which represent the square transform.</param>
  277.     /// <param name="color">Color of the square.</param>
  278.     /// <param name="duration">How long the square should be visible for.</param>
  279.     /// <param name="depthTest">Should the square be obscured by objects closer to the camera ?</param>
  280.     public static void DrawSquare (Matrix4x4 matrix, Color? color = null, float duration = 0, bool depthTest = false)
  281.     {
  282.         Vector3
  283.             p_1 = matrix.MultiplyPoint3x4 (new Vector3 ( .5f, 0,  .5f)),
  284.             p_2 = matrix.MultiplyPoint3x4 (new Vector3 ( .5f, 0, -.5f)),
  285.             p_3 = matrix.MultiplyPoint3x4 (new Vector3 (-.5f, 0, -.5f)),
  286.             p_4 = matrix.MultiplyPoint3x4 (new Vector3 (-.5f, 0,  .5f));
  287.        
  288.         DrawLine (p_1, p_2, color, duration, depthTest);
  289.         DrawLine (p_2, p_3, color, duration, depthTest);
  290.         DrawLine (p_3, p_4, color, duration, depthTest);
  291.         DrawLine (p_4, p_1, color, duration, depthTest);
  292.     }
  293.    
  294.     /// <summary>
  295.     /// Draw a cube with color for a duration of time and with or without depth testing.
  296.     /// If duration is 0 then the square is renderer 1 frame.
  297.     /// </summary>
  298.     /// <param name="pos">Center of the cube in world space.</param>
  299.     /// <param name="rot">Rotation of the cube in euler angles in world space.</param>
  300.     /// <param name="scale">Size of the cube.</param>
  301.     /// <param name="color">Color of the cube.</param>
  302.     /// <param name="duration">How long the cube should be visible for.</param>
  303.     /// <param name="depthTest">Should the cube be obscured by objects closer to the camera ?</param>
  304.     public static void DrawCube (Vector3 pos, Vector3? rot = null, Vector3? scale = null, Color? color = null, float duration = 0, bool depthTest = false)
  305.     {
  306.         DrawCube (Matrix4x4.TRS (pos, Quaternion.Euler (rot ?? Vector3.zero), scale ?? Vector3.one), color, duration, depthTest);
  307.     }
  308.     /// <summary>
  309.     /// Draw a cube with color for a duration of time and with or without depth testing.
  310.     /// If duration is 0 then the square is renderer 1 frame.
  311.     /// </summary>
  312.     /// <param name="pos">Center of the cube in world space.</param>
  313.     /// <param name="rot">Rotation of the cube in world space.</param>
  314.     /// <param name="scale">Size of the cube.</param>
  315.     /// <param name="color">Color of the cube.</param>
  316.     /// <param name="duration">How long the cube should be visible for.</param>
  317.     /// <param name="depthTest">Should the cube be obscured by objects closer to the camera ?</param>
  318.     public static void DrawCube (Vector3 pos, Quaternion? rot = null, Vector3? scale = null, Color? color = null, float duration = 0, bool depthTest = false)
  319.     {
  320.         DrawCube (Matrix4x4.TRS (pos, rot ?? Quaternion.identity, scale ?? Vector3.one), color, duration, depthTest);
  321.     }
  322.     /// <summary>
  323.     /// Draw a cube with color for a duration of time and with or without depth testing.
  324.     /// If duration is 0 then the square is renderer 1 frame.
  325.     /// </summary>
  326.     /// <param name="matrix">Transformation matrix which represent the cube transform.</param>
  327.     /// <param name="color">Color of the cube.</param>
  328.     /// <param name="duration">How long the cube should be visible for.</param>
  329.     /// <param name="depthTest">Should the cube be obscured by objects closer to the camera ?</param>
  330.     public static void DrawCube (Matrix4x4 matrix, Color? color = null, float duration = 0, bool depthTest = false)
  331.     {
  332.         Vector3
  333.             down_1  = matrix.MultiplyPoint3x4 (new Vector3 ( .5f, -.5f,  .5f)),
  334.             down_2  = matrix.MultiplyPoint3x4 (new Vector3 ( .5f, -.5f, -.5f)),
  335.             down_3  = matrix.MultiplyPoint3x4 (new Vector3 (-.5f, -.5f, -.5f)),
  336.             down_4  = matrix.MultiplyPoint3x4 (new Vector3 (-.5f, -.5f,  .5f)),
  337.             up_1    = matrix.MultiplyPoint3x4 (new Vector3 ( .5f,  .5f,  .5f)),
  338.             up_2    = matrix.MultiplyPoint3x4 (new Vector3 ( .5f,  .5f, -.5f)),
  339.             up_3    = matrix.MultiplyPoint3x4 (new Vector3 (-.5f,  .5f, -.5f)),
  340.             up_4    = matrix.MultiplyPoint3x4 (new Vector3 (-.5f,  .5f,  .5f));
  341.        
  342.         DrawLine (down_1, down_2, color, duration, depthTest);
  343.         DrawLine (down_2, down_3, color, duration, depthTest);
  344.         DrawLine (down_3, down_4, color, duration, depthTest);
  345.         DrawLine (down_4, down_1, color, duration, depthTest);
  346.        
  347.         DrawLine (down_1, up_1, color, duration, depthTest);
  348.         DrawLine (down_2, up_2, color, duration, depthTest);
  349.         DrawLine (down_3, up_3, color, duration, depthTest);
  350.         DrawLine (down_4, up_4, color, duration, depthTest);
  351.        
  352.         DrawLine (up_1, up_2, color, duration, depthTest);
  353.         DrawLine (up_2, up_3, color, duration, depthTest);
  354.         DrawLine (up_3, up_4, color, duration, depthTest);
  355.         DrawLine (up_4, up_1, color, duration, depthTest);
  356.     }
  357. }
RAW Paste Data