CalibrationMarkerBehavior.cs 3.6 KB

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