123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Kinect = Windows.Kinect;
- using System;
- public class Visualizer_FadeInSeries : MonoBehaviour
- {
- public Material boneMaterial;
- public Material transparentMat;
- public BodySourceView bsv;
- private List<JointsData> jointsDataSeries = new List<JointsData>();
- private List<float> recordingTimesSeries = new List<float>();
- private float t;
- private bool beginUpdate;
- private List<GameObject> bodies = new List<GameObject>();
- void Update()
- {
- if (!beginUpdate)
- return;
- if (t >= 1)
- {
- Destroy(gameObject);
- return;
- }
- t += Time.deltaTime / 8;
- }
- public void SetData(List<JointsData> jointsDataSeries, List<float> recordingTimesSeries)
- {
- this.jointsDataSeries = jointsDataSeries;
- this.recordingTimesSeries = recordingTimesSeries;
- }
- public void ShowBody()
- {
- for (int i = 0; i < jointsDataSeries.Count; i++)
- {
- // Create GameObject body
- GameObject body = new GameObject("Recorded Body (Visualizer_FadeInSeries) " + i);
- body.transform.parent = gameObject.transform;
- bodies.Add(body);
- JointsData jd = jointsDataSeries[i];
- Vector3[] joints = HelperScript.ConvertJointsDataToVector3Array(jd);
- Vector3[] rescaledJoints = HelperScript.RescaleJoints(joints);
- for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
- {
- // Skip these joints
- if (jt == Kinect.JointType.Head || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
- || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
- || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
- continue;
- // Create GameObject cubes for joints
- GameObject jointObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
- jointObj.GetComponent<MeshRenderer>().material = transparentMat;
- // Give BoxCollider.isTrigger true and TriggerDetector to WristLeft and WristRight
- if (jt == Kinect.JointType.WristLeft || jt == Kinect.JointType.WristRight)
- {
- jointObj.GetComponent<BoxCollider>().isTrigger = true;
- jointObj.AddComponent<TriggerDetectorSeries>();
- }
- LineRenderer lr = jointObj.AddComponent<LineRenderer>();
- lr.positionCount = 2;
- lr.material = boneMaterial;
- lr.startWidth = 0.05f;
- lr.endWidth = 0.05f;
- jointObj.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
- jointObj.name = jt.ToString();
- jointObj.transform.position = rescaledJoints[(int)jt];
- jointObj.transform.parent = body.transform;
- // Don't show joint if current and previous are almost the same position
- if (i > 0)
- {
- JointsData prevJd = jointsDataSeries[i - 1];
- Vector3[] prevJoints = HelperScript.ConvertJointsDataToVector3Array(prevJd);
- Vector3[] prevRescaledJoints = HelperScript.RescaleJoints(prevJoints);
- Vector3 prev = prevRescaledJoints[(int)jt];
- float distance = (jointObj.transform.position - prev).magnitude;
- if (distance < 0.05f)
- {
- jointObj.SetActive(false);
- }
- }
- // Set alpha depends on i
- Color newColor = new Color(0.8f, 1, 0, Mathf.Lerp(0, 1, (float)(i + 1) / jointsDataSeries.Count));
- jointObj.GetComponent<MeshRenderer>().material.color = newColor;
- jointObj.GetComponent<LineRenderer>().startColor = newColor;
- jointObj.GetComponent<LineRenderer>().endColor = newColor;
- // Remove LineRenderer component from neck
- if (jt == Kinect.JointType.Neck)
- {
- Destroy(jointObj.GetComponent<LineRenderer>());
- }
- }
- // 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 other these joints
- if (!HelperScript._BoneMap.ContainsKey(jt) || jt == Kinect.JointType.Neck
- || jt == Kinect.JointType.ThumbLeft || jt == Kinect.JointType.ThumbRight
- || jt == Kinect.JointType.HandLeft || jt == Kinect.JointType.HandRight
- || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
- continue;
- Transform jointObj = body.transform.Find(jt.ToString());
- Transform targetJoint = body.transform.Find(HelperScript._BoneMap[jt].ToString());
- LineRenderer lr = jointObj.GetComponent<LineRenderer>();
- lr.SetPosition(0, jointObj.localPosition);
- lr.SetPosition(1, targetJoint.localPosition);
- }
- }
- beginUpdate = true;
- }
- private void OnDestroy()
- {
- foreach (GameObject go in bodies)
- {
- Destroy(go);
- }
- }
- }
|