PlayerReplay.cs 12 KB

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