TrackingImageVisualizerForAnchor.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using System;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. namespace NRKernal.Experimental.Persistence
  13. {
  14. /// <summary>
  15. /// Uses 4 frame corner objects to visualize an TrackingImage.
  16. /// </summary> </summary>
  17. public class TrackingImageVisualizerForAnchor : MonoBehaviour
  18. {
  19. public Action OnConfirm;
  20. /// <summary> The TrackingImage to visualize. </summary>
  21. public NRTrackableImage Image;
  22. /// <summary>
  23. /// A model for the lower left corner of the frame to place when an image is detected. </summary>
  24. public GameObject FrameLowerLeft;
  25. /// <summary>
  26. /// A model for the lower right corner of the frame to place when an image is detected. </summary>
  27. public GameObject FrameLowerRight;
  28. /// <summary>
  29. /// A model for the upper left corner of the frame to place when an image is detected. </summary>
  30. public GameObject FrameUpperLeft;
  31. /// <summary>
  32. /// A model for the upper right corner of the frame to place when an image is detected. </summary>
  33. public GameObject FrameUpperRight;
  34. /// <summary> The axis. </summary>
  35. public GameObject Axis;
  36. /// <summary> The save control. </summary>
  37. public Button SaveButton;
  38. void Start()
  39. {
  40. SaveButton.onClick.AddListener(new UnityEngine.Events.UnityAction(() =>
  41. {
  42. OnConfirm?.Invoke();
  43. }));
  44. }
  45. /// <summary> Updates this object. </summary>
  46. public void Update()
  47. {
  48. if (Image == null || Image.GetTrackingState() != TrackingState.Tracking)
  49. {
  50. FrameLowerLeft.SetActive(false);
  51. FrameLowerRight.SetActive(false);
  52. FrameUpperLeft.SetActive(false);
  53. FrameUpperRight.SetActive(false);
  54. Axis.SetActive(false);
  55. SaveButton.gameObject.SetActive(false);
  56. return;
  57. }
  58. float halfWidth = Image.ExtentX / 2;
  59. float halfHeight = Image.ExtentZ / 2;
  60. FrameLowerLeft.transform.localPosition = (halfWidth * Vector3.left) + (halfHeight * Vector3.back);
  61. FrameLowerRight.transform.localPosition = (halfWidth * Vector3.right) + (halfHeight * Vector3.back);
  62. FrameUpperLeft.transform.localPosition = (halfWidth * Vector3.left) + (halfHeight * Vector3.forward);
  63. FrameUpperRight.transform.localPosition = (halfWidth * Vector3.right) + (halfHeight * Vector3.forward);
  64. SaveButton.transform.localPosition = (halfHeight + 0.1f) * Vector3.back;
  65. var center = Image.GetCenterPose();
  66. transform.position = center.position;
  67. transform.rotation = center.rotation;
  68. FrameLowerLeft.SetActive(true);
  69. FrameLowerRight.SetActive(true);
  70. FrameUpperLeft.SetActive(true);
  71. FrameUpperRight.SetActive(true);
  72. SaveButton.gameObject.SetActive(true);
  73. Axis.SetActive(true);
  74. }
  75. }
  76. }