TrackableApi.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections.Generic;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. //namespace SC.XR.Unity
  5. //{
  6. public class TrackableApi
  7. {
  8. private const int PER_PLANE_DATA_COUNT = 68;
  9. private static Dictionary<int, PlaneTrackable> trackableDic = new Dictionary<int, PlaneTrackable>();
  10. #if !UNITY_EDITOR
  11. #else
  12. public static int ScGetPanelInfo(float[] info)
  13. {
  14. info[0] = TrackablePlaneGenerator.twoPlane ? 2f : 1f;
  15. info[1] = 11f;
  16. info[2] = 2.893829f;
  17. info[3] = -0.238066f;
  18. info[4] = 0.532357f;
  19. info[5] = 2.645216f;
  20. info[6] = -0.238066f;
  21. info[7] = -0.600777f;
  22. info[8] = 1.980295f;
  23. info[9] = -0.238066f;
  24. info[10] = -1.802898f;
  25. info[11] = -0.126405f;
  26. info[12] = -0.238066f;
  27. info[13] = -1.982685f;
  28. info[14] = -0.821304f;
  29. info[15] = -0.238066f;
  30. info[16] = -1.763392f;
  31. info[17] = -1.553233f;
  32. info[18] = -0.238066f;
  33. info[19] = -0.675771f;
  34. info[20] = -1.991623f;
  35. info[21] = -0.238066f;
  36. info[22] = 4.984020f;
  37. info[23] = -1.931745f;
  38. info[24] = -0.238066f;
  39. info[25] = 5.263874f;
  40. info[26] = -1.695218f;
  41. info[27] = -0.238066f;
  42. info[28] = 5.598719f;
  43. info[29] = 1.800027f;
  44. info[30] = -0.238066f;
  45. info[31] = 5.302855f;
  46. info[32] = 2.451164f;
  47. info[33] = -0.238066f;
  48. info[34] = 2.975676f;
  49. if (TrackablePlaneGenerator.twoPlane)
  50. {
  51. info[69] = 3f;
  52. info[70] = 2.893829f;
  53. info[71] = 0;
  54. info[72] = 0.532357f;
  55. info[73] = 2.645216f;
  56. info[74] = 0;
  57. info[75] = -0.600777f;
  58. info[76] = 1.980295f;
  59. info[77] = 0;
  60. info[78] = -1.802898f;
  61. }
  62. return 0;
  63. }
  64. public static int ScGetPanel()
  65. {
  66. return TrackablePlaneGenerator.twoPlane ? 2 : 1;
  67. }
  68. #endif
  69. /// <summary>
  70. /// index:
  71. /// 0: panelId
  72. /// 1: verticeCount
  73. /// 2~67 x y z
  74. /// </summary>
  75. /// <param name="trackables"></param>
  76. public static void GetPlaneInfo<T>(List<T> trackables) where T : Trackable
  77. {
  78. if (trackables == null)
  79. {
  80. Debug.LogError("please init trackables first!!!");
  81. return;
  82. }
  83. trackables.Clear();
  84. int planeCount =
  85. #if UNITY_EDITOR
  86. ScGetPanel();
  87. #else
  88. API_GSXR_Slam.GSXR_Get_PanelNum();
  89. #endif
  90. float[] rawData = new float[planeCount * PER_PLANE_DATA_COUNT];
  91. #if UNITY_EDITOR
  92. ScGetPanelInfo(rawData);
  93. #else
  94. API_GSXR_Slam.GSXR_Get_PanelInfo(rawData);
  95. #endif
  96. for (int i = 0; i < planeCount; i++)//plane loop
  97. {
  98. int planeId = (int)rawData[i * PER_PLANE_DATA_COUNT];
  99. int planeVerticesCount = (int)rawData[i * PER_PLANE_DATA_COUNT + 1];
  100. Vector3[] vertices = new Vector3[planeVerticesCount];
  101. for (int j = 0; j < vertices.Length; j++) // plane vertices loop
  102. {
  103. float x = rawData[(i * PER_PLANE_DATA_COUNT + 2) + (vertices.Length - j - 1) * 3];
  104. float y = rawData[(i * PER_PLANE_DATA_COUNT + 2) + (vertices.Length - j - 1) * 3 + 1];
  105. float z = -rawData[(i * PER_PLANE_DATA_COUNT + 2) + (vertices.Length - j - 1) * 3 + 2];//
  106. vertices[j] = new Vector3(x, y, z);
  107. }
  108. PlaneTrackable trackable = CreateTrackable(planeId, vertices);//new PlaneTrackable(planeId, vertices);
  109. trackables.SafeAdd(trackable);
  110. }
  111. }
  112. private static PlaneTrackable CreateTrackable(int planeId, Vector3[] vertices)
  113. {
  114. if (trackableDic.ContainsKey(planeId))
  115. {
  116. PlaneTrackable planeTrackableCache = trackableDic[planeId];
  117. planeTrackableCache.UpdateVertices(vertices);
  118. return planeTrackableCache;
  119. }
  120. PlaneTrackable newTrackablePlane = new PlaneTrackable(planeId, vertices);
  121. trackableDic.Add(planeId, newTrackablePlane);
  122. return newTrackablePlane;
  123. }
  124. }
  125. //}