PlayerReplay.cs 5.0 KB

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