123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using Assets.StreetLight.Poco;
- using Assets.StreetLight.Scripts;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- namespace Assets.StreetLight
- {
- public class PersonVisualizer : MonoBehaviour
- {
- PersonManager PersonManager => personManagerLazy.Value;
- Lazy<PersonManager> personManagerLazy;
- public GameObject boundingBoxPrefab;
- IDictionary<int, GameObject> markers = new Dictionary<int, GameObject>();
- private void Awake()
- {
- personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
- }
- GameObject[] spheres;
- void Start()
- {
- spheres = new GameObject[] { GameObject.CreatePrimitive(PrimitiveType.Sphere), GameObject.CreatePrimitive(PrimitiveType.Sphere) };
- }
- void Update()
- {
- int count = 0;
- foreach (var person in PersonManager.Persons.OrderBy(p => p.Id))
- {
- GameObject sphere = spheres[count];
- var unityPosition = person.GetUnityPosition();
- sphere.transform.position = new Vector3(unityPosition.x, unityPosition.y, unityPosition.z);
- count++;
- }
- }
- float GroundPositionToSceneCoordinate(float minValue, float actualValue)
- {
- return 0;
- }
- private void ReturnBoxToPool(int id, RectTransform bbox)
- {
- bbox.gameObject.SetActive(false);
- bbox.name = "Unused";
- bboxPool.Push(bbox.gameObject);
- if (liveBBoxes.ContainsKey(id))
- {
- liveBBoxes.Remove(id);
- }
- else
- {
- Debug.LogError("Tried to remove object ID " + id + " from active bboxes, but it wasn't in the dictionary.");
- }
- }
- private Dictionary<int, RectTransform> liveBBoxes = new Dictionary<int, RectTransform>();
- private Dictionary<int, Color> idColorDict = new Dictionary<int, Color>();
- private Stack<GameObject> bboxPool = new Stack<GameObject>();
- /// <summary>
- /// Returs the RectTransform within the GameObject (instantiated from boundingBoxPrefab) that represents the provided DetectedObject.
- /// If none exists, it retrieves one from the pool (or instantiates a new one if none is available) and
- /// sets it up with the proper ID and colors.
- /// </summary>
- private RectTransform GetBBoxForObject(Person dobj)
- {
- if (!liveBBoxes.ContainsKey(dobj.Id))
- {
- GameObject newbox = GetAvailableBBox();
- //newbox.transform.SetParent(canvas.transform, false);
- newbox.name = "Object #" + dobj.Id;
- Color col;
- if (idColorDict.ContainsKey(dobj.Id))
- {
- col = idColorDict[dobj.Id];
- }
- else
- {
- col = GetNextColor();
- idColorDict.Add(dobj.Id, col);
- }
- BBox2DHandler boxhandler = newbox.GetComponent<BBox2DHandler>();
- if (boxhandler)
- {
- boxhandler.SetColor(col);
- boxhandler.SetID(dobj.Id);
- }
- RectTransform newrecttrans = newbox.GetComponent<RectTransform>();
- if (!newrecttrans)
- {
- Debug.LogError("BBox prefab needs a RectTransform in the root object.");
- return null;
- }
- liveBBoxes[dobj.Id] = newrecttrans;
- return newrecttrans;
- }
- else return liveBBoxes[dobj.Id];
- }
- public List<Color> boxColors = new List<Color>()
- {
- new Color(.231f, .909f, .69f, 1),
- new Color(.098f, .686f, .816f, 1),
- new Color(.412f, .4f, .804f, 1),
- new Color(1, .725f, 0f, 1),
- new Color(.989f, .388f, .419f, 1)
- };
- private int nextColorIndex = 0;
- private Color GetNextColor()
- {
- if (boxColors.Count == 0)
- {
- return new Color(.043f, .808f, .435f, 1);
- }
- if (nextColorIndex >= boxColors.Count)
- {
- nextColorIndex = 0;
- }
- Color returncol = boxColors[nextColorIndex];
- nextColorIndex++;
- return returncol;
- }
- /// <summary>
- /// Gets an available GameObject (instantiated from boundingBoxPrefab) from the pool,
- /// or instantiates a new one if none are available.
- /// </summary>
- /// <returns></returns>
- private GameObject GetAvailableBBox()
- {
- if (bboxPool.Count == 0)
- {
- GameObject newbbox = Instantiate(boundingBoxPrefab);
- newbbox.transform.SetParent(transform, false);
- bboxPool.Push(newbbox);
- }
- GameObject bbox = bboxPool.Pop();
- bbox.SetActive(true);
- return bbox;
- }
- }
- }
|