PlayerReplay.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 GameObject textFinish;
  17. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  18. {
  19. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  20. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  21. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  22. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  23. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  24. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  25. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  26. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  27. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  28. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  29. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  30. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  31. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  32. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  33. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  34. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  35. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  36. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  37. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  38. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  39. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  40. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  41. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  42. { Kinect.JointType.Neck, Kinect.JointType.Head },
  43. };
  44. public void AddJoints(float recordingTime)
  45. {
  46. Vector3[] positions = new Vector3[25];
  47. for (int i = 0; i < joints.Length; i++)
  48. {
  49. positions[i] = joints[i].position;
  50. }
  51. jointsSequence.Add(positions);
  52. recordingTimes.Add(recordingTime);
  53. }
  54. public void ResetRecording()
  55. {
  56. jointsSequence.Clear();
  57. recordingTimes.Clear();
  58. }
  59. public void Save()
  60. {
  61. if (jointsSequence.Count == 0)
  62. {
  63. Debug.Log("jointsSequence is empty");
  64. return;
  65. }
  66. SaveSystem.Save(jointsSequence, recordingTimes);
  67. Debug.Log("Save success");
  68. }
  69. public void Load()
  70. {
  71. JointsDataSequence data = SaveSystem.Load();
  72. if (data == null)
  73. {
  74. Debug.Log("Load failed");
  75. return;
  76. }
  77. ShowJoints(data);
  78. Debug.Log("Load success");
  79. }
  80. private void ShowJoints(JointsDataSequence data)
  81. {
  82. List<JointsData> jointsData = data.jointsDataSequence;
  83. List<float> recordingTimes = data.recordingTimes;
  84. // Filtered datas according to distance
  85. List<JointsData> newJointsData = new List<JointsData>();
  86. List<float> newRecordingTimes = new List<float>();
  87. JointsData prevJd = jointsData[0];
  88. // Add first pose
  89. newJointsData.Add(jointsData[0]);
  90. newRecordingTimes.Add(recordingTimes[0]);
  91. for (int i = 1; i < jointsData.Count; i++)
  92. {
  93. JointsData jd = jointsData[i];
  94. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  95. {
  96. // Skip these joints
  97. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  98. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  99. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  100. continue;
  101. Vector3 prevJointPosition = new Vector3(prevJd.jointsPositionsX[(int)jt], prevJd.jointsPositionsY[(int)jt], prevJd.jointsPositionsZ[(int)jt]);
  102. Vector3 jointPosition = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  103. float distance = Vector3.Distance(prevJointPosition, jointPosition);
  104. // If a joint is bigger than a certain distance, add it to the replay, else ignore it
  105. // TODO: maybe distance need to be adjusted
  106. if (distance >= 0.2f)
  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. lr.material.color = new Color(1, 0.8f, 0.6f);
  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. // Remove LineRenderer component from neck
  150. if (jt == Kinect.JointType.Neck)
  151. {
  152. Destroy(jointObj.GetComponent<LineRenderer>());
  153. }
  154. }
  155. // Connect the joints with LineRenderer
  156. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  157. {
  158. // Skip if dictionary not contains the joint or other these joints
  159. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  160. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  161. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  162. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  163. continue;
  164. Transform jointObj = body.transform.Find(jt.ToString());
  165. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  166. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  167. lr.SetPosition(0, jointObj.localPosition);
  168. lr.SetPosition(1, targetJoint.localPosition);
  169. }
  170. yield return wait;
  171. Destroy(body);
  172. }
  173. }
  174. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  175. {
  176. for (int i = 0; i < newJointsData.Count; i++)
  177. {
  178. float waitTime = 1;
  179. if (i < newRecordingTimes.Count - 1)
  180. {
  181. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  182. }
  183. WaitForSeconds wait = new WaitForSeconds(waitTime);
  184. Visualizer_FadeIn body = Instantiate(vfi) as Visualizer_FadeIn;
  185. body.SetData(newJointsData[i]);
  186. body.ShowBody();
  187. yield return wait;
  188. }
  189. }
  190. private IEnumerator VisualizeFadeInSeries(List<JointsData> jointsData, List<float> recordingTimes, List<JointsData> newJointsData, List<float> newRecordingTimes)
  191. {
  192. float waitTime = 3;
  193. List<JointsData> jointsDataDemo = new List<JointsData>();
  194. List<float> recordingTimesDemo = new List<float>();
  195. List<JointsData> jointsDataSeries = new List<JointsData>();
  196. List<float> recordingTimesSeries = new List<float>();
  197. int indexDemo = 0;
  198. int indexSeries = 0;
  199. while (true)
  200. {
  201. // Visualization demo
  202. for (int i = indexDemo; i < jointsData.Count; i++)
  203. {
  204. if (recordingTimes[i] < waitTime)
  205. {
  206. jointsDataDemo.Add(jointsData[i]);
  207. recordingTimesDemo.Add(recordingTimes[i]);
  208. }
  209. else
  210. {
  211. indexDemo = i;
  212. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  213. // TODO: Implementing color feedback
  214. bc.SetDataDemo(jointsDataDemo, recordingTimesDemo);
  215. jointsDataDemo.Clear();
  216. recordingTimesDemo.Clear();
  217. // Wait for input
  218. yield return ViveInput.WaitForControllerPress();
  219. break;
  220. }
  221. }
  222. // Visualization steps/series
  223. for (int i = indexSeries; i < newJointsData.Count; i++)
  224. {
  225. if (newRecordingTimes[i] < waitTime)
  226. {
  227. jointsDataSeries.Add(newJointsData[i]);
  228. recordingTimesSeries.Add(newRecordingTimes[i]);
  229. }
  230. else
  231. {
  232. indexSeries = i;
  233. waitTime += 3;
  234. Visualizer_FadeInSeries body = Instantiate(vfis) as Visualizer_FadeInSeries;
  235. body.transform.parent = gameObject.transform;
  236. body.SetData(jointsDataSeries, recordingTimesSeries);
  237. body.ShowBody();
  238. jointsDataSeries.Clear();
  239. recordingTimesSeries.Clear();
  240. yield return bc.StartCompare();
  241. //yield return new WaitForSeconds(3);
  242. yield return ViveInput.WaitForControllerPress();
  243. break;
  244. }
  245. }
  246. if (jointsDataSeries.Count != 0)
  247. break;
  248. }
  249. // Last visualization
  250. yield return Visualize(jointsDataDemo, recordingTimesDemo);
  251. bc.SetDataDemo(jointsDataDemo, recordingTimesDemo);
  252. jointsDataDemo.Clear();
  253. recordingTimesDemo.Clear();
  254. yield return ViveInput.WaitForControllerPress();
  255. Visualizer_FadeInSeries bodyEnd = Instantiate(vfis) as Visualizer_FadeInSeries;
  256. bodyEnd.transform.parent = gameObject.transform;
  257. bodyEnd.SetData(jointsDataSeries, recordingTimesSeries);
  258. bodyEnd.ShowBody();
  259. jointsDataSeries.Clear();
  260. recordingTimesSeries.Clear();
  261. yield return bc.StartCompare();
  262. //yield return new WaitForSeconds(3);
  263. textFinish.SetActive(true);
  264. yield return new WaitForSeconds(3);
  265. textFinish.SetActive(false);
  266. }
  267. }