PlayerReplay.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Kinect = Windows.Kinect;
  5. public class PlayerReplay : MonoBehaviour
  6. {
  7. public Transform[] joints = new Transform[25];
  8. public Material boneMaterial;
  9. private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
  10. {
  11. { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
  12. { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
  13. { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
  14. { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
  15. { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
  16. { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
  17. { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
  18. { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
  19. { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
  20. { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
  21. { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
  22. { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
  23. { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
  24. { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
  25. { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
  26. { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
  27. { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
  28. { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
  29. { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
  30. { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
  31. { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
  32. { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
  33. { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
  34. { Kinect.JointType.Neck, Kinect.JointType.Head },
  35. };
  36. // (need to implement controller click to trigger save)
  37. public void Save()
  38. {
  39. if(joints[0] == null)
  40. {
  41. Debug.Log("joints have not been assigned");
  42. return;
  43. }
  44. SaveSystem.Save(this);
  45. Debug.Log("Save success");
  46. }
  47. public void Load()
  48. {
  49. JointsData data = SaveSystem.Load();
  50. if (data == null)
  51. {
  52. Debug.Log("Load failed");
  53. return;
  54. }
  55. // Create GameObject body
  56. GameObject body = new GameObject("Recorded Body");
  57. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  58. {
  59. // skip if head joint
  60. if (jt == Kinect.JointType.Head)
  61. continue;
  62. // Create GameObject cubes for joints
  63. GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  64. LineRenderer lr = jointObj.AddComponent<LineRenderer>();
  65. lr.positionCount = 2;
  66. lr.material = boneMaterial;
  67. lr.startWidth = 0.3f;
  68. lr.endWidth = 0.3f;
  69. jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
  70. jointObj.name = jt.ToString();
  71. jointObj.transform.position = new Vector3(data.jointsPositionsX[(int)jt], data.jointsPositionsY[(int)jt], data.jointsPositionsZ[(int)jt]);
  72. jointObj.transform.parent = body.transform;
  73. }
  74. // Connect the joints with LineRenderer
  75. for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
  76. {
  77. // skip if dictionary not contains the joint or neck joint
  78. if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck)
  79. continue;
  80. Transform jointObj = body.transform.Find(jt.ToString());
  81. LineRenderer lr = jointObj.GetComponent<LineRenderer>();
  82. Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
  83. lr.SetPosition(0, jointObj.localPosition);
  84. lr.SetPosition(1, targetJoint.localPosition);
  85. }
  86. Debug.Log("Load success");
  87. }
  88. }