Visualizer_FadeIn.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kinect = Windows.Kinect;
  5. public class Visualizer_FadeIn : MonoBehaviour
  6. {
  7. public Material boneMaterial;
  8. public Material transparentMat;
  9. public BodySourceView bsv;
  10. public bool wristLeftTriggered;
  11. public bool wristRightTriggered;
  12. private JointsData data;
  13. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  14. {
  15. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  16. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  17. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  18. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  19. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  20. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  21. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  22. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  23. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  24. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  25. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  26. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  27. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  28. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  29. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  30. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  31. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  32. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  33. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  34. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  35. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  36. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  37. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  38. { Kinect.JointType.Neck, Kinect.JointType.Head },
  39. };
  40. private float t;
  41. private List<GameObject> jointObjs = new List<GameObject>();
  42. private bool beginUpdate;
  43. private GameObject body;
  44. void Update()
  45. {
  46. if (!beginUpdate)
  47. return;
  48. if (t >= 1)
  49. {
  50. Destroy(body);
  51. Destroy(gameObject);
  52. return;
  53. }
  54. t += Time.deltaTime / 2;
  55. Color newColor = new Color(1, 1, 1, Mathf.Lerp(0, 1, t));
  56. foreach(GameObject go in jointObjs)
  57. {
  58. go.GetComponent<MeshRenderer>().material.color = newColor;
  59. go.GetComponent<LineRenderer>().startColor = newColor;
  60. go.GetComponent<LineRenderer>().endColor = newColor;
  61. }
  62. // After half time, report if player WristLeft or WristRight late or not
  63. if (t > 0.5f)
  64. {
  65. if (!wristLeftTriggered)
  66. {
  67. bsv.wristLeftLate = true;
  68. }
  69. if (!wristRightTriggered)
  70. {
  71. bsv.wristRightLate = true;
  72. }
  73. }
  74. }
  75. public void SetData(JointsData data)
  76. {
  77. this.data = data;
  78. }
  79. public void ShowBody()
  80. {
  81. // Create GameObject body
  82. body = new GameObject("Recorded Body (Visualizer_FadeIn)");
  83. body.transform.parent = gameObject.transform;
  84. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  85. {
  86. // Skip these joints
  87. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  88. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  89. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  90. continue;
  91. // Create GameObject cubes for joints
  92. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  93. jointObj.GetComponent<MeshRenderer>().material = transparentMat;
  94. // Give BoxCollider.isTrigger true and TriggerDetector to WristLeft and WristRight
  95. if (jt == Kinect.JointType.WristLeft || jt == Kinect.JointType.WristRight)
  96. {
  97. jointObj.GetComponent<BoxCollider>().isTrigger = true;
  98. jointObj.AddComponent<TriggerDetector>();
  99. }
  100. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  101. lr.positionCount = 2;
  102. lr.material = boneMaterial;
  103. lr.startWidth = 0.3f;
  104. lr.endWidth = 0.3f;
  105. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  106. jointObj.name = jt.ToString();
  107. jointObj.transform.position = new Vector3(data.jointsPositionsX[(int)jt], data.jointsPositionsY[(int)jt], data.jointsPositionsZ[(int)jt]);
  108. jointObj.transform.parent = body.transform;
  109. // Add to list
  110. jointObjs.Add(jointObj);
  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. beginUpdate = true;
  128. }
  129. }