123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- using System.Collections;
- using System.Collections.Generic;
- using Unity.Collections;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- namespace Seengene.XDKUnityPluginCloud {
- public class CloudDemoController : MonoBehaviour {
- #region Properties
- [Header("Base Settings")]
- /// <summary>
- /// XDK会话对象
- /// </summary>
- private XDKCloudSession m_XDKCloudSession = null;
- [SerializeField] private Transform m_ARHead = null;
- /// <summary>
- /// 场景参照根对象
- /// </summary>
- [SerializeField] private Transform m_SceneRoot = null;
- /// <summary>
- /// 是否开始算法重定位
- /// </summary>
- [SerializeField] private Toggle m_ToggleRelocationOn = null;
- [Header("Debug Setttings")]
- /// <summary>
- /// 输入日志开关
- /// </summary>
- [SerializeField] private Toggle m_ToggleLogOn = null;
- /// <summary>
- /// 保存图片到本地开关
- /// </summary>
- [SerializeField] private Toggle m_ToggleSaveImages = null;
- /// <summary>
- /// 模拟首次定位成功
- /// </summary>
- [SerializeField] private Button m_BtnSimulatFirstRelocSuccess = null;
- /// <summary>
- /// 提示信息
- /// </summary>
- [SerializeField] private Text m_InfoSceneRoot = null;
- [SerializeField] private Text m_InfoUpdateFrameNative = null;
- [SerializeField] private Text m_InfoGetSpatialMappingNative = null;
- /// <summary>
- /// 定位时间间隔
- /// </summary>
- private float m_Interval;
- /// <summary>
- /// 上一次定位时间
- /// </summary>
- private float m_LastTime;
- /// <summary>
- /// 预览图
- /// </summary>
- [SerializeField] private RawImage m_PreviewImage = null;
- private bool ifShowKeyPoints = true;
- [SerializeField] private GameObject keyPointPrefab = null;
- List<GameObject> keyPointsList = new List<GameObject>();
- [Header("Smooth Move Settings")]
- [SerializeField] SmoothMoveType m_SmoothMoveType = SmoothMoveType.Linear;
- [SerializeField] float m_SmoothMoveTime = 1.0f;
- Vector3 m_StartPosition = Vector3.zero;
- Quaternion m_StartRotation = Quaternion.identity;
- Vector3 m_TargetPosition = Vector3.zero;
- Quaternion m_TargetRotation = Quaternion.identity;
- [Header("Unity Events")]
- /// <summary>
- /// 首次重定位成功事件
- /// </summary>
- [SerializeField] private UnityEvent m_OnFirstRelocationSuccessEvent = new UnityEvent();
- /// <summary>
- /// Debug模式切换事件
- /// </summary>
- [SerializeField] private UnityEventBool m_OnDebugModeChangeEvent = new UnityEventBool();
- #endregion
- #region Unity Methods
- private void Awake() {
- m_XDKCloudSession = GetComponent<XDKCloudSession>();
- m_LastTime = Time.realtimeSinceStartup;
- if (XDKConfigs.IfSaveImages) {
- m_ToggleSaveImages.isOn = true;
- } else {
- m_ToggleSaveImages.isOn = false;
- }
- m_ToggleSaveImages.onValueChanged.AddListener(OnSaveImagesLogChanged);
- if (XDKConfigs.IfLogOn) {
- m_ToggleLogOn.isOn = true;
- } else {
- m_ToggleLogOn.isOn = false;
- }
- m_ToggleLogOn.onValueChanged.AddListener(OnLogOnToggleChanged);
- if (XDKConfigs.IfRelocationOn) {
- m_ToggleRelocationOn.isOn = true;
- } else {
- m_ToggleRelocationOn.isOn = false;
- }
- m_ToggleRelocationOn.onValueChanged.AddListener(OnToggleRelocationOnChanged);
- if (m_XDKCloudSession != null)
- m_XDKCloudSession.OnUpdateDebugInfoEvent += OnOnUpdateDebugInfo;
- if (m_BtnSimulatFirstRelocSuccess != null) {
- m_BtnSimulatFirstRelocSuccess.onClick.AddListener(() => {
- m_XDKCloudSession.SimulateFirstRelocatSuccess();
- });
- }
- }
- private void OnEnable() {
- if (m_XDKCloudSession != null) {
- m_XDKCloudSession.SpatialMappedEvent += OnSpatialMapped;
- m_XDKCloudSession.OnFirstRelocationSuccessEvent += OnFirstRelocationSuccess;
- m_XDKCloudSession.UpdatePreviewImageEvent += OnPreviewImageEvent;
- if (ifShowKeyPoints)
- m_XDKCloudSession.UpdateKeyPointsEvent += OnUpdateKeyPoints;
- }
- XDKConfigs.OnDebugModeChange += OnDebugModeChangeEvent;
- }
- private void OnDisable() {
- if (m_XDKCloudSession != null) {
- m_XDKCloudSession.SpatialMappedEvent -= OnSpatialMapped;
- m_XDKCloudSession.OnFirstRelocationSuccessEvent -= OnFirstRelocationSuccess;
- m_XDKCloudSession.UpdatePreviewImageEvent -= OnPreviewImageEvent;
- if (ifShowKeyPoints)
- m_XDKCloudSession.UpdateKeyPointsEvent -= OnUpdateKeyPoints;
- }
- XDKConfigs.OnDebugModeChange -= OnDebugModeChangeEvent;
- }
- private void Update() {
- if (XDKConfigs.IfDebugOn && m_ARHead != null) {
- m_InfoSceneRoot.text = string.Format("<SceneRoot>\nPos:{0},Rot:{1},Scale:{2}\n<ARCamera>\nPos:{3},Rot:{4}\n<Interval> {5}",
- m_SceneRoot.position.ToString("f2"),
- m_SceneRoot.eulerAngles.ToString("f2"),
- m_SceneRoot.localScale.ToString("f2"),
- m_ARHead.position.ToString("f2"),
- m_ARHead.eulerAngles.ToString("f2"),
- m_Interval.ToString("f2")
- );
- }
- }
- #endregion
- #region Private Methods
- private void OnSpatialMapped(Vector3 position, Quaternion rotation, Vector3 scale, int relocSuccessCounter) {
- m_Interval = Time.realtimeSinceStartup - m_LastTime;
- m_LastTime = Time.realtimeSinceStartup;
- if (m_SceneRoot != null) {
- if (relocSuccessCounter == 1) {
- m_SceneRoot.localScale = scale;
- m_SceneRoot.localPosition = position;
- m_SceneRoot.localRotation = rotation;
- } else {
- m_SceneRoot.localScale = scale;
- StopCoroutine("CorMoveToTarget");
- m_TargetPosition = position;
- m_TargetRotation = rotation;
- StartCoroutine("CorMoveToTarget");
- }
- }
- }
- IEnumerator CorMoveToTarget() {
- m_StartPosition = m_SceneRoot.position;
- m_StartRotation = m_SceneRoot.rotation;
- float factor = 0;
- while (factor != 1) {
- factor += Time.deltaTime / m_SmoothMoveTime;
- factor = Mathf.Clamp01(factor);
- if (m_SmoothMoveType == SmoothMoveType.Linear) {
- m_SceneRoot.position = Vector3.Lerp(m_StartPosition, m_TargetPosition, factor);
- m_SceneRoot.rotation = Quaternion.Lerp(m_StartRotation, m_TargetRotation, factor);
- } else {
- m_SceneRoot.position = Vector3.Lerp(m_SceneRoot.position, m_TargetPosition, factor);
- m_SceneRoot.rotation = Quaternion.Lerp(m_SceneRoot.rotation, m_TargetRotation, factor);
- }
- yield return new WaitForEndOfFrame();
- }
- }
- private void OnUpdateKeyPoints(List<Vector3> points) {
- keyPointsList.ForEach((go) => { DestroyImmediate(go); });
- keyPointsList.Clear();
- for (int i = 0; i < points.Count; i++) {
- GameObject go = Instantiate(keyPointPrefab, m_SceneRoot);
- go.transform.localPosition = new Vector3(points[i].x, -points[i].y, points[i].z);
- keyPointsList.Add(go);
- }
- }
- private void OnPreviewImageEvent(Texture texture) {
- m_PreviewImage.texture = texture;
- m_PreviewImage.rectTransform.sizeDelta = new Vector2(texture.width, texture.height);
- }
- private void OnFirstRelocationSuccess() {
- Debug.Log("首次重定位成功!");
- if (m_OnFirstRelocationSuccessEvent != null)
- m_OnFirstRelocationSuccessEvent.Invoke();
- }
- private void OnOnUpdateDebugInfo(string debugInfo, DebugInfoType type) {
- switch (type) {
- case DebugInfoType.UpdateFrameNative:
- m_InfoUpdateFrameNative.text = debugInfo;
- break;
- case DebugInfoType.GetSpatialMappingNative:
- m_InfoGetSpatialMappingNative.text = debugInfo;
- break;
- default:
- break;
- }
- }
- private void OnSaveImagesLogChanged(bool value) {
- XDKConfigs.IfSaveImages = value;
- m_PreviewImage.gameObject.SetActive(value);
- }
- private void OnLogOnToggleChanged(bool value) {
- XDKConfigs.IfLogOn = value;
- }
- private void OnToggleRelocationOnChanged(bool value) {
- XDKConfigs.IfRelocationOn = value;
- if (XDKConfigs.IfLogOn) {
- string strDebug;
- if (XDKConfigs.IfRelocationOn) {
- strDebug = "开启重定位功能!";
- } else {
- strDebug = "关闭重定位功能!";
- }
- Debug.Log(strDebug);
- }
- }
- private void OnDebugModeChangeEvent() {
- Debug.LogFormat("OnDebugModeChangeEvent: {0}", XDKConfigs.IfDebugOn);
- if (m_OnDebugModeChangeEvent != null)
- m_OnDebugModeChangeEvent.Invoke(XDKConfigs.IfDebugOn);
- if (XDKConfigs.IfDebugOn == false) {
- ///清空关键点
- if (keyPointsList != null) {
- keyPointsList.ForEach((go) => { DestroyImmediate(go); });
- keyPointsList.Clear();
- }
- }
- }
- #endregion
- }
- }
|