CalibrationMarkerBehavior.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Assets.StreetLight.Scripts;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class CalibrationMarkerBehavior : MonoBehaviour
  6. {
  7. Vector3[] frustumCorners;
  8. List<Vector3> targetPositions;
  9. PersonManager PersonManager => personManagerLazy.Value;
  10. Lazy<PersonManager> personManagerLazy;
  11. public event EventHandler PathFinished;
  12. private void Awake()
  13. {
  14. personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
  15. }
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. var camera = Camera.main;
  20. frustumCorners = new Vector3[4];
  21. camera.CalculateFrustumCorners(camera.rect, camera.transform.position.y, camera.stereoActiveEye, frustumCorners);
  22. targetPositions = new List<Vector3>()
  23. {
  24. new Vector3(frustumCorners[0].x, 0, frustumCorners[0].y),
  25. new Vector3(frustumCorners[1].x, 0, frustumCorners[1].y),
  26. new Vector3(frustumCorners[2].x, 0, frustumCorners[2].y),
  27. new Vector3(frustumCorners[3].x, 0, frustumCorners[3].y),
  28. };
  29. int count = 1;
  30. const int increment = 2;
  31. while (count < 4)
  32. {
  33. targetPositions.Add(new Vector3(frustumCorners[0].x + count * increment, 0, frustumCorners[0].y + (count - 1) * increment));
  34. targetPositions.Add(new Vector3(frustumCorners[1].x + count * increment, 0, frustumCorners[1].y - count * increment));
  35. targetPositions.Add(new Vector3(frustumCorners[2].x - count * increment, 0, frustumCorners[2].y - count * increment));
  36. targetPositions.Add(new Vector3(frustumCorners[3].x - count * increment, 0, frustumCorners[3].y + count * increment));
  37. count += 1;
  38. }
  39. currentTarget = GameObject.Find("CalibrationMarker").transform.position;
  40. targetPositions.Add(currentTarget);
  41. enabled = false;
  42. PersonManager.DetectionReady += PersonManager_DetectionReady;
  43. }
  44. private void PersonManager_DetectionReady(object sender, EventArgs e)
  45. {
  46. enabled = true;
  47. }
  48. int targetIndex = -1;
  49. Vector3 currentTarget = Vector3.zero;
  50. Vector3 normalizedTranslationVector = Vector3.zero;
  51. // Update is called once per frame
  52. void Update()
  53. {
  54. var marker = GameObject.Find("CalibrationMarker");
  55. // TODO: make variable
  56. const float speed = 0.5f;
  57. if (Vector3.Distance(marker.transform.position, currentTarget) >= 0.05f)
  58. {
  59. marker.transform.position += Vector3.ClampMagnitude(speed * Time.deltaTime * normalizedTranslationVector, (currentTarget - marker.transform.position).magnitude);
  60. }
  61. else if (targetIndex < targetPositions.Count - 1)
  62. {
  63. targetIndex++;
  64. currentTarget = targetPositions[targetIndex];
  65. normalizedTranslationVector = (currentTarget - marker.transform.position).normalized;
  66. }
  67. else
  68. {
  69. PathFinished?.Invoke(this, EventArgs.Empty);
  70. }
  71. Color color = Color.Lerp(Color.green, Color.red, Time.deltaTime / 0.2f);
  72. Debug.Log(string.Format("<color=#{0:X2}{1:X2}{2:X2}>{3}</color>", (byte)(color.r * 255f), (byte)(color.g * 255f), (byte)(color.b * 255f), $"deltaTime: {Time.deltaTime}"));
  73. }
  74. }