SteamVRTrack.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Valve.VR;
  6. using JsonConvert = Valve.Newtonsoft.Json.JsonConvert;
  7. using TrackpointApp;
  8. public class SteamVRTrack : MonoBehaviour
  9. {
  10. public string filePath;
  11. public int rotationCorrection;
  12. private const int divisor = Configuration.divisor;
  13. private GameObject rotationObject;
  14. private GameObject meshObject;
  15. private List<ActionPoint> actionPoints = new List<ActionPoint>();
  16. private TrackpointMesh trackpointMesh;
  17. private SteamVR_TrackedObject tracking;
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. rotationObject = new GameObject();
  22. rotationObject.name = "TrackingRotation";
  23. meshObject = new GameObject();
  24. meshObject.name = "TrackingMesh";
  25. rotationObject.transform.parent = this.transform;
  26. trackpointMesh = meshObject.AddComponent<TrackpointMesh>();
  27. trackpointMesh.transform.parent = rotationObject.transform;
  28. trackpointMesh.setup(filePath, TrackingSystem.SteamVRTrack);
  29. setupTrackpointTranslationAndRotation();
  30. setupActionPoints();
  31. tracking = gameObject.AddComponent<SteamVR_TrackedObject>();
  32. tracking.index = (SteamVR_TrackedObject.EIndex)1;
  33. }
  34. void setupTrackpointTranslationAndRotation()
  35. {
  36. string metadata = trackpointMesh.getMetaData(TrackingSystem.SteamVRTrack);
  37. List<trackpoints> metaObject = JsonConvert.DeserializeObject<List<trackpoints>>(metadata);
  38. if (metaObject.Count != 1)
  39. {
  40. Debug.Log("TrackpointApp Error: Only one tracker per element supported for SteamVR.");
  41. return;
  42. }
  43. float[] point = metaObject[0].point;
  44. float[] normal = metaObject[0].normal;
  45. trackpointMesh.transform.localPosition = new Vector3(-(point[0] / divisor), -(point[1] / divisor), -(point[2] / divisor));
  46. Vector3 unityNormal = new Vector3(normal[0], normal[2], normal[1]);
  47. Quaternion rotateObjectToTrackpoint = Quaternion.FromToRotation(Vector3.up, unityNormal);
  48. Quaternion correction = Quaternion.AngleAxis(rotationCorrection, Vector3.back);
  49. Quaternion result = correction * rotateObjectToTrackpoint;
  50. rotationObject.transform.rotation = result;
  51. }
  52. void setupActionPoints()
  53. {
  54. string metadata = trackpointMesh.getActionPointMetaData();
  55. Dictionary<String, trackpoints> metaObject = JsonConvert.DeserializeObject<Dictionary<String, trackpoints>>(metadata);
  56. foreach (KeyValuePair<String, trackpoints> actionPoint in metaObject)
  57. {
  58. GameObject anchor = new GameObject();
  59. anchor.name = "ActionPoint " + actionPoint.Key;
  60. anchor.transform.parent = trackpointMesh.transform;
  61. float[] point = actionPoint.Value.point;
  62. float[] normal = actionPoint.Value.normal;
  63. anchor.transform.localPosition = new Vector3(point[0] / divisor, point[1] / divisor, point[2] / divisor);
  64. Vector3 unityNormal = new Vector3(normal[0], normal[2], normal[1]);
  65. anchor.transform.rotation = Quaternion.FromToRotation(Vector3.up, unityNormal);
  66. ActionPoint actionPointObject = anchor.AddComponent<ActionPoint>();
  67. actionPointObject.setup();
  68. actionPoints.Add(actionPointObject);
  69. }
  70. }
  71. }
  72. public class trackpoints
  73. {
  74. public float[] point
  75. {
  76. get;
  77. set;
  78. }
  79. public float[] normal
  80. {
  81. get;
  82. set;
  83. }
  84. }