Advertisement
napland

marker lines 2

Aug 10th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class MinimapGlLineManager : MonoBehaviour
  5. {
  6. private List<Transform> icons = new List<Transform>();
  7.  
  8. // Choose the Unlit/Color shader in the Material Settings
  9. // You can change that color, to change the color of the connecting lines
  10. [SerializeField]
  11. private Material lineMat;
  12.  
  13. public Camera miniMapCamera;
  14.  
  15. public MeshRenderer miniMap;
  16.  
  17. private List<GameObject> debugObjects = new List<GameObject>();
  18.  
  19. [SerializeField]
  20. private float heightAboveMap = 0.05f;
  21.  
  22.  
  23. void Awake()
  24. {
  25.  
  26. }
  27.  
  28. public void RegisterIcon(Transform icon)
  29. {
  30. icons.Add(icon);
  31. GameObject dot = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  32. dot.transform.localScale = Vector3.one * 0.1f;
  33. dot.GetComponent<MeshRenderer>().sharedMaterial = lineMat;
  34. debugObjects.Add(dot);
  35. }
  36.  
  37. public void DeregisterIcon(Transform icon)
  38. {
  39. if (icons.Contains(icon))
  40. icons.Remove(icon);
  41. else
  42. {
  43. Debug.LogError("icon not found in list. Cannot deregister: " + icon.gameObject.name);
  44. }
  45. }
  46.  
  47. void DrawConnectingLines(Vector3 go1, Vector3 go2)
  48. {
  49. GL.Begin(GL.LINES);
  50. lineMat.SetPass(0);
  51. GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));
  52. GL.Vertex3(go1.x, go1.y, go1.z);
  53. GL.Vertex3(go2.x, go2.y, go2.z);
  54. GL.End();
  55. }
  56.  
  57. private Vector3 GetPointOnMiniMap(Vector3 iconWorldsPosition)
  58. {
  59. // Get screen point
  60. Vector3 screenPoint = miniMapCamera.WorldToScreenPoint(iconWorldsPosition);
  61.  
  62. // Convert that to a % of the screen (0,0 in lower left)
  63. Vector2 screenPointPercent = new Vector2(
  64. screenPoint.x / miniMapCamera.pixelWidth,
  65. screenPoint.y / miniMapCamera.pixelHeight);
  66.  
  67. // thank god the minimap is planar
  68. // get point on minimap via size of the mesh renderer
  69. Vector3 lowerLeft = miniMap.bounds.min;
  70. Vector3 miniMapPoint = new Vector3(
  71. lowerLeft.x + miniMap.bounds.size.x * screenPointPercent.x,
  72. miniMap.transform.position.y + heightAboveMap,
  73. lowerLeft.z + miniMap.bounds.size.z * screenPointPercent.y);
  74.  
  75. return miniMapPoint;
  76. }
  77.  
  78. private bool doOnce;
  79.  
  80. private void OnPostRender()
  81. {
  82. for (int i = 0; i < icons.Count; i++)
  83. {
  84. if (icons[i] == null)
  85. continue;
  86.  
  87. DrawConnectingLines(
  88. icons[i].transform.position,
  89. GetPointOnMiniMap(icons[i].transform.position));
  90.  
  91. debugObjects[i].transform.position = GetPointOnMiniMap(icons[i].transform.position);
  92.  
  93. if (!doOnce)
  94. {
  95. doOnce = true;
  96. Debug.LogFormat(
  97. "Draw line from: {0} to {1}",
  98. icons[i].transform.position,
  99. GetPointOnMiniMap(icons[i].transform.position));
  100. }
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement