AbstractTrackingSystem.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Newtonsoft.Json;
  5. using UnityEngine;
  6. namespace TrackpointApp
  7. {
  8. public class AbstractTrackingSystem : MonoBehaviour
  9. {
  10. public string filePath;
  11. private const int divisor = Configuration.divisor;
  12. private GameObject rotationObject;
  13. private GameObject meshObject;
  14. private List<ActionPoint> actionPoints = new List<ActionPoint>();
  15. protected TrackpointMesh trackpointMesh;
  16. protected void generalSetup()
  17. {
  18. rotationObject = new GameObject();
  19. rotationObject.name = "TrackingRotation";
  20. meshObject = new GameObject();
  21. meshObject.name = "TrackingMesh";
  22. rotationObject.transform.parent = this.transform;
  23. trackpointMesh = meshObject.AddComponent<TrackpointMesh>();
  24. trackpointMesh.transform.parent = rotationObject.transform;
  25. }
  26. protected void setupTrackpointTranslation(TrackpointData trackpoint)
  27. {
  28. float[] point = trackpoint.point;
  29. trackpointMesh.transform.localPosition = new Vector3(-(point[0] / divisor), -(point[1] / divisor), -(point[2] / divisor));
  30. }
  31. protected void setupTrackpointRotation(TrackpointData trackpoint, int rotationCorrection)
  32. {
  33. float[] normal = trackpoint.normal;
  34. Vector3 unityNormal = new Vector3(normal[0], normal[2], normal[1]);
  35. Quaternion rotateObjectToTrackpoint = Quaternion.FromToRotation(Vector3.up, unityNormal);
  36. Quaternion correction = Quaternion.AngleAxis(rotationCorrection, Vector3.back);
  37. Quaternion result = correction * rotateObjectToTrackpoint;
  38. rotationObject.transform.rotation = result;
  39. }
  40. protected void setupActionPoints()
  41. {
  42. string metadata = trackpointMesh.getActionPointMetaData();
  43. if (metadata != "")
  44. {
  45. Dictionary<String, TrackpointData> metaObject =
  46. JsonConvert.DeserializeObject<Dictionary<String, TrackpointData>>(metadata);
  47. if (metaObject.Count() <= 0) return;
  48. foreach (KeyValuePair<String, TrackpointData> actionPoint in metaObject)
  49. {
  50. GameObject anchor = new GameObject();
  51. anchor.name = "ActionPoint " + actionPoint.Key;
  52. anchor.transform.parent = trackpointMesh.transform;
  53. float[] point = actionPoint.Value.point;
  54. float[] normal = actionPoint.Value.normal;
  55. anchor.transform.localPosition =
  56. new Vector3(point[0] / divisor, point[1] / divisor, point[2] / divisor);
  57. Vector3 unityNormal = new Vector3(normal[0], normal[1], normal[2]);
  58. anchor.transform.localRotation = Quaternion.FromToRotation(Vector3.up, unityNormal);
  59. ActionPoint actionPointObject = anchor.AddComponent<ActionPoint>();
  60. actionPointObject.setup();
  61. actionPoints.Add(actionPointObject);
  62. }
  63. }
  64. }
  65. }
  66. }