PlayerReplay.cs 14 KB

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