using System; 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 jointsSequence = new List(); public List recordingTimes = new List(); public Material boneMaterial; public Material transparentMat; private Dictionary _BoneMap = new Dictionary() { { 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 }, }; private GameObject body; private bool beginAnimation; private float t; private void Update() { // TODO: wrong, put it to new class if (!beginAnimation) return; t += Time.deltaTime / 2; Color newColor = new Color(1, 1, 1, Mathf.Lerp(0, 1, t)); foreach (MeshRenderer mr in body.GetComponentsInChildren()) { mr.material.color = newColor; } foreach (LineRenderer lr in body.GetComponentsInChildren()) { lr.startColor = newColor; lr.endColor = newColor; } } public void AddJoints(float recordingTime) { Vector3[] positions = new Vector3[25]; for (int i = 0; i < joints.Length; i++) { positions[i] = joints[i].position; } jointsSequence.Add(positions); recordingTimes.Add(recordingTime); } public void Save() { if (jointsSequence.Count == 0) { Debug.Log("jointsSequence is empty"); return; } SaveSystem.Save(jointsSequence, recordingTimes); Debug.Log("Save success"); } public void Load() { JointsDataSequence data = SaveSystem.Load(); if (data == null) { Debug.Log("Load failed"); return; } ShowJoints(data); Debug.Log("Load success"); } private void ShowJoints(JointsDataSequence data) { List jointsData = data.jointsDataSequence; List recordingTimes = data.recordingTimes; // Filtered datas according to distance List newJointsData = new List(); List newRecordingTimes = new List(); JointsData prevJd = jointsData[0]; // Add first pose newJointsData.Add(jointsData[0]); newRecordingTimes.Add(recordingTimes[0]); for (int i = 1; i < jointsData.Count; i++) { JointsData jd = jointsData[i]; for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++) { // Skip if head joint if (jt == Kinect.JointType.Head) continue; Vector3 prevJointPosition = new Vector3(prevJd.jointsPositionsX[(int)jt], prevJd.jointsPositionsY[(int)jt], prevJd.jointsPositionsZ[(int)jt]); Vector3 jointPosition = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]); float distance = Vector3.Distance(prevJointPosition, jointPosition); // If a joint is bigger than certain distance, add it to the replay, else ignore it // TODO: need to test distance value if (distance >= 2) { newJointsData.Add(jd); newRecordingTimes.Add(recordingTimes[i]); prevJd = jd; continue; } } } // TODO: visualize need to test StartCoroutine(Visualize(newJointsData, newRecordingTimes)); } private IEnumerator Visualize(List newJointsData, List newRecordingTimes) { for (int i = 0; i < newJointsData.Count; i++) { float waitTime = 1; if (i < newRecordingTimes.Count - 1) { waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i]; } WaitForSeconds wait = new WaitForSeconds(waitTime); // Create GameObject body GameObject body = new GameObject("Recorded Body " + i); 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(); 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(); JointsData jd = newJointsData[i]; 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(); lr.SetPosition(0, jointObj.localPosition); lr.SetPosition(1, targetJoint.localPosition); } yield return wait; Destroy(body); } } private IEnumerator VisualizeFadeIn(List newJointsData, List newRecordingTimes) { for (int i = 0; i < newJointsData.Count; i++) { float waitTime = 1; if (i < newRecordingTimes.Count - 1) { waitTime = newRecordingTimes[i + 1] - newRecordingTimes[i]; } WaitForSeconds wait = new WaitForSeconds(waitTime); // TODO: create new class to make every body run fade in yield return wait; Destroy(body); } } }