Visualizer_FadeInSeries.cs 5.5 KB

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