Visualizer_FadeInSeries.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. public bool wristLeftTriggered;
  11. public bool wristRightTriggered;
  12. private List<JointsData> jointsDataSeries = new List<JointsData>();
  13. private List<float> recordingTimesSeries = new List<float>();
  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 float t;
  42. private List<GameObject> jointObjs = new List<GameObject>();
  43. private bool beginUpdate;
  44. private List<GameObject> bodies = new List<GameObject>();
  45. void Update()
  46. {
  47. if (!beginUpdate)
  48. return;
  49. if (t >= 1)
  50. {
  51. foreach (GameObject go in bodies)
  52. {
  53. Destroy(go);
  54. }
  55. Destroy(gameObject);
  56. return;
  57. }
  58. t += Time.deltaTime / 3;
  59. // TODO: different way to do it
  60. // After half time, report if player WristLeft or WristRight late or not
  61. if (t > 0.5f)
  62. {
  63. if (!wristLeftTriggered)
  64. {
  65. bsv.wristLeftLate = true;
  66. }
  67. if (!wristRightTriggered)
  68. {
  69. bsv.wristRightLate = true;
  70. }
  71. }
  72. }
  73. public void SetData(List<JointsData> jointsDataSeries, List<float> recordingTimesSeries)
  74. {
  75. this.jointsDataSeries = jointsDataSeries;
  76. this.recordingTimesSeries = recordingTimesSeries;
  77. }
  78. public void ShowBody()
  79. {
  80. for (int i = 0; i < jointsDataSeries.Count; i++)
  81. {
  82. // Create GameObject body
  83. GameObject body = new GameObject("Recorded Body (Visualizer_FadeInSeries) " + i);
  84. body.transform.parent = gameObject.transform;
  85. bodies.Add(body);
  86. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  87. {
  88. // Skip these joints
  89. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  90. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  91. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  92. continue;
  93. // Create GameObject cubes for joints
  94. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  95. jointObj.GetComponent<MeshRenderer>().material = transparentMat;
  96. // Give BoxCollider.isTrigger true and TriggerDetector to WristLeft and WristRight
  97. if (jt == Kinect.JointType.WristLeft || jt == Kinect.JointType.WristRight)
  98. {
  99. jointObj.GetComponent<BoxCollider>().isTrigger = true;
  100. jointObj.AddComponent<TriggerDetectorSeries>();
  101. }
  102. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  103. lr.positionCount = 2;
  104. lr.material = boneMaterial;
  105. lr.startWidth = 0.05f;
  106. lr.endWidth = 0.05f;
  107. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  108. jointObj.name = jt.ToString();
  109. jointObj.transform.position = new Vector3(jointsDataSeries[i].jointsPositionsX[(int)jt], jointsDataSeries[i].jointsPositionsY[(int)jt], jointsDataSeries[i].jointsPositionsZ[(int)jt]);
  110. jointObj.transform.parent = body.transform;
  111. // Set alpha depends on i
  112. Color newColor = new Color(1, 1, 1, Mathf.Lerp(0, 1, (float) (i + 1) / jointsDataSeries.Count));
  113. jointObj.GetComponent<MeshRenderer>().material.color = newColor;
  114. jointObj.GetComponent<LineRenderer>().startColor = newColor;
  115. jointObj.GetComponent<LineRenderer>().endColor = newColor;
  116. // Remove LineRenderer component from neck
  117. if (jt == Kinect.JointType.Neck)
  118. {
  119. Destroy(jointObj.GetComponent<LineRenderer>());
  120. }
  121. }
  122. // Connect the joints with LineRenderer
  123. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  124. {
  125. // Skip if dictionary not contains the joint or other these joints
  126. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  127. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  128. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  129. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  130. continue;
  131. Transform jointObj = body.transform.Find(jt.ToString());
  132. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  133. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  134. lr.SetPosition(0, jointObj.localPosition);
  135. lr.SetPosition(1, targetJoint.localPosition);
  136. }
  137. }
  138. beginUpdate = true;
  139. }
  140. }