PersonVisualizer.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Assets.StreetLight.Poco;
  2. using Assets.StreetLight.Scripts;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7. namespace Assets.StreetLight
  8. {
  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. void Update()
  25. {
  26. int count = 0;
  27. foreach (var person in PersonManager.Persons.OrderBy(p => p.Id))
  28. {
  29. GameObject sphere = spheres[count];
  30. var unityPosition = PersonManager.CalculateUnityPosition(person);
  31. sphere.transform.position = new Vector3(unityPosition.x, unityPosition.y, unityPosition.z);
  32. count++;
  33. }
  34. }
  35. float GroundPositionToSceneCoordinate(float minValue, float actualValue)
  36. {
  37. return 0;
  38. }
  39. private void ReturnBoxToPool(int id, RectTransform bbox)
  40. {
  41. bbox.gameObject.SetActive(false);
  42. bbox.name = "Unused";
  43. bboxPool.Push(bbox.gameObject);
  44. if (liveBBoxes.ContainsKey(id))
  45. {
  46. liveBBoxes.Remove(id);
  47. }
  48. else
  49. {
  50. Debug.LogError("Tried to remove object ID " + id + " from active bboxes, but it wasn't in the dictionary.");
  51. }
  52. }
  53. private Dictionary<int, RectTransform> liveBBoxes = new Dictionary<int, RectTransform>();
  54. private Dictionary<int, Color> idColorDict = new Dictionary<int, Color>();
  55. private Stack<GameObject> bboxPool = new Stack<GameObject>();
  56. /// <summary>
  57. /// Returs the RectTransform within the GameObject (instantiated from boundingBoxPrefab) that represents the provided DetectedObject.
  58. /// If none exists, it retrieves one from the pool (or instantiates a new one if none is available) and
  59. /// sets it up with the proper ID and colors.
  60. /// </summary>
  61. private RectTransform GetBBoxForObject(Person dobj)
  62. {
  63. if (!liveBBoxes.ContainsKey(dobj.Id))
  64. {
  65. GameObject newbox = GetAvailableBBox();
  66. //newbox.transform.SetParent(canvas.transform, false);
  67. newbox.name = "Object #" + dobj.Id;
  68. Color col;
  69. if (idColorDict.ContainsKey(dobj.Id))
  70. {
  71. col = idColorDict[dobj.Id];
  72. }
  73. else
  74. {
  75. col = GetNextColor();
  76. idColorDict.Add(dobj.Id, col);
  77. }
  78. BBox2DHandler boxhandler = newbox.GetComponent<BBox2DHandler>();
  79. if (boxhandler)
  80. {
  81. boxhandler.SetColor(col);
  82. boxhandler.SetID(dobj.Id);
  83. }
  84. RectTransform newrecttrans = newbox.GetComponent<RectTransform>();
  85. if (!newrecttrans)
  86. {
  87. Debug.LogError("BBox prefab needs a RectTransform in the root object.");
  88. return null;
  89. }
  90. liveBBoxes[dobj.Id] = newrecttrans;
  91. return newrecttrans;
  92. }
  93. else return liveBBoxes[dobj.Id];
  94. }
  95. public List<Color> boxColors = new List<Color>()
  96. {
  97. new Color(.231f, .909f, .69f, 1),
  98. new Color(.098f, .686f, .816f, 1),
  99. new Color(.412f, .4f, .804f, 1),
  100. new Color(1, .725f, 0f, 1),
  101. new Color(.989f, .388f, .419f, 1)
  102. };
  103. private int nextColorIndex = 0;
  104. private Color GetNextColor()
  105. {
  106. if (boxColors.Count == 0)
  107. {
  108. return new Color(.043f, .808f, .435f, 1);
  109. }
  110. if (nextColorIndex >= boxColors.Count)
  111. {
  112. nextColorIndex = 0;
  113. }
  114. Color returncol = boxColors[nextColorIndex];
  115. nextColorIndex++;
  116. return returncol;
  117. }
  118. /// <summary>
  119. /// Gets an available GameObject (instantiated from boundingBoxPrefab) from the pool,
  120. /// or instantiates a new one if none are available.
  121. /// </summary>
  122. /// <returns></returns>
  123. private GameObject GetAvailableBBox()
  124. {
  125. if (bboxPool.Count == 0)
  126. {
  127. GameObject newbbox = Instantiate(boundingBoxPrefab);
  128. newbbox.transform.SetParent(transform, false);
  129. bboxPool.Push(newbbox);
  130. }
  131. GameObject bbox = bboxPool.Pop();
  132. bbox.SetActive(true);
  133. return bbox;
  134. }
  135. }
  136. }