void Update()
    {
        if (CurrentUserTracker.CurrentUser != 0)
        {
            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;
            message = "Skeleton found";

            for (int q = 0; q < typeJoint.Length; q++)
            {
                nuitrack.Joint joint       = skeleton.GetJoint(typeJoint[q]);
                Vector3        newPosition = 0.001f * joint.ToVector3();
                CreatedJoint[q].transform.localPosition = newPosition;
            }
            for (int i = 0; i < typeConnection.GetLength(0); i++)
            {
                nuitrack.Joint startJoint = skeleton.GetJoint(typeConnection[i, 0]);
                nuitrack.Joint endJoint   = skeleton.GetJoint(typeConnection[i, 1]);

                CreatedConnection[i].transform.position = startJoint.ToVector3();
                CreatedConnection[i].transform.right    = endJoint.ToVector3() - startJoint.ToVector3();
                float distance = Vector3.Distance(endJoint.ToVector3(), startJoint.ToVector3());
                CreatedConnection[i].transform.localScale = new Vector3(distance, 1f, 1f);
                Debug.Log(CreatedConnection [i].transform.position.ToString());
                connectionParticleSystem[i].transform.position = CreatedConnection[i].transform.position;
            }
        }
        else
        {
            message = "Skeleton not found";
        }
    }
Exemplo n.º 2
0
    void Update()
    {
        frameNum++;
        if (CurrentUserTracker.CurrentUser != 0)
        {
            string newData = "";
            newData += frameNum + "," + System.DateTime.Now.Hour + "" + System.DateTime.Now.Minute + System.DateTime.Now.Second + System.DateTime.Now.Millisecond;


            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;

            for (int q = 0; q < typeJoint.Length; q++)
            {
                nuitrack.Joint joint       = skeleton.GetJoint(typeJoint[q]);
                Vector3        newPosition = 0.001f * joint.ToVector3();
                CreatedJoint[q].transform.localPosition = newPosition;

                newData += "," + joint.Confidence;
                newData += "," + joint.ToVector3().x;
                newData += "," + joint.ToVector3().y;
                newData += "," + joint.ToVector3().z;
            }
            file.WriteLine(newData);
        }
        else
        {
        }
    }
Exemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        if (CurrentUserTracker.CurrentUser != 0)
        {
            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;
            joint = skeleton.GetJoint(typeJoint);
            Vector3 position = 0.001f * joint.ToVector3();

            if (joint.ToVector3().z < 800)
            {
                message = "Please step away from camera";
            }
            else if (joint.ToVector3().z > 1500)
            {
                message = "Please step towards the camera";
            }
            else
            {
                message = "";
            }
        }
        else
        {
            message = "User not found";
        }
    }
    public static Quaternion ToQuaternion(this nuitrack.Joint joint)
    {
        Vector3 jointUp      = new Vector3(joint.Orient.Matrix[1], joint.Orient.Matrix[4], joint.Orient.Matrix[7]); //Y(Up)
        Vector3 jointForward = new Vector3(joint.Orient.Matrix[2], joint.Orient.Matrix[5], joint.Orient.Matrix[8]); //Z(Forward)

        return(Quaternion.LookRotation(jointForward, jointUp));
    }
Exemplo n.º 5
0
    /// <summary>
    /// Getting skeleton data from thr sensor and updating transforms of the model bones
    /// </summary>
    void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        foreach (var riggedJoint in jointsRigged)
        {
            //Get joint from the Nuitrack
            nuitrack.Joint joint = skeleton.GetJoint(riggedJoint.Key);

            //Get modelJoint
            ModelJoint modelJoint = riggedJoint.Value;

            //Bone position
            Vector3 newPos = Quaternion.Euler(0f, 180f, 0f) * (0.001f * joint.ToVector3());
            modelJoint.bone.position = newPos;

            //Bone rotation
            Quaternion jointOrient = Quaternion.Inverse(CalibrationInfo.SensorOrientation) * (joint.ToQuaternionMirrored()) * modelJoint.baseRotOffset;
            modelJoint.bone.rotation = jointOrient;

            //Bone scale
            if (modelJoint.parentBone != null)
            {
                //Take the Transform of a parent bone
                Transform parentBone = modelJoint.parentBone;
                //calculate how many times the distance between the child bone and its parent bone has changed compared to the base distance (which was recorded at the start)
                float scaleDif = modelJoint.baseDistanceToParent / Vector3.Distance(newPos, parentBone.position);
                //change the size of the bone to the resulting value (On default bone size (1,1,1))
                parentBone.localScale = Vector3.one / scaleDif;
            }
        }
    }
    public void UpdateFace(Instances instance, nuitrack.Joint headJoint)
    {
        Vector3 headProjPosition = headJoint.Proj.ToVector3();

        faceRaw.transform.position = new Vector2(headProjPosition.x * Screen.width, Screen.height - headProjPosition.y * Screen.height);

        headRoot.localPosition = new Vector3(0, 0, -headJoint.Real.Z * 0.001f);

        Face face = instance.face;

        newRotation = baseRotation;

        if (instance.face.landmark == null)
        {
            return;
        }

        //Mouth
        faceMeshRenderer.SetBlendShapeWeight(jawOpen, blendshapeWeights.GetJawOpen(face));

        //Eyes
        faceMeshRenderer.SetBlendShapeWeight(eyeBlinkLeft, blendshapeWeights.GetEyeBlinkLeft(face));
        faceMeshRenderer.SetBlendShapeWeight(eyeBlinkRight, blendshapeWeights.GetEyeBlinkRight(face));

        //Smile
        faceMeshRenderer.SetBlendShapeWeight(mouthLeft, blendshapeWeights.GetSmile(face));
        faceMeshRenderer.SetBlendShapeWeight(mouthRight, blendshapeWeights.GetSmile(face));

        //Brows
        faceMeshRenderer.SetBlendShapeWeight(browUpLeft, blendshapeWeights.GetBrowUpLeft(face));
        faceMeshRenderer.SetBlendShapeWeight(browUpRight, blendshapeWeights.GetBrowUpRight(face));

        //Head rotation
        newRotation = baseRotation * Quaternion.Euler(face.angles.yaw, -face.angles.pitch, face.angles.roll);
    }
