// Update is called once per frame
    void Update()
    {
        timeFrame += Time.deltaTime;
        if (timeFrame >= animation.length)
        {
            timeFrame %= animation.length;
        }
        for (int i = 0; i < boneXForms.Length; i++)
        {
            TransformData tData = animation.GetTransformAt(i, timeFrame);

            /*
             * debugs[i].SetRow(0, new Vector4(tData.position.x, tData.position.y, tData.position.z));
             * debugs[i].SetRow(1, new Vector4(tData.rotation.x, tData.rotation.y, tData.rotation.z, tData.rotation.w));
             * debugs[i].SetRow(2, new Vector4(tData.scale.x, tData.scale.y, tData.scale.z));
             */
            tDat[i]       = tData;
            boneXForms[i] = Matrix4x4.TRS(tData.position, tData.rotation, tData.scale);
            debugs[i]     = boneXForms[i];
        }
        QJoint joint = animation.hierarchy;

        foreach (QJoint child in joint.children)
        {
            ApplyPoseTransform(child, joint);
        }
        boneXForms[0] = joint.inverseBindTransform * boneXForms[0];

        /*
         * for(int i = 0; i < boneXForms.Length; i++)
         * {
         *  QJoint joint = animation.FindJointByIndex(i);
         *
         *  foreach(QJoint child in joint.children)
         *  {
         *      boneXForms[child.index] =  boneXForms[child.index] * boneXForms[joint.index];
         *  }
         *
         *  boneXForms[joint.index] *= joint.inverseBindTransform;
         * }
         */
        xFormBuf.SetData(boneXForms);
        mat.SetBuffer("xforms", xFormBuf);
        for (int i = 0; i < boneXForms.Length; i++)
        {
            Quaternion rot = boneXForms[i].rotation;
            bonePlaceholders[i].transform.position = new Vector3(boneXForms[i].m03, boneXForms[i].m13, boneXForms[i].m23);
            bonePlaceholders[i].transform.rotation = rot;
            //bonePlaceholders[i].transform.localPosition = tDat[i].position;
            //bonePlaceholders[i].transform.localRotation = tDat[i].rotation;
        }
    }
Ejemplo n.º 2
0
    public void OnGUI()
    {
        clip = EditorGUILayout.ObjectField("Clip", clip, typeof(AnimationClip), false) as AnimationClip;
        mesh = EditorGUILayout.ObjectField("Model", mesh, typeof(Mesh), false) as Mesh;

        if (GUILayout.Button("Encode Animation Curves"))
        {
            if (clip != null)
            {
                Dictionary <string, List <string> > hierarchedBindings = new Dictionary <string, List <string> >();

                List <string> encodings = new List <string>();

                foreach (var binding in AnimationUtility.GetCurveBindings(clip))
                {
                    if (!hierarchedBindings.ContainsKey(binding.path))
                    {
                        List <string> bindFrames = new List <string>();
                        hierarchedBindings.Add(binding.path, bindFrames);
                    }

                    AnimationCurve curve = AnimationUtility.GetEditorCurve(clip, binding);

                    string thisJointField = string.Format("#_#{0}##{1}", binding.propertyName, curve.keys.Length);
                    hierarchedBindings[binding.path].Add(thisJointField);

                    foreach (Keyframe key in curve.keys)
                    {
                        string thisKeyFrame = string.Format("{0}*#*{1}*#*{2}*#*{3}", key.inTangent, key.value, key.outTangent, key.time);
                        hierarchedBindings[binding.path].Add(thisKeyFrame);
                    }
                }

                encodings.Add(clip.length.ToString());

                var bindingEnum = hierarchedBindings.Keys.GetEnumerator();
                while (bindingEnum.MoveNext())
                {
                    if (encodings.Count > 1)
                    {
                        encodings.Add(string.Format("&$*{0}", bindingEnum.Current));
                    }
                    else
                    {
                        encodings.Add(bindingEnum.Current);
                    }
                    var keyframes = hierarchedBindings[bindingEnum.Current];
                    foreach (string frame in keyframes)
                    {
                        encodings.Add(frame);
                    }
                }
                string path = Application.dataPath + @"\Scripts\" + clip.name + ".bagel";
                System.IO.File.WriteAllLines(path, encodings.ToArray());

                BagelLoader bagel  = new BagelLoader(mesh);
                QAnimation  myAnim = bagel.LoadBagel(path);
                for (int i = 0; i < 5; i++)
                {
                    TransformData sample = myAnim.GetTransformAt(0, myAnim.length / 5 * i);
                    Debug.Log(string.Format("Position: {0} \nRotation: {1} \nScale: {2}", sample.position.ToString(), sample.rotation.ToString(), sample.scale.ToString()));
                }
                var th = myAnim.GetType().TypeHandle;

                /*unsafe
                 * {
                 *  long size = *(*(long**)&th + 1);
                 *  Debug.Log(size);
                 * }*/
            }
        }
    }