PlayerReplay.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 Save()
  72. {
  73. if (jointsSequence.Count == 0)
  74. {
  75. Debug.Log("jointsSequence is empty");
  76. return;
  77. }
  78. SaveSystem.Save(jointsSequence, recordingTimes);
  79. Debug.Log("Save success");
  80. }
  81. public void Load()
  82. {
  83. JointsDataSequence data = SaveSystem.Load();
  84. if (data == null)
  85. {
  86. Debug.Log("Load failed");
  87. return;
  88. }
  89. ShowJoints(data);
  90. Debug.Log("Load success");
  91. }
  92. private void ShowJoints(JointsDataSequence data)
  93. {
  94. List<JointsData> jointsData = data.jointsDataSequence;
  95. List<float> recordingTimes = data.recordingTimes;
  96. // Filtered datas according to distance
  97. List<JointsData> newJointsData = new List<JointsData>();
  98. List<float> newRecordingTimes = new List<float>();
  99. JointsData prevJd = jointsData[0];
  100. // Add first pose
  101. newJointsData.Add(jointsData[0]);
  102. newRecordingTimes.Add(recordingTimes[0]);
  103. for (int i = 1; i < jointsData.Count; i++)
  104. {
  105. JointsData jd = jointsData[i];
  106. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  107. {
  108. // Skip if head joint
  109. if (jt == Kinect.JointType.Head)
  110. continue;
  111. Vector3 prevJointPosition = new Vector3(prevJd.jointsPositionsX[(int)jt], prevJd.jointsPositionsY[(int)jt], prevJd.jointsPositionsZ[(int)jt]);
  112. Vector3 jointPosition = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  113. float distance = Vector3.Distance(prevJointPosition, jointPosition);
  114. // If a joint is bigger than certain distance, add it to the replay, else ignore it
  115. // TODO: need to test distance value
  116. if (distance >= 2)
  117. {
  118. newJointsData.Add(jd);
  119. newRecordingTimes.Add(recordingTimes[i]);
  120. prevJd = jd;
  121. continue;
  122. }
  123. }
  124. }
  125. // TODO: visualize need to test
  126. StartCoroutine(Visualize(newJointsData, newRecordingTimes));
  127. }
  128. private IEnumerator Visualize(List<JointsData> newJointsData, List<float> newRecordingTimes)
  129. {
  130. for (int i = 0; i < newJointsData.Count; i++)
  131. {
  132. float waitTime = 1;
  133. if (i < newRecordingTimes.Count - 1)
  134. {
  135. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  136. }
  137. WaitForSeconds wait = new WaitForSeconds(waitTime);
  138. // Create GameObject body
  139. GameObject body = new GameObject("Recorded Body " + i);
  140. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  141. {
  142. // skip if head joint
  143. if (jt == Kinect.JointType.Head)
  144. continue;
  145. // Create GameObject cubes for joints
  146. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  147. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  148. lr.positionCount = 2;
  149. lr.material = boneMaterial;
  150. lr.startWidth = 0.3f;
  151. lr.endWidth = 0.3f;
  152. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  153. jointObj.name = jt.ToString();
  154. JointsData jd = newJointsData[i];
  155. jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
  156. jointObj.transform.parent = body.transform;
  157. }
  158. // Connect the joints with LineRenderer
  159. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  160. {
  161. // skip if dictionary not contains the joint or neck joint
  162. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck)
  163. continue;
  164. Transform jointObj = body.transform.Find(jt.ToString());
  165. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  166. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  167. lr.SetPosition(0, jointObj.localPosition);
  168. lr.SetPosition(1, targetJoint.localPosition);
  169. }
  170. yield return wait;
  171. Destroy(body);
  172. }
  173. }
  174. private IEnumerator VisualizeFadeIn(List<JointsData> newJointsData, List<float> newRecordingTimes)
  175. {
  176. for (int i = 0; i < newJointsData.Count; i++)
  177. {
  178. float waitTime = 1;
  179. if (i < newRecordingTimes.Count - 1)
  180. {
  181. waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i];
  182. }
  183. WaitForSeconds wait = new WaitForSeconds(waitTime);
  184. // TODO: create new class to make every body run fade in
  185. yield return wait;
  186. Destroy(body);
  187. }
  188. }
  189. }