Exemplo n.º 7
0
    public static Quaternion ToQuaternion(this nuitrack.Joint joint)
    {
        //Vector3 jointRight =  new Vector3( joint.Orient.Matrix[0], joint.Orient.Matrix[3], joint.Orient.Matrix[6] );   //X(Right) not really needed here
        Vector3 jointUp      = new Vector3(joint.Orient.Matrix[1], joint.Orient.Matrix[4], joint.Orient.Matrix[7]);      //Y(Up)
        Vector3 jointForward = new Vector3(joint.Orient.Matrix[2], joint.Orient.Matrix[5], joint.Orient.Matrix[8]);      //Z(Forward)

        return(Quaternion.LookRotation(jointForward, jointUp));
    }
Exemplo n.º 8
0
    public void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        if (skeleton == null)
        {
            return;
        }

        if (!gameObject.activeSelf)
        {
            gameObject.SetActive(true);
        }

        for (int i = 0; i < jointsInfo.Length; i++)
        {
            nuitrack.Joint j = skeleton.GetJoint(jointsInfo[i]);
            if (j.Confidence > 0.5f)
            {
                if (!joints[jointsInfo[i]].activeSelf)
                {
                    joints[jointsInfo[i]].SetActive(true);
                }

                joints[jointsInfo[i]].transform.position = 0.001f * j.ToVector3();
                joints[jointsInfo[i]].transform.rotation = j.ToQuaternionMirrored();
            }
            else
            {
                if (joints[jointsInfo[i]].activeSelf)
                {
                    joints[jointsInfo[i]].SetActive(false);
                }
            }
        }

        for (int i = 0; i < connectionsInfo.GetLength(0); i++)
        {
            if (joints[connectionsInfo[i, 0]].activeSelf && joints[connectionsInfo[i, 1]].activeSelf)
            {
                if (!connections[i].activeSelf)
                {
                    connections[i].SetActive(true);
                }

                Vector3 diff = joints[connectionsInfo[i, 1]].transform.position - joints[connectionsInfo[i, 0]].transform.position;

                connections[i].transform.position   = joints[connectionsInfo[i, 0]].transform.position;
                connections[i].transform.rotation   = Quaternion.LookRotation(diff);
                connections[i].transform.localScale = new Vector3(1f, 1f, diff.magnitude);
            }
            else
            {
                if (connections[i].activeSelf)
                {
                    connections[i].SetActive(false);
                }
            }
        }
    }
Exemplo n.º 9
0
    public void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        if (skeleton == null)
        {
            return;
        }

        if (!gameObject.activeSelf)
        {
            gameObject.SetActive(true);
        }

        for (int i = 0; i < jointsInfo.Length; i++)
        {
            nuitrack.Joint j = skeleton.GetJoint(jointsInfo[i]);
            if (j.Confidence > 0.5f)
            {
                if (!joints[jointsInfo[i]].activeSelf)
                {
                    joints[jointsInfo[i]].SetActive(true);
                }

                joints[jointsInfo[i]].transform.position = new Vector2(j.Proj.X * Screen.width, Screen.height - j.Proj.Y * Screen.height);
            }
            else
            {
                if (joints[jointsInfo[i]].activeSelf)
                {
                    joints[jointsInfo[i]].SetActive(false);
                }
            }
        }

        for (int i = 0; i < connectionsInfo.GetLength(0); i++)
        {
            if (joints[connectionsInfo[i, 0]].activeSelf && joints[connectionsInfo[i, 1]].activeSelf)
            {
                if (!connections[i].activeSelf)
                {
                    connections[i].SetActive(true);
                }

                Vector3 diff = joints[connectionsInfo[i, 1]].transform.position - joints[connectionsInfo[i, 0]].transform.position;
                connections[i].transform.position   = joints[connectionsInfo[i, 0]].transform.position;
                connections[i].transform.right      = joints[connectionsInfo[i, 1]].transform.position - connections[i].transform.position;
                connections[i].transform.localScale = new Vector3(diff.magnitude, 1f, 1f);
            }
            else
            {
                if (connections[i].activeSelf)
                {
                    connections[i].SetActive(false);
                }
            }
        }
    }
 private void RefreshJointRotation(nuitrack.Skeleton skeleton)
 {
     foreach (var riggedJoint in jointsRigged)
     {
         nuitrack.Joint joint       = skeleton.GetJoint(riggedJoint.Key);
         ModelJoint     modelJoint  = riggedJoint.Value;
         Quaternion     jointOrient = Quaternion.Inverse(CalibrationInfo.SensorOrientation) * (joint.ToQuaternion()) * modelJoint.baseRotOffset;
         modelJoint.bone.rotation = jointOrient;
     }
 }
