CreatesGroundPlaneByJson.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. namespace Ximmerse.XR.Tag
  6. {
  7. [System.Serializable]
  8. public struct GroundPlanePlacementData
  9. {
  10. public GroundPlanePlacementItem[] items;
  11. }
  12. [System.Serializable]
  13. public class GroundPlanePlacementItem
  14. {
  15. public int beacon_id;
  16. public int group_id;
  17. public float size;
  18. public Vector3 position;
  19. public Vector3 rotation;
  20. public int coord_system_flag; // 0 = left hand (unity), 1 = right hand (openXR)
  21. public float confidence_thresh = 0.9f;
  22. public float min_distance_thresh = 0.1f;
  23. public float max_distance_thresh = 1.8f;
  24. public float drift_recenter_angle_threshold = 1.0f;
  25. public float drift_recenter_distance_threshold = 1.0f;
  26. }
  27. /// <summary>
  28. /// 读取指定路径的json配置文件,自动创建 Ground Plane。
  29. /// 配置文件举例:
  30. /*
  31. {
  32. "items": [
  33. {
  34. "beacon_id": 67,
  35. "group_id": 0,
  36. "size": 0.17,
  37. "position": {
  38. "x": 0.0,
  39. "y": 0.0,
  40. "z": 0.0
  41. },
  42. "rotation": {
  43. "x": 0.0,
  44. "y": 0.0,
  45. "z": 0.0
  46. },
  47. "coord_system_flag": 1,
  48. "confidence_thresh": 0.9,
  49. "max_distance_thresh": 1.8,
  50. "min_distance_thresh": 0.1,
  51. "drift_recenter_angle_threshold": 1,
  52. "drift_recenter_distance_threshold": 1
  53. },
  54. {
  55. "beacon_id": 66,
  56. "group_id": 1,
  57. "size": 0.17,
  58. "position": {
  59. "x": 1.0,
  60. "y": 1.0,
  61. "z": 1.0
  62. },
  63. "rotation": {
  64. "x": 0.0,
  65. "y": 90.0,
  66. "z": 0.0
  67. },
  68. "coord_system_flag": 1,
  69. "confidence_thresh": 0.9,
  70. "max_distance_thresh": 1.8,
  71. "min_distance_thresh": 0.1,
  72. "drift_recenter_angle_threshold": 1,
  73. "drift_recenter_distance_threshold": 1
  74. },
  75. {
  76. "beacon_id": 65,
  77. "group_id": 1,
  78. "size": 0.17,
  79. "position": {
  80. "x": 0.0,
  81. "y": 0.0,
  82. "z": 1.0
  83. },
  84. "rotation": {
  85. "x": 0.0,
  86. "y": 0.0,
  87. "z": 0.0
  88. },
  89. "coord_system_flag": 1,
  90. "confidence_thresh": 0.9,
  91. "max_distance_thresh": 1.8,
  92. "min_distance_thresh": 0.1,
  93. "drift_recenter_angle_threshold": 1,
  94. "drift_recenter_distance_threshold": 1
  95. }
  96. ]
  97. }
  98. */
  99. /// </summary>
  100. public class CreatesGroundPlaneByJson : MonoBehaviour
  101. {
  102. private static CreatesGroundPlaneByJson instance;
  103. private CreatesGroundPlaneByJson() { }
  104. public static CreatesGroundPlaneByJson Instance
  105. {
  106. get
  107. {
  108. return instance;
  109. }
  110. }
  111. private void Awake()
  112. {
  113. instance = this;
  114. }
  115. public string JsonFilePath = "/sdcard/GroundPlaneConfig.txt";
  116. public bool autoCreates = true;
  117. public bool debugView = false;
  118. public float size = 0.17f;
  119. private void Start()
  120. {
  121. if (autoCreates)
  122. {
  123. TagProfileLoading.Instance.GroundPlaneList.Clear();
  124. CreateGroundPlanesFromConfig();
  125. }
  126. }
  127. [ContextMenu("Create ground plane from json config")]
  128. public void CreateGroundPlanesFromConfig()
  129. {
  130. try
  131. {
  132. if (!File.Exists(JsonFilePath))
  133. {
  134. return;
  135. }
  136. var txt = File.ReadAllText(JsonFilePath);
  137. //var txt = Resources.Load<TextAsset>("groundplane-layout").ToString();
  138. GroundPlanePlacementData placementData = JsonUtility.FromJson<GroundPlanePlacementData>(txt);
  139. for (int i = 0; i < placementData.items.Length; i++)
  140. {
  141. GroundPlanePlacementItem groundPlaneItem = placementData.items[i];
  142. GameObject go = GameObject.Instantiate(Resources.Load("Tag/Prefabs/GroundPlane")) as GameObject;
  143. go.name = "GroundPlane - " + groundPlaneItem.beacon_id;
  144. var gp = go.GetComponent<TagGroundPlane>();
  145. if (debugView)
  146. {
  147. gp.DebugView = true;
  148. gp.Size = size;
  149. }
  150. gp.TrackId = groundPlaneItem.beacon_id;
  151. //gp.text.text = gp.track_id.ToString();
  152. gp.BeaconDriftRecenterAngleThreshold = groundPlaneItem.drift_recenter_angle_threshold;
  153. gp.BeaconDriftRecenterDistanceThreshold = groundPlaneItem.drift_recenter_distance_threshold;
  154. gp.BeaconConfidenceThresh = groundPlaneItem.confidence_thresh;
  155. gp.BeaconMaxDistanceThresh = groundPlaneItem.max_distance_thresh;
  156. gp.BeaconMinDistanceThresh = groundPlaneItem.min_distance_thresh;
  157. gp.BeaconCoordSystemFlag = groundPlaneItem.coord_system_flag;
  158. go.transform.position = groundPlaneItem.position;
  159. go.transform.eulerAngles = groundPlaneItem.rotation;
  160. TagProfileLoading.Instance.GroundPlaneList.Add(go);
  161. }
  162. }
  163. catch (System.Exception ex)
  164. {
  165. Debug.LogException(ex);
  166. }
  167. TagProfileLoading.Instance.SettingData();
  168. }
  169. [ContextMenu("Test convert to json string")]
  170. public void TestToJson()
  171. {
  172. GroundPlanePlacementData data = new GroundPlanePlacementData();
  173. data.items = new GroundPlanePlacementItem[]
  174. {
  175. new GroundPlanePlacementItem()
  176. {
  177. beacon_id = 1, position = Vector3.zero, rotation = Vector3.zero,
  178. },
  179. new GroundPlanePlacementItem()
  180. {
  181. beacon_id = 2, position = Vector3.one, rotation = new Vector3(0,90,0),
  182. confidence_thresh = 0.85f, coord_system_flag = 0, drift_recenter_angle_threshold = 7.5f,
  183. drift_recenter_distance_threshold = 0.75f,
  184. group_id = 1,
  185. max_distance_thresh= 1.1f,
  186. min_distance_thresh = 0.5f,
  187. }
  188. };
  189. Debug.Log(JsonUtility.ToJson(data, true));
  190. }
  191. }
  192. }