PlayerReplay.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. public BodyComparer bc;
  16. public StartStepPreview startStepPreview;
  17. public GameObject textFinish;
  18. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  19. {
  20. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  21. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  22. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  23. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  24. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  25. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  26. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  27. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  28. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  29. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  30. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  31. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  32. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  33. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  34. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  35. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  36. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  37. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  38. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  39. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  40. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  41. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  42. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  43. { Kinect.JointType.Neck, Kinect.JointType.Head },
  44. };
  45. public void AddJoints(float recordingTime)
  46. {
  47. Vector3[] positions = new Vector3[25];
  48. for (int i = 0; i < joints.Length; i++)
  49. {
  50. positions[i] = joints[i].position;
  51. }
  52. jointsSequence.Add(positions);
  53. recordingTimes.Add(recordingTime);
  54. }
  55. public void ResetRecording()
  56. {
  57. jointsSequence.Clear();
  58. recordingTimes.Clear();
  59. }
  60. public void Save()
  61. {
  62. if (jointsSequence.Count == 0)
  63. {
  64. Debug.Log("jointsSequence is empty");
  65. return;
  66. }
  67. SaveSystem.Save(jointsSequence, recordingTimes);
  68. Debug.Log("Save success");
  69. }
  70. public void Load()
  71. {
  72. JointsDataSequence data = SaveSystem.Load();
  73. if (data == null)
  74. {
  75. Debug.Log("Load failed");
  76. return;
  77. }
  78. ShowJoints(data);
  79. Debug.Log("Load success");
  80. }
  81. private void ShowJoints(JointsDataSequence data)
  82. {
  83. List<JointsData> jointsData = data.jointsDataSequence;
  84. List<float> recordingTimes = data.recordingTimes;
  85. // Filtered datas according to distance
  86. List<JointsData> newJointsData = new List<JointsData>();
  87. List<float> newRecordingTimes = new List<float>();
  88. JointsData prevJd = jointsData[0];
  89. // Add first pose
  90. newJointsData.Add(jointsData[0]);
  91. newRecordingTimes.Add(recordingTimes[0]);
  92. for (int i = 1; i < jointsData.Count; i++)
  93. {
  94. JointsData jd = jointsData[i];
  95. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  96. {
  97. // Skip these joints
  98. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  99. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  100. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  101. continue;
  102. Vector3 prevJointPosition = new Vector3(prevJd.jointsPositionsX[(int)jt], prevJd.jointsPositionsY[(int)jt], prevJd.jointsPositionsZ[(int)jt]);
  103. Vector3 jointPosition = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  104. float distance = Vector3.Distance(prevJointPosition, jointPosition);
  105. // If a joint is bigger than a certain distance, add it to the replay, else ignore it
  106. if (distance >= 0.15f)
  107. {
  108. newJointsData.Add(jd);
  109. newRecordingTimes.Add(recordingTimes[i]);
  110. prevJd = jd;
  111. continue;
  112. }
  113. }
  114. }
  115. StartCoroutine(VisualizeFadeInSeries(jointsData, recordingTimes, newJointsData, newRecordingTimes));
  116. }
  117. private IEnumerator Visualize(List<JointsData> newJointsData, List<float> newRecordingTimes)
  118. {
  119. for (int i = 0; i < newJointsData.Count; i++)
  120. {
  121. float waitTime = 1;
  122. if (i < newRecordingTimes.Count - 1)
  123. {
  124. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  125. }
  126. WaitForSeconds wait = new WaitForSeconds(waitTime);
  127. // Create GameObject body
  128. GameObject body = new GameObject("Recorded Body Demo " + i);
  129. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  130. {
  131. // Skip these joints
  132. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  133. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  134. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  135. continue;
  136. // Create GameObject cubes for joints
  137. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  138. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  139. lr.positionCount = 2;
  140. lr.material = new Material(Shader.Find("Sprites/Default"))
  141. {
  142. color = new Color(1, 0.8f, 0.6f)
  143. };
  144. lr.startWidth = 0.05f;
  145. lr.endWidth = 0.05f;
  146. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  147. jointObj.name = jt.ToString();
  148. JointsData jd = newJointsData[i];
  149. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  150. jointObj.transform.parent = body.transform;
  151. // Remove LineRenderer component from neck
  152. if (jt == Kinect.JointType.Neck)
  153. {
  154. Destroy(jointObj.GetComponent<LineRenderer>());
  155. }
  156. }
  157. // Connect the joints with LineRenderer
  158. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  159. {
  160. // Skip if dictionary not contains the joint or other these joints
  161. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  162. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  163. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  164. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  165. continue;
  166. Transform jointObj = body.transform.Find(jt.ToString());
  167. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  168. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  169. lr.SetPosition(0, jointObj.localPosition);
  170. lr.SetPosition(1, targetJoint.localPosition);
  171. }
  172. yield return wait;
  173. Destroy(body);
  174. }
  175. }
  176. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  177. {
  178. for (int i = 0; i < newJointsData.Count; i++)
  179. {
  180. float waitTime = 1;
  181. if (i < newRecordingTimes.Count - 1)
  182. {
  183. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  184. }
  185. WaitForSeconds wait = new WaitForSeconds(waitTime);
  186. Visualizer_FadeIn body = Instantiate(vfi) as Visualizer_FadeIn;
  187. body.SetData(newJointsData[i]);
  188. body.ShowBody();
  189. yield return wait;
  190. }
  191. }
  192. private IEnumerator VisualizeFadeInSeries(List<JointsData> jointsData, List<float> recordingTimes, List<JointsData> newJointsData, List<float> newRecordingTimes)
  193. {
  194. float waitTime = 3;
  195. List<JointsData> jointsDataDemo = new List<JointsData>();
  196. List<float> recordingTimesDemo = new List<float>();
  197. List<JointsData> jointsDataSeries = new List<JointsData>();
  198. List<float> recordingTimesSeries = new List<float>();
  199. int indexDemo = 0;
  200. int indexSeries = 0;
  201. while (true)
  202. {
  203. // Visualization demo
  204. for (int i = indexDemo; i < jointsData.Count; i++)
  205. {
  206. if (recordingTimes[i] < waitTime)
  207. {
  208. jointsDataDemo.Add(jointsData[i]);
  209. recordingTimesDemo.Add(recordingTimes[i]);
  210. }
  211. else
  212. {
  213. indexDemo = i;
  214. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  215. bc.SetDataDemo(jointsDataDemo, recordingTimesDemo);
  216. break;
  217. }
  218. }
  219. // Visualization steps/series
  220. for (int i = indexSeries; i < newJointsData.Count; i++)
  221. {
  222. if (newRecordingTimes[i] < waitTime)
  223. {
  224. jointsDataSeries.Add(newJointsData[i]);
  225. recordingTimesSeries.Add(newRecordingTimes[i]);
  226. }
  227. else
  228. {
  229. // Show start position of steps
  230. startStepPreview.SetData(jointsDataSeries[0]);
  231. startStepPreview.ShowBody();
  232. // Wait for input
  233. yield return ViveInput.WaitForControllerPress();
  234. // Delete start position of steps
  235. startStepPreview.DeleteBody();
  236. indexSeries = i;
  237. waitTime += 3;
  238. Visualizer_FadeInSeries body = Instantiate(vfis) as Visualizer_FadeInSeries;
  239. body.transform.parent = gameObject.transform;
  240. body.SetData(jointsDataSeries, recordingTimesSeries);
  241. body.ShowBody();
  242. // Compare visualization demo with body
  243. yield return bc.StartCompare();
  244. jointsDataDemo.Clear();
  245. recordingTimesDemo.Clear();
  246. jointsDataSeries.Clear();
  247. recordingTimesSeries.Clear();
  248. // Wait for input
  249. yield return ViveInput.WaitForControllerPress();
  250. break;
  251. }
  252. }
  253. if (jointsDataSeries.Count != 0)
  254. break;
  255. }
  256. // Last visualization
  257. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  258. bc.SetDataDemo(jointsDataDemo, recordingTimesDemo);
  259. // Show start position of steps
  260. startStepPreview.SetData(jointsDataSeries[0]);
  261. startStepPreview.ShowBody();
  262. // Wait for input
  263. yield return ViveInput.WaitForControllerPress();
  264. // Delete start position of steps
  265. startStepPreview.DeleteBody();
  266. Visualizer_FadeInSeries bodyEnd = Instantiate(vfis) as Visualizer_FadeInSeries;
  267. bodyEnd.transform.parent = gameObject.transform;
  268. bodyEnd.SetData(jointsDataSeries, recordingTimesSeries);
  269. bodyEnd.ShowBody();
  270. // Compare visualization demo with body
  271. yield return bc.StartCompare();
  272. jointsDataDemo.Clear();
  273. recordingTimesDemo.Clear();
  274. jointsDataSeries.Clear();
  275. recordingTimesSeries.Clear();
  276. textFinish.SetActive(true);
  277. yield return new WaitForSeconds(3);
  278. textFinish.SetActive(false);
  279. // Write positions to csv
  280. bc.WriteCSV();
  281. ViveInput.StopPlaying();
  282. }
  283. }