Exemplo n.º 11
0
    // Update is called once per frame
    void Update()
    {
        if (NuitrackManager.NumUsers > 0)
        {
            if (timer <= waitTime)
            {
                timer += Time.deltaTime;
                BoneScalingProgress.text = "Bone Scaling: In Progress...";
            }
            else if (timer >= waitTime)
            {
                BoneScalingProgress.text = "Bone Scaling: Completed";
            }

            // get ID of user
            nuitrack.User[] users         = NuitrackManager.Users;
            nuitrack.User   CurrentUser   = users[0];
            int             CurrentUserID = CurrentUser.ID;

            foreach (var riggedJoint in jointsRigged)
            {
                //Get joint from the Nuitrack
                nuitrack.Joint joint = NuitrackManager.SkeletonData.GetSkeletonByID(CurrentUserID).GetJoint(riggedJoint.Key);

                if (joint.Confidence > 0.5)                         //Currently, there are only two values of confidence: 0 (Nuitrack thinks that this isn't a joint) and 0.75 (a joint).
                {
                    ModelJoint     modeljoint  = riggedJoint.Value; //get modelJoint
                    nuitrack.Joint parentjoint = NuitrackManager.SkeletonData.GetSkeletonByID(CurrentUserID).GetJoint(modeljoint.parentJointType);

                    Vector3 newPos    = 0.001f * joint.ToVector3(); //given in mm
                    Vector3 parentPos = 0.001f * parentjoint.ToVector3();

                    // Coinvert nuitrack joint orientation to quaternion
                    Quaternion jointOrient = joint.ToQuaternion();

                    // Update Model joint to tracked orientation

                    modeljoint.bone.rotation = jointOrient * modeljoint.baseRotOffset;


                    // perform bone  scaling for 5 seconds at the start maybe?
                    if (modeljoint.parentBone != null && timer <= waitTime)
                    {
                        Debug.Log("BONE SCALING PERFORMED...........");
                        // take the transform of the parent bone
                        //Transform parentBone = modeljoint.parentBone;
                        // calculate how many times the distance between the child bone and its parent bone has changed compared to the base distances (which was recorded at the start)
                        float scaleDif = modeljoint.baseDistanceToParent / Vector3.Distance(newPos, parentPos);
                        // change the size of the bone to the resulting value
                        modeljoint.parentBone.localScale = Vector3.one / scaleDif;
                    }
                }
            }
        }
    }
Exemplo n.º 12
0
    public static Quaternion ToQuaternionMirrored(this nuitrack.Joint joint)
    {
        Vector3 jointUp      = new Vector3(-joint.Orient.Matrix[1], joint.Orient.Matrix[4], -joint.Orient.Matrix[7]); //Y(Up)
        Vector3 jointForward = new Vector3(joint.Orient.Matrix[2], -joint.Orient.Matrix[5], joint.Orient.Matrix[8]);  //Z(Forward)

        if (jointForward.magnitude < 0.01f)
        {
            return(Quaternion.identity); //should not happen
        }
        return(Quaternion.LookRotation(jointForward, jointUp));
    }
Exemplo n.º 13
0
    void Update()
    {
        if (CurrentUserTracker.CurrentUser != 0)
        {
            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;
            message = "";

            for (int q = 0; q < typeJoint.Length; q++)
            {
                nuitrack.Joint joint       = skeleton.GetJoint(typeJoint[q]);
                Vector3        newPosition = 0.001f * joint.ToVector3();
                CreatedJoint[q].transform.localPosition = newPosition;
            }

            contadorAgachamentos.text = agachamentos.ToString(); //atualiza mensagem dos agachamentos

            coordenadasJoints[0] = scalK * skeleton.GetJoint(nuitrack.JointType.Head).ToVector3();
            coordenadasJoints[1] = scalK * skeleton.GetJoint(nuitrack.JointType.Neck).ToVector3();
            coordenadasJoints[2] = scalK * skeleton.GetJoint(nuitrack.JointType.RightShoulder).ToVector3();
            coordenadasJoints[3] = scalK * skeleton.GetJoint(nuitrack.JointType.LeftShoulder).ToVector3();

            if (!isPosicaoInicialGuardada) //inicia a contagem decrescente para guardar a posição inicial e iniciar o exercício
            {
                timerComecar      -= Time.deltaTime;;
                timerMensagem.text = timerComecar.ToString();

                if (timerComecar < 0)
                {
                    GuardaPosicaoInicial(coordenadasJoints);
                }

                return;
            }

            if (agachamentos > 0)
            {
                VerificaAgachamento(coordenadasJoints);

                if (isAgachado)
                {
                    ContaAgachamento(coordenadasJoints);
                }
            }
            else
            {
                DescansaProximaSerie();
            }
        }
        else
        {
            message = "Skeleton not found!";
        }
    }
Exemplo n.º 14
0
    void LateUpdate()
    {
        if (CurrentUserTracker.CurrentSkeleton != null)
        {
            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;
            transform.position = Quaternion.Euler(0f, 180f, 0f) * (0.001f * skeleton.GetJoint(rootJoint).ToVector3());

            foreach (SimpleJoint item in joints)
            {
                nuitrack.Joint joint = skeleton.GetJoint(item.nuitrackJoint);

                Quaternion rotation = Quaternion.Inverse(CalibrationInfo.SensorOrientation) * joint.ToQuaternionMirrored() * item.Offset;
                item.Bone.rotation = rotation;
            }
        }
    }
Exemplo n.º 15
0
    void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        //Vector3 torsoPos = Quaternion.Euler(0f, 0f, 0f) * (0.001f * skeleton.GetJoint(nuitrack.JointType.Torso).ToVector3());
        //transform.position = torsoPos;

        foreach (var riggedJoint in jointsRigged)
        {
            nuitrack.Joint joint      = skeleton.GetJoint(riggedJoint.Key);
            ModelJoint     modelJoint = riggedJoint.Value;

            Quaternion jointOrient =
                Quaternion.Inverse(CalibrationInfo.SensorOrientation)
                * (joint.ToQuaternionMirrored())
                * modelJoint.baseRotOffset;
            modelJoint.bone.rotation = jointOrient;
        }
    }
    /// <summary>
    /// Getting skeleton data from sensor and update model bones transforms
    /// </summary>
    void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        if (skeleton == null)
        {
            return;
        }

        if (!firstOffset)
        {
            firstOffset = true;
            StartCoroutine(CalculateOffset());
        }

        foreach (var riggedJoint in jointsRigged)
        {
            nuitrack.Joint j = skeleton.GetJoint(riggedJoint.Key);
            if (j.Confidence > 0.5f)
            {
                //Bone position
                Vector3 newPos = (q180) * (Vector3.up * CalibrationInfo.FloorHeight + CalibrationInfo.SensorOrientation * (0.001f * j.ToVector3())) * scale + basePivotOffset;

                ModelJoint rj = riggedJoint.Value;

                //Bone scale
                if (rj.parentBone != null)
                {
                    Transform bone = rj.parentBone;
                    bone.parent = bone.root;
                    float scaleDif = rj.baseDistanceToParent / Vector3.Distance(newPos, bone.position);
                    bone.localScale = Vector3.one / scaleDif;
                }

                rj.bone.position = newPos;

                if (j.Type != nuitrack.JointType.None)
                {
                    Quaternion jointOrient = CalibrationInfo.SensorOrientation * (j.ToQuaternionMirrored());
                    rj.bone.rotation = q0 * Quaternion.Inverse(CalibrationInfo.SensorOrientation) * jointOrient * rj.baseRotOffset;
                }
            }
        }

        leftHandPos  = jointsRigged[nuitrack.JointType.LeftWrist].bone.position;
        rightHandPos = jointsRigged[nuitrack.JointType.RightWrist].bone.position;
    }
Exemplo n.º 17
0
 // Update is called once per frame
 void Update()
 {
     if (CurrentUserTracker.CurrentUser != 0)
     {
         msg = "Skeleton Found";
         nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;
         for (int q = 0; q < typeJoint.Length; q++)
         {
             nuitrack.Joint joint  = skeleton.GetJoint(typeJoint[q]);
             Vector3        newPos = 0.001f * joint.ToVector3();
             CreatedJoint[q].transform.localPosition = newPos;
         }
     }
     else
     {
         msg = "Skeleton not found";
     }
 }
Exemplo n.º 18
0
    public void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        if (skeleton == null)
        {
            return;
        }

        for (int i = 0; i < jointsInfo.Length; i++)
        {
            nuitrack.Joint j = skeleton.GetJoint(jointsInfo[i]);
            if (j.Confidence > 0.5f)
            {
                joints[jointsInfo[i]].SetActive(true);
                joints[jointsInfo[i]].transform.position = new Vector2(j.Proj.X * Screen.width, Screen.height - j.Proj.Y * Screen.height);
                exportJoints[jointsInfo[i]] = joints[jointsInfo[i]].transform.position;
                //exportJoints[jointsInfoStr[i]] = joints[jointsInfo[i]].transform.position;
                //exportOrientations[jointsInfoStr[i]] = joints[jointsInfo[i]].Orient;
            }
            else
            {
                joints[jointsInfo[i]].SetActive(false);
            }
        }

        for (int i = 0; i < connectionsInfo.GetLength(0); i++)
        {
            GameObject startJoint = joints[connectionsInfo[i, 0]];
            GameObject endJoint   = joints[connectionsInfo[i, 1]];

            if (startJoint.activeSelf && endJoint.activeSelf)
            {
                connections[i].SetActive(true);

                connections[i].transform.position = startJoint.transform.position;
                connections[i].transform.right    = endJoint.transform.position - startJoint.transform.position;
                float distance = Vector3.Distance(endJoint.transform.position, startJoint.transform.position);
                connections[i].transform.localScale = new Vector3(distance, 1f, 1f);
            }
            else
            {
                connections[i].SetActive(false);
            }
        }
    }
Exemplo n.º 19
0
    void Update()
    {
        string json = nuitrack.Nuitrack.GetInstancesJson();

        faceInfo = JsonUtility.FromJson <JsonInfo>(json.Replace("\"\"", "[]"));

        faces = faceInfo.Instances;
        for (int i = 0; i < faceControllers.Count; i++)
        {
            if (faces != null && i < faces.Length)
            {
                int  id          = 0;
                Face currentFace = faces[i].face;
                // Pass the face to FaceController
                faceControllers[i].SetFace(currentFace);
                faceControllers[i].gameObject.SetActive(true);

                // IDs of faces and skeletons are the same
                id = faces[i].id;

                nuitrack.Skeleton skeleton = null;
                if (NuitrackManager.SkeletonData != null)
                {
                    skeleton = NuitrackManager.SkeletonData.GetSkeletonByID(id);
                }

                if (skeleton != null)
                {
                    nuitrack.Joint head = skeleton.GetJoint(nuitrack.JointType.Head);

                    faceControllers[i].transform.position = new Vector2(head.Proj.X * Screen.width, Screen.height - head.Proj.Y * Screen.height);
                    //stretch the face to fit the rectangle
                    if (currentFace.rectangle != null)
                    {
                        faceControllers[i].transform.localScale = new Vector2(currentFace.rectangle.width * Screen.width, currentFace.rectangle.height * Screen.height);
                    }
                }
            }
            else
            {
                faceControllers[i].gameObject.SetActive(false);
            }
        }
    }
Exemplo n.º 20
0
    void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        //Calculate the model position: take the Torso position and invert movement along the Z axis
        Vector3 torsoPos = Quaternion.Euler(0f, 180f, 0f) * (0.001f * skeleton.GetJoint(nuitrack.JointType.Torso).ToVector3());

        transform.position = torsoPos + basePivotOffset;

        foreach (var riggedJoint in jointsRigged)
        {
            //Get joint from the Nuitrack
            nuitrack.Joint joint = skeleton.GetJoint(riggedJoint.Key);

            ModelJoint modelJoint = riggedJoint.Value;

            //Calculate the model bone rotation: take the mirrored joint orientation, add a basic rotation of the model bone, invert movement along the Z axis
            Quaternion jointOrient = Quaternion.Inverse(CalibrationInfo.SensorOrientation) * (joint.ToQuaternionMirrored()) * modelJoint.baseRotOffset;
            modelJoint.bone.rotation = jointOrient;
        }
    }
Exemplo n.º 21
0
    void Update()
    {
        if (CurrentUserTracker.CurrentUser != 0)
        {
            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;
            message = "Skeleton found";//通过CurrentUserTracker.CurrentSkeleton动态获取USER的信息

            //遍历信息到各个虚拟关节点信息上
            for (int q = 0; q < typeJoint.Length; q++)
            {
                nuitrack.Joint joint       = skeleton.GetJoint(typeJoint[q]);
                Vector3        newPosition = 0.001f * joint.ToVector3();
                CreatedJoint[q].transform.localPosition = newPosition;
            }
        }
        else
        {
            message = "Skeleton not found";
        }
    }
Exemplo n.º 22
0
 // Update is called once per frame
 void Update()
 {
     // Check user presence in frame
     if (CurrentUserTracker.CurrentUser != 0)
     {
         message = "Skeleton found!";
         nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton; // Get current user skeleton info
         for (int i = 0; i < typeJoint.Length; i++)
         {
             // Get current joint data
             nuitrack.Joint joint       = skeleton.GetJoint(typeJoint[i]);
             Vector3        newPosition = 0.001f * joint.ToVector3(); // Convert to milimeters
             CreatedJoint[i].transform.localPosition = newPosition;
         }
     }
     else
     {
         message = "Skeleton not found!";
     }
 }
Exemplo n.º 23
0
    public static Quaternion ToQuaternionMirrored(this nuitrack.Joint joint)
    {
        /* Debug:
         * if (joint.Type == nuitrack.JointType.Torso)
         * {
         *  Debug.Log("Torso matrix: " +
         *  joint.Orient.Matrix[0].ToString() + ",  " + joint.Orient.Matrix[3].ToString() + ",  " + joint.Orient.Matrix[6].ToString() + "; " +
         *  joint.Orient.Matrix[1].ToString() + ",  " + joint.Orient.Matrix[4].ToString() + ",  " + joint.Orient.Matrix[7].ToString() + "; " +
         *  joint.Orient.Matrix[2].ToString() + ",  " + joint.Orient.Matrix[5].ToString() + ",  " + joint.Orient.Matrix[8].ToString());
         * }*/

        //Vector3 jointRight =  new Vector3(  joint.Orient.Matrix[0], -joint.Orient.Matrix[3],  joint.Orient.Matrix[6] );   //X(Right) not really needed here
        Vector3 jointUp      = new Vector3(-joint.Orient.Matrix[1], joint.Orient.Matrix[4], -joint.Orient.Matrix[7]);       //Y(Up)
        Vector3 jointForward = new Vector3(joint.Orient.Matrix[2], -joint.Orient.Matrix[5], joint.Orient.Matrix[8]);        //Z(Forward)

        if (jointForward.magnitude < 0.01f)
        {
            return(Quaternion.identity);                                //should not happen
        }
        return(Quaternion.LookRotation(jointForward, jointUp));
    }
    }// end OnDestroy

    // Update is called once per frame
    void Update()
    {
        float initMousePos = Input.GetAxis("Mouse X");

        if (CurrentUserTracker.CurrentUser != 0) // If there is a user in frame...
        {
            // Get the user's skeleton from nuitrack's scripts.
            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;

            // Get the right and left joint positions.
            nuitrack.Joint rightJoint = skeleton.GetJoint(typeJoint[0]);
            Vector3        rightPos   = rightJoint.ToVector3();

            nuitrack.Joint leftJoint = skeleton.GetJoint(typeJoint[1]);
            Vector3        leftPos   = leftJoint.ToVector3();

            ///// Tracking user with camera. //////

            // Get right hand positions
            Vector3 rightNewPos = new Vector3(rightPos.x + camera.transform.position.x, rightPos.y + camera.transform.position.y, ZPosition);
            if (orthographic)
            {
                rightNewPos.x = rightNewPos.x / 100;
                rightNewPos.y = rightNewPos.y / 100;
            }

            rightHand.transform.position = rightNewPos;
            //Debug.Log("Right: " + rightNewPos);

            // Get left hand positions
            Vector3 leftNewPos = new Vector3(leftPos.x + camera.transform.position.x, leftPos.y + camera.transform.position.y, ZPosition);
            if (orthographic)
            {
                leftNewPos.x = leftNewPos.x / 100;
                leftNewPos.y = leftNewPos.y / 100;
            }
            leftHand.transform.position = leftNewPos;
            //Debug.Log("Left: " + leftNewPos);
        } // end if user is in frame
    }     // end Update
Exemplo n.º 25
0
 public static Vector3 ToVector3(this nuitrack.Joint joint)
 {
     return(new Vector3(joint.Real.X, joint.Real.Y, joint.Real.Z));
 }
    void ProcessSkeletons(nuitrack.SkeletonData skeletonData)
    {
        if (skeletonData == null)
        {
            HideAllSkeletons();
            return;
        }
        //Debug.Log("NumUsers: " + skeletonData.NumUsers.ToString());

        int[] skelIds = new int[skeletonsRoots.Keys.Count];
        skeletonsRoots.Keys.CopyTo(skelIds, 0);

        for (int i = 0; i < skelIds.Length; i++)
        {
            if (skeletonData.GetSkeletonByID(skelIds[i]) == null)
            {
                skeletonsRoots[skelIds[i]].SetActive(false);
            }
        }

        foreach (nuitrack.Skeleton skeleton in skeletonData.Skeletons)
        {
            if (!skeletonsRoots.ContainsKey(skeleton.ID)) // if don't have gameObjects for skeleton ID, create skeleton gameobjects (root, joints and connections)
            {
                GameObject skelRoot = new GameObject();
                skelRoot.name = "Root_" + skeleton.ID.ToString();

                skeletonsRoots.Add(skeleton.ID, skelRoot);

                Dictionary <nuitrack.JointType, GameObject> skelJoints = new Dictionary <nuitrack.JointType, GameObject>();


                for (int i = 0; i < jointsInfo.Length; i++)
                {
                    GameObject joint = (GameObject)Instantiate(jointPrefab, Vector3.zero, Quaternion.identity);
                    skelJoints.Add(jointsInfo[i], joint);
                    joint.transform.parent = skelRoot.transform;
                    joint.SetActive(false);
                }

                joints.Add(skeleton.ID, skelJoints);

                GameObject[] skelConnections = new GameObject[connectionsInfo.GetLength(0)];

                for (int i = 0; i < skelConnections.Length; i++)
                {
                    GameObject conn = (GameObject)Instantiate(connectionPrefab, Vector3.zero, Quaternion.identity);
                    skelConnections[i]    = conn;
                    conn.transform.parent = skelRoot.transform;
                    conn.SetActive(false);
                }

                connections.Add(skeleton.ID, skelConnections);
            }

            if (!skeletonsRoots[skeleton.ID].activeSelf)
            {
                skeletonsRoots[skeleton.ID].SetActive(true);
            }

            for (int i = 0; i < jointsInfo.Length; i++)
            {
                nuitrack.Joint j = skeleton.GetJoint(jointsInfo[i]);
                if (j.Confidence > 0.5f)
                {
                    if (!joints[skeleton.ID][jointsInfo[i]].activeSelf)
                    {
                        joints[skeleton.ID][jointsInfo[i]].SetActive(true);
                    }

                    joints[skeleton.ID][jointsInfo[i]].transform.position = 0.001f * new Vector3(j.Real.X, j.Real.Y, j.Real.Z);

                    //skel.Joints[i].Orient.Matrix:
                    // 0,       1,      2,
                    // 3,       4,      5,
                    // 6,       7,      8
                    // -------
                    // right(X),  up(Y),    forward(Z)

                    //Vector3 jointRight =  new Vector3(  j.Orient.Matrix[0],  j.Orient.Matrix[3],  j.Orient.Matrix[6] );
                    Vector3 jointUp      = new Vector3(j.Orient.Matrix[1], j.Orient.Matrix[4], j.Orient.Matrix[7]);
                    Vector3 jointForward = new Vector3(j.Orient.Matrix[2], j.Orient.Matrix[5], j.Orient.Matrix[8]);
                    joints[skeleton.ID][jointsInfo[i]].transform.rotation = Quaternion.LookRotation(jointForward, jointUp);
                }
                else
                {
                    if (joints[skeleton.ID][jointsInfo[i]].activeSelf)
                    {
                        joints[skeleton.ID][jointsInfo[i]].SetActive(false);
                    }
                }
            }

            for (int i = 0; i < connectionsInfo.GetLength(0); i++)
            {
                if (joints[skeleton.ID][connectionsInfo[i, 0]].activeSelf && joints[skeleton.ID][connectionsInfo[i, 1]].activeSelf)
                {
                    if (!connections[skeleton.ID][i].activeSelf)
                    {
                        connections[skeleton.ID][i].SetActive(true);
                    }

                    Vector3 diff = joints[skeleton.ID][connectionsInfo[i, 1]].transform.position - joints[skeleton.ID][connectionsInfo[i, 0]].transform.position;

                    connections[skeleton.ID][i].transform.position   = joints[skeleton.ID][connectionsInfo[i, 0]].transform.position;
                    connections[skeleton.ID][i].transform.rotation   = Quaternion.LookRotation(diff);
                    connections[skeleton.ID][i].transform.localScale = new Vector3(1f, 1f, diff.magnitude);
                }
                else
                {
                    if (connections[skeleton.ID][i].activeSelf)
                    {
                        connections[skeleton.ID][i].SetActive(false);
                    }
                }
            }
        }
    }
    void ProcessSkeleton(nuitrack.Skeleton skeleton)
    {
        if (skeleton == null)
        {
            return;
        }

        if (headTransform != null)
        {
#if UNITY_IOS
            headTransform.position = headDirectionTransform.rotation * neckHMDOffset + (rotate180 ? q180 : q0) * (Vector3.up * CalibrationInfo.FloorHeight + CalibrationInfo.SensorOrientation * (0.001f * skeleton.GetJoint(nuitrack.JointType.Neck).ToVector3())) + basePivotOffset;
#else
            headTransform.position = (rotate180 ? q180 : q0) * (Vector3.up * CalibrationInfo.FloorHeight + CalibrationInfo.SensorOrientation * (0.001f * skeleton.GetJoint(nuitrack.JointType.Head).ToVector3())) + basePivotOffset;
#endif

            basePivot = (rotate180 ? q180 : q0) * (Vector3.up * CalibrationInfo.FloorHeight + CalibrationInfo.SensorOrientation * (0.001f * skeleton.GetJoint(nuitrack.JointType.Waist).ToVector3())) + basePivotOffset;
        }

        if (!skeletonRoot.activeSelf)
        {
            skeletonRoot.SetActive(true);
        }

        for (int i = 0; i < jointsInfo.Length; i++)
        {
            nuitrack.Joint j = skeleton.GetJoint(jointsInfo[i]);
            if (j.Confidence > 0.5f)
            {
                if (!joints[jointsInfo[i]].activeSelf)
                {
                    joints[jointsInfo[i]].SetActive(true);
                }

                joints[jointsInfo[i]].transform.position = (rotate180 ? q180 : q0) * (Vector3.up * CalibrationInfo.FloorHeight + CalibrationInfo.SensorOrientation * (0.001f * j.ToVector3())) + basePivotOffset;
                joints[jointsInfo[i]].transform.rotation = (rotate180 ? q180 : q0) * CalibrationInfo.SensorOrientation * j.ToQuaternionMirrored();

                leftHandPos  = (rotate180 ? q180 : q0) * (Vector3.up * CalibrationInfo.FloorHeight + CalibrationInfo.SensorOrientation * (0.001f * skeleton.GetJoint(nuitrack.JointType.LeftHand).ToVector3())) + basePivotOffset;
                rightHandPos = (rotate180 ? q180 : q0) * (Vector3.up * CalibrationInfo.FloorHeight + CalibrationInfo.SensorOrientation * (0.001f * skeleton.GetJoint(nuitrack.JointType.RightHand).ToVector3())) + basePivotOffset;
            }
            else
            {
                if (joints[jointsInfo[i]].activeSelf)
                {
                    joints[jointsInfo[i]].SetActive(false);
                }
            }
        }

        for (int i = 0; i < connectionsInfo.GetLength(0); i++)
        {
            if (joints[connectionsInfo[i, 0]].activeSelf && joints[connectionsInfo[i, 1]].activeSelf)
            {
                if (!connections[i].activeSelf)
                {
                    connections[i].SetActive(true);
                }

                Vector3 diff = joints[connectionsInfo[i, 1]].transform.position - joints[connectionsInfo[i, 0]].transform.position;

                connections[i].transform.position   = joints[connectionsInfo[i, 0]].transform.position;
                connections[i].transform.rotation   = Quaternion.LookRotation(diff);
                connections[i].transform.localScale = new Vector3(1f, 1f, diff.magnitude);
            }
            else
            {
                if (connections[i].activeSelf)
                {
                    connections[i].SetActive(false);
                }
            }
        }
    }
Exemplo n.º 28
0
    void Update()
    {
        if (CurrentUserTracker.CurrentUser != 0)
        {
            nuitrack.Skeleton skeleton = CurrentUserTracker.CurrentSkeleton;
            message = "Skeleton found";

            for (int q = 0; q < typeJoint.Length; q++)
            {
                nuitrack.Joint joint       = skeleton.GetJoint(typeJoint[q]);
                Vector3        newPosition = 0.001f * joint.ToVector3();
                CreatedJoint[q].transform.localPosition = newPosition;

                if (q == 0)
                {
                    UpwardParticleSystem[0].transform.localPosition = newPosition;
                }
                else if (q == 3)
                {
                    UpwardParticleSystem[1].transform.localPosition = newPosition;
                }
                else if (q == 4)
                {
                    UpwardParticleSystem[2].transform.localPosition = newPosition;
                }
                else if (q == 5)
                {
                    UpwardParticleSystem[3].transform.localPosition = newPosition;
                }
                else if (q == 13)
                {
                    UpwardParticleSystem[4].transform.localPosition = newPosition;
                }
                else if (q == 14)
                {
                    UpwardParticleSystem[5].transform.localPosition = newPosition;
                }
                else if (q == 15)
                {
                    UpwardParticleSystem[6].transform.localPosition = newPosition;
                }
            }
            ConnectJoints(0, 1, 0);
            ConnectJoints(1, 2, 1);
            ConnectJoints(3, 4, 3);
            ConnectJoints(4, 5, 4);
            ConnectJoints(6, 7, 6);
            ConnectJoints(7, 8, 7);
            ConnectJoints(9, 10, 9);
            ConnectJoints(10, 12, 10);
            ConnectJoints(13, 14, 13);
            ConnectJoints(14, 15, 14);

            foreach (GameObject ps_clone in psSlider)
            {
                ParticleSystem psssss = ps_clone.GetComponentInChildren <ParticleSystem>();
                var            em     = psssss.emission;
                em.enabled = true;
                em.rate    = Emission.value;
            }
            foreach (GameObject ps_clone in psuSlider)
            {
                ParticleSystem psssss = ps_clone.GetComponentInChildren <ParticleSystem>();
                var            em     = psssss.emission;
                em.enabled = true;
                em.rate    = EmissionUpwards.value;
            }
            //Emission.text = Emission.value.ToString("0.0");
            //EmissionUpwards.text = EmissionUpwards.value.ToString("0.0");
            textComponent.text = Mathf.Round(Emission.value * 100).ToString();
        }
        else
        {
            message = "Skeleton not found";
        }
    }
    void Update()
    {
        if (CurrentUserTracker.CurrentUser == 0)
        {
            return;
        }

        //Run once
        if (!versionChecked)
        {
            nuitrack.Joint curCamJoint = CurrentUserTracker.CurrentSkeleton.GetJoint(nuitrack.JointType.None);

//			Debug.Log ("Real:" + curCamJoint.Real.X + " " + curCamJoint.Real.Y + " " + curCamJoint.Real.Z + " **** conf:" + curCamJoint.Confidence); //on ios

            Vector3 camVec = Vector3.zero;

            if (curCamJoint.Confidence != 0)
            {
                camVec = new Vector3(curCamJoint.Orient.Matrix[1], curCamJoint.Orient.Matrix[4], curCamJoint.Orient.Matrix[7]);
            }

            if (camVec.magnitude > 0.9) //The norm should be equal to 1.
            {
                Debug.Log("VERSION with camera joint");
                cameraJointVersion = true;
            }
            else
            {
                Debug.Log("VERSION without camera joint");
            }
            versionChecked = true;
        }

        if (!firstCheck && versionChecked)
        {
            if (cameraJointVersion)
            {
                FindObjectOfType <TPoseCalibration>().onSuccess += NativeRecenter;
            }

//			Debug.Log("cameraJointVersion: " + cameraJointVersion + CurrentUserTracker.CurrentSkeleton.GetJoint(nuitrack.JointType.None).ToQuaternion());

            firstCheck = true;
        }

        if (cameraJointVersion)
        {
            //Vector3 cameraPosition = 0.001f * (TPoseCalibration.SensorOrientation * CurrentUserTracker.CurrentSkeleton.GetJoint(nuitrack.JointType.None).ToVector3());
        #if UNITY_IOS
            Quaternion cameraRotation = Quaternion.identity;
        #else
            Quaternion cameraRotation = TPoseCalibration.SensorOrientation * CurrentUserTracker.CurrentSkeleton.GetJoint(nuitrack.JointType.None).ToQuaternion();
        #endif

            //transform.position = 0.001f * (TPoseCalibration.SensorOrientation * CurrentUserTracker.CurrentSkeleton.GetJoint(nuitrack.JointType.None).ToVector3());

            if (GameVersion.currentPlatform == Platform.Default)
            {
                cameraRotation.eulerAngles = cameraRotation.eulerAngles + new Vector3(0, 180, 0);
                vrCamera.eulerAngles       = cameraRotation.eulerAngles;
            }

            if (GameVersion.currentPlatform == Platform.GearVR)
            {
                //headBase.position = cameraPosition;

                Vector3 gazeDirection = gearvrCamera.forward;
                Vector3 gazeDirHead   = headBase.InverseTransformVector(gazeDirection);

                Quaternion currentRotation = Quaternion.Euler(0f, Mathf.Atan2(gazeDirHead.x, gazeDirHead.z) * Mathf.Rad2Deg, 0f);

                Vector3    vicovrDirection = cameraRotation * Vector3.forward;
                Quaternion yPartRotation   = Quaternion.Euler(0f, Mathf.Atan2(vicovrDirection.x, vicovrDirection.z) * Mathf.Rad2Deg, 0f);

                Quaternion correction = yPartRotation * Quaternion.Inverse(currentRotation);
                float      delta      = Mathf.Atan2((correction * Vector3.forward).x, (correction * Vector3.forward).z) * Mathf.Rad2Deg;
                Quaternion corrected  = yCorrection.localRotation * correction * Quaternion.Euler(0, 180, 0);
                float      slerpCoef  = 0.01f;

                if (Mathf.Abs(delta) > 40.0)
                {
                    slerpCoef = 1.0f;
                }

                yCorrection.localRotation = Quaternion.Slerp(yCorrection.localRotation, corrected, slerpCoef);
            }
        }

        if (GameVersion.currentPlatform == Platform.IOS)         //or can use if(curcamJoint.Confidence == 0)
        {
            transform.localRotation = sensorRotation.Rotation;
//			Debug.Log ("Ios camera rotation");
        }
    }
Exemplo n.º 30
0
 public Joint(nuitrack.Joint joint)
 {
     RawJoint = joint;
 }