Visualizer_FadeInSeries.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kinect = Windows.Kinect;
  5. public class Visualizer_FadeInSeries : MonoBehaviour
  6. {
  7. public Material boneMaterial;
  8. public Material transparentMat;
  9. public BodySourceView bsv;
  10. private List<JointsData> jointsDataSeries = new List<JointsData>();
  11. private List<float> recordingTimesSeries = new List<float>();
  12. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  13. {
  14. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  15. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  16. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  17. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  18. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  19. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  20. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  21. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  22. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  23. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  24. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  25. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  26. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  27. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  28. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  29. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  30. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  31. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  32. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  33. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  34. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  35. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  36. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  37. { Kinect.JointType.Neck, Kinect.JointType.Head },
  38. };
  39. private float t;
  40. private bool beginUpdate;
  41. private List<GameObject> bodies = new List<GameObject>();
  42. void Update()
  43. {
  44. if (!beginUpdate)
  45. return;
  46. if (t >= 1)
  47. {
  48. Destroy(gameObject);
  49. return;
  50. }
  51. t += Time.deltaTime / 8;
  52. }
  53. public void SetData(List<JointsData> jointsDataSeries, List<float> recordingTimesSeries)
  54. {
  55. this.jointsDataSeries = jointsDataSeries;
  56. this.recordingTimesSeries = recordingTimesSeries;
  57. }
  58. public void ShowBody()
  59. {
  60. for (int i = 0; i < jointsDataSeries.Count; i++)
  61. {
  62. // Create GameObject body
  63. GameObject body = new GameObject("Recorded Body (Visualizer_FadeInSeries) " + i);
  64. body.transform.parent = gameObject.transform;
  65. bodies.Add(body);
  66. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  67. {
  68. // Skip these joints
  69. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  70. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  71. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  72. continue;
  73. // Create GameObject cubes for joints
  74. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  75. jointObj.GetComponent<MeshRenderer>().material = transparentMat;
  76. // Give BoxCollider.isTrigger true and TriggerDetector to WristLeft and WristRight
  77. if (jt == Kinect.JointType.WristLeft || jt == Kinect.JointType.WristRight)
  78. {
  79. jointObj.GetComponent<BoxCollider>().isTrigger = true;
  80. jointObj.AddComponent<TriggerDetectorSeries>();
  81. }
  82. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  83. lr.positionCount = 2;
  84. lr.material = boneMaterial;
  85. lr.startWidth = 0.05f;
  86. lr.endWidth = 0.05f;
  87. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  88. jointObj.name = jt.ToString();
  89. jointObj.transform.position = new Vector3(jointsDataSeries[i].jointsPositionsX[(int)jt], jointsDataSeries[i].jointsPositionsY[(int)jt], jointsDataSeries[i].jointsPositionsZ[(int)jt]);
  90. jointObj.transform.parent = body.transform;
  91. // Don't show joint if current and previous are almost the same position
  92. if (i > 0)
  93. {
  94. Vector3 prev = new Vector3(jointsDataSeries[i - 1].jointsPositionsX[(int)jt], jointsDataSeries[i - 1].jointsPositionsY[(int)jt], jointsDataSeries[i - 1].jointsPositionsZ[(int)jt]);
  95. float distance = (jointObj.transform.position - prev).magnitude;
  96. if (distance < 0.05f)
  97. {
  98. jointObj.SetActive(false);
  99. }
  100. }
  101. // Set alpha depends on i
  102. Color newColor = new Color(0.8f, 1, 0, Mathf.Lerp(0, 1, (float)(i + 1) / jointsDataSeries.Count));
  103. jointObj.GetComponent<MeshRenderer>().material.color = newColor;
  104. jointObj.GetComponent<LineRenderer>().startColor = newColor;
  105. jointObj.GetComponent<LineRenderer>().endColor = newColor;
  106. // Remove LineRenderer component from neck
  107. if (jt == Kinect.JointType.Neck)
  108. {
  109. Destroy(jointObj.GetComponent<LineRenderer>());
  110. }
  111. }
  112. // Connect the joints with LineRenderer
  113. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  114. {
  115. // Skip if dictionary not contains the joint or other these joints
  116. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  117. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  118. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  119. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  120. continue;
  121. Transform jointObj = body.transform.Find(jt.ToString());
  122. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  123. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  124. lr.SetPosition(0, jointObj.localPosition);
  125. lr.SetPosition(1, targetJoint.localPosition);
  126. }
  127. }
  128. beginUpdate = true;
  129. }
  130. private void OnDestroy()
  131. {
  132. foreach (GameObject go in bodies)
  133. {
  134. Destroy(go);
  135. }
  136. }
  137. }