MapListController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using System.Collections.Generic;
  9. using System.Threading.Tasks;
  10. using UnityEngine;
  11. using TMPro;
  12. using Immersal.AR;
  13. using Immersal.REST;
  14. namespace Immersal.Samples
  15. {
  16. [RequireComponent(typeof(TMP_Dropdown))]
  17. public class MapListController : MonoBehaviour
  18. {
  19. [SerializeField]
  20. private ARMap m_ARMap = null;
  21. private List<SDKJob> m_Maps;
  22. private TMP_Dropdown m_Dropdown;
  23. private List<JobAsync> m_Jobs = new List<JobAsync>();
  24. private int m_JobLock = 0;
  25. private TextAsset m_EmbeddedMap;
  26. void Awake()
  27. {
  28. m_Dropdown = GetComponent<TMP_Dropdown>();
  29. m_Dropdown.ClearOptions();
  30. if (m_ARMap?.mapFile != null)
  31. {
  32. m_Dropdown.AddOptions( new List<string>() { string.Format("<{0}>", m_ARMap.mapFile.name) });
  33. m_EmbeddedMap = m_ARMap.mapFile;
  34. }
  35. else
  36. {
  37. m_Dropdown.AddOptions( new List<string>() { "Load map..." });
  38. }
  39. m_Maps = new List<SDKJob>();
  40. }
  41. void Start()
  42. {
  43. Invoke("GetMaps", 0.5f);
  44. }
  45. void Update()
  46. {
  47. if (m_JobLock == 1)
  48. return;
  49. if (m_Jobs.Count > 0)
  50. {
  51. m_JobLock = 1;
  52. RunJob(m_Jobs[0]);
  53. }
  54. }
  55. public async void OnValueChanged(TMP_Dropdown dropdown)
  56. {
  57. int value = dropdown.value - 1;
  58. // use embedded map
  59. if (m_EmbeddedMap != null && value == -1)
  60. {
  61. m_ARMap.mapFile = m_EmbeddedMap;
  62. await m_ARMap.LoadMap();
  63. }
  64. else
  65. {
  66. if (value >= 0)
  67. {
  68. SDKJob map = m_Maps[value];
  69. LoadMap(map);
  70. }
  71. }
  72. }
  73. public void GetMaps()
  74. {
  75. JobListJobsAsync j = new JobListJobsAsync();
  76. j.token = ImmersalSDK.Instance.developerToken;
  77. j.OnResult += (SDKJobsResult result) =>
  78. {
  79. if (result.count > 0)
  80. {
  81. List<string> names = new List<string>();
  82. foreach (SDKJob job in result.jobs)
  83. {
  84. if (job.type != (int)SDKJobType.Alignment && (job.status == SDKJobState.Sparse || job.status == SDKJobState.Done))
  85. {
  86. this.m_Maps.Add(job);
  87. names.Add(job.name);
  88. }
  89. }
  90. this.m_Dropdown.AddOptions(names);
  91. }
  92. };
  93. m_Jobs.Add(j);
  94. }
  95. public void ClearMaps()
  96. {
  97. ARMap[] arMaps = GameObject.FindObjectsOfType<ARMap>();
  98. foreach (ARMap arMap in arMaps)
  99. {
  100. arMap.FreeMap(true);
  101. }
  102. m_Dropdown.SetValueWithoutNotify(0);
  103. }
  104. public void LoadMap(SDKJob job)
  105. {
  106. JobLoadMapBinaryAsync j = new JobLoadMapBinaryAsync();
  107. j.id = job.id;
  108. j.OnResult += async (SDKMapResult result) =>
  109. {
  110. Debug.Log(string.Format("Load map {0} ({1} bytes)", job.id, result.mapData.Length));
  111. Color pointCloudColor = ARMap.pointCloudColors[UnityEngine.Random.Range(0, ARMap.pointCloudColors.Length)];
  112. ARMap.RenderMode renderMode = m_ARMap?.renderMode ?? ARMap.RenderMode.EditorAndRuntime;
  113. await ARSpace.LoadAndInstantiateARMap(null, result, renderMode, pointCloudColor);
  114. };
  115. m_Jobs.Add(j);
  116. }
  117. private async void RunJob(JobAsync j)
  118. {
  119. await j.RunJobAsync();
  120. if (m_Jobs.Count > 0)
  121. m_Jobs.RemoveAt(0);
  122. m_JobLock = 0;
  123. }
  124. }
  125. }