TrackpointMesh.cs 4.6 KB

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