PlayerReplay.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Kinect = Windows.Kinect;
  6. public class PlayerReplay : MonoBehaviour
  7. {
  8. // Joints already given the values from BodySourceView.cs that keeps updating
  9. public Transform[] joints = new Transform[25];
  10. public List<Vector3[]> jointsSequence = new List<Vector3[]>();
  11. public List<float> recordingTimes = new List<float>();
  12. public Material boneMaterial;
  13. public Material transparentMat;
  14. public Visualizer_FadeIn vfi;
  15. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  16. {
  17. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  18. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  19. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  20. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  21. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  22. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  23. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  24. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  25. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  26. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  27. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  28. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  29. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  30. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  31. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  32. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  33. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  34. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  35. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  36. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  37. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  38. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  39. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  40. { Kinect.JointType.Neck, Kinect.JointType.Head },
  41. };
  42. public void AddJoints(float recordingTime)
  43. {
  44. Vector3[] positions = new Vector3[25];
  45. for (int i = 0; i < joints.Length; i++)
  46. {
  47. positions[i] = joints[i].position;
  48. }
  49. jointsSequence.Add(positions);
  50. recordingTimes.Add(recordingTime);
  51. }
  52. public void ResetRecording()
  53. {
  54. jointsSequence.Clear();
  55. recordingTimes.Clear();
  56. }
  57. public void Save()
  58. {
  59. if (jointsSequence.Count == 0)
  60. {
  61. Debug.Log("jointsSequence is empty");
  62. return;
  63. }
  64. SaveSystem.Save(jointsSequence, recordingTimes);
  65. Debug.Log("Save success");
  66. }
  67. public void Load()
  68. {
  69. JointsDataSequence data = SaveSystem.Load();
  70. if (data == null)
  71. {
  72. Debug.Log("Load failed");
  73. return;
  74. }
  75. ShowJoints(data);
  76. Debug.Log("Load success");
  77. }
  78. private void ShowJoints(JointsDataSequence data)
  79. {
  80. List<JointsData> jointsData = data.jointsDataSequence;
  81. List<float> recordingTimes = data.recordingTimes;
  82. // Filtered datas according to distance
  83. List<JointsData> newJointsData = new List<JointsData>();
  84. List<float> newRecordingTimes = new List<float>();
  85. JointsData prevJd = jointsData[0];
  86. // Add first pose
  87. newJointsData.Add(jointsData[0]);
  88. newRecordingTimes.Add(recordingTimes[0]);
  89. for (int i = 1; i < jointsData.Count; i++)
  90. {
  91. JointsData jd = jointsData[i];
  92. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  93. {
  94. // Skip these joints
  95. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  96. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  97. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  98. continue;
  99. Vector3 prevJointPosition = new Vector3(prevJd.jointsPositionsX[(int)jt], prevJd.jointsPositionsY[(int)jt], prevJd.jointsPositionsZ[(int)jt]);
  100. Vector3 jointPosition = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  101. float distance = Vector3.Distance(prevJointPosition, jointPosition);
  102. // If a joint is bigger than a certain distance, add it to the replay, else ignore it
  103. if (distance >= 2)
  104. {
  105. newJointsData.Add(jd);
  106. newRecordingTimes.Add(recordingTimes[i]);
  107. prevJd = jd;
  108. continue;
  109. }
  110. }
  111. }
  112. //StartCoroutine(Visualize(newJointsData, newRecordingTimes));
  113. StartCoroutine(VisualizeFadeIn(newJointsData, newRecordingTimes));
  114. }
  115. private IEnumerator Visualize(List<JointsData> newJointsData, List<float> newRecordingTimes)
  116. {
  117. for (int i = 0; i < newJointsData.Count; i++)
  118. {
  119. float waitTime = 1;
  120. if (i < newRecordingTimes.Count - 1)
  121. {
  122. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  123. }
  124. WaitForSeconds wait = new WaitForSeconds(waitTime);
  125. // Create GameObject body
  126. GameObject body = new GameObject("Recorded Body " + i);
  127. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  128. {
  129. // Skip if head joint
  130. if (jt == Kinect.JointType.Head)
  131. continue;
  132. // Create GameObject cubes for joints
  133. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  134. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  135. lr.positionCount = 2;
  136. lr.material = boneMaterial;
  137. lr.startWidth = 0.3f;
  138. lr.endWidth = 0.3f;
  139. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  140. jointObj.name = jt.ToString();
  141. JointsData jd = newJointsData[i];
  142. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  143. jointObj.transform.parent = body.transform;
  144. }
  145. // Connect the joints with LineRenderer
  146. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  147. {
  148. // Skip if dictionary not contains the joint or neck joint
  149. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck)
  150. continue;
  151. Transform jointObj = body.transform.Find(jt.ToString());
  152. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  153. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  154. lr.SetPosition(0, jointObj.localPosition);
  155. lr.SetPosition(1, targetJoint.localPosition);
  156. }
  157. yield return wait;
  158. Destroy(body);
  159. }
  160. }
  161. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  162. {
  163. for (int i = 0; i < newJointsData.Count; i++)
  164. {
  165. float waitTime = 1;
  166. if (i < newRecordingTimes.Count - 1)
  167. {
  168. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  169. }
  170. WaitForSeconds wait = new WaitForSeconds(waitTime);
  171. Visualizer_FadeIn body = Instantiate(vfi) as Visualizer_FadeIn;
  172. body.SetData(newJointsData[i]);
  173. body.ShowBody();
  174. yield return wait;
  175. }
  176. }
  177. }