StartStepPreview.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public void SetData(JointsData jointsData)
  12. {
  13. this.jointsData = jointsData;
  14. }
  15. public void ShowBody()
  16. {
  17. // Create GameObject body
  18. body = new GameObject("StartStepPreview body");
  19. body.transform.parent = gameObject.transform;
  20. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  21. {
  22. // Skip these joints
  23. if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  24. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  25. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  26. continue;
  27. // Create GameObject cubes for joints
  28. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  29. jointObj.GetComponent<MeshRenderer>().material = transparentMat;
  30. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  31. lr.positionCount = 2;
  32. lr.material = boneMaterial;
  33. lr.startWidth = 0.05f;
  34. lr.endWidth = 0.05f;
  35. jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
  36. jointObj.name = jt.ToString();
  37. Vector3[] joints = HelperScript.ConvertJointsDataToVector3Array(jointsData);
  38. Vector3[] rescaledJoints = HelperScript.RescaleJoints(joints);
  39. jointObj.transform.position = rescaledJoints[(int)jt];
  40. jointObj.transform.parent = body.transform;
  41. // Set alpha
  42. Color newColor = new Color(0.5f, 0.5f, 0.5f, 0.5f);
  43. jointObj.GetComponent<MeshRenderer>().material.color = newColor;
  44. jointObj.GetComponent<LineRenderer>().startColor = newColor;
  45. jointObj.GetComponent<LineRenderer>().endColor = newColor;
  46. // Remove LineRenderer component from neck
  47. if (jt == Kinect.JointType.Neck)
  48. {
  49. Destroy(jointObj.GetComponent<LineRenderer>());
  50. }
  51. }
  52. // Connect the joints with LineRenderer
  53. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  54. {
  55. // Skip if dictionary not contains the joint or other these joints
  56. if (!HelperScript._BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
  57. || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
  58. || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
  59. || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
  60. continue;
  61. Transform jointObj = body.transform.Find(jt.ToString());
  62. Transform targetJoint = body.transform.Find(HelperScript._BoneMap[jt].ToString());
  63. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  64. lr.SetPosition(0, jointObj.localPosition);
  65. lr.SetPosition(1, targetJoint.localPosition);
  66. }
  67. }
  68. public void DeleteBody()
  69. {
  70. Destroy(body);
  71. }
  72. }