Exemplo n.º 1
0
        public void AddFromClips(GameObject obj, IEnumerable <AnimationClip> clips)
        {
            if (Application.isPlaying)
            {
                return;
            }

            // Add RootMotion TR property in animation mode snapshot as well if animator bindings didn't do it.
            var  animator = obj.GetComponent <Animator>();
            bool addRoot  = (animator != null && !animator.isHuman && !animator.applyRootMotion);

            m_CurveBindingSet.Clear();
            foreach (var c in clips)
            {
                addRoot |= c.hasRootCurves | c.hasMotionCurves | c.hasRootMotion;

                m_CurveBindingSet.UnionWith(AnimationClipCurveCache.Instance.GetCurveInfo(c).rawBindings);
                m_CurveBindingSet.UnionWith(AnimationClipCurveCache.Instance.GetCurveInfo(c).objectBindings);
            }
            m_CurveBindingSet.UnionWith(m_AnimatorCache.GetAnimatorBindings(obj));

            foreach (var binding in m_CurveBindingSet)
            {
                if (binding.type != typeof(Animator) && !IsEulerHint(binding))
                {
                    AnimationMode.AddEditorCurveBinding(obj, binding);
                }
            }

            if (addRoot)
            {
                AnimationMode.AddTransformTR(obj, string.Empty);
            }
        }
Exemplo n.º 2
0
        public static void PreviewFromCurves(GameObject animatorRoot, IEnumerable <EditorCurveBinding> keys)
        {
            if (!AnimationMode.InAnimationMode())
            {
                return;
            }

            var avatarRoot = GetAvatarRoot(animatorRoot);

            foreach (var binding in keys)
            {
                if (IsAvatarBinding(binding) || IsEuler(binding))
                {
                    continue;
                }

                bool isTransform = typeof(Transform).IsAssignableFrom(binding.type);
                if (isTransform && binding.propertyName.Equals(AnimatorBindingCache.TRPlaceHolder))
                {
                    AddTRBinding(animatorRoot, binding);
                }
                else if (isTransform && binding.propertyName.Equals(AnimatorBindingCache.ScalePlaceholder))
                {
                    AddScaleBinding(animatorRoot, binding);
                }
                else
                {
                    AnimationMode.AddEditorCurveBinding(avatarRoot, binding);
                }
            }
        }
Exemplo n.º 3
0
 private static void AddScaleBinding(GameObject root, EditorCurveBinding binding)
 {
     // AnimationMode.AddTransformTRS is slow
     binding.propertyName = k_ScaleX; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_ScaleY; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_ScaleZ; AnimationMode.AddEditorCurveBinding(root, binding);
 }
Exemplo n.º 4
0
 private static void AddTRBinding(GameObject root, EditorCurveBinding binding)
 {
     // This is faster than AnimationMode.AddTransformTR
     binding.propertyName = k_PosX; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_PosY; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_PosZ; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_RotX; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_RotY; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_RotZ; AnimationMode.AddEditorCurveBinding(root, binding);
     binding.propertyName = k_RotW; AnimationMode.AddEditorCurveBinding(root, binding);
 }
        void SamplePose(float time)
        {
            if (sampler == null)
            {
                return;
            }

            if (!AnimationMode.InAnimationMode())
            {
                AnimationMode.StartAnimationMode();
            }

            AnimationMode.BeginSampling();

            try
            {
                // 1. Sample pose
                using (TransformBuffer.Memory pose = sampler.SamplePose(time))
                {
                    // 2. Save bindings so that we can restore the joints to their initial transform once preview is finished
                    foreach (EditorCurveBinding binding in bindings)
                    {
                        AnimationMode.AddEditorCurveBinding(targetObject, binding);
                    }

                    // 3. Sample trajectory
                    AffineTransform rootTransform = sampler.SampleTrajectory(time);
                    if (previousTime >= 0.0f && targetJoints[0] != null)
                    {
                        // If the character was previously animated, we apply delta trajectory to current transform.
                        // Otherwise, we don't move the character (this allows user to move manually previewed character between frames)
                        var currentRootTransform =
                            Missing.Convert(targetObject.transform) *
                            previousRootTransform.inverseTimes(rootTransform);

                        targetJoints[0].position = currentRootTransform.t;
                        targetJoints[0].rotation = currentRootTransform.q;
                    }

                    previousTime          = time;
                    previousRootTransform = rootTransform;

                    // 4. Sample all joints
                    for (int i = 1; i < targetJoints.Length; ++i)
                    {
                        if (targetJoints[i] == null)
                        {
                            continue;
                        }

                        var localJointTransform = pose.Ref.transforms[i];

                        targetJoints[i].localPosition = localJointTransform.t;
                        targetJoints[i].localRotation = localJointTransform.q;
                    }

                    // 5. sample past & future trajectory
                    SampleTrajectory(time);
                }
            }
            finally
            {
                AnimationMode.EndSampling();
            }
        }