SteamVRTrack.cs 3.7 KB

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