Exemplo n.º 1
0
    private void iterateFrameForChildren(BVHParser.BVHBone currBone, Transform currTransform, float frame, bool normalizeSkeleton)
    {
        int        currFrame = (int)frame;
        float      t         = frame - (float)currFrame;
        Quaternion rot1      = fromEulerYXZ(new Vector3(currBone.channels[3].values[currFrame],
                                                        currBone.channels[4].values[currFrame],
                                                        currBone.channels[5].values[currFrame]));
        Quaternion rot2 = fromEulerYXZ(new Vector3(currBone.channels[3].values[currFrame + 1],
                                                   currBone.channels[4].values[currFrame + 1],
                                                   currBone.channels[5].values[currFrame + 1]));

        currTransform.localRotation = Quaternion.Slerp(rot1, rot2, t);
        if (currTransform.gameObject.name != currBone.name)
        {
            print("missmatch found");
        }

        if (normalizeSkeleton)
        {
            currTransform.localPosition = new Vector3(-currBone.offsetX, currBone.offsetY, currBone.offsetZ) / this.size;
        }
        else
        {
            currTransform.localPosition = new Vector3(-currBone.offsetX, currBone.offsetY, currBone.offsetZ);
        }

        int i = 0;

        foreach (BVHParser.BVHBone currChildBone in currBone.children)
        {
            iterateFrameForChildren(currChildBone, currTransform.GetChild(i), frame, normalizeSkeleton);
            i++;
        }
    }
Exemplo n.º 2
0
    private void iterateChildBone(BVHParser.BVHBone currBone, GameObject parent)
    {
        GameObject currObj = new GameObject();

        currObj.name                    = currBone.name;
        currObj.transform.parent        = parent.transform;
        currObj.transform.localPosition = new Vector3(-currBone.offsetX, currBone.offsetY, currBone.offsetZ);

        if (currObj.transform.position.y < minpos)
        {
            minpos = currObj.transform.position.y;
        }

        if (currObj.transform.position.y > maxpos)
        {
            maxpos = currObj.transform.position.y;
        }

        transforms.Add(currObj.transform);

        foreach (BVHParser.BVHBone currChildBone in currBone.children)
        {
            iterateChildBone(currChildBone, currObj);
        }
    }
Exemplo n.º 3
0
    private void BlendBVH(ref BVHParser.BVHBone node1, BVHParser.BVHBone node2)
    {
        node1.offsetX = ((BlendPersent) * node1.offsetX + (1.0f - BlendPersent) * node2.offsetX) / 2.0f;
        node1.offsetY = ((BlendPersent) * node1.offsetY + (1.0f - BlendPersent) * node2.offsetY) / 2.0f;
        node1.offsetZ = ((BlendPersent) * node1.offsetZ + (1.0f - BlendPersent) * node2.offsetZ) / 2.0f;

        for (int k = 0; k < 3; k++)
        {
            if ((node1.channels[k].enabled == true) && (node2.channels[k].enabled == true))
            {
                int shortFrame = node1.channels[k].values.Length;
                if (shortFrame > node2.channels[k].values.Length)
                {
                    shortFrame = node2.channels[k].values.Length;
                }

                for (int j = 0; j < shortFrame; j++)
                {
                    float tempAngle = ((BlendPersent) * node1.channels[k].values[j] + (1.0f - BlendPersent) * node2.channels[k].values[j]) / 2.0f;
                    node1.channels[k].values[j] = tempAngle;
                }
            }
        }


        if ((node1.channels[3].enabled == true) && (node2.channels[3].enabled == true))
        {
            int shortFrame = node1.channels[3].values.Length;
            if (shortFrame > node2.channels[3].values.Length)
            {
                shortFrame = node2.channels[3].values.Length;
            }
            for (int j = 0; j < shortFrame; j++)
            {
                Vector3    eulerBVH1 = new Vector3(wrapAngle(node1.channels[3].values[j]), wrapAngle(node1.channels[4].values[j]), wrapAngle(node1.channels[5].values[j]));
                Vector3    eulerBVH2 = new Vector3(wrapAngle(node2.channels[3].values[j]), wrapAngle(node2.channels[4].values[j]), wrapAngle(node2.channels[5].values[j]));
                Quaternion rot1      = fromEulerZXY(eulerBVH1);
                Quaternion rot2      = fromEulerZXY(eulerBVH2);

                //rot1 = rot1 * rot2;
                rot1 = Quaternion.Lerp(rot1, rot2, BlendPersent);
                Vector3 temp = eulerZXY(new Vector4(rot1.x, rot1.y, rot1.z, rot1.w));
                node1.channels[3].values[j] = wrapAngle(temp.x);
                node1.channels[4].values[j] = wrapAngle(temp.y);
                node1.channels[5].values[j] = wrapAngle(temp.z);
            }
        }



        //for children
        for (int i = 0; i < node1.children.Count; i++)
        {
            BVHParser.BVHBone nodeCh1 = node1.children[i];
            BVHParser.BVHBone nodeCh2 = node2.children[i];
            BlendBVH(ref nodeCh1, nodeCh2);
        }
    }
Exemplo n.º 4
0
    public void createFromBVH(BVHParser parser)
    {
        minpos = 0;
        maxpos = 0;
        size   = 0;

        BVHParser.BVHBone currBone = parser.root;
        rootObj      = new GameObject();
        rootObj.name = currBone.name;
        rootObj.transform.localPosition = new Vector3(-currBone.offsetX, currBone.offsetY, currBone.offsetZ);

        transforms.Add(rootObj.transform);

        foreach (BVHParser.BVHBone currChildBone in currBone.children)
        {
            iterateChildBone(currChildBone, rootObj);
        }

        size = maxpos - minpos;
    }
Exemplo n.º 5
0
    private void getCurves(string path, BVHParser.BVHBone node, Transform bone, bool first)
    {
        bool posX = false;
        bool posY = false;
        bool posZ = false;
        bool rotX = false;
        bool rotY = false;
        bool rotZ = false;

        float[][]    values        = new float[6][];
        Keyframe[][] keyframes     = new Keyframe[7][];
        string[]     props         = new string[7];
        Transform    nodeTransform = getBoneByName(node.name, bone, first);

        if (path != prefix)
        {
            path += "/";
        }
        if (rootBone != targetAvatar.transform || !first)
        {
            path += nodeTransform.name;
        }

        // This needs to be changed to gather from all channels into two vector3, invert the coordinate system transformation and then make keyframes from it
        for (int channel = 0; channel < 6; channel++)
        {
            if (!node.channels[channel].enabled)
            {
                continue;
            }

            switch (channel)
            {
            case 0:
                posX           = true;
                props[channel] = "localPosition.x";
                break;

            case 1:
                posY           = true;
                props[channel] = "localPosition.y";
                break;

            case 2:
                posZ           = true;
                props[channel] = "localPosition.z";
                break;

            case 3:
                rotX           = true;
                props[channel] = "localRotation.x";
                break;

            case 4:
                rotY           = true;
                props[channel] = "localRotation.y";
                break;

            case 5:
                rotZ           = true;
                props[channel] = "localRotation.z";
                break;

            default:
                channel = -1;
                break;
            }
            if (channel == -1)
            {
                continue;
            }

            keyframes[channel] = new Keyframe[frames];
            values[channel]    = node.channels[channel].values;
            if (rotX && rotY && rotZ && keyframes[6] == null)
            {
                keyframes[6] = new Keyframe[frames];
                props[6]     = "localRotation.w";
            }
        }

        float time = 0f;

        if (posX && posY && posZ)
        {
            Vector3 offset;
            if (blender)
            {
                offset = new Vector3(-node.offsetX, node.offsetZ, -node.offsetY);
            }
            else
            {
                offset = new Vector3(-node.offsetX, node.offsetY, node.offsetZ);
            }
            for (int i = 0; i < frames; i++)
            {
                time += 1f / frameRate;
                keyframes[0][i].time = time;
                keyframes[1][i].time = time;
                keyframes[2][i].time = time;
                if (blender)
                {
                    keyframes[0][i].value = -values[0][i];
                    keyframes[1][i].value = values[2][i];
                    keyframes[2][i].value = -values[1][i];
                }
                else
                {
                    keyframes[0][i].value = -values[0][i];
                    keyframes[1][i].value = values[1][i];
                    keyframes[2][i].value = values[2][i];
                }
                if (first)
                {
                    Vector3 bvhPosition = bone.transform.parent.InverseTransformPoint(new Vector3(keyframes[0][i].value, keyframes[1][i].value, keyframes[2][i].value) + targetAvatar.transform.position + offset);
                    keyframes[0][i].value = bvhPosition.x * targetAvatar.transform.localScale.x;
                    keyframes[1][i].value = bvhPosition.y * targetAvatar.transform.localScale.y;
                    keyframes[2][i].value = bvhPosition.z * targetAvatar.transform.localScale.z;
                }
            }
            if (first)
            {
                clip.SetCurve(path, typeof(Transform), props[0], new AnimationCurve(keyframes[0]));
                clip.SetCurve(path, typeof(Transform), props[1], new AnimationCurve(keyframes[1]));
                clip.SetCurve(path, typeof(Transform), props[2], new AnimationCurve(keyframes[2]));
            }
            else
            {
                Debug.LogWarning("Position information on bones other than the root bone is currently not supported and has been ignored. If you exported this file from Blender, please tick the \"Root Translation Only\" option next time.");
            }
        }

        time = 0f;
        if (rotX && rotY && rotZ)
        {
            Quaternion oldRotation = bone.transform.rotation;
            for (int i = 0; i < frames; i++)
            {
                Vector3    eulerBVH = new Vector3(wrapAngle(values[3][i]), wrapAngle(values[4][i]), wrapAngle(values[5][i]));
                Quaternion rot      = fromEulerZXY(eulerBVH);
                if (blender)
                {
                    keyframes[3][i].value = rot.x;
                    keyframes[4][i].value = -rot.z;
                    keyframes[5][i].value = rot.y;
                    keyframes[6][i].value = rot.w;
                    //rot2 = new Quaternion(rot.x, -rot.z, rot.y, rot.w);
                }
                else
                {
                    keyframes[3][i].value = rot.x;
                    keyframes[4][i].value = -rot.y;
                    keyframes[5][i].value = -rot.z;
                    keyframes[6][i].value = rot.w;
                    //rot2 = new Quaternion(rot.x, -rot.y, -rot.z, rot.w);
                }
                if (first)
                {
                    bone.transform.rotation = new Quaternion(keyframes[3][i].value, keyframes[4][i].value, keyframes[5][i].value, keyframes[6][i].value);
                    keyframes[3][i].value   = bone.transform.localRotation.x;
                    keyframes[4][i].value   = bone.transform.localRotation.y;
                    keyframes[5][i].value   = bone.transform.localRotation.z;
                    keyframes[6][i].value   = bone.transform.localRotation.w;
                }

                /*Vector3 euler = rot2.eulerAngles;
                 *
                 * keyframes[3][i].value = wrapAngle(euler.x);
                 * keyframes[4][i].value = wrapAngle(euler.y);
                 * keyframes[5][i].value = wrapAngle(euler.z);*/

                time += 1f / frameRate;
                keyframes[3][i].time = time;
                keyframes[4][i].time = time;
                keyframes[5][i].time = time;
                keyframes[6][i].time = time;
            }
            bone.transform.rotation = oldRotation;
            clip.SetCurve(path, typeof(Transform), props[3], new AnimationCurve(keyframes[3]));
            clip.SetCurve(path, typeof(Transform), props[4], new AnimationCurve(keyframes[4]));
            clip.SetCurve(path, typeof(Transform), props[5], new AnimationCurve(keyframes[5]));
            clip.SetCurve(path, typeof(Transform), props[6], new AnimationCurve(keyframes[6]));
        }

        foreach (BVHParser.BVHBone child in node.children)
        {
            getCurves(path, child, nodeTransform, false);
        }
    }
Exemplo n.º 6
0
    public int setToFrame(float frame, bool centerAnimation, bool alignHips, bool normalizeSkeleton)
    {
        if (bvhParser != null)
        {
            int   currFrame = (int)frame;
            float t         = frame - (float)currFrame;

            BVHParser.BVHBone currBone = bvhParser.root;

            if (!alignHips)
            {
                Quaternion rot1 = fromEulerYXZ(new Vector3(currBone.channels[3].values[currFrame],
                                                           currBone.channels[4].values[currFrame],
                                                           currBone.channels[5].values[currFrame]));
                Quaternion rot2 = fromEulerYXZ(new Vector3(currBone.channels[3].values[currFrame + 1],
                                                           currBone.channels[4].values[currFrame + 1],
                                                           currBone.channels[5].values[currFrame + 1]));
                root.localRotation = Quaternion.Slerp(rot1, rot2, t);
            }
            else
            {
                root.localRotation = new Quaternion();
            }

            if (!centerAnimation)
            {
                Vector3 pos1 = new Vector3(-currBone.channels[0].values[currFrame] - currBone.offsetX,
                                           currBone.channels[1].values[currFrame] + currBone.offsetY,
                                           currBone.channels[2].values[currFrame] + currBone.offsetZ);
                Vector3 pos2 = new Vector3(-currBone.channels[0].values[currFrame + 1] - currBone.offsetX,
                                           currBone.channels[1].values[currFrame + 1] + currBone.offsetY,
                                           currBone.channels[2].values[currFrame + 1] + currBone.offsetZ);
                root.localPosition = Vector3.Lerp(pos1, pos2, t);
            }
            else
            {
                root.localPosition = new Vector3();
            }

            if (normalizeSkeleton)
            {
                root.localPosition = root.localPosition / this.size;
            }


            int i = 0;

            foreach (BVHParser.BVHBone currChildBone in currBone.children)
            {
                iterateFrameForChildren(currChildBone, root.GetChild(i), frame, normalizeSkeleton);
                i++;
            }

            if (centerAnimation)
            {
                Transform head = root.Find("LowerBack/Spine/Spine1/Neck/Neck1/Head");
                if (head != null)
                {
                    this.transform.position -= head.position;// + this.transform.position;
                }
            }

            return(currFrame);
        }
        return(0);
    }
Exemplo n.º 7
0
    private void getCurves(string path, BVHParser.BVHBone node, Transform bone, bool first)
    {
        bool posX = false;
        bool posY = false;
        bool posZ = false;
        bool rotX = false;
        bool rotY = false;
        bool rotZ = false;

        float[][]    values        = new float[6][];
        Keyframe[][] keyframes     = new Keyframe[7][];
        string[]     props         = new string[7];
        Transform    nodeTransform = getBoneByName(node.name, bone, first);

        if (path != prefix)
        {
            path += "/";
        }
        if (rootBone != targetAvatar.transform || !first)
        {
            path += nodeTransform.name;
        }

        // This needs to be changed to gather from all channels into two vector3, invert the coordinate system transformation and then make keyframes from it
        for (int channel = 0; channel < 6; channel++)
        {
            if (!node.channels[channel].enabled)
            {
                continue;
            }

            switch (channel)
            {
            case 0:
                posX           = true;
                props[channel] = "localPosition.x";
                break;

            case 1:
                posY           = true;
                props[channel] = "localPosition.y";
                break;

            case 2:
                posZ           = true;
                props[channel] = "localPosition.z";
                break;

            case 3:
                rotX           = true;
                props[channel] = "localEulerAnglesBaked.x";
                props[channel] = "localRotation.x";
                break;

            case 4:
                rotY           = true;
                props[channel] = "localEulerAnglesBaked.y";
                props[channel] = "localRotation.y";
                break;

            case 5:
                rotZ           = true;
                props[channel] = "localEulerAnglesBaked.z";
                props[channel] = "localRotation.z";
                break;

            default:
                channel = -1;
                break;
            }
            if (channel == -1)
            {
                continue;
            }

            keyframes[channel] = new Keyframe[frames];
            values[channel]    = node.channels[channel].values;
            if (rotX && rotY && rotZ && keyframes[6] == null)
            {
                keyframes[6] = new Keyframe[frames];
                props[6]     = "localRotation.w";
            }
        }

        float time = 0f;

        if (posX && posY && posZ)
        {
            for (int i = 0; i < frames; i++)
            {
                time += frameTime / 1000f;
                keyframes[0][i].time = time;
                keyframes[1][i].time = time;
                keyframes[2][i].time = time;
                if (blender)
                {
                    keyframes[0][i].value = -values[0][i];
                    keyframes[1][i].value = values[2][i];
                    keyframes[2][i].value = -values[1][i];
                }
                else
                {
                    keyframes[0][i].value = -values[0][i];
                    keyframes[1][i].value = values[1][i];
                    keyframes[2][i].value = values[2][i];
                }
            }
            clip.SetCurve(path, typeof(Transform), props[0], new AnimationCurve(keyframes[0]));
            clip.SetCurve(path, typeof(Transform), props[1], new AnimationCurve(keyframes[1]));
            clip.SetCurve(path, typeof(Transform), props[2], new AnimationCurve(keyframes[2]));
        }

        time = 0f;
        if (rotX && rotY && rotZ)
        {
            for (int i = 0; i < frames; i++)
            {
                Vector3    eulerBVH = new Vector3(wrapAngle(values[3][i]), wrapAngle(values[4][i]), wrapAngle(values[5][i]));
                Quaternion rot      = fromEulerZXY(eulerBVH);
                if (blender)
                {
                    keyframes[3][i].value = rot.x;
                    keyframes[4][i].value = -rot.z;
                    keyframes[5][i].value = rot.y;
                    keyframes[6][i].value = rot.w;
                    //rot2 = new Quaternion(rot.x, -rot.z, rot.y, rot.w);
                }
                else
                {
                    keyframes[3][i].value = rot.x;
                    keyframes[4][i].value = -rot.y;
                    keyframes[5][i].value = -rot.z;
                    keyframes[6][i].value = rot.w;
                    //rot2 = new Quaternion(rot.x, -rot.y, -rot.z, rot.w);
                }

                /*Vector3 euler = rot2.eulerAngles;
                 *
                 * keyframes[3][i].value = wrapAngle(euler.x);
                 * keyframes[4][i].value = wrapAngle(euler.y);
                 * keyframes[5][i].value = wrapAngle(euler.z);*/

                time += frameTime / 1000f;
                keyframes[3][i].time = time;
                keyframes[4][i].time = time;
                keyframes[5][i].time = time;
                keyframes[6][i].time = time;
            }
            clip.SetCurve(path, typeof(Transform), props[3], new AnimationCurve(keyframes[3]));
            clip.SetCurve(path, typeof(Transform), props[4], new AnimationCurve(keyframes[4]));
            clip.SetCurve(path, typeof(Transform), props[5], new AnimationCurve(keyframes[5]));
            clip.SetCurve(path, typeof(Transform), props[6], new AnimationCurve(keyframes[6]));
        }

        foreach (BVHParser.BVHBone child in node.children)
        {
            getCurves(path, child, nodeTransform, false);
        }
    }