PlayerReplay.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kinect = Windows.Kinect;
  5. public class PlayerReplay : MonoBehaviour
  6. {
  7. // Joints already given the values from BodySourceView.cs that keeps updating
  8. public Transform[] joints = new Transform[25];
  9. public List<Transform[]> jointsSequence = new List<Transform[]>();
  10. public Material boneMaterial;
  11. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  12. {
  13. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  14. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  15. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  16. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  17. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  18. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  19. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  20. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  21. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  22. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  23. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  24. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  25. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  26. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  27. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  28. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  29. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  30. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  31. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  32. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  33. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  34. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  35. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  36. { Kinect.JointType.Neck, Kinect.JointType.Head },
  37. };
  38. public void AddJoints()
  39. {
  40. jointsSequence.Add(joints);
  41. }
  42. public void Save()
  43. {
  44. if(jointsSequence.Count == 0)
  45. {
  46. Debug.Log("jointsSequence is empty");
  47. return;
  48. }
  49. SaveSystem.Save(jointsSequence);
  50. Debug.Log("Save success");
  51. }
  52. public void Load()
  53. {
  54. JointsDataSequence data = SaveSystem.Load();
  55. if (data == null)
  56. {
  57. Debug.Log("Load failed");
  58. return;
  59. }
  60. StartCoroutine(ShowJoints(data));
  61. Debug.Log("Load success");
  62. }
  63. private IEnumerator ShowJoints(JointsDataSequence data)
  64. {
  65. // Show joints every 2 seconds
  66. WaitForSeconds wait = new WaitForSeconds(2);
  67. List<JointsData> jointsData = data.jointsDataSequence;
  68. foreach(JointsData jd in jointsData)
  69. {
  70. // Create GameObject body
  71. GameObject body = new GameObject("Recorded Body");
  72. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  73. {
  74. // skip if head joint
  75. if (jt == Kinect.JointType.Head)
  76. continue;
  77. // Create GameObject cubes for joints
  78. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  79. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  80. lr.positionCount = 2;
  81. lr.material = boneMaterial;
  82. lr.startWidth = 0.3f;
  83. lr.endWidth = 0.3f;
  84. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  85. jointObj.name = jt.ToString();
  86. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  87. jointObj.transform.parent = body.transform;
  88. }
  89. // Connect the joints with LineRenderer
  90. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  91. {
  92. // skip if dictionary not contains the joint or neck joint
  93. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck)
  94. continue;
  95. Transform jointObj = body.transform.Find(jt.ToString());
  96. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  97. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  98. lr.SetPosition(0, jointObj.localPosition);
  99. lr.SetPosition(1, targetJoint.localPosition);
  100. }
  101. yield return wait;
  102. }
  103. }
  104. }