Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.1 KiB

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace AmplifyShaderEditor
  5. {
  6. public class GLDraw
  7. {
  8. /*
  9. * Clipping code: http://forum.unity3d.com/threads/17066-How-to-draw-a-GUI-2D-quot-line-quot?p=230386#post230386
  10. * Thick line drawing code: http://unifycommunity.com/wiki/index.php?title=VectorLine
  11. */
  12. public static Material LineMaterial = null;
  13. public static bool MultiLine = false;
  14. private static Shader LineShader = null;
  15. private static Rect BoundBox = new Rect();
  16. private static Vector3[] Allv3Points = new Vector3[] { };
  17. private static Vector2[] AllPerpendiculars = new Vector2[] { };
  18. private static Color[] AllColors = new Color[] { };
  19. private static Vector2 StartPt = Vector2.zero;
  20. private static Vector2 EndPt = Vector2.zero;
  21. private static Vector3 Up = new Vector3( 0, 1, 0 );
  22. private static Vector3 Zero = new Vector3( 0, 0, 0 );
  23. private static Vector2 Aux1Vec2 = Vector2.zero;
  24. private static int HigherBoundArray = 0;
  25. public static void CreateMaterial()
  26. {
  27. if( (object)LineMaterial != null && (object)LineShader != null )
  28. return;
  29. LineShader = AssetDatabase.LoadAssetAtPath<Shader>( AssetDatabase.GUIDToAssetPath( "50fc796413bac8b40aff70fb5a886273" ) );
  30. LineMaterial = new Material( LineShader );
  31. LineMaterial.hideFlags = HideFlags.HideAndDontSave;
  32. }
  33. public static void DrawCurve( Vector3[] allPoints, Vector2[] allNormals, Color[] allColors, int pointCount )
  34. {
  35. CreateMaterial();
  36. LineMaterial.SetPass( ( MultiLine ? 1 : 0 ) );
  37. GL.Begin( GL.TRIANGLE_STRIP );
  38. for( int i = 0; i < pointCount; i++ )
  39. {
  40. GL.Color( allColors[ i ] );
  41. GL.TexCoord( Zero );
  42. GL.Vertex3( allPoints[ i ].x - allNormals[ i ].x, allPoints[ i ].y - allNormals[ i ].y, 0 );
  43. GL.TexCoord( Up );
  44. GL.Vertex3( allPoints[ i ].x + allNormals[ i ].x, allPoints[ i ].y + allNormals[ i ].y, 0 );
  45. }
  46. GL.End();
  47. }
  48. public static Rect DrawBezier( Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, int type = 1 )
  49. {
  50. int segments = Mathf.FloorToInt( ( start - end ).magnitude / 20 ) * 3; // Three segments per distance of 20
  51. return DrawBezier( start, startTangent, end, endTangent, color, width, segments, type );
  52. }
  53. public static Rect DrawBezier( Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color color, float width, int segments, int type = 1 )
  54. {
  55. return DrawBezier( start, startTangent, end, endTangent, color, color, width, segments, type );
  56. }
  57. public static Rect DrawBezier( Vector2 start, Vector2 startTangent, Vector2 end, Vector2 endTangent, Color startColor, Color endColor, float width, int segments, int type = 1 )
  58. {
  59. int pointsCount = segments + 1;
  60. int linesCount = segments;
  61. HigherBoundArray = HigherBoundArray > pointsCount ? HigherBoundArray : pointsCount;
  62. Allv3Points = Handles.MakeBezierPoints( start, end, startTangent, endTangent, pointsCount );
  63. if( AllColors.Length < HigherBoundArray )
  64. {
  65. AllColors = new Color[ HigherBoundArray ];
  66. AllPerpendiculars = new Vector2[ HigherBoundArray ];
  67. }
  68. startColor.a = ( type * 0.25f );
  69. endColor.a = ( type * 0.25f );
  70. float minX = Allv3Points[ 0 ].x;
  71. float minY = Allv3Points[ 0 ].y;
  72. float maxX = Allv3Points[ 0 ].x;
  73. float maxY = Allv3Points[ 0 ].y;
  74. float amount = 1 / (float)linesCount;
  75. for( int i = 0; i < pointsCount; i++ )
  76. {
  77. if( i == 0 )
  78. {
  79. AllColors[ 0 ] = startColor;
  80. StartPt.Set( startTangent.y, start.x );
  81. EndPt.Set( start.y, startTangent.x );
  82. }
  83. else if( i == pointsCount - 1 )
  84. {
  85. AllColors[ pointsCount - 1 ] = endColor;
  86. StartPt.Set( end.y, endTangent.x );
  87. EndPt.Set( endTangent.y, end.x );
  88. }
  89. else
  90. {
  91. AllColors[ i ] = Color.LerpUnclamped( startColor, endColor, amount * i );
  92. minX = ( Allv3Points[ i ].x < minX ) ? Allv3Points[ i ].x : minX;
  93. minY = ( Allv3Points[ i ].y < minY ) ? Allv3Points[ i ].y : minY;
  94. maxX = ( Allv3Points[ i ].x > maxX ) ? Allv3Points[ i ].x : maxX;
  95. maxY = ( Allv3Points[ i ].y > maxY ) ? Allv3Points[ i ].y : maxY;
  96. StartPt.Set( Allv3Points[ i + 1 ].y, Allv3Points[ i - 1 ].x );
  97. EndPt.Set( Allv3Points[ i - 1 ].y, Allv3Points[ i + 1 ].x );
  98. }
  99. Aux1Vec2.Set( StartPt.x - EndPt.x, StartPt.y - EndPt.y );
  100. FastNormalized( ref Aux1Vec2 );
  101. //aux1Vec2.FastNormalized();
  102. Aux1Vec2.Set( Aux1Vec2.x * width, Aux1Vec2.y * width );
  103. AllPerpendiculars[ i ] = Aux1Vec2;
  104. }
  105. BoundBox.Set( minX, minY, ( maxX - minX ), ( maxY - minY ) );
  106. DrawCurve( Allv3Points, AllPerpendiculars, AllColors, pointsCount );
  107. return BoundBox;
  108. }
  109. private static void FastNormalized( ref Vector2 v )
  110. {
  111. float len = Mathf.Sqrt( v.x * v.x + v.y * v.y );
  112. v.Set( v.x / len, v.y / len );
  113. }
  114. public static void Destroy()
  115. {
  116. GameObject.DestroyImmediate( LineMaterial );
  117. LineMaterial = null;
  118. Resources.UnloadAsset( LineShader );
  119. LineShader = null;
  120. }
  121. }
  122. //public static class VectorEx
  123. //{
  124. // public static void FastNormalized( this Vector2 v )
  125. // {
  126. // float len = Mathf.Sqrt( v.x * v.x + v.y * v.y );
  127. // v.Set( v.x / len, v.y / len );
  128. // }
  129. //}
  130. }