PlayerReplay.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 boneMaterial;
  13. public Material transparentMat;
  14. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  15. {
  16. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  17. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  18. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  19. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  20. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  21. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  22. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  23. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  24. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  25. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  26. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  27. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  28. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  29. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  30. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  31. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  32. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  33. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  34. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  35. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  36. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  37. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  38. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  39. { Kinect.JointType.Neck, Kinect.JointType.Head },
  40. };
  41. private GameObject body;
  42. private bool beginAnimation;
  43. private float t;
  44. private void Update()
  45. {
  46. // TODO: wrong, put it to new class
  47. if (!beginAnimation)
  48. return;
  49. t += Time.deltaTime / 2;
  50. Color newColor = new Color(1, 1, 1, Mathf.Lerp(0, 1, t));
  51. foreach (MeshRenderer mr in body.GetComponentsInChildren<MeshRenderer>())
  52. {
  53. mr.material.color = newColor;
  54. }
  55. foreach (LineRenderer lr in body.GetComponentsInChildren<LineRenderer>())
  56. {
  57. lr.startColor = newColor;
  58. lr.endColor = newColor;
  59. }
  60. }
  61. public void AddJoints(float recordingTime)
  62. {
  63. Vector3[] positions = new Vector3[25];
  64. for (int i = 0; i < joints.Length; i++)
  65. {
  66. positions[i] = joints[i].position;
  67. }
  68. jointsSequence.Add(positions);
  69. recordingTimes.Add(recordingTime);
  70. }
  71. public void ResetRecording()
  72. {
  73. jointsSequence.Clear();
  74. recordingTimes.Clear();
  75. }
  76. public void Save()
  77. {
  78. if (jointsSequence.Count == 0)
  79. {
  80. Debug.Log("jointsSequence is empty");
  81. return;
  82. }
  83. SaveSystem.Save(jointsSequence, recordingTimes);
  84. Debug.Log("Save success");
  85. }
  86. public void Load()
  87. {
  88. JointsDataSequence data = SaveSystem.Load();
  89. if (data == null)
  90. {
  91. Debug.Log("Load failed");
  92. return;
  93. }
  94. ShowJoints(data);
  95. Debug.Log("Load success");
  96. }
  97. private void ShowJoints(JointsDataSequence data)
  98. {
  99. List<JointsData> jointsData = data.jointsDataSequence;
  100. List<float> recordingTimes = data.recordingTimes;
  101. // Filtered datas according to distance
  102. List<JointsData> newJointsData = new List<JointsData>();
  103. List<float> newRecordingTimes = new List<float>();
  104. JointsData prevJd = jointsData[0];
  105. // Add first pose
  106. newJointsData.Add(jointsData[0]);
  107. newRecordingTimes.Add(recordingTimes[0]);
  108. for (int i = 1; i < jointsData.Count; i++)
  109. {
  110. JointsData jd = jointsData[i];
  111. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  112. {
  113. // Skip if head joint
  114. if (jt == Kinect.JointType.Head)
  115. continue;
  116. Vector3 prevJointPosition = new Vector3(prevJd.jointsPositionsX[(int)jt], prevJd.jointsPositionsY[(int)jt], prevJd.jointsPositionsZ[(int)jt]);
  117. Vector3 jointPosition = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  118. float distance = Vector3.Distance(prevJointPosition, jointPosition);
  119. // If a joint is bigger than a certain distance, add it to the replay, else ignore it
  120. if (distance >= 2)
  121. {
  122. newJointsData.Add(jd);
  123. newRecordingTimes.Add(recordingTimes[i]);
  124. prevJd = jd;
  125. continue;
  126. }
  127. }
  128. }
  129. StartCoroutine(Visualize(newJointsData, newRecordingTimes));
  130. }
  131. private IEnumerator Visualize(List<JointsData> newJointsData, List<float> newRecordingTimes)
  132. {
  133. for (int i = 0; i < newJointsData.Count; i++)
  134. {
  135. float waitTime = 1;
  136. if (i < newRecordingTimes.Count - 1)
  137. {
  138. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  139. }
  140. WaitForSeconds wait = new WaitForSeconds(waitTime);
  141. // Create GameObject body
  142. GameObject body = new GameObject("Recorded Body " + i);
  143. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  144. {
  145. // Skip if head joint
  146. if (jt == Kinect.JointType.Head)
  147. continue;
  148. // Create GameObject cubes for joints
  149. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  150. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  151. lr.positionCount = 2;
  152. lr.material = boneMaterial;
  153. lr.startWidth = 0.3f;
  154. lr.endWidth = 0.3f;
  155. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  156. jointObj.name = jt.ToString();
  157. JointsData jd = newJointsData[i];
  158. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  159. jointObj.transform.parent = body.transform;
  160. }
  161. // Connect the joints with LineRenderer
  162. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  163. {
  164. // Skip if dictionary not contains the joint or neck joint
  165. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck)
  166. continue;
  167. Transform jointObj = body.transform.Find(jt.ToString());
  168. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  169. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  170. lr.SetPosition(0, jointObj.localPosition);
  171. lr.SetPosition(1, targetJoint.localPosition);
  172. }
  173. yield return wait;
  174. Destroy(body);
  175. }
  176. }
  177. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  178. {
  179. for (int i = 0; i < newJointsData.Count; i++)
  180. {
  181. float waitTime = 1;
  182. if (i < newRecordingTimes.Count - 1)
  183. {
  184. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  185. }
  186. WaitForSeconds wait = new WaitForSeconds(waitTime);
  187. // TODO: create new class to make every body run fade in
  188. yield return wait;
  189. Destroy(body);
  190. }
  191. }
  192. }