StartStepPreview.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kinect = Windows.Kinect;
  5. public class StartStepPreview : MonoBehaviour
  6. {
  7. public Material boneMaterial;
  8. public Material transparentMat;
  9. private JointsData jointsData;
  10. private GameObject body;
  11. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  12. {
  13. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  14. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  15. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  16. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  17. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  18. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  19. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  20. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  21. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  22. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  23. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  24. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  25. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  26. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  27. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  28. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  29. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  30. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  31. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  32. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  33. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  34. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  35. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  36. { Kinect.JointType.Neck, Kinect.JointType.Head },
  37. };
  38. public void SetData(JointsData jointsData)
  39. {
  40. this.jointsData = jointsData;
  41. }
  42. public void ShowBody()
  43. {
  44. // Create GameObject body
  45. body = new GameObject("StartStepPreview body");
  46. body.transform.parent = gameObject.transform;
  47. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  48. {
  49. // Skip these joints
  50. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  51. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  52. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  53. continue;
  54. // Create GameObject cubes for joints
  55. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  56. jointObj.GetComponent<MeshRenderer>().material = transparentMat;
  57. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  58. lr.positionCount = 2;
  59. lr.material = boneMaterial;
  60. lr.startWidth = 0.05f;
  61. lr.endWidth = 0.05f;
  62. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  63. jointObj.name = jt.ToString();
  64. jointObj.transform.position = new Vector3(jointsData.jointsPositionsX[(int)jt], jointsData.jointsPositionsY[(int)jt], jointsData.jointsPositionsZ[(int)jt]);
  65. jointObj.transform.parent = body.transform;
  66. // Set alpha
  67. Color newColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
  68. jointObj.GetComponent<MeshRenderer>().material.color = newColor;
  69. jointObj.GetComponent<LineRenderer>().startColor = newColor;
  70. jointObj.GetComponent<LineRenderer>().endColor = newColor;
  71. // Remove LineRenderer component from neck
  72. if (jt == Kinect.JointType.Neck)
  73. {
  74. Destroy(jointObj.GetComponent<LineRenderer>());
  75. }
  76. }
  77. // Connect the joints with LineRenderer
  78. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  79. {
  80. // Skip if dictionary not contains the joint or other these joints
  81. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  82. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  83. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  84. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  85. continue;
  86. Transform jointObj = body.transform.Find(jt.ToString());
  87. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  88. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  89. lr.SetPosition(0, jointObj.localPosition);
  90. lr.SetPosition(1, targetJoint.localPosition);
  91. }
  92. }
  93. public void DeleteBody()
  94. {
  95. Destroy(body);
  96. }
  97. }