PersonVisualizer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using Assets.Logging;
  2. using Assets.StreetLight.Poco;
  3. using Assets.ZED.SDK.Helpers.Scripts;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using UnityEngine;
  9. public class PersonVisualizer : MonoBehaviour
  10. {
  11. PersonManager PersonManager => personManagerLazy.Value;
  12. Lazy<PersonManager> personManagerLazy;
  13. public GameObject boundingBoxPrefab;
  14. IDictionary<int, GameObject> markers = new Dictionary<int, GameObject>();
  15. private void Awake()
  16. {
  17. personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
  18. }
  19. GameObject[] spheres;
  20. void Start()
  21. {
  22. spheres = new GameObject[] { GameObject.CreatePrimitive(PrimitiveType.Sphere), GameObject.CreatePrimitive(PrimitiveType.Sphere) };
  23. }
  24. float minX = int.MaxValue;
  25. float minY = int.MaxValue;
  26. float maxX = int.MinValue;
  27. float maxY = int.MinValue;
  28. void Update()
  29. {
  30. int count = 0;
  31. foreach (var person in PersonManager.Persons)
  32. {
  33. GameObject sphere = spheres[count];
  34. sphere.transform.position = new Vector3(person.UnityPosition.x, person.UnityPosition.y, person.UnityPosition.z);
  35. //var camera = Camera.main;
  36. //Vector3[] frustumCorners = new Vector3[4];
  37. //camera.CalculateFrustumCorners(new Rect(0, 0, 1, 1), camera.transform.position.y, Camera.MonoOrStereoscopicEye.Mono, frustumCorners);
  38. //GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  39. //sphere.transform.position = new Vector3(0, 0, 0);
  40. //sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  41. //sphere.transform.position = new Vector3(0, 0, Camera.main.rect.width);
  42. //sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  43. //sphere.transform.position = new Vector3(Camera.main.rect.height, 0, 0);
  44. //sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  45. //sphere.transform.position = new Vector3(Camera.main.rect.height, 0, Camera.main.rect.width);
  46. count++;
  47. }
  48. }
  49. float GroundPositionToSceneCoordinate(float minValue, float actualValue)
  50. {
  51. return 0;
  52. }
  53. private void ReturnBoxToPool(int id, RectTransform bbox)
  54. {
  55. bbox.gameObject.SetActive(false);
  56. bbox.name = "Unused";
  57. bboxPool.Push(bbox.gameObject);
  58. if (liveBBoxes.ContainsKey(id))
  59. {
  60. liveBBoxes.Remove(id);
  61. }
  62. else
  63. {
  64. Debug.LogError("Tried to remove object ID " + id + " from active bboxes, but it wasn't in the dictionary.");
  65. }
  66. }
  67. private Dictionary<int, RectTransform> liveBBoxes = new Dictionary<int, RectTransform>();
  68. private Dictionary<int, Color> idColorDict = new Dictionary<int, Color>();
  69. private Stack<GameObject> bboxPool = new Stack<GameObject>();
  70. /// <summary>
  71. /// Returs the RectTransform within the GameObject (instantiated from boundingBoxPrefab) that represents the provided DetectedObject.
  72. /// If none exists, it retrieves one from the pool (or instantiates a new one if none is available) and
  73. /// sets it up with the proper ID and colors.
  74. /// </summary>
  75. private RectTransform GetBBoxForObject(Person dobj)
  76. {
  77. if (!liveBBoxes.ContainsKey(dobj.Id))
  78. {
  79. GameObject newbox = GetAvailableBBox();
  80. //newbox.transform.SetParent(canvas.transform, false);
  81. newbox.name = "Object #" + dobj.Id;
  82. Color col;
  83. if (idColorDict.ContainsKey(dobj.Id))
  84. {
  85. col = idColorDict[dobj.Id];
  86. }
  87. else
  88. {
  89. col = GetNextColor();
  90. idColorDict.Add(dobj.Id, col);
  91. }
  92. BBox2DHandler boxhandler = newbox.GetComponent<BBox2DHandler>();
  93. if (boxhandler)
  94. {
  95. boxhandler.SetColor(col);
  96. boxhandler.SetID(dobj.Id);
  97. }
  98. RectTransform newrecttrans = newbox.GetComponent<RectTransform>();
  99. if (!newrecttrans)
  100. {
  101. Debug.LogError("BBox prefab needs a RectTransform in the root object.");
  102. return null;
  103. }
  104. liveBBoxes[dobj.Id] = newrecttrans;
  105. return newrecttrans;
  106. }
  107. else return liveBBoxes[dobj.Id];
  108. }
  109. public List<Color> boxColors = new List<Color>()
  110. {
  111. new Color(.231f, .909f, .69f, 1),
  112. new Color(.098f, .686f, .816f, 1),
  113. new Color(.412f, .4f, .804f, 1),
  114. new Color(1, .725f, 0f, 1),
  115. new Color(.989f, .388f, .419f, 1)
  116. };
  117. private int nextColorIndex = 0;
  118. private Color GetNextColor()
  119. {
  120. if (boxColors.Count == 0)
  121. {
  122. return new Color(.043f, .808f, .435f, 1);
  123. }
  124. if (nextColorIndex >= boxColors.Count)
  125. {
  126. nextColorIndex = 0;
  127. }
  128. Color returncol = boxColors[nextColorIndex];
  129. nextColorIndex++;
  130. return returncol;
  131. }
  132. /// <summary>
  133. /// Gets an available GameObject (instantiated from boundingBoxPrefab) from the pool,
  134. /// or instantiates a new one if none are available.
  135. /// </summary>
  136. /// <returns></returns>
  137. private GameObject GetAvailableBBox()
  138. {
  139. if (bboxPool.Count == 0)
  140. {
  141. GameObject newbbox = Instantiate(boundingBoxPrefab);
  142. newbbox.transform.SetParent(transform, false);
  143. bboxPool.Push(newbbox);
  144. }
  145. GameObject bbox = bboxPool.Pop();
  146. bbox.SetActive(true);
  147. return bbox;
  148. }
  149. }