TrackpointMesh.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Lib3MF;
  5. using TrackpointApp;
  6. public enum TrackingSystem {
  7. OptiTrack = 0,
  8. EMFTrack = 1,
  9. SteamVRTrack = 2
  10. };
  11. public class TrackpointMesh : MonoBehaviour
  12. {
  13. private const string META_NAMESPACE = "tk-ar-tracking";
  14. private const int divisor = Configuration.divisor;
  15. private CModel model;
  16. private CMeshObject fileMesh;
  17. public void setup(string filePath, TrackingSystem trackingSystem)
  18. {
  19. MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
  20. meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
  21. MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
  22. Mesh mesh = new Mesh();
  23. // Load trackpoint file
  24. model = Wrapper.CreateModel();
  25. CReader reader = model.QueryReader("3mf");
  26. reader.ReadFromFile(filePath);
  27. CMeshObjectIterator iterator = model.GetMeshObjects();
  28. fileMesh = getMesh(iterator, trackingSystem);
  29. if (fileMesh == null)
  30. {
  31. Debug.Log("TrackpointApp Error: No valid mesh found.");
  32. return;
  33. }
  34. sPosition[] fileVertices;
  35. sTriangle[] fileTriangles;
  36. fileMesh.GetVertices(out fileVertices);
  37. fileMesh.GetTriangleIndices(out fileTriangles);
  38. uint vertexCount = fileMesh.GetTriangleCount() * 3;
  39. Vector3[] vertices = new Vector3[vertexCount];
  40. int[] tris = new int[vertexCount];
  41. Vector3[] normals = new Vector3[vertexCount];
  42. for (int i = 0; i < fileMesh.GetTriangleCount(); i++)
  43. {
  44. sTriangle current = fileTriangles[i];
  45. sPosition one = fileVertices[current.Indices[0]];
  46. vertices[i * 3] = new Vector3(one.Coordinates[0] / divisor, one.Coordinates[1] / divisor, one.Coordinates[2] / divisor);
  47. sPosition two = fileVertices[current.Indices[1]];
  48. vertices[i * 3 + 1] = new Vector3(two.Coordinates[0] / divisor, two.Coordinates[1] / divisor, two.Coordinates[2] / divisor);
  49. sPosition three = fileVertices[current.Indices[2]];
  50. vertices[i * 3 + 2] = new Vector3(three.Coordinates[0] / divisor, three.Coordinates[1] / divisor, three.Coordinates[2] / divisor);
  51. tris[i * 3] = i * 3;
  52. tris[i * 3 + 1] = i * 3 + 1;
  53. tris[i * 3 + 2] = i * 3 + 2;
  54. Vector3 lineOne = vertices[i * 3 + 1] - vertices[i * 3];
  55. Vector3 lineTwo = vertices[i * 3 + 2] - vertices[i * 3];
  56. Vector3 normal = Vector3.Cross(lineOne, lineTwo).normalized;
  57. normals[i * 3] = normal;
  58. normals[i * 3 + 1] = normal;
  59. normals[i * 3 + 2] = normal;
  60. }
  61. mesh.vertices = vertices;
  62. mesh.triangles = tris;
  63. mesh.normals = normals;
  64. meshFilter.mesh = mesh;
  65. }
  66. public string getMetaData(TrackingSystem trackingSystem)
  67. {
  68. string key;
  69. switch (trackingSystem)
  70. {
  71. case TrackingSystem.OptiTrack:
  72. key = "trackpoints-optitrack";
  73. break;
  74. case TrackingSystem.EMFTrack:
  75. key = "trackpoints-emftrack";
  76. break;
  77. case TrackingSystem.SteamVRTrack:
  78. key = "trackpoints-steamvrtrack";
  79. break;
  80. default:
  81. key = "";
  82. Debug.Log("No tracking system specified. This error is fatal.");
  83. break;
  84. }
  85. CMetaDataGroup metaDataGroup = fileMesh.GetMetaDataGroup();
  86. CMetaData metaData = metaDataGroup.GetMetaDataByKey(META_NAMESPACE, key);
  87. return metaData.GetValue();
  88. }
  89. public string getActionPointMetaData()
  90. {
  91. CMetaDataGroup metaDataGroup = model.GetMetaDataGroup();
  92. CMetaData metaData = metaDataGroup.GetMetaDataByKey(META_NAMESPACE, "trackpoints-actionpoints");
  93. if (metaData.GetValue() == "null")
  94. {
  95. return "";
  96. }
  97. else
  98. {
  99. return metaData.GetValue();
  100. }
  101. }
  102. CMeshObject getMesh(CMeshObjectIterator iterator, TrackingSystem trackingSystem)
  103. {
  104. string comparator;
  105. switch (trackingSystem)
  106. {
  107. case TrackingSystem.OptiTrack:
  108. comparator = "optitrack";
  109. break;
  110. case TrackingSystem.EMFTrack:
  111. comparator = "emftrack";
  112. break;
  113. case TrackingSystem.SteamVRTrack:
  114. comparator = "steamvrtrack";
  115. break;
  116. default:
  117. comparator = "";
  118. Debug.Log("No tracking system specified. This error is fatal.");
  119. break;
  120. }
  121. ulong count = iterator.Count();
  122. CMeshObject fileMesh;
  123. for (ulong i = 0; i < count; i++)
  124. {
  125. iterator.MoveNext();
  126. fileMesh = iterator.GetCurrentMeshObject();
  127. if (fileMesh.GetName() == comparator)
  128. {
  129. return fileMesh;
  130. }
  131. }
  132. return null;
  133. }
  134. }