Browse Source

record joints in a list

now able to record the joints sequence and load to show them every 2 seconds
Kenkart 2 years ago
parent
commit
3b6c53d68b

+ 2 - 4
Assets/Scripts/JointsData.cs

@@ -9,11 +9,9 @@ public class JointsData
     public List<float> jointsPositionsY = new List<float>();
     public List<float> jointsPositionsZ = new List<float>();
 
-    public JointsData(PlayerReplay playerReplay)
+    public JointsData(Transform[] jointsData)
     {
-        Transform[] joints = playerReplay.joints;
-
-        foreach(Transform t in joints)
+        foreach(Transform t in jointsData)
         {
             jointsPositionsX.Add(t.position.x);
             jointsPositionsY.Add(t.position.y);

+ 18 - 0
Assets/Scripts/JointsDataSequence.cs

@@ -0,0 +1,18 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+[System.Serializable]
+public class JointsDataSequence
+{
+    public List<JointsData> jointsDataSequence = new List<JointsData>();
+
+    public JointsDataSequence(List<Transform[]> jointsSequence)
+    {
+        foreach(Transform[] tArray in jointsSequence)
+        {
+            JointsData jointsData = new JointsData(tArray);
+            jointsDataSequence.Add(jointsData);
+        }
+    }
+}

+ 11 - 0
Assets/Scripts/JointsDataSequence.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: f7fbf9a6262b0b74ca82b7de5c79593b
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 61 - 46
Assets/Scripts/PlayerReplay.cs

@@ -5,7 +5,9 @@ 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<Transform[]> jointsSequence = new List<Transform[]>();
     public Material boneMaterial;
 
     private Dictionary<Kinect.JointType, Kinect.JointType> _BoneMap = new Dictionary<Kinect.JointType, Kinect.JointType>()
@@ -40,22 +42,25 @@ public class PlayerReplay : MonoBehaviour
         { Kinect.JointType.Neck, Kinect.JointType.Head },
     };
 
+    public void AddJoints()
+    {
+        jointsSequence.Add(joints);
+    }
 
-    // (need to implement controller click to trigger save)
     public void Save()
     {
-        if(joints[0] == null)
+        if(jointsSequence.Count == 0)
         {
-            Debug.Log("joints have not been assigned");
+            Debug.Log("jointsSequence is empty");
             return;
         }
-        SaveSystem.Save(this);
+        SaveSystem.Save(jointsSequence);
         Debug.Log("Save success");
     }
 
     public void Load()
     {
-        JointsData data = SaveSystem.Load();
+        JointsDataSequence data = SaveSystem.Load();
 
         if (data == null)
         {
@@ -63,50 +68,60 @@ public class PlayerReplay : MonoBehaviour
             return;
         }
 
-        // Create GameObject body
-        GameObject body = new GameObject("Recorded Body");
-        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(data.jointsPositionsX[(int)jt], data.jointsPositionsY[(int)jt], data.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());
-            LineRenderer lr = jointObj.GetComponent<LineRenderer>();
-
+        StartCoroutine(ShowJoints(data));
 
-            Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
-
-            lr.SetPosition(0, jointObj.localPosition);
-            lr.SetPosition(1, targetJoint.localPosition);
+        Debug.Log("Load success");
+    }
 
+    private IEnumerator ShowJoints(JointsDataSequence data)
+    {
+        // Show joints every 2 seconds
+        WaitForSeconds wait = new WaitForSeconds(2);
+        List<JointsData> jointsData = data.jointsDataSequence;
 
+        foreach(JointsData jd in jointsData)
+        {
+            // Create GameObject body
+            GameObject body = new GameObject("Recorded Body");
+            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());
+                LineRenderer lr = jointObj.GetComponent<LineRenderer>();
+
+
+                Transform targetJoint = body.transform.Find(_BoneMap[jt].ToString());
+
+                lr.SetPosition(0, jointObj.localPosition);
+                lr.SetPosition(1, targetJoint.localPosition);
+            }
+
+            yield return wait;
         }
-
-
-
-        Debug.Log("Load success");
     }
 }

+ 4 - 4
Assets/Scripts/SaveSystem.cs

@@ -8,24 +8,24 @@ public static class SaveSystem
 {
     private static string path = Application.persistentDataPath + "/joints.sav";
 
-    public static void Save(PlayerReplay playerReplay)
+    public static void Save(List<Transform[]> jointsSequence)
     {
         BinaryFormatter formatter = new BinaryFormatter();
         FileStream stream = new FileStream(path, FileMode.Create);
 
-        JointsData data = new JointsData(playerReplay);
+        JointsDataSequence data = new JointsDataSequence(jointsSequence);
         formatter.Serialize(stream, data);
         stream.Close();
     }
 
-    public static JointsData Load()
+    public static JointsDataSequence Load()
     {
         if (File.Exists(path))
         {
             BinaryFormatter formatter = new BinaryFormatter();
             FileStream stream = new FileStream(path, FileMode.Open);
 
-            JointsData data = formatter.Deserialize(stream) as JointsData;
+            JointsDataSequence data = formatter.Deserialize(stream) as JointsDataSequence;
             stream.Close();
             return data;
         } else

+ 8 - 12
Assets/Scripts/ViveInput.cs

@@ -9,29 +9,25 @@ public class ViveInput : MonoBehaviour
 
     private SteamVR_Action_Boolean grabPinch;
     private SteamVR_Action_Boolean grabGrib;
-
-
-    //private void Awake()
-    //{
-    //    actionBoolean = SteamVR_Actions._default.GrabPinch;
-    //}
-
-    //// Start is called before the first frame update
-    //void Start()
-    //{
-    //    actionSet.Activate(SteamVR_Input_Sources.Any, 0, true);
-    //}
+    private SteamVR_Action_Boolean teleport;
 
     private void Start()
     {
         grabPinch = SteamVR_Actions.default_GrabPinch;
         grabGrib = SteamVR_Actions.default_GrabGrip;
+        teleport = SteamVR_Actions.default_Teleport;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (grabPinch.GetStateDown(SteamVR_Input_Sources.Any))
+        {
+            playerReplay.AddJoints();
+        }
+
+        // TODO: need to test
+        if (teleport.GetStateDown(SteamVR_Input_Sources.Any))
         {
             playerReplay.Save();
         }