TrackingImageVisualizer.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. namespace NRKernal.NRExamples
  10. {
  11. using UnityEngine;
  12. /// <summary> Uses 4 frame corner objects to visualize an TrackingImage. </summary>
  13. public class TrackingImageVisualizer : MonoBehaviour
  14. {
  15. /// <summary> The TrackingImage to visualize. </summary>
  16. public NRTrackableImage Image;
  17. /// <summary>
  18. /// A model for the lower left corner of the frame to place when an image is detected. </summary>
  19. public GameObject FrameLowerLeft;
  20. /// <summary>
  21. /// A model for the lower right corner of the frame to place when an image is detected. </summary>
  22. public GameObject FrameLowerRight;
  23. /// <summary>
  24. /// A model for the upper left corner of the frame to place when an image is detected. </summary>
  25. public GameObject FrameUpperLeft;
  26. /// <summary>
  27. /// A model for the upper right corner of the frame to place when an image is detected. </summary>
  28. public GameObject FrameUpperRight;
  29. /// <summary> The axis. </summary>
  30. public GameObject Axis;
  31. /// <summary> Updates this object. </summary>
  32. public void Update()
  33. {
  34. if (Image == null || Image.GetTrackingState() != TrackingState.Tracking)
  35. {
  36. FrameLowerLeft.SetActive(false);
  37. FrameLowerRight.SetActive(false);
  38. FrameUpperLeft.SetActive(false);
  39. FrameUpperRight.SetActive(false);
  40. Axis.SetActive(false);
  41. return;
  42. }
  43. float halfWidth = Image.ExtentX / 2;
  44. float halfHeight = Image.ExtentZ / 2;
  45. FrameLowerLeft.transform.localPosition = (halfWidth * Vector3.left) + (halfHeight * Vector3.back);
  46. FrameLowerRight.transform.localPosition = (halfWidth * Vector3.right) + (halfHeight * Vector3.back);
  47. FrameUpperLeft.transform.localPosition = (halfWidth * Vector3.left) + (halfHeight * Vector3.forward);
  48. FrameUpperRight.transform.localPosition = (halfWidth * Vector3.right) + (halfHeight * Vector3.forward);
  49. var center = Image.GetCenterPose();
  50. transform.position = center.position;
  51. transform.rotation = center.rotation;
  52. FrameLowerLeft.SetActive(true);
  53. FrameLowerRight.SetActive(true);
  54. FrameUpperLeft.SetActive(true);
  55. FrameUpperRight.SetActive(true);
  56. Axis.SetActive(true);
  57. }
  58. }
  59. }