CloudDemoController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.Collections;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. namespace Seengene.XDKUnityPluginCloud {
  9. public class CloudDemoController : MonoBehaviour {
  10. #region Properties
  11. [Header("Base Settings")]
  12. /// <summary>
  13. /// XDK会话对象
  14. /// </summary>
  15. private XDKCloudSession m_XDKCloudSession = null;
  16. [SerializeField] private Transform m_ARHead = null;
  17. /// <summary>
  18. /// 场景参照根对象
  19. /// </summary>
  20. [SerializeField] private Transform m_SceneRoot = null;
  21. /// <summary>
  22. /// 是否开始算法重定位
  23. /// </summary>
  24. [SerializeField] private Toggle m_ToggleRelocationOn = null;
  25. [Header("Debug Setttings")]
  26. /// <summary>
  27. /// 输入日志开关
  28. /// </summary>
  29. [SerializeField] private Toggle m_ToggleLogOn = null;
  30. /// <summary>
  31. /// 保存图片到本地开关
  32. /// </summary>
  33. [SerializeField] private Toggle m_ToggleSaveImages = null;
  34. /// <summary>
  35. /// 模拟首次定位成功
  36. /// </summary>
  37. [SerializeField] private Button m_BtnSimulatFirstRelocSuccess = null;
  38. /// <summary>
  39. /// 提示信息
  40. /// </summary>
  41. [SerializeField] private Text m_InfoSceneRoot = null;
  42. [SerializeField] private Text m_InfoUpdateFrameNative = null;
  43. [SerializeField] private Text m_InfoGetSpatialMappingNative = null;
  44. /// <summary>
  45. /// 定位时间间隔
  46. /// </summary>
  47. private float m_Interval;
  48. /// <summary>
  49. /// 上一次定位时间
  50. /// </summary>
  51. private float m_LastTime;
  52. /// <summary>
  53. /// 预览图
  54. /// </summary>
  55. [SerializeField] private RawImage m_PreviewImage = null;
  56. private bool ifShowKeyPoints = true;
  57. [SerializeField] private GameObject keyPointPrefab = null;
  58. List<GameObject> keyPointsList = new List<GameObject>();
  59. [Header("Smooth Move Settings")]
  60. [SerializeField] SmoothMoveType m_SmoothMoveType = SmoothMoveType.Linear;
  61. [SerializeField] float m_SmoothMoveTime = 1.0f;
  62. Vector3 m_StartPosition = Vector3.zero;
  63. Quaternion m_StartRotation = Quaternion.identity;
  64. Vector3 m_TargetPosition = Vector3.zero;
  65. Quaternion m_TargetRotation = Quaternion.identity;
  66. [Header("Unity Events")]
  67. /// <summary>
  68. /// 首次重定位成功事件
  69. /// </summary>
  70. [SerializeField] private UnityEvent m_OnFirstRelocationSuccessEvent = new UnityEvent();
  71. /// <summary>
  72. /// Debug模式切换事件
  73. /// </summary>
  74. [SerializeField] private UnityEventBool m_OnDebugModeChangeEvent = new UnityEventBool();
  75. #endregion
  76. #region Unity Methods
  77. private void Awake() {
  78. m_XDKCloudSession = GetComponent<XDKCloudSession>();
  79. m_LastTime = Time.realtimeSinceStartup;
  80. if (XDKConfigs.IfSaveImages) {
  81. m_ToggleSaveImages.isOn = true;
  82. } else {
  83. m_ToggleSaveImages.isOn = false;
  84. }
  85. m_ToggleSaveImages.onValueChanged.AddListener(OnSaveImagesLogChanged);
  86. if (XDKConfigs.IfLogOn) {
  87. m_ToggleLogOn.isOn = true;
  88. } else {
  89. m_ToggleLogOn.isOn = false;
  90. }
  91. m_ToggleLogOn.onValueChanged.AddListener(OnLogOnToggleChanged);
  92. if (XDKConfigs.IfRelocationOn) {
  93. m_ToggleRelocationOn.isOn = true;
  94. } else {
  95. m_ToggleRelocationOn.isOn = false;
  96. }
  97. m_ToggleRelocationOn.onValueChanged.AddListener(OnToggleRelocationOnChanged);
  98. if (m_XDKCloudSession != null)
  99. m_XDKCloudSession.OnUpdateDebugInfoEvent += OnOnUpdateDebugInfo;
  100. if (m_BtnSimulatFirstRelocSuccess != null) {
  101. m_BtnSimulatFirstRelocSuccess.onClick.AddListener(() => {
  102. m_XDKCloudSession.SimulateFirstRelocatSuccess();
  103. });
  104. }
  105. }
  106. private void OnEnable() {
  107. if (m_XDKCloudSession != null) {
  108. m_XDKCloudSession.SpatialMappedEvent += OnSpatialMapped;
  109. m_XDKCloudSession.OnFirstRelocationSuccessEvent += OnFirstRelocationSuccess;
  110. m_XDKCloudSession.UpdatePreviewImageEvent += OnPreviewImageEvent;
  111. if (ifShowKeyPoints)
  112. m_XDKCloudSession.UpdateKeyPointsEvent += OnUpdateKeyPoints;
  113. }
  114. XDKConfigs.OnDebugModeChange += OnDebugModeChangeEvent;
  115. }
  116. private void OnDisable() {
  117. if (m_XDKCloudSession != null) {
  118. m_XDKCloudSession.SpatialMappedEvent -= OnSpatialMapped;
  119. m_XDKCloudSession.OnFirstRelocationSuccessEvent -= OnFirstRelocationSuccess;
  120. m_XDKCloudSession.UpdatePreviewImageEvent -= OnPreviewImageEvent;
  121. if (ifShowKeyPoints)
  122. m_XDKCloudSession.UpdateKeyPointsEvent -= OnUpdateKeyPoints;
  123. }
  124. XDKConfigs.OnDebugModeChange -= OnDebugModeChangeEvent;
  125. }
  126. private void Update() {
  127. if (XDKConfigs.IfDebugOn && m_ARHead != null) {
  128. m_InfoSceneRoot.text = string.Format("<SceneRoot>\nPos:{0},Rot:{1},Scale:{2}\n<ARCamera>\nPos:{3},Rot:{4}\n<Interval> {5}",
  129. m_SceneRoot.position.ToString("f2"),
  130. m_SceneRoot.eulerAngles.ToString("f2"),
  131. m_SceneRoot.localScale.ToString("f2"),
  132. m_ARHead.position.ToString("f2"),
  133. m_ARHead.eulerAngles.ToString("f2"),
  134. m_Interval.ToString("f2")
  135. );
  136. }
  137. }
  138. #endregion
  139. #region Private Methods
  140. private void OnSpatialMapped(Vector3 position, Quaternion rotation, Vector3 scale, int relocSuccessCounter) {
  141. m_Interval = Time.realtimeSinceStartup - m_LastTime;
  142. m_LastTime = Time.realtimeSinceStartup;
  143. if (m_SceneRoot != null) {
  144. if (relocSuccessCounter == 1) {
  145. m_SceneRoot.localScale = scale;
  146. m_SceneRoot.localPosition = position;
  147. m_SceneRoot.localRotation = rotation;
  148. } else {
  149. m_SceneRoot.localScale = scale;
  150. StopCoroutine("CorMoveToTarget");
  151. m_TargetPosition = position;
  152. m_TargetRotation = rotation;
  153. StartCoroutine("CorMoveToTarget");
  154. }
  155. }
  156. }
  157. IEnumerator CorMoveToTarget() {
  158. m_StartPosition = m_SceneRoot.position;
  159. m_StartRotation = m_SceneRoot.rotation;
  160. float factor = 0;
  161. while (factor != 1) {
  162. factor += Time.deltaTime / m_SmoothMoveTime;
  163. factor = Mathf.Clamp01(factor);
  164. if (m_SmoothMoveType == SmoothMoveType.Linear) {
  165. m_SceneRoot.position = Vector3.Lerp(m_StartPosition, m_TargetPosition, factor);
  166. m_SceneRoot.rotation = Quaternion.Lerp(m_StartRotation, m_TargetRotation, factor);
  167. } else {
  168. m_SceneRoot.position = Vector3.Lerp(m_SceneRoot.position, m_TargetPosition, factor);
  169. m_SceneRoot.rotation = Quaternion.Lerp(m_SceneRoot.rotation, m_TargetRotation, factor);
  170. }
  171. yield return new WaitForEndOfFrame();
  172. }
  173. }
  174. private void OnUpdateKeyPoints(List<Vector3> points) {
  175. keyPointsList.ForEach((go) => { DestroyImmediate(go); });
  176. keyPointsList.Clear();
  177. for (int i = 0; i < points.Count; i++) {
  178. GameObject go = Instantiate(keyPointPrefab, m_SceneRoot);
  179. go.transform.localPosition = new Vector3(points[i].x, -points[i].y, points[i].z);
  180. keyPointsList.Add(go);
  181. }
  182. }
  183. private void OnPreviewImageEvent(Texture texture) {
  184. m_PreviewImage.texture = texture;
  185. m_PreviewImage.rectTransform.sizeDelta = new Vector2(texture.width, texture.height);
  186. }
  187. private void OnFirstRelocationSuccess() {
  188. Debug.Log("首次重定位成功!");
  189. if (m_OnFirstRelocationSuccessEvent != null)
  190. m_OnFirstRelocationSuccessEvent.Invoke();
  191. }
  192. private void OnOnUpdateDebugInfo(string debugInfo, DebugInfoType type) {
  193. switch (type) {
  194. case DebugInfoType.UpdateFrameNative:
  195. m_InfoUpdateFrameNative.text = debugInfo;
  196. break;
  197. case DebugInfoType.GetSpatialMappingNative:
  198. m_InfoGetSpatialMappingNative.text = debugInfo;
  199. break;
  200. default:
  201. break;
  202. }
  203. }
  204. private void OnSaveImagesLogChanged(bool value) {
  205. XDKConfigs.IfSaveImages = value;
  206. m_PreviewImage.gameObject.SetActive(value);
  207. }
  208. private void OnLogOnToggleChanged(bool value) {
  209. XDKConfigs.IfLogOn = value;
  210. }
  211. private void OnToggleRelocationOnChanged(bool value) {
  212. XDKConfigs.IfRelocationOn = value;
  213. if (XDKConfigs.IfLogOn) {
  214. string strDebug;
  215. if (XDKConfigs.IfRelocationOn) {
  216. strDebug = "开启重定位功能!";
  217. } else {
  218. strDebug = "关闭重定位功能!";
  219. }
  220. Debug.Log(strDebug);
  221. }
  222. }
  223. private void OnDebugModeChangeEvent() {
  224. Debug.LogFormat("OnDebugModeChangeEvent: {0}", XDKConfigs.IfDebugOn);
  225. if (m_OnDebugModeChangeEvent != null)
  226. m_OnDebugModeChangeEvent.Invoke(XDKConfigs.IfDebugOn);
  227. if (XDKConfigs.IfDebugOn == false) {
  228. ///清空关键点
  229. if (keyPointsList != null) {
  230. keyPointsList.ForEach((go) => { DestroyImmediate(go); });
  231. keyPointsList.Clear();
  232. }
  233. }
  234. }
  235. #endregion
  236. }
  237. }