Esempio n. 1
0
        public void Parse(BinaryReader buffer)
        {
            long basePtr  = buffer.BaseStream.Position;
            uint numSlots = buffer.ReadUInt16();

            numBones = buffer.ReadByte();
            buffer.ReadByte(); // padding
            uint size         = buffer.ReadUInt32();
            uint dataOffset   = buffer.ReadUInt32() + 8;
            long slotOffset   = buffer.ReadUInt32() + 8;
            long headerOffset = slotOffset + numSlots;



            numAnimations = (headerOffset - numSlots - 16) / (numBones * 4 + 10);
            //Debug.Log("numAnimations : "+ numAnimations);
            animations = new VSAnim[numAnimations];
            for (int i = 0; i < numAnimations; i++)
            {
                animations[i] = new VSAnim(i, numBones, buffer);
            }

            int[] slots = new int[numSlots];
            for (int i = 0; i < numSlots; i++)
            {
                byte slot = buffer.ReadByte();
                if (slot >= numAnimations && slot != 255)
                {
                    Debug.Log('?');
                }
                slots[i] = slot;
            }

            for (int i = 0; i < numAnimations; i++)
            {
                animations[i].getData(buffer, basePtr, headerOffset, animations);
            }
        }
Esempio n. 2
0
        public AnimationClip[] BuildAnimationClips(GameObject model)
        {
            if (UseDebug)
            {
                Debug.Log("BuildAnimationClips : " + FileName + "(" + numAnimations + ")  in model " + model.name);
            }


            clips = new AnimationClip[numAnimations];
            int t = 0;

            for (int i = 0; i < numAnimations; i++)
            {
                AnimationClip clip = new AnimationClip();
                clip.name = FileName + "_c_" + i;

                // Translation
                int             tkl       = animations[i].transKeys.Count;
                List <NVector4> keyframes = animations[i].transKeys;

                /*
                 * Debug.Log("animations["+i+"].trans : "+ animations[i].trans.ToString());
                 * int tx = (int)animations[i].trans.x/64;
                 * int ty = (int)animations[i].trans.y/64;
                 * int tz = (int)animations[i].trans.z/64;
                 */
                float tx = 0;
                float ty = 0;
                float tz = 0;

                List <Keyframe> keysTX = new List <Keyframe>();
                List <Keyframe> keysTY = new List <Keyframe>();
                List <Keyframe> keysTZ = new List <Keyframe>();

                t = 0;
                for (int k = 0; k < tkl; k++)
                {
                    int f = (int)keyframes[k].f;
                    t += f;
                    if (keyframes[k].x == null)
                    {
                        keyframes[k].x = keyframes[k - 1].x;
                    }

                    if (keyframes[k].y == null)
                    {
                        keyframes[k].y = keyframes[k - 1].y;
                    }

                    if (keyframes[k].z == null)
                    {
                        keyframes[k].z = keyframes[k - 1].z;
                    }


                    tx += (float)keyframes[k].x / 64 * f;
                    ty += (float)keyframes[k].y / 64 * f;
                    tz += (float)keyframes[k].z / 64 * f;

                    keysTX.Add(new Keyframe((float)((t) * 0.04), tx));
                    keysTY.Add(new Keyframe((float)((t) * 0.04), ty));
                    keysTZ.Add(new Keyframe((float)((t) * 0.04), tz));
                }
                clip.SetCurve("", typeof(Transform), "localPosition.x", new AnimationCurve(keysTX.ToArray()));
                clip.SetCurve("", typeof(Transform), "localPosition.y", new AnimationCurve(keysTY.ToArray()));
                clip.SetCurve("", typeof(Transform), "localPosition.z", new AnimationCurve(keysTZ.ToArray()));

                // Bones rotation and scale
                for (int j = 0; j < numBones; j++)
                {
                    List <Keyframe> keysRX = new List <Keyframe>();
                    List <Keyframe> keysRY = new List <Keyframe>();
                    List <Keyframe> keysRZ = new List <Keyframe>();
                    List <Keyframe> keysRW = new List <Keyframe>();
                    List <Keyframe> keysSX = new List <Keyframe>();
                    List <Keyframe> keysSY = new List <Keyframe>();
                    List <Keyframe> keysSZ = new List <Keyframe>();

                    VSAnim baseAnim = (animations[i].baseAnimationId == -1) ? animations[i] : animations[animations[i].baseAnimationId];

                    if (j < animations[i].rotationKeysPerBone.Length)
                    {
                        keyframes = animations[i].rotationKeysPerBone[j];
                        Vector3 pose = baseAnim.rotationPerBone[j];
                        int     rx   = (int)pose.x * 2;
                        int     ry   = (int)pose.y * 2;
                        int     rz   = (int)pose.z * 2;
                        t = 0;
                        int kfl = animations[i].rotationKeysPerBone[j].Count;

                        for (int k = 0; k < kfl; k++)
                        {
                            int f = (int)keyframes[k].f;
                            t += f;
                            if (keyframes[k].x == null)
                            {
                                keyframes[k].x = keyframes[k - 1].x;
                            }

                            if (keyframes[k].y == null)
                            {
                                keyframes[k].y = keyframes[k - 1].y;
                            }

                            if (keyframes[k].z == null)
                            {
                                keyframes[k].z = keyframes[k - 1].z;
                            }

                            rx += ((int)keyframes[k].x * f);
                            ry += ((int)keyframes[k].y * f);
                            rz += ((int)keyframes[k].z * f);

                            Quaternion qu   = ToolBox.quatFromAxisAnle(Vector3.right, ToolBox.rot13toRad(rx));
                            Quaternion qv   = ToolBox.quatFromAxisAnle(Vector3.up, ToolBox.rot13toRad(ry));
                            Quaternion qw   = ToolBox.quatFromAxisAnle(Vector3.forward, ToolBox.rot13toRad(rz));
                            Quaternion quat = qw * qv * qu;

                            keysRX.Add(new Keyframe((float)((t) * 0.04), quat.x));
                            keysRY.Add(new Keyframe((float)((t) * 0.04), quat.y));
                            keysRZ.Add(new Keyframe((float)((t) * 0.04), quat.z));
                            keysRW.Add(new Keyframe((float)((t) * 0.04), quat.w));
                        }
                    }
                    if (j < animations[i].scaleKeysPerBone.Length)
                    {
                        Vector3         baseScale = ((animations[i].scaleFlags & 0x1) > 0) ? animations[i].scalePerBone[j] : new Vector3(64, 64, 64);
                        List <NVector4> scaleKeys = ((animations[i].scaleFlags & 0x2) > 0) ? animations[i].scaleKeysPerBone[j] : new List <NVector4>();
                        int             skl       = scaleKeys.Count;
                        if (skl == 0)
                        {
                            scaleKeys.Add(NVector4.zero);
                        }
                        skl = scaleKeys.Count;
                        Vector3 scale = baseScale / 64;
                        t = 0;
                        for (int k = 0; k < skl; k++)
                        {
                            NVector4 key = scaleKeys[k];
                            int      f   = (int)key.f;
                            if (key.x == null)
                            {
                                key.x = scaleKeys[i - 1].x;
                            }
                            if (key.y == null)
                            {
                                key.y = scaleKeys[i - 1].y;
                            }
                            if (key.z == null)
                            {
                                key.z = scaleKeys[i - 1].z;
                            }
                            t       += f;
                            scale.x += (float)key.x / 64 * f;
                            scale.y += (float)key.y / 64 * f;
                            scale.z += (float)key.z / 64 * f;

                            keysSX.Add(new Keyframe((float)((t) * 0.04), scale.x));
                            keysSY.Add(new Keyframe((float)((t) * 0.04), scale.y));
                            keysSZ.Add(new Keyframe((float)((t) * 0.04), scale.z));
                        }
                    }
                    string bonePath = ToolBox.GetGameObjectPath(ToolBox.findBoneIn("bone_" + j, model), model.name);
                    clip.SetCurve(bonePath, typeof(Transform), "localRotation.x", new AnimationCurve(keysRX.ToArray()));
                    clip.SetCurve(bonePath, typeof(Transform), "localRotation.y", new AnimationCurve(keysRY.ToArray()));
                    clip.SetCurve(bonePath, typeof(Transform), "localRotation.z", new AnimationCurve(keysRZ.ToArray()));
                    clip.SetCurve(bonePath, typeof(Transform), "localRotation.w", new AnimationCurve(keysRW.ToArray()));
                    clip.SetCurve(bonePath, typeof(Transform), "localScale.x", new AnimationCurve(keysSX.ToArray()));
                    clip.SetCurve(bonePath, typeof(Transform), "localScale.y", new AnimationCurve(keysSY.ToArray()));
                    clip.SetCurve(bonePath, typeof(Transform), "localScale.z", new AnimationCurve(keysSZ.ToArray()));
                }
                clips[i] = clip;
            }
            return(clips);
        }