123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- namespace Ximmerse.XR.Tag
- {
- [System.Serializable]
- public struct GroundPlanePlacementData
- {
- public GroundPlanePlacementItem[] items;
- }
- [System.Serializable]
- public class GroundPlanePlacementItem
- {
- public int beacon_id;
- public int group_id;
- public float size;
- public Vector3 position;
- public Vector3 rotation;
- public int coord_system_flag; // 0 = left hand (unity), 1 = right hand (openXR)
- public float confidence_thresh = 0.9f;
- public float min_distance_thresh = 0.1f;
- public float max_distance_thresh = 1.8f;
- public float drift_recenter_angle_threshold = 1.0f;
- public float drift_recenter_distance_threshold = 1.0f;
- }
- /// <summary>
- /// 读取指定路径的json配置文件,自动创建 Ground Plane。
- /// 配置文件举例:
- /*
- {
- "items": [
- {
- "beacon_id": 67,
- "group_id": 0,
- "size": 0.17,
- "position": {
- "x": 0.0,
- "y": 0.0,
- "z": 0.0
- },
- "rotation": {
- "x": 0.0,
- "y": 0.0,
- "z": 0.0
- },
- "coord_system_flag": 1,
- "confidence_thresh": 0.9,
- "max_distance_thresh": 1.8,
- "min_distance_thresh": 0.1,
- "drift_recenter_angle_threshold": 1,
- "drift_recenter_distance_threshold": 1
- },
- {
- "beacon_id": 66,
- "group_id": 1,
- "size": 0.17,
- "position": {
- "x": 1.0,
- "y": 1.0,
- "z": 1.0
- },
- "rotation": {
- "x": 0.0,
- "y": 90.0,
- "z": 0.0
- },
- "coord_system_flag": 1,
- "confidence_thresh": 0.9,
- "max_distance_thresh": 1.8,
- "min_distance_thresh": 0.1,
- "drift_recenter_angle_threshold": 1,
- "drift_recenter_distance_threshold": 1
- },
- {
- "beacon_id": 65,
- "group_id": 1,
- "size": 0.17,
- "position": {
- "x": 0.0,
- "y": 0.0,
- "z": 1.0
- },
- "rotation": {
- "x": 0.0,
- "y": 0.0,
- "z": 0.0
- },
- "coord_system_flag": 1,
- "confidence_thresh": 0.9,
- "max_distance_thresh": 1.8,
- "min_distance_thresh": 0.1,
- "drift_recenter_angle_threshold": 1,
- "drift_recenter_distance_threshold": 1
- }
- ]
- }
- */
- /// </summary>
- public class CreatesGroundPlaneByJson : MonoBehaviour
- {
- private static CreatesGroundPlaneByJson instance;
- private CreatesGroundPlaneByJson() { }
- public static CreatesGroundPlaneByJson Instance
- {
- get
- {
- return instance;
- }
- }
- private void Awake()
- {
- instance = this;
- }
- public string JsonFilePath = "/sdcard/GroundPlaneConfig.txt";
- public bool autoCreates = true;
- public bool debugView = false;
- public float size = 0.17f;
- private void Start()
- {
- if (autoCreates)
- {
- TagProfileLoading.Instance.GroundPlaneList.Clear();
- CreateGroundPlanesFromConfig();
- }
- }
- [ContextMenu("Create ground plane from json config")]
- public void CreateGroundPlanesFromConfig()
- {
- try
- {
- if (!File.Exists(JsonFilePath))
- {
- return;
- }
- var txt = File.ReadAllText(JsonFilePath);
- //var txt = Resources.Load<TextAsset>("groundplane-layout").ToString();
- GroundPlanePlacementData placementData = JsonUtility.FromJson<GroundPlanePlacementData>(txt);
- for (int i = 0; i < placementData.items.Length; i++)
- {
- GroundPlanePlacementItem groundPlaneItem = placementData.items[i];
- GameObject go = GameObject.Instantiate(Resources.Load("Tag/Prefabs/GroundPlane")) as GameObject;
- go.name = "GroundPlane - " + groundPlaneItem.beacon_id;
- var gp = go.GetComponent<TagGroundPlane>();
- if (debugView)
- {
- gp.DebugView = true;
- gp.Size = size;
- }
- gp.TrackId = groundPlaneItem.beacon_id;
- //gp.text.text = gp.track_id.ToString();
- gp.BeaconDriftRecenterAngleThreshold = groundPlaneItem.drift_recenter_angle_threshold;
- gp.BeaconDriftRecenterDistanceThreshold = groundPlaneItem.drift_recenter_distance_threshold;
- gp.BeaconConfidenceThresh = groundPlaneItem.confidence_thresh;
- gp.BeaconMaxDistanceThresh = groundPlaneItem.max_distance_thresh;
- gp.BeaconMinDistanceThresh = groundPlaneItem.min_distance_thresh;
- gp.BeaconCoordSystemFlag = groundPlaneItem.coord_system_flag;
- go.transform.position = groundPlaneItem.position;
- go.transform.eulerAngles = groundPlaneItem.rotation;
-
- TagProfileLoading.Instance.GroundPlaneList.Add(go);
- }
- }
- catch (System.Exception ex)
- {
- Debug.LogException(ex);
- }
- TagProfileLoading.Instance.SettingData();
- }
- [ContextMenu("Test convert to json string")]
- public void TestToJson()
- {
- GroundPlanePlacementData data = new GroundPlanePlacementData();
- data.items = new GroundPlanePlacementItem[]
- {
- new GroundPlanePlacementItem()
- {
- beacon_id = 1, position = Vector3.zero, rotation = Vector3.zero,
- },
- new GroundPlanePlacementItem()
- {
- beacon_id = 2, position = Vector3.one, rotation = new Vector3(0,90,0),
- confidence_thresh = 0.85f, coord_system_flag = 0, drift_recenter_angle_threshold = 7.5f,
- drift_recenter_distance_threshold = 0.75f,
- group_id = 1,
- max_distance_thresh= 1.1f,
- min_distance_thresh = 0.5f,
- }
- };
- Debug.Log(JsonUtility.ToJson(data, true));
- }
- }
- }
|