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. public float speed = 0.05f;
  8. Vector3[] frustumCorners;
  9. List<Vector3> targetPositions;
  10. PersonManager PersonManager => personManagerLazy.Value;
  11. Lazy<PersonManager> personManagerLazy;
  12. public event EventHandler PathFinished;
  13. private void Awake()
  14. {
  15. personManagerLazy = new Lazy<PersonManager>(FindObjectOfType<PersonManager>);
  16. }
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. var camera = Camera.main;
  21. frustumCorners = new Vector3[4];
  22. camera.CalculateFrustumCorners(camera.rect, camera.transform.position.y, camera.stereoActiveEye, frustumCorners);
  23. targetPositions = new List<Vector3>()
  24. {
  25. new Vector3(frustumCorners[0].x, 0, frustumCorners[0].y),
  26. new Vector3(frustumCorners[1].x, 0, frustumCorners[1].y),
  27. new Vector3(frustumCorners[2].x, 0, frustumCorners[2].y),
  28. new Vector3(frustumCorners[3].x, 0, frustumCorners[3].y),
  29. };
  30. int count = 1;
  31. const int increment = 2;
  32. while (count < 4)
  33. {
  34. targetPositions.Add(new Vector3(frustumCorners[0].x + count * increment, 0, frustumCorners[0].y + (count - 1) * increment));
  35. targetPositions.Add(new Vector3(frustumCorners[1].x + count * increment, 0, frustumCorners[1].y - count * increment));
  36. targetPositions.Add(new Vector3(frustumCorners[2].x - count * increment, 0, frustumCorners[2].y - count * increment));
  37. targetPositions.Add(new Vector3(frustumCorners[3].x - count * increment, 0, frustumCorners[3].y + count * increment));
  38. count += 1;
  39. }
  40. currentTarget = GameObject.Find("CalibrationMarker").transform.position;
  41. targetPositions.Add(currentTarget);
  42. enabled = false;
  43. PersonManager.DetectionReady += PersonManager_DetectionReady;
  44. }
  45. private void PersonManager_DetectionReady(object sender, EventArgs e)
  46. {
  47. enabled = true;
  48. }
  49. int targetIndex = -1;
  50. Vector3 currentTarget = Vector3.zero;
  51. Vector3 normalizedTranslationVector = Vector3.zero;
  52. // Update is called once per frame
  53. void Update()
  54. {
  55. var marker = GameObject.Find("CalibrationMarker");
  56. if (Vector3.Distance(marker.transform.position, currentTarget) >= 0.05f)
  57. {
  58. marker.transform.position += Vector3.ClampMagnitude(speed * Time.deltaTime * normalizedTranslationVector, (currentTarget - marker.transform.position).magnitude);
  59. }
  60. else if (targetIndex < targetPositions.Count - 1)
  61. {
  62. targetIndex++;
  63. currentTarget = targetPositions[targetIndex];
  64. normalizedTranslationVector = (currentTarget - marker.transform.position).normalized;
  65. }
  66. else
  67. {
  68. enabled = false;
  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. }