PlayerReplay.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. // TODO: maybe distance need to be adjusted
  107. if (distance >= 0.15f)
  108. {
  109. newJointsData.Add(jd);
  110. newRecordingTimes.Add(recordingTimes[i]);
  111. prevJd = jd;
  112. continue;
  113. }
  114. }
  115. }
  116. StartCoroutine(VisualizeFadeInSeries(jointsData, recordingTimes, 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 Demo " + 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 = new Material(Shader.Find("Sprites/Default"));
  142. lr.material.color = new Color(1, 0.8f, 0.6f);
  143. lr.startWidth = 0.05f;
  144. lr.endWidth = 0.05f;
  145. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  146. jointObj.name = jt.ToString();
  147. JointsData jd = newJointsData[i];
  148. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  149. jointObj.transform.parent = body.transform;
  150. // Remove LineRenderer component from neck
  151. if (jt == Kinect.JointType.Neck)
  152. {
  153. Destroy(jointObj.GetComponent<LineRenderer>());
  154. }
  155. }
  156. // Connect the joints with LineRenderer
  157. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  158. {
  159. // Skip if dictionary not contains the joint or other these joints
  160. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  161. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  162. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  163. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  164. continue;
  165. Transform jointObj = body.transform.Find(jt.ToString());
  166. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  167. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  168. lr.SetPosition(0, jointObj.localPosition);
  169. lr.SetPosition(1, targetJoint.localPosition);
  170. }
  171. yield return wait;
  172. Destroy(body);
  173. }
  174. }
  175. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  176. {
  177. for (int i = 0; i < newJointsData.Count; i++)
  178. {
  179. float waitTime = 1;
  180. if (i < newRecordingTimes.Count - 1)
  181. {
  182. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  183. }
  184. WaitForSeconds wait = new WaitForSeconds(waitTime);
  185. Visualizer_FadeIn body = Instantiate(vfi) as Visualizer_FadeIn;
  186. body.SetData(newJointsData[i]);
  187. body.ShowBody();
  188. yield return wait;
  189. }
  190. }
  191. private IEnumerator VisualizeFadeInSeries(List<JointsData> jointsData, List<float> recordingTimes, List<JointsData> newJointsData, List<float> newRecordingTimes)
  192. {
  193. float waitTime = 3;
  194. List<JointsData> jointsDataDemo = new List<JointsData>();
  195. List<float> recordingTimesDemo = new List<float>();
  196. List<JointsData> jointsDataSeries = new List<JointsData>();
  197. List<float> recordingTimesSeries = new List<float>();
  198. int indexDemo = 0;
  199. int indexSeries = 0;
  200. while (true)
  201. {
  202. // Visualization demo
  203. for (int i = indexDemo; i < jointsData.Count; i++)
  204. {
  205. if (recordingTimes[i] < waitTime)
  206. {
  207. jointsDataDemo.Add(jointsData[i]);
  208. recordingTimesDemo.Add(recordingTimes[i]);
  209. }
  210. else
  211. {
  212. indexDemo = i;
  213. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  214. bc.SetDataDemo(jointsDataDemo, recordingTimesDemo);
  215. break;
  216. }
  217. }
  218. // Visualization steps/series
  219. for (int i = indexSeries; i < newJointsData.Count; i++)
  220. {
  221. if (newRecordingTimes[i] < waitTime)
  222. {
  223. jointsDataSeries.Add(newJointsData[i]);
  224. recordingTimesSeries.Add(newRecordingTimes[i]);
  225. }
  226. else
  227. {
  228. // Show start position of steps
  229. startStepPreview.SetData(jointsDataSeries[0]);
  230. startStepPreview.ShowBody();
  231. // Wait for input
  232. yield return ViveInput.WaitForControllerPress();
  233. // Delete start position of steps
  234. startStepPreview.DeleteBody();
  235. indexSeries = i;
  236. waitTime += 3;
  237. Visualizer_FadeInSeries body = Instantiate(vfis) as Visualizer_FadeInSeries;
  238. body.transform.parent = gameObject.transform;
  239. body.SetData(jointsDataSeries, recordingTimesSeries);
  240. body.ShowBody();
  241. // Compare visualization demo with body
  242. yield return bc.StartCompare();
  243. jointsDataDemo.Clear();
  244. recordingTimesDemo.Clear();
  245. jointsDataSeries.Clear();
  246. recordingTimesSeries.Clear();
  247. // Wait for input
  248. yield return ViveInput.WaitForControllerPress();
  249. break;
  250. }
  251. }
  252. if (jointsDataSeries.Count != 0)
  253. break;
  254. }
  255. // Last visualization
  256. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  257. bc.SetDataDemo(jointsDataDemo, recordingTimesDemo);
  258. // Show start position of steps
  259. startStepPreview.SetData(jointsDataSeries[0]);
  260. startStepPreview.ShowBody();
  261. // Wait for input
  262. yield return ViveInput.WaitForControllerPress();
  263. // Delete start position of steps
  264. startStepPreview.DeleteBody();
  265. Visualizer_FadeInSeries bodyEnd = Instantiate(vfis) as Visualizer_FadeInSeries;
  266. bodyEnd.transform.parent = gameObject.transform;
  267. bodyEnd.SetData(jointsDataSeries, recordingTimesSeries);
  268. bodyEnd.ShowBody();
  269. // Compare visualization demo with body
  270. yield return bc.StartCompare();
  271. jointsDataDemo.Clear();
  272. recordingTimesDemo.Clear();
  273. jointsDataSeries.Clear();
  274. recordingTimesSeries.Clear();
  275. textFinish.SetActive(true);
  276. yield return new WaitForSeconds(3);
  277. textFinish.SetActive(false);
  278. }
  279. }