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 personManagerLazy; public GameObject boundingBoxPrefab; IDictionary markers = new Dictionary(); private void Awake() { personManagerLazy = new Lazy(FindObjectOfType); } 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 liveBBoxes = new Dictionary(); private Dictionary idColorDict = new Dictionary(); private Stack bboxPool = new Stack(); /// /// 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. /// 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(); if (boxhandler) { boxhandler.SetColor(col); boxhandler.SetID(dobj.Id); } RectTransform newrecttrans = newbox.GetComponent(); 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 boxColors = new List() { 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; } /// /// Gets an available GameObject (instantiated from boundingBoxPrefab) from the pool, /// or instantiates a new one if none are available. /// /// 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; } } }