PlayerReplay.cs 9.5 KB

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