PlayerReplay.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 these joints
  133. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  134. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  135. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  136. continue;
  137. // Create GameObject cubes for joints
  138. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  139. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  140. lr.positionCount = 2;
  141. lr.material = boneMaterial;
  142. lr.startWidth = 0.05f;
  143. lr.endWidth = 0.05f;
  144. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  145. jointObj.name = jt.ToString();
  146. JointsData jd = newJointsData[i];
  147. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  148. jointObj.transform.parent = body.transform;
  149. }
  150. // Connect the joints with LineRenderer
  151. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  152. {
  153. // Skip if dictionary not contains the joint or other these joints
  154. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  155. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  156. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  157. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  158. continue;
  159. Transform jointObj = body.transform.Find(jt.ToString());
  160. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  161. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  162. lr.SetPosition(0, jointObj.localPosition);
  163. lr.SetPosition(1, targetJoint.localPosition);
  164. }
  165. yield return wait;
  166. Destroy(body);
  167. }
  168. }
  169. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  170. {
  171. for (int i = 0; i < newJointsData.Count; i++)
  172. {
  173. float waitTime = 1;
  174. if (i < newRecordingTimes.Count - 1)
  175. {
  176. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  177. }
  178. WaitForSeconds wait = new WaitForSeconds(waitTime);
  179. Visualizer_FadeIn body = Instantiate(vfi) as Visualizer_FadeIn;
  180. body.SetData(newJointsData[i]);
  181. body.ShowBody();
  182. yield return wait;
  183. }
  184. }
  185. private IEnumerator VisualizeFadeInSeries(List<JointsData> newJointsData, List<float> newRecordingTimes)
  186. {
  187. float waitTime = 3;
  188. List<JointsData> jointsDataSeries = new List<JointsData>();
  189. List<float> recordingTimesSeries = new List<float>();
  190. for (int i = 0; i < newJointsData.Count; i++)
  191. {
  192. if (newRecordingTimes[i] < waitTime)
  193. {
  194. jointsDataSeries.Add(newJointsData[i]);
  195. recordingTimesSeries.Add(newRecordingTimes[i]);
  196. } else
  197. {
  198. waitTime += 3;
  199. // TODO: Show visualization first (need to test)
  200. StartCoroutine(Visualize(jointsDataSeries, recordingTimesSeries));
  201. // Show visualization steps
  202. Visualizer_FadeInSeries body = Instantiate(vfis) as Visualizer_FadeInSeries;
  203. body.transform.parent = gameObject.transform;
  204. body.SetData(jointsDataSeries, recordingTimesSeries);
  205. body.ShowBody();
  206. jointsDataSeries.Clear();
  207. recordingTimesSeries.Clear();
  208. WaitForSeconds wait = new WaitForSeconds(3);
  209. yield return wait;
  210. }
  211. }
  212. Visualizer_FadeInSeries bodyEnd = Instantiate(vfis) as Visualizer_FadeInSeries;
  213. bodyEnd.SetData(jointsDataSeries, recordingTimesSeries);
  214. bodyEnd.ShowBody();
  215. jointsDataSeries.Clear();
  216. recordingTimesSeries.Clear();
  217. }
  218. }