PlayerReplay.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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<Vector3[]> jointsSequence = new List<Vector3[]>();
  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. // TODO: still not working, all elements are replaced with new added array
  41. Vector3[] positions = new Vector3[25];
  42. for(int i = 0; i < joints.Length; i++)
  43. {
  44. positions[i] = joints[i].position;
  45. }
  46. jointsSequence.Add(positions);
  47. }
  48. public void Save()
  49. {
  50. if(jointsSequence.Count == 0)
  51. {
  52. Debug.Log("jointsSequence is empty");
  53. return;
  54. }
  55. SaveSystem.Save(jointsSequence);
  56. Debug.Log("Save success");
  57. }
  58. public void Load()
  59. {
  60. JointsDataSequence data = SaveSystem.Load();
  61. if (data == null)
  62. {
  63. Debug.Log("Load failed");
  64. return;
  65. }
  66. StartCoroutine(ShowJoints(data));
  67. Debug.Log("Load success");
  68. }
  69. private IEnumerator ShowJoints(JointsDataSequence data)
  70. {
  71. // Show joints every 2 seconds
  72. WaitForSeconds wait = new WaitForSeconds(2);
  73. List<JointsData> jointsData = data.jointsDataSequence;
  74. int counter = 0;
  75. foreach(JointsData jd in jointsData)
  76. {
  77. // Create GameObject body
  78. GameObject body = new GameObject("Recorded Body " + counter);
  79. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  80. {
  81. // skip if head joint
  82. if (jt == Kinect.JointType.Head)
  83. continue;
  84. // Create GameObject cubes for joints
  85. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  86. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  87. lr.positionCount = 2;
  88. lr.material = boneMaterial;
  89. lr.startWidth = 0.3f;
  90. lr.endWidth = 0.3f;
  91. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  92. jointObj.name = jt.ToString();
  93. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  94. jointObj.transform.parent = body.transform;
  95. }
  96. // Connect the joints with LineRenderer
  97. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  98. {
  99. // skip if dictionary not contains the joint or neck joint
  100. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck)
  101. continue;
  102. Transform jointObj = body.transform.Find(jt.ToString());
  103. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  104. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  105. lr.SetPosition(0, jointObj.localPosition);
  106. lr.SetPosition(1, targetJoint.localPosition);
  107. }
  108. counter++;
  109. yield return wait;
  110. }
  111. }
  112. }