TrackableApi.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // [DllImport("svrplugin")]
  12. // public static extern int ScGetPanelInfo(float[] info);
  13. // [DllImport("svrplugin")]
  14. // public static extern int ScGetPanel();
  15. //#else
  16. // public static int ScGetPanelInfo(float[] info)
  17. // {
  18. // info[0] = 1f;
  19. // info[1] = 3f;
  20. // info[2] = 1f;
  21. // info[3] = 0f;
  22. // info[4] = 0f;
  23. // info[5] = 0f;
  24. // info[6] = 0f;
  25. // info[7] = -1f;
  26. // info[8] = -1f;
  27. // info[9] = 0f;
  28. // info[10] = 0f;
  29. // return 0;
  30. // }
  31. // public static int ScGetPanel()
  32. // {
  33. // return 1;
  34. // }
  35. //#endif
  36. /// <summary>
  37. /// index:
  38. /// 0: panelId
  39. /// 1: verticeCount
  40. /// 2~67 x y z
  41. /// </summary>
  42. /// <param name="trackables"></param>
  43. public static void GetPlaneInfo<T>(List<T> trackables) where T : Trackable
  44. {
  45. if (trackables == null)
  46. {
  47. Debug.LogError("please init trackables first!!!");
  48. return;
  49. }
  50. trackables.Clear();
  51. int planeCount = API_GSXR_Slam.GSXR_Get_PanelNum();
  52. float[] rawData = new float[planeCount * PER_PLANE_DATA_COUNT];
  53. API_GSXR_Slam.GSXR_Get_PanelInfo(rawData);
  54. for (int i = 0; i < planeCount; i++)//plane loop
  55. {
  56. int planeId = (int)rawData[i * PER_PLANE_DATA_COUNT];
  57. int planeVerticesCount = (int)rawData[i * PER_PLANE_DATA_COUNT + 1];
  58. Vector3[] vertices = new Vector3[planeVerticesCount];
  59. for (int j = 0; j < vertices.Length; j++) // plane vertices loop
  60. {
  61. float x = rawData[(i * PER_PLANE_DATA_COUNT + 2) + (vertices.Length - j - 1) * 3];
  62. float y = rawData[(i * PER_PLANE_DATA_COUNT + 2) + (vertices.Length - j - 1) * 3 + 1];
  63. float z = -rawData[(i * PER_PLANE_DATA_COUNT + 2) +(vertices.Length - j - 1) * 3 + 2];
  64. vertices[j] = new Vector3(x, y, z);
  65. }
  66. PlaneTrackable trackable = CreateTrackable(planeId, vertices);//new PlaneTrackable(planeId, vertices);
  67. trackables.SafeAdd(trackable);
  68. }
  69. }
  70. private static PlaneTrackable CreateTrackable(int planeId, Vector3[] vertices)
  71. {
  72. if (trackableDic.ContainsKey(planeId))
  73. {
  74. PlaneTrackable planeTrackableCache = trackableDic[planeId];
  75. planeTrackableCache.UpdateVertices(vertices);
  76. return planeTrackableCache;
  77. }
  78. PlaneTrackable newTrackablePlane = new PlaneTrackable(planeId, vertices);
  79. trackableDic.Add(planeId, newTrackablePlane);
  80. return newTrackablePlane;
  81. }
  82. }
  83. //}