CreateZEDModel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// Spawns a prefab 3D model of the ZED camera or ZED Mini, depending on which is connected.
  6. /// Used in the MR Calibration scene to visualize its position relative to a tracked object.
  7. /// </summary>
  8. public class CreateZEDModel : MonoBehaviour
  9. {
  10. /// <summary>
  11. /// Prefab of a scaled model of the original ZED camera.
  12. /// </summary>
  13. [Tooltip("Prefab of a scaled model of the original ZED camera.")]
  14. public GameObject zedModelPrefab;
  15. /// <summary>
  16. /// Prefab of a scaled model of the ZED Mini camera.
  17. /// </summary>
  18. [Tooltip("Prefab of a scaled model of the ZED Mini camera. ")]
  19. public GameObject zedMiniModelPrefab;
  20. /// <summary>
  21. /// If true, the object is centered on the physical center of the camera. Otherwise, it's centered
  22. /// on the left lens, which is the main reference point of the ZED's position and the origin of the
  23. /// left (primary) video feed.
  24. /// </summary>
  25. [Tooltip("If true, the object is centered on the physical center of the camera. Otherwise, it's centered " +
  26. "on the left lens, which is the main reference point of the ZED's position and the origin of the " +
  27. "left (primary) video feed.")]
  28. public bool centerOnGeometry = false;
  29. /// <summary>
  30. /// Camera ID to use to determine which ZED model to display. Should always be 1 if you only have one ZED connected.
  31. /// </summary>
  32. [Tooltip("Camera ID to use to determine which ZED model to display. Should always be 1 if you only have one ZED connected.")]
  33. public sl.ZED_CAMERA_ID cameraID = sl.ZED_CAMERA_ID.CAMERA_ID_01;
  34. private GameObject zedModelObject;
  35. private ZEDManager zedManager
  36. {
  37. get
  38. {
  39. return ZEDManager.GetInstance(cameraID);
  40. }
  41. }
  42. // Use this for initialization
  43. void Awake()
  44. {
  45. if (zedManager != null)
  46. {
  47. //If ZEDManager is already ready, make the model. Otherwise, listen for it to be ready.
  48. if (zedManager.IsZEDReady) CreateModel();
  49. zedManager.OnZEDReady += CreateModel; //Still subscribe anyway in case the ZED is replaced with a different model.
  50. }
  51. }
  52. /// <summary>
  53. /// Instantiates the proper prefab for the ZED or ZED Mini as a child of this object.
  54. /// </summary>
  55. private void CreateModel()
  56. {
  57. if (zedModelObject != null) Destroy(zedModelObject); //Shouldn't happen but just in case.
  58. sl.MODEL model = zedManager.zedCamera.CameraModel;
  59. if (model == sl.MODEL.ZED)
  60. {
  61. if (!zedModelPrefab)
  62. {
  63. Debug.LogError("CameraAnchor tried to spawn ZED Model but zedModelPrefab is null.");
  64. return;
  65. }
  66. zedModelObject = Instantiate(zedModelPrefab, transform);
  67. zedModelObject.layer = gameObject.layer;
  68. }
  69. else if (model == sl.MODEL.ZED_M)
  70. {
  71. if (!zedMiniModelPrefab)
  72. {
  73. Debug.LogError("CameraAnchor tried to spawn ZED Mini Model but zedMiniModelPrefab is null.");
  74. return;
  75. }
  76. zedModelObject = Instantiate(zedMiniModelPrefab, transform, false);
  77. }
  78. if (zedModelObject != null && centerOnGeometry == true)
  79. {
  80. float halfbaseline = zedManager.zedCamera.Baseline / 2f;
  81. zedModelObject.transform.localPosition = new Vector3(-halfbaseline, 0, 0);
  82. }
  83. }
  84. private void OnDestroy()
  85. {
  86. if(zedManager != null) zedManager.OnZEDReady -= CreateModel;
  87. }
  88. }