NRAnchor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. /// <summary> Update the transform of a trackable. </summary>
  15. public partial class NRAnchor : MonoBehaviour
  16. {
  17. /// <summary> Dictionary of anchors. </summary>
  18. private static Dictionary<Int64, NRAnchor> m_AnchorDict = new Dictionary<Int64, NRAnchor>();
  19. /// <summary> The trackable. </summary>
  20. public NRTrackable Trackable;
  21. /// <summary> True if is session destroyed, false if not. </summary>
  22. private bool m_IsSessionDestroyed;
  23. /// <summary> Create a anchor for the trackable object. </summary>
  24. /// <param name="trackable"> Instantiate a NRAnchor object which Update trackable pose every frame.</param>
  25. /// <returns> NRAnchor. </returns>
  26. public static NRAnchor Factory(NRTrackable trackable)
  27. {
  28. if (trackable == null)
  29. {
  30. return null;
  31. }
  32. NRAnchor result;
  33. if (m_AnchorDict.TryGetValue(trackable.GetDataBaseIndex(), out result))
  34. {
  35. return result;
  36. }
  37. NRAnchor anchor = (new GameObject()).AddComponent<NRAnchor>();
  38. anchor.gameObject.name = "Anchor";
  39. anchor.Trackable = trackable;
  40. m_AnchorDict.Add(trackable.GetDataBaseIndex(), anchor);
  41. return anchor;
  42. }
  43. /// <summary> Updates this object. </summary>
  44. private void Update()
  45. {
  46. if (Trackable == null)
  47. {
  48. NRDebugger.Error("NRAnchor components instantiated outside of NRInternel are not supported. " +
  49. "Please use a 'Create' method within NRInternel to instantiate anchors.");
  50. return;
  51. }
  52. if (IsSessionDestroyed())
  53. {
  54. return;
  55. }
  56. var pose = Trackable.GetCenterPose();
  57. transform.position = pose.position;
  58. transform.rotation = pose.rotation;
  59. }
  60. /// <summary> Executes the 'destroy' action. </summary>
  61. private void OnDestroy()
  62. {
  63. if (Trackable == null)
  64. {
  65. return;
  66. }
  67. m_AnchorDict.Remove(Trackable.GetDataBaseIndex());
  68. }
  69. /// <summary> Check whether the session is already destroyed. </summary>
  70. /// <returns> True if session destroyed, false if not. </returns>
  71. private bool IsSessionDestroyed()
  72. {
  73. if (!m_IsSessionDestroyed)
  74. {
  75. var subsystem = NRSessionManager.Instance.TrackableFactory.TrackableSubsystem;
  76. if (subsystem != Trackable.TrackableSubsystem)
  77. {
  78. Debug.LogErrorFormat("The session which created this anchor has been destroyed. " +
  79. "The anchor on GameObject {0} can no longer update.",
  80. this.gameObject != null ? this.gameObject.name : "Unknown");
  81. m_IsSessionDestroyed = true;
  82. }
  83. }
  84. return m_IsSessionDestroyed;
  85. }
  86. }
  87. }