PlayerReplay.cs 12 KB

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