PlayerReplay.cs 14 KB

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