Esempio n. 1
0
        public List <NVector4> ReadKeys(BinaryReader buffer)
        {
            List <NVector4> keys = new List <NVector4>();

            keys.Add(NVector4.zero); // Unity need this for root bone i think
            int f = 0;

            while (true)
            {
                NVector4 key = ReadKey(buffer);
                if (key == null)
                {
                    break;
                }
                keys.Add(key);
                if (key.f.HasValue)
                {
                    f += (int)key.f;
                }

                if (f >= length - 1)
                {
                    break;
                }
            }
            return(keys);
        }
Esempio n. 2
0
        private NVector4 ReadKey(BinaryReader buffer)
        {
            byte code = buffer.ReadByte();

            if (code == 0x00)
            {
                return(null);
            }

            NVector4 key = new NVector4();

            if ((code & 0xe0) > 0)
            {
                // number of frames, byte case

                key.f = code & 0x1f;

                if (key.f == 0x1f)
                {
                    key.f = 0x20 + buffer.ReadByte();
                }
                else
                {
                    key.f = 1 + key.f;
                }
            }
            else
            {
                // number of frames, half word case

                key.f = code & 0x3;

                if (key.f == 0x3)
                {
                    key.f = 4 + buffer.ReadByte();
                }
                else
                {
                    key.f = 1 + key.f;
                }

                // half word values

                code = (byte)(code << 3);

                short h = BitConverter.ToInt16(ToolBox.EndianSwitcher(buffer.ReadBytes(2)), 0);

                if ((h & 0x4) > 0)
                {
                    key.x = h >> 3;
                    code  = (byte)(code & 0x60);

                    if ((h & 0x2) > 0)
                    {
                        key.y = BitConverter.ToInt16(ToolBox.EndianSwitcher(buffer.ReadBytes(2)), 0);
                        code  = (byte)(code & 0xa0);
                    }

                    if ((h & 0x1) > 0)
                    {
                        key.z = BitConverter.ToInt16(ToolBox.EndianSwitcher(buffer.ReadBytes(2)), 0);
                        code  = (byte)(code & 0xc0);
                    }
                }
                else if ((h & 0x2) > 0)
                {
                    key.y = h >> 3;
                    code  = (byte)(code & 0xa0);

                    if ((h & 0x1) > 0)
                    {
                        key.z = BitConverter.ToInt16(ToolBox.EndianSwitcher(buffer.ReadBytes(2)), 0);
                        code  = (byte)(code & 0xc0);
                    }
                }
                else if ((h & 0x1) > 0)
                {
                    key.z = h >> 3;
                    code  = (byte)(code & 0xc0);
                }
            }

            // byte values (fallthrough)

            if ((code & 0x80) > 0)
            {
                if (key.x != null)
                {
                    Debug.Log("Expected undefined x in SEQ animation data");
                }

                key.x = buffer.ReadSByte();
            }

            if ((code & 0x40) > 0)
            {
                if (key.y != null)
                {
                    Debug.Log("Expected undefined y in SEQ animation data");
                }

                key.y = buffer.ReadSByte();
            }

            if ((code & 0x20) > 0)
            {
                if (key.z != null)
                {
                    Debug.Log("Expected undefined z in SEQ animation data");
                }

                key.z = buffer.ReadSByte();
            }
            //Debug.Log(key.ToString());
            return(key);
        }
Esempio n. 3
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);
        }