PlayerReplay.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 transparentMat;
  13. public Visualizer_FadeIn vfi;
  14. public Visualizer_FadeInSeries vfis;
  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. // TODO: maybe distance need to be adjusted
  104. if (distance >= 0.2f)
  105. {
  106. newJointsData.Add(jd);
  107. newRecordingTimes.Add(recordingTimes[i]);
  108. prevJd = jd;
  109. continue;
  110. }
  111. }
  112. }
  113. StartCoroutine(VisualizeFadeInSeries(jointsData, recordingTimes, 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 Demo " + i);
  127. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  128. {
  129. // Skip these joints
  130. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  131. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  132. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  133. continue;
  134. // Create GameObject cubes for joints
  135. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  136. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  137. lr.positionCount = 2;
  138. lr.material = new Material(Shader.Find("Sprites/Default"));
  139. lr.material.color = new Color(1, 0.8f, 0.6f);
  140. lr.startWidth = 0.05f;
  141. lr.endWidth = 0.05f;
  142. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  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. // Remove LineRenderer component from neck
  148. if (jt == Kinect.JointType.Neck)
  149. {
  150. Destroy(jointObj.GetComponent<LineRenderer>());
  151. }
  152. }
  153. // Connect the joints with LineRenderer
  154. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  155. {
  156. // Skip if dictionary not contains the joint or other these joints
  157. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  158. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  159. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  160. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  161. continue;
  162. Transform jointObj = body.transform.Find(jt.ToString());
  163. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  164. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  165. lr.SetPosition(0, jointObj.localPosition);
  166. lr.SetPosition(1, targetJoint.localPosition);
  167. }
  168. yield return wait;
  169. Destroy(body);
  170. }
  171. }
  172. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  173. {
  174. for (int i = 0; i < newJointsData.Count; i++)
  175. {
  176. float waitTime = 1;
  177. if (i < newRecordingTimes.Count - 1)
  178. {
  179. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  180. }
  181. WaitForSeconds wait = new WaitForSeconds(waitTime);
  182. Visualizer_FadeIn body = Instantiate(vfi) as Visualizer_FadeIn;
  183. body.SetData(newJointsData[i]);
  184. body.ShowBody();
  185. yield return wait;
  186. }
  187. }
  188. private IEnumerator VisualizeFadeInSeries(List<JointsData> jointsData, List<float> recordingTimes, List<JointsData> newJointsData, List<float> newRecordingTimes)
  189. {
  190. float waitTime = 3;
  191. List<JointsData> jointsDataDemo = new List<JointsData>();
  192. List<float> recordingTimesDemo = new List<float>();
  193. List<JointsData> jointsDataSeries = new List<JointsData>();
  194. List<float> recordingTimesSeries = new List<float>();
  195. int indexDemo = 0;
  196. int indexSeries = 0;
  197. while (true)
  198. {
  199. // Visualization demo
  200. for (int i = indexDemo; i < jointsData.Count; i++)
  201. {
  202. if (recordingTimes[i] < waitTime)
  203. {
  204. jointsDataDemo.Add(jointsData[i]);
  205. recordingTimesDemo.Add(recordingTimes[i]);
  206. }
  207. else
  208. {
  209. indexDemo = i;
  210. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  211. jointsDataDemo.Clear();
  212. recordingTimesDemo.Clear();
  213. break;
  214. }
  215. }
  216. // Visualization steps/series
  217. for (int i = indexSeries; i < newJointsData.Count; i++)
  218. {
  219. if (newRecordingTimes[i] < waitTime)
  220. {
  221. jointsDataSeries.Add(newJointsData[i]);
  222. recordingTimesSeries.Add(newRecordingTimes[i]);
  223. }
  224. else
  225. {
  226. indexSeries = i;
  227. waitTime += 3;
  228. Visualizer_FadeInSeries body = Instantiate(vfis) as Visualizer_FadeInSeries;
  229. body.transform.parent = gameObject.transform;
  230. body.SetData(jointsDataSeries, recordingTimesSeries);
  231. body.ShowBody();
  232. jointsDataSeries.Clear();
  233. recordingTimesSeries.Clear();
  234. yield return new WaitForSeconds(4);
  235. break;
  236. }
  237. }
  238. if (jointsDataSeries.Count != 0)
  239. break;
  240. }
  241. // Last visualization
  242. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  243. jointsDataDemo.Clear();
  244. recordingTimesDemo.Clear();
  245. Visualizer_FadeInSeries bodyEnd = Instantiate(vfis) as Visualizer_FadeInSeries;
  246. bodyEnd.transform.parent = gameObject.transform;
  247. bodyEnd.SetData(jointsDataSeries, recordingTimesSeries);
  248. bodyEnd.ShowBody();
  249. jointsDataSeries.Clear();
  250. recordingTimesSeries.Clear();
  251. }
  252. }