Example #1
0
    //copies the transforms of the object and set it to a key frame number
    void copyDownDataAndSet(int jointID, int frameNumber)
    {
        //create new keyframe
        Transform poseObj       = gameObjectHierarchy.getObject(jointID).transform;
        Vector3   localPosition = animData.poseBase[jointID].getLocalPosition();
        Vector3   localRotation = animData.poseBase[jointID].getLocalRotationEuler();

        KeyFrame newKey = new KeyFrame(poseObj.localPosition - localPosition, poseObj.localRotation, poseObj.localScale, animData.keyFrameCount - 1);

        animData.poseBase[jointID].keyFrames[frameNumber] = newKey;
    }
Example #2
0
    void updateJoints()
    {
        //for each joint...
        for (int i = 0; i < animData.poseBase.Length; i++)
        {
            KeyFrame toKey;
            if (animData.keyFrameCount > frameCount)
            {
                //get next key
                toKey = animData.poseBase[i].keyFrames[frameCount];

                int     parentIndex   = animData.poseBase[i].parentNodeIndex;
                Vector3 localPosition = animData.poseBase[i].getLocalPosition();
                Vector3 localRotation = animData.poseBase[i].getLocalRotationEuler();

                //find delta change from localpose
                Matrix4x4 deltaMatrix = Matrix4x4.TRS(localPosition + toKey.keyPosition, toKey.keyQRotation, new Vector4(1, 1, 1, 1));

                if (parentIndex == -1)
                {
                    //is root
                    animData.poseBase[i].currentTransform = deltaMatrix;
                }
                else
                {
                    //current transform = take the parent index current transform and multiply with delta matrix
                    animData.poseBase[i].currentTransform = animData.poseBase[parentIndex].currentTransform * deltaMatrix;
                }

                animData.poseBase[i].updateNewPosition(objHierarchy.getObject(i));
            }
        }
    }
Example #3
0
    private void Update()
    {
        for (int i = 0; i < animData.poseBase.Length; i++)
        {
            animationTransformData poseresult = new animationTransformData();
            animationTransformData dataPose0  = new animationTransformData();
            animationTransformData dataPose1  = new animationTransformData();
            KeyFrame key0 = animData.poseBase[i].keyFrames[keyframe0];
            KeyFrame key1 = animData.poseBase[i].keyFrames[keyframe1];
            dataPose0.setTransformIndividual(key0.keyPosition, Quaternion.Euler(key0.keyRotation), new Vector3(1, 1, 1));
            dataPose1.setTransformIndividual(key1.keyPosition, Quaternion.Euler(key1.keyRotation), new Vector3(1, 1, 1));
            switch (blendMode)
            {
            case testBlendMode.LERP:
                poseresult = blendStatic.lerp(dataPose0, dataPose1, parameter0, usingQuaternion);
                break;

            case testBlendMode.ADD:
                poseresult = blendStatic.add(dataPose0, dataPose1, usingQuaternion);
                break;

            case testBlendMode.SCALE:
                poseresult = blendStatic.scale(new identity(), dataPose0, parameter0, usingQuaternion);
                break;

            case testBlendMode.AVERAGE:
                poseresult = blendStatic.average(new identity(), dataPose0, dataPose1, parameter0, parameter1, usingQuaternion);
                break;
            }

            int     parentIndex   = animData.poseBase[i].parentNodeIndex;
            Vector3 localPosition = animData.poseBase[i].getLocalPosition();
            Vector3 localRotation = animData.poseBase[i].getLocalRotationEuler();

            //find delta change from localpose
            Matrix4x4 deltaMatrix = Matrix4x4.TRS(localPosition + poseresult.localPosition, Quaternion.Euler(localRotation + poseresult.localRotation.eulerAngles), new Vector4(1, 1, 1, 1));

            if (parentIndex == -1)
            {
                //is root
                animData.poseBase[i].currentTransform = deltaMatrix;
            }
            else
            {
                //current transform = take the parent index current transform and multiply with delta matrix
                animData.poseBase[i].currentTransform = animData.poseBase[parentIndex].currentTransform * deltaMatrix;
            }

            animData.poseBase[i].updateNewPosition(gameObjectData.getObject(i));
        }
    }
    void blendAnimation()
    {
        bool usingQuaternion = true;

        for (int i = 0; i < currentAnim.poseBase.Length; i++)
        {
            animationTransformData poseresult = new animationTransformData();
            animationTransformData dataPose0  = new animationTransformData();
            animationTransformData dataPose1  = new animationTransformData();
            KeyFrame key0 = prevAnim.poseBase[i].keyFrames[currentFrame];
            KeyFrame key1 = currentAnim.poseBase[i].keyFrames[currentFrame];
            dataPose0.setTransformIndividual(key0.keyPosition, Quaternion.Euler(key0.keyRotation), new Vector3(1, 1, 1));
            dataPose1.setTransformIndividual(key1.keyPosition, Quaternion.Euler(key1.keyRotation), new Vector3(1, 1, 1));

            poseresult = blendStatic.lerp(dataPose0, dataPose1, transitionParameter, usingQuaternion);

            int     parentIndex   = currentAnim.poseBase[i].parentNodeIndex;
            Vector3 localPosition = currentAnim.poseBase[i].getLocalPosition();
            Vector3 localRotation = currentAnim.poseBase[i].getLocalRotationEuler();

            //find delta change from localpose
            Matrix4x4 deltaMatrix = Matrix4x4.TRS(localPosition + poseresult.localPosition, Quaternion.Euler(localRotation + poseresult.localRotation.eulerAngles), new Vector4(1, 1, 1, 1));

            if (parentIndex == -1)
            {
                //is root
                currentAnim.poseBase[i].currentTransform = deltaMatrix;
            }
            else
            {
                //current transform = take the parent index current transform and multiply with delta matrix
                currentAnim.poseBase[i].currentTransform = currentAnim.poseBase[parentIndex].currentTransform * deltaMatrix;
            }

            currentAnim.poseBase[i].updateNewPosition(objectHierarchy.getObject(i));
        }
    }
Example #5
0
    void processFile()
    {
        done = false;

        StreamReader reader = new StreamReader(path);

        //read header
        curMode = 0;
        Debug.Log(reader.ReadLine());
        Debug.Log(reader.ReadLine());
        Debug.Log(reader.ReadLine());
        Debug.Log(reader.ReadLine());
        Debug.Log(reader.ReadLine());
        Debug.Log(reader.ReadLine());
        animData.createBase(readToSpaceInt(reader.ReadLine()));            // number of segments
        animData.generateFrames(readToSpaceInt(reader.ReadLine()));        //number of frames
        animData.setFramePerSecond(readToSpaceInt(reader.ReadLine()));     //frame rate
        Debug.Log(reader.ReadLine());                                      //rotation order
        animData.setCalibrationUnit(readToSpaceString(reader.ReadLine())); //calibration units
        Debug.Log(reader.ReadLine());                                      //rotation units
        Debug.Log(reader.ReadLine());                                      //globalaxisofGravity
        Debug.Log(reader.ReadLine());                                      //bone length axis
        animData.scaleFactor = readToSpaceFloat(reader.ReadLine());        //scale factor
        //end of header

        //for setting hierarchy
        int            jointCount       = 0;                   //keep track of the where the index it is putting in the basepose
        List <string>  jointIndexList   = new List <string>(); //keeps track of the string names for easier hierarchy building/checking
        string         currentJoint     = "";                  //for keyframing
        gameObjectMain mainObjStructure = null;

        while (!done)
        {
            string textLine = "";
            if (readLine(ref textLine, ref reader, ref currentJoint))
            {
                if (curMode == currentHTRMode.SEGMENTHIERARCHY)
                {
                    string first = "", second = "";
                    parseTextToTwoBetweenTab(ref first, ref second, textLine); //find the two strings

                    //add and update joint
                    animData.poseBase[jointCount].name = first;
                    jointIndexList.Add(first);

                    //generate parent index using the jointIndexList
                    if (second != "GLOBAL")
                    {
                        animData.poseBase[jointCount].parentNodeIndex = jointIndexList.IndexOf(second);
                    }
                    else
                    {
                        animData.poseBase[jointCount].parentNodeIndex = -1;
                    }

                    jointCount++;
                }
                else if (curMode == currentHTRMode.BASE_POSITION)
                {
                    //create base pose
                    DataInput data  = superParseDataIntoInputBase(textLine);
                    int       index = jointIndexList.IndexOf(data.name);
                    animData.poseBase[index].boneLength = data.boneLength;

                    //generate local matrix using pared data
                    Matrix4x4 localMat = Matrix4x4.TRS(data.transform, Quaternion.Euler(data.rotation), new Vector4(1, 1, 1, 1));
                    animData.poseBase[index].localBaseTransform = localMat;

                    //do forward kinematics
                    int parentIndex = animData.poseBase[index].parentNodeIndex;

                    GameObject parentObj = null;
                    if (parentIndex == -1)
                    {
                        //is root
                        animData.poseBase[index].globalBaseTransform = localMat;

                        //generate joint in scene (test) https://answers.unity.com/questions/402280/how-to-decompose-a-trs-matrix.html
                        GameObject newJoint = Instantiate(jointObject, animData.poseBase[index].globalBaseTransform.GetColumn(3), Quaternion.Euler((animData.poseBase[index].globalBaseTransform.GetRow(1))));
                        newJoint.name = data.name;

                        newJoint.AddComponent <gameObjectMain>();
                        mainObjStructure = newJoint.GetComponent <gameObjectMain>();
                        mainObjStructure.newList();
                        mainObjStructure.addObject(newJoint);
                    }
                    else
                    {
                        //create global transform by taking the parent's transform and multiply with the local matrix
                        animData.poseBase[index].globalBaseTransform = (animData.poseBase[parentIndex].globalBaseTransform * localMat);
                        parentObj = mainObjStructure.getObject(parentIndex);

                        //generate joint in scene (test) https://answers.unity.com/questions/402280/how-to-decompose-a-trs-matrix.html
                        GameObject newJoint = Instantiate(jointObject, animData.poseBase[index].globalBaseTransform.GetColumn(3), Quaternion.Euler((animData.poseBase[index].globalBaseTransform.GetRow(1))), parentObj.transform);
                        newJoint.name = data.name;
                        mainObjStructure.addObject(newJoint);
                    }

                    animData.poseBase[index].currentTransform = animData.poseBase[index].globalBaseTransform;
                }
                else if (curMode == currentHTRMode.FRAMING)
                {
                    //get the keyframe and set it in
                    DataInput data  = superParseDataIntoInputKeyFrame(textLine);
                    int       index = jointIndexList.IndexOf(currentJoint);
                    animData.poseBase[index].keyFrames[data.frame].atFrame     = data.frame;
                    animData.poseBase[index].keyFrames[data.frame].keyPosition = data.transform;
                    animData.poseBase[index].keyFrames[data.frame].keyRotation = data.rotation;
                    animData.poseBase[index].keyFrames[data.frame].scale       = new Vector3(data.scaleFactor, data.scaleFactor, data.scaleFactor);
                }
            }
        }
    }