123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Kinect = Windows.Kinect;
- public class PlayerReplay : MonoBehaviour
- {
- // Joints already given the values from BodySourceView.cs that keeps updating
- public Transform[] joints = new Transform[25];
- public List<Vector3[]> jointsSequence = new List<Vector3[]>();
- public Material boneMaterial;
- private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
- {
- { Kinect.JointType.FootLeft, Kinect.JointType.AnkleLeft },
- { Kinect.JointType.AnkleLeft, Kinect.JointType.KneeLeft },
- { Kinect.JointType.KneeLeft, Kinect.JointType.HipLeft },
- { Kinect.JointType.HipLeft, Kinect.JointType.SpineBase },
- { Kinect.JointType.FootRight, Kinect.JointType.AnkleRight },
- { Kinect.JointType.AnkleRight, Kinect.JointType.KneeRight },
- { Kinect.JointType.KneeRight, Kinect.JointType.HipRight },
- { Kinect.JointType.HipRight, Kinect.JointType.SpineBase },
- { Kinect.JointType.HandTipLeft, Kinect.JointType.HandLeft },
- { Kinect.JointType.ThumbLeft, Kinect.JointType.HandLeft },
- { Kinect.JointType.HandLeft, Kinect.JointType.WristLeft },
- { Kinect.JointType.WristLeft, Kinect.JointType.ElbowLeft },
- { Kinect.JointType.ElbowLeft, Kinect.JointType.ShoulderLeft },
- { Kinect.JointType.ShoulderLeft, Kinect.JointType.SpineShoulder },
- { Kinect.JointType.HandTipRight, Kinect.JointType.HandRight },
- { Kinect.JointType.ThumbRight, Kinect.JointType.HandRight },
- { Kinect.JointType.HandRight, Kinect.JointType.WristRight },
- { Kinect.JointType.WristRight, Kinect.JointType.ElbowRight },
- { Kinect.JointType.ElbowRight, Kinect.JointType.ShoulderRight },
- { Kinect.JointType.ShoulderRight, Kinect.JointType.SpineShoulder },
- { Kinect.JointType.SpineBase, Kinect.JointType.SpineMid },
- { Kinect.JointType.SpineMid, Kinect.JointType.SpineShoulder },
- { Kinect.JointType.SpineShoulder, Kinect.JointType.Neck },
- { Kinect.JointType.Neck, Kinect.JointType.Head },
- };
- public void AddJoints()
- {
- Vector3[] positions = new Vector3[25];
- for(int i = 0; i < joints.Length; i++)
- {
- positions[i] = joints[i].position;
- }
- jointsSequence.Add(positions);
- }
- public void Save()
- {
- if(jointsSequence.Count == 0)
- {
- Debug.Log("jointsSequence is empty");
- return;
- }
- SaveSystem.Save(jointsSequence);
- Debug.Log("Save success");
- }
- public void Load()
- {
- JointsDataSequence data = SaveSystem.Load();
- if (data == null)
- {
- Debug.Log("Load failed");
- return;
- }
- StartCoroutine(ShowJoints(data));
- Debug.Log("Load success");
- }
- private IEnumerator ShowJoints(JointsDataSequence data)
- {
- // Show joints one by one every 2 seconds
- WaitForSeconds wait = new WaitForSeconds(2);
- List<JointsData> jointsData = data.jointsDataSequence;
- int counter = 0;
- foreach(JointsData jd in jointsData)
- {
- // Create GameObject body
- GameObject body = new GameObject("Recorded Body " + counter);
- for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
- {
- // skip if head joint
- if (jt == Kinect.JointType.Head)
- continue;
- // Create GameObject cubes for joints
- GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
- LineRenderer lr = jointObj.AddComponent<LineRenderer>();
- lr.positionCount = 2;
- lr.material = boneMaterial;
- lr.startWidth = 0.3f;
- lr.endWidth = 0.3f;
- jointObj.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
- jointObj.name = jt.ToString();
- jointObj.transform.position = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
- jointObj.transform.parent = body.transform;
- }
- // Connect the joints with LineRenderer
- for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
- {
- // skip if dictionary not contains the joint or neck joint
- if (!_BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck)
- continue;
- Transform jointObj = body.transform.Find(jt.ToString());
- Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
- LineRenderer lr = jointObj.GetComponent<LineRenderer>();
- lr.SetPosition(0, jointObj.localPosition);
- lr.SetPosition(1, targetJoint.localPosition);
- }
- yield return wait;
- counter++;
- Destroy(body);
- }
- }
- }
|