AutomaticCaptureManager.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. using UnityEngine.XR.ARFoundation;
  13. using TMPro;
  14. using Immersal.Samples.Util;
  15. namespace Immersal.Samples.Mapping
  16. {
  17. public struct ARPoint
  18. {
  19. public float x;
  20. public float y;
  21. public float z;
  22. public ARPoint(Vector3 pos)
  23. {
  24. x = pos.x;
  25. y = pos.y;
  26. z = pos.z;
  27. }
  28. }
  29. public class AutomaticCaptureManager : MonoBehaviour
  30. {
  31. public const int MAX_VERTICES = 65535;
  32. [SerializeField] private int m_MaxImages = 40;
  33. [SerializeField] private float m_ImageInterval = 0.6f;
  34. [SerializeField] private Image m_CaptureButtonIcon = null;
  35. [SerializeField] private Sprite m_StartCaptureSprite = null;
  36. [SerializeField] private Sprite m_StopCaptureSprite = null;
  37. [SerializeField] private CanvasGroup m_MoveDevicePromptCanvasGroup = null;
  38. protected bool m_IsMapping = false;
  39. private int m_ImagesSubmitted = 0;
  40. private int m_ImagesUploaded = 0;
  41. private Camera m_MainCamera = null;
  42. private bool m_CameraHasMoved = false;
  43. private Vector3 camPrevPos = Vector3.zero;
  44. private Quaternion camPrevRot = Quaternion.identity;
  45. private GameObject m_PointCloud = null;
  46. private Mesh m_Mesh = null;
  47. private MeshFilter m_MeshFilter = null;
  48. private MeshRenderer m_MeshRenderer = null;
  49. private List<ARPoint> m_Points = new List<ARPoint>();
  50. protected List<ARPoint> m_CurrentPoints = new List<ARPoint>();
  51. protected AutomaticCapture m_AutomaticCapture = null;
  52. private ARPointCloudManager m_ArPointCloudManager = null;
  53. private Fader m_MoveDevicePromptFader = null;
  54. virtual protected void Start()
  55. {
  56. InitMesh();
  57. m_MainCamera = Camera.main;
  58. camPrevPos = m_MainCamera.transform.position;
  59. camPrevRot = m_MainCamera.transform.rotation;
  60. m_AutomaticCapture = GetComponent<AutomaticCapture>();
  61. m_ArPointCloudManager = FindObjectOfType<ARPointCloudManager>();
  62. m_CaptureButtonIcon.sprite = m_StartCaptureSprite;
  63. m_MoveDevicePromptFader = m_MoveDevicePromptCanvasGroup.GetComponent<Fader>();
  64. if (m_AutomaticCapture != null)
  65. m_AutomaticCapture.OnImageUploaded += OnImageUploaded;
  66. if (m_ArPointCloudManager != null)
  67. m_ArPointCloudManager.pointCloudsChanged += PointCloudManager_pointCloudsChanged;
  68. }
  69. virtual protected void OnDisable()
  70. {
  71. if (m_AutomaticCapture != null)
  72. m_AutomaticCapture.OnImageUploaded -= OnImageUploaded;
  73. if (m_ArPointCloudManager != null)
  74. m_ArPointCloudManager.pointCloudsChanged -= PointCloudManager_pointCloudsChanged;
  75. }
  76. virtual protected void Update()
  77. {
  78. CheckCameraMovement();
  79. }
  80. protected void OnImageUploaded()
  81. {
  82. m_ImagesUploaded++;
  83. }
  84. private void CheckCameraMovement()
  85. {
  86. Vector3 camPos = m_MainCamera.transform.position;
  87. Quaternion camRot = m_MainCamera.transform.rotation;
  88. m_CameraHasMoved = (camPos - camPrevPos).magnitude > 0.02f;
  89. }
  90. private void PointCloudManager_pointCloudsChanged(ARPointCloudChangedEventArgs obj)
  91. {
  92. m_CurrentPoints.Clear();
  93. List<ARPoint> addedPoints = new List<ARPoint>();
  94. foreach (var pointCloud in obj.added)
  95. {
  96. foreach (var pos in pointCloud.positions)
  97. {
  98. ARPoint newPoint = new ARPoint(pos);
  99. addedPoints.Add(newPoint);
  100. }
  101. }
  102. List<ARPoint> updatedPoints = new List<ARPoint>();
  103. foreach (var pointCloud in obj.updated)
  104. {
  105. foreach (var pos in pointCloud.positions)
  106. {
  107. ARPoint newPoint = new ARPoint(pos);
  108. updatedPoints.Add(newPoint);
  109. m_CurrentPoints.Add(newPoint);
  110. }
  111. }
  112. }
  113. private void InitMesh()
  114. {
  115. m_PointCloud = new GameObject("Automatic Capture Preview Point Cloud", typeof(MeshFilter), typeof(MeshRenderer));
  116. m_MeshFilter = m_PointCloud.GetComponent<MeshFilter>();
  117. m_MeshRenderer = m_PointCloud.GetComponent<MeshRenderer>();
  118. m_Mesh = new Mesh();
  119. m_MeshFilter.mesh = m_Mesh;
  120. Material material = new Material(Shader.Find("Immersal/Point Cloud"));
  121. m_MeshRenderer.material = material;
  122. m_MeshRenderer.material.SetFloat("_PointSize", 20f);
  123. m_MeshRenderer.material.SetFloat("_PerspectiveEnabled", 0f);
  124. m_MeshRenderer.material.SetColor("_PointColor", new Color(0.57f, 0.93f, 0.12f));
  125. }
  126. public void CreateCloud(Vector3[] points, int totalPoints, Matrix4x4 offset)
  127. {
  128. int numPoints = totalPoints >= MAX_VERTICES ? MAX_VERTICES : totalPoints;
  129. int[] indices = new int[numPoints];
  130. Vector3[] pts = new Vector3[numPoints];
  131. Color32[] col = new Color32[numPoints];
  132. for (int i = 0; i < numPoints; ++i)
  133. {
  134. indices[i] = i;
  135. pts[i] = offset.MultiplyPoint3x4(points[i]);
  136. }
  137. m_Mesh.Clear();
  138. m_Mesh.vertices = pts;
  139. m_Mesh.colors32 = col;
  140. m_Mesh.SetIndices(indices, MeshTopology.Points, 0);
  141. m_Mesh.bounds = new Bounds(transform.position, new Vector3(float.MaxValue, float.MaxValue, float.MaxValue));
  142. }
  143. public void CreateCloud(Vector3[] points, int totalPoints)
  144. {
  145. CreateCloud(points, totalPoints, Matrix4x4.identity);
  146. }
  147. private void UpdatePoints()
  148. {
  149. foreach (ARPoint p in m_CurrentPoints)
  150. {
  151. m_Points.Add(p);
  152. }
  153. int maxPoints = m_Points.Count >= MAX_VERTICES ? MAX_VERTICES : m_Points.Count;
  154. Vector3[] points = new Vector3[maxPoints];
  155. for (int i = 0; i < maxPoints; i++)
  156. {
  157. float x = m_Points[i].x;
  158. float y = m_Points[i].y;
  159. float z = m_Points[i].z;
  160. points[i] = new Vector3(x, y, z);
  161. }
  162. CreateCloud(points, points.Length);
  163. }
  164. private void ResetPoints()
  165. {
  166. m_Points.Clear();
  167. Vector3[] points = new Vector3[0];
  168. CreateCloud(points, points.Length);
  169. }
  170. public void ToggleMapping()
  171. {
  172. if (!m_IsMapping)
  173. {
  174. StartMapping();
  175. }
  176. else
  177. {
  178. StopMapping();
  179. }
  180. }
  181. public void CancelMapping()
  182. {
  183. StopMapping();
  184. }
  185. private void StartMapping()
  186. {
  187. ResetPoints();
  188. m_IsMapping = true;
  189. m_CaptureButtonIcon.sprite = m_StopCaptureSprite;
  190. StartCoroutine("CaptureImages");
  191. Debug.Log("Auto mapping started");
  192. }
  193. private void StopMapping()
  194. {
  195. ResetPoints();
  196. m_IsMapping = false;
  197. m_CaptureButtonIcon.sprite = m_StartCaptureSprite;
  198. StopCoroutine("CaptureImages");
  199. Debug.Log("Auto mapping stopped");
  200. }
  201. private IEnumerator CaptureImages()
  202. {
  203. m_ImagesSubmitted = 0;
  204. m_ImagesUploaded = 0;
  205. float t = 0f;
  206. bool promptActive = false;
  207. while (m_ImagesUploaded < m_MaxImages)
  208. {
  209. t += Time.deltaTime;
  210. if (t > m_ImageInterval)
  211. {
  212. if (m_CameraHasMoved)
  213. {
  214. m_MoveDevicePromptFader.fadeTime = 0.15f;
  215. m_MoveDevicePromptFader.FadeOut();
  216. promptActive = false;
  217. float progressStart = (float)m_ImagesSubmitted / (float)m_MaxImages;
  218. float progressEnd = progressStart + 1f / (float)m_MaxImages;
  219. // Capture image
  220. m_AutomaticCapture.Capture();
  221. t = 0f;
  222. m_ImagesSubmitted++;
  223. camPrevPos = m_MainCamera.transform.position;
  224. camPrevRot = m_MainCamera.transform.rotation;
  225. UpdatePoints();
  226. }
  227. else if(!promptActive)
  228. {
  229. m_MoveDevicePromptFader.fadeTime = 1f;
  230. m_MoveDevicePromptFader.FadeIn();
  231. promptActive = true;
  232. }
  233. }
  234. yield return null;
  235. }
  236. StopMapping();
  237. }
  238. public void SetMaxImages(float maxImages)
  239. {
  240. m_MaxImages = Mathf.RoundToInt(maxImages);
  241. }
  242. public void SetInterval(float interval)
  243. {
  244. m_ImageInterval = interval;
  245. }
  246. }
  247. }