Visualizer_FadeIn.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // TODO: need to test
  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. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  84. {
  85. // Skip these joints
  86. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  87. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  88. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  89. continue;
  90. // Create GameObject cubes for joints
  91. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  92. jointObj.GetComponent<MeshRenderer>().material = transparentMat;
  93. // TODO: need to test
  94. if (jt == Kinect.JointType.WristLeft || jt == Kinect.JointType.WristRight)
  95. {
  96. jointObj.GetComponent<BoxCollider>().isTrigger = true;
  97. jointObj.AddComponent<TriggerDetector>();
  98. }
  99. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  100. lr.positionCount = 2;
  101. lr.material = boneMaterial;
  102. lr.startWidth = 0.3f;
  103. lr.endWidth = 0.3f;
  104. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  105. jointObj.name = jt.ToString();
  106. jointObj.transform.position = new Vector3(data.jointsPositionsX[(int)jt], data.jointsPositionsY[(int)jt], data.jointsPositionsZ[(int)jt]);
  107. jointObj.transform.parent = body.transform;
  108. // Add to list
  109. jointObjs.Add(jointObj);
  110. }
  111. // Connect the joints with LineRenderer
  112. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  113. {
  114. // Skip if dictionary not contains the joint or other these joints
  115. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  116. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  117. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  118. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  119. continue;
  120. Transform jointObj = body.transform.Find(jt.ToString());
  121. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  122. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  123. lr.SetPosition(0, jointObj.localPosition);
  124. lr.SetPosition(1, targetJoint.localPosition);
  125. }
  126. beginUpdate = true;
  127. }
  128. }