예제 #1
0
    /// Returns the local translation for a bone, scaled according to its scale and the scale of the bones above it
    /// in the heirarchy, apart from the root translation
    public Output_GetSegmentLocalTranslation GetScaledSegmentTranslation(string SubjectName, string SegmentName)
    {
        double[] OutputScale = new double[3];
        OutputScale[0] = OutputScale[1] = OutputScale[2] = 1.0;

        // Check first whether we have a parent, as we don't wish to scale the root node's position
        Output_GetSegmentParentName Parent = GetSegmentParentName(SubjectName, SegmentName);

        string CurrentSegmentName = SegmentName;

        if (Parent.Result == Result.Success)
        {
            do
            {
                // We have a parent. First get our scale, and then iterate through the nodes above us
                Output_GetSegmentStaticScale Scale = GetSegmentScale(SubjectName, CurrentSegmentName);
                if (Scale.Result == Result.Success)
                {
                    for (uint i = 0; i < 3; ++i)
                    {
                        if (Scale.Scale[i] != 0.0)
                        {
                            OutputScale[i] = OutputScale[i] * Scale.Scale[i];
                        }
                    }
                }

                Parent = GetSegmentParentName(SubjectName, CurrentSegmentName);
                if (Parent.Result == Result.Success)
                {
                    CurrentSegmentName = Parent.SegmentName;
                }
            } while (Parent.Result == Result.Success);
        }

        Output_GetSegmentLocalTranslation Translation = GetSegmentTranslation(SubjectName, SegmentName);

        if (Translation.Result == Result.Success)
        {
            for (uint i = 0; i < 3; ++i)
            {
                Translation.Translation[i] = Translation.Translation[i] / OutputScale[i];
            }
        }
        return(Translation);
    }
        // map the orientation back for forward

        private void ApplyBoneTransform(Transform Bone)
        {
            string BoneName = strip(Bone.gameObject.name);
            // update the bone transform from the data stream
            Output_GetSegmentLocalRotationQuaternion ORot = Client.GetSegmentRotation(SubjectName, BoneName);

            if (ORot.Result == Result.Success)
            {
                // mapping back to default data stream axis
                //Quaternion Rot = new Quaternion(-(float)ORot.Rotation[2], -(float)ORot.Rotation[0], (float)ORot.Rotation[1], (float)ORot.Rotation[3]);
                Quaternion Rot = new Quaternion((float)ORot.Rotation[0], (float)ORot.Rotation[1], (float)ORot.Rotation[2], (float)ORot.Rotation[3]);
                // mapping right hand to left hand flipping x
                Bone.localRotation = new Quaternion(Rot.x, -Rot.y, -Rot.z, Rot.w);
            }

            Output_GetSegmentLocalTranslation OTran;

            if (IsScaled)
            {
                OTran = Client.GetScaledSegmentTranslation(SubjectName, BoneName);
            }
            else
            {
                OTran = Client.GetSegmentTranslation(SubjectName, BoneName);
            }

            if (OTran.Result == Result.Success)
            {
                //Vector3 Translate = new Vector3(-(float)OTran.Translation[2] * 0.001f, -(float)OTran.Translation[0] * 0.001f, (float)OTran.Translation[1] * 0.001f);
                Vector3 Translate = new Vector3((float)OTran.Translation[0] * 0.001f, (float)OTran.Translation[1] * 0.001f, (float)OTran.Translation[2] * 0.001f);
                Bone.localPosition = new Vector3(-Translate.x, Translate.y, Translate.z);
            }

            // If there's a scale for this subject in the datastream, apply it here.
            if (IsScaled)
            {
                Output_GetSegmentStaticScale OScale = Client.GetSegmentScale(SubjectName, BoneName);
                if (OScale.Result == Result.Success)
                {
                    Bone.localScale = new Vector3((float)OScale.Scale[0], (float)OScale.Scale[1], (float)OScale.Scale[2]);
                }
            }
        }