Пример #1
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);
                }
            }
        }
    }