示例#1
0
        private List <AnimationClip> GenerateClips(bool split)
        {
            List <AnimationClip> clips = new List <AnimationClip>();


            if (split)
            {
                for (int f = 0, F = this.frame_count; f < F; f++)
                {
                    var clip = new AnimationClip();
                    clip.frameRate = 1;



                    for (int n = 0, N = this.nodes.Length; n < N; n++)
                    {
                        var pose_translation_frame = new Keyframe[] {
                            new Keyframe(0, this.keyframes[0, n].position.x * Constants.RESCALE_MULTIPLY),
                            new Keyframe(0, this.keyframes[0, n].position.z * Constants.RESCALE_MULTIPLY),
                            new Keyframe(0, this.keyframes[0, n].position.y * Constants.RESCALE_MULTIPLY)
                        };

                        var pose_rotation_frame = new Keyframe[] {
                            new Keyframe(0, this.keyframes[0, n].rotation.x),
                            new Keyframe(0, this.keyframes[0, n].rotation.z),
                            new Keyframe(0, this.keyframes[0, n].rotation.y),
                            new Keyframe(0, this.keyframes[0, n].rotation.w)
                        };


                        var translation_frame = new Keyframe[] {
                            new Keyframe(1, this.keyframes[f, n].position.x * Constants.RESCALE_MULTIPLY),
                            new Keyframe(1, this.keyframes[f, n].position.z * Constants.RESCALE_MULTIPLY),
                            new Keyframe(1, this.keyframes[f, n].position.y * Constants.RESCALE_MULTIPLY)
                        };

                        var rotation_frame = new Keyframe[] {
                            new Keyframe(1, this.keyframes[f, n].rotation.x),
                            new Keyframe(1, this.keyframes[f, n].rotation.z),
                            new Keyframe(1, this.keyframes[f, n].rotation.y),
                            new Keyframe(1, this.keyframes[f, n].rotation.w)
                        };

                        string relativePath = GetNodePath(n);

                        clip.SetCurve(
                            relativePath,
                            typeof(Transform),
                            "localPosition.x",
                            new AnimationCurve(new Keyframe[] { pose_translation_frame[0], translation_frame[0] })
                            );

                        clip.SetCurve(
                            relativePath,
                            typeof(Transform),
                            "localPosition.y",
                            new AnimationCurve(new Keyframe[] { pose_translation_frame[1], translation_frame[1] })
                            );

                        clip.SetCurve(
                            relativePath,
                            typeof(Transform),
                            "localPosition.z",
                            new AnimationCurve(new Keyframe[] { pose_translation_frame[2], translation_frame[2] })
                            );

                        clip.SetCurve(
                            relativePath,
                            typeof(Transform),
                            "localRotation.x",
                            new AnimationCurve(new Keyframe[] { pose_rotation_frame[0], rotation_frame[0] })
                            );

                        clip.SetCurve(
                            relativePath,
                            typeof(Transform),
                            "localRotation.y",
                            new AnimationCurve(new Keyframe[] { pose_rotation_frame[1], rotation_frame[1] })
                            );

                        clip.SetCurve(
                            relativePath,
                            typeof(Transform),
                            "localRotation.z",
                            new AnimationCurve(new Keyframe[] { pose_rotation_frame[2], rotation_frame[2] })
                            );

                        clip.SetCurve(
                            relativePath,
                            typeof(Transform),
                            "localRotation.w",
                            new AnimationCurve(new Keyframe[] { pose_rotation_frame[3], rotation_frame[3] })
                            );
                    }
                    AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(clip);
                    settings.additiveReferencePoseClip = clip;
                    settings.additiveReferencePoseTime = 0;


                    AnimationUtility.SetAnimationClipSettings(clip, settings);
                    clips.Add(clip);
                }
            }
            else
            {
                var clip = new AnimationClip();
                clip.frameRate = this.frame_rate;
                for (int n = 0, N = this.nodes.Length; n < N; n++)
                {
                    Vector3[]    positions = new Vector3[this.frame_count];
                    Quaternion[] rotations = new Quaternion[this.frame_count];

                    for (int f = 0, F = this.frame_count; f < F; f++)
                    {
                        positions[f] = this.keyframes[f, n].position;
                        rotations[f] = this.keyframes[f, n].rotation;
                    }

                    var translation_frames = positions.AsEnumerable().Select((e, t) => new Keyframe[] {
                        new Keyframe((float)t / this.frame_count, e.x * Constants.RESCALE_MULTIPLY),
                        new Keyframe((float)t / this.frame_count, e.z * Constants.RESCALE_MULTIPLY),
                        new Keyframe((float)t / this.frame_count, e.y * Constants.RESCALE_MULTIPLY)
                    });

                    var rotation_frames = rotations.AsEnumerable().Select((e, t) => new Keyframe[] {
                        new Keyframe((float)t / this.frame_count, e.x),
                        new Keyframe((float)t / this.frame_count, e.z),
                        new Keyframe((float)t / this.frame_count, e.y),
                        new Keyframe((float)t / this.frame_count, e.w)
                    });

                    string relativePath = GetNodePath(n);

                    clip.SetCurve(
                        relativePath,
                        typeof(Transform),
                        "localPosition.x",
                        new AnimationCurve(translation_frames.Select(e => e[0]).ToArray())
                        );

                    clip.SetCurve(
                        relativePath,
                        typeof(Transform),
                        "localPosition.y",
                        new AnimationCurve(translation_frames.Select(e => e[1]).ToArray())
                        );

                    clip.SetCurve(
                        relativePath,
                        typeof(Transform),
                        "localPosition.z",
                        new AnimationCurve(translation_frames.Select(e => e[2]).ToArray())
                        );

                    clip.SetCurve(
                        relativePath,
                        typeof(Transform),
                        "localRotation.x",
                        new AnimationCurve(rotation_frames.Select(e => e[0]).ToArray())
                        );

                    clip.SetCurve(
                        relativePath,
                        typeof(Transform),
                        "localRotation.y",
                        new AnimationCurve(rotation_frames.Select(e => e[1]).ToArray())
                        );

                    clip.SetCurve(
                        relativePath,
                        typeof(Transform),
                        "localRotation.z",
                        new AnimationCurve(rotation_frames.Select(e => e[2]).ToArray())
                        );

                    clip.SetCurve(
                        relativePath,
                        typeof(Transform),
                        "localRotation.w",
                        new AnimationCurve(rotation_frames.Select(e => e[3]).ToArray())
                        );
                    AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(clip);
                    if (this.loopTime)
                    {
                        settings.loopTime = true;
                    }


                    AnimationUtility.SetAnimationClipSettings(clip, settings);
                }
                clips.Add(clip);
            }

            return(clips);
        }