Esempio n. 1
0
        private void InitController()
        {
            gameObject = dg_get_PreviewObject();
            if (gameObject == null)
            {
                return;
            }
            originalGameObject = uAvatarPreviewSelection.GetPreview(dg_get_animationClipType());

            animator  = dg_get_Animator();
            animation = gameObject.GetComponent <Animation>();
            if (originalGameObject != null)
            {
                transformPoseSave = new TransformPoseSave(originalGameObject);
                transformPoseSave.ChangeStartTransform();
                transformPoseSave.ChangeTransformReference(gameObject);
            }
            else
            {
                transformPoseSave = new TransformPoseSave(gameObject);
            }
            blendShapeWeightSave = new BlendShapeWeightSave(gameObject);

            var clip = dg_get_m_SourcePreviewMotion(instance);

            if (clip.legacy || instance == null || !((UnityEngine.Object)animator != (UnityEngine.Object)null))
            {
                if (animation != null)
                {
                    animation.enabled = false;  //If vaw.animation.enabled, it is not updated during execution. bug?
                }
            }
            else
            {
                animator.fireEvents      = false;
                animator.applyRootMotion = EditorPrefs.GetBool(EditorPrefsApplyRootMotion, false);

#if UNITY_2019_1_OR_NEWER
                animator.enabled = true;
                UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, null);

                m_PlayableGraph = PlayableGraph.Create("Avatar Preview PlayableGraph");
                m_PlayableGraph.SetTimeUpdateMode(DirectorUpdateMode.Manual);

                m_AnimationClipPlayable = AnimationClipPlayable.Create(m_PlayableGraph, clip);
                m_AnimationClipPlayable.SetApplyPlayableIK(false);
                m_AnimationClipPlayable.SetApplyFootIK(isIKOnFeet);
                if (m_UAnimationClipPlayable == null)
                {
                    m_UAnimationClipPlayable = new UAnimationClipPlayable();
                }
                m_UAnimationClipPlayable.SetRemoveStartOffset(m_AnimationClipPlayable, true);
                m_UAnimationClipPlayable.SetOverrideLoopTime(m_AnimationClipPlayable, true);
                m_UAnimationClipPlayable.SetLoopTime(m_AnimationClipPlayable, true);

                Playable rootPlayable = m_AnimationClipPlayable;

#if VERYANIMATION_ANIMATIONRIGGING
                m_VARigBuilder = gameObject.GetComponent <VeryAnimationRigBuilder>();
                if (m_VARigBuilder != null)
                {
                    RigBuilder.DestroyImmediate(m_VARigBuilder);
                    m_VARigBuilder = null;
                }
                m_RigBuilder = gameObject.GetComponent <RigBuilder>();
                if (m_RigBuilder != null)
                {
                    RigBuilder.DestroyImmediate(m_RigBuilder);
                    m_RigBuilder = null;
                }
                if (originalGameObject != null)
                {
                    var rigBuilder = originalGameObject.GetComponent <RigBuilder>();
                    if (rigBuilder != null && rigBuilder.isActiveAndEnabled && rigBuilder.enabled)
                    {
                        var layers = new List <GameObject>();
                        foreach (var layer in rigBuilder.layers)
                        {
                            if (layer.rig == null || !layer.active)
                            {
                                continue;
                            }
                            var originalRig = layer.rig.GetComponent <Rig>();
                            if (originalRig == null)
                            {
                                continue;
                            }

                            Func <Transform, Transform> GetPreviewTransform = (t) =>
                            {
                                if (t == null)
                                {
                                    return(null);
                                }
                                var path = AnimationUtility.CalculateTransformPath(t, originalGameObject.transform);
                                return(gameObject.transform.Find(path));
                            };
                            var previewT = GetPreviewTransform(originalRig.transform);
                            if (previewT == null)
                            {
                                continue;
                            }
                            var newRig = GameObject.Instantiate <GameObject>(originalRig.gameObject, previewT.parent);
                            newRig.name = previewT.name;
                            GameObject.DestroyImmediate(previewT.gameObject);
                            AnimationRigging.ReplaceConstraintTransformReference(gameObject, newRig.GetComponent <Rig>(), originalGameObject, originalRig);
                            layers.Add(newRig);
                        }
                        if (layers.Count > 0)
                        {
                            m_VARigBuilder = gameObject.AddComponent <VeryAnimationRigBuilder>();
                            m_RigBuilder   = gameObject.GetComponent <RigBuilder>();
                            foreach (var layer in layers)
                            {
                                var rig = layer.GetComponent <Rig>();
                                #region RemoveEffector
                                {
                                    var effectors = rig.effectors as List <RigEffectorData>;
                                    effectors.Clear();
                                }
                                #endregion

#if UNITY_2020_1_OR_NEWER
                                var rigLayer = new RigLayer(rig);            //version 0.3.2
#else
                                var rigLayer = new RigBuilder.RigLayer(rig); //version 0.2.5
#endif
                                m_RigBuilder.layers.Add(rigLayer);
                            }
                        }
                    }
                    if (m_VARigBuilder != null && m_RigBuilder != null)
                    {
                        m_VARigBuilder.enabled = m_RigBuilder.enabled = EditorPrefs.GetBool(EditorPrefsARConstraint);
                        if (m_RigBuilder.enabled)
                        {
                            m_VARigBuilder.StartPreview();
                            m_RigBuilder.StartPreview();
                            rootPlayable = m_VARigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable);
                            rootPlayable = m_RigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable);
                        }
                    }
                }
#endif

                if (animator.applyRootMotion)
                {
                    bool hasRootMotionBone = false;
                    if (animator.isHuman)
                    {
                        hasRootMotionBone = true;
                    }
                    else
                    {
                        UAvatar uAvatar = new UAvatar();
                        var     genericRootMotionBonePath = uAvatar.GetGenericRootMotionBonePath(animator.avatar);
                        hasRootMotionBone = !string.IsNullOrEmpty(genericRootMotionBonePath);
                    }
                    if (hasRootMotionBone)
                    {
                        if (m_UAnimationOffsetPlayable == null)
                        {
                            m_UAnimationOffsetPlayable = new UAnimationOffsetPlayable();
                        }
                        m_AnimationOffsetPlayable = m_UAnimationOffsetPlayable.Create(m_PlayableGraph, transformPoseSave.startLocalPosition, transformPoseSave.startLocalRotation, 1);
                        m_AnimationOffsetPlayable.SetInputWeight(0, 1f);
                        m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationOffsetPlayable, 0);
                        rootPlayable = m_AnimationOffsetPlayable;
                    }
                    {
                        if (m_UAnimationMotionXToDeltaPlayable == null)
                        {
                            m_UAnimationMotionXToDeltaPlayable = new UAnimationMotionXToDeltaPlayable();
                        }
                        m_AnimationMotionXToDeltaPlayable = m_UAnimationMotionXToDeltaPlayable.Create(m_PlayableGraph);
                        m_UAnimationMotionXToDeltaPlayable.SetAbsoluteMotion(m_AnimationMotionXToDeltaPlayable, true);
                        m_AnimationMotionXToDeltaPlayable.SetInputWeight(0, 1f);
                        m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationMotionXToDeltaPlayable, 0);
                        rootPlayable = m_AnimationMotionXToDeltaPlayable;
                    }
                }

                var playableOutput = AnimationPlayableOutput.Create(m_PlayableGraph, "Animation", animator);
                playableOutput.SetSourcePlayable(rootPlayable);
#else
                if (m_Controller == null)
                {
                    m_Controller            = new UnityEditor.Animations.AnimatorController();
                    m_Controller.name       = "Avatar Preview AnimatorController";
                    m_Controller.hideFlags |= HideFlags.HideAndDontSave;
                    uAnimatorController.SetPushUndo(m_Controller, false);
                    m_Controller.AddLayer("preview");
                    m_StateMachine = m_Controller.layers[0].stateMachine;
                    uAnimatorStateMachine.SetPushUndo(m_StateMachine, false);
                    m_StateMachine.hideFlags |= HideFlags.HideAndDontSave;
                }
                if (m_State == null)
                {
                    m_State = m_StateMachine.AddState("preview");
                    uAnimatorState.SetPushUndo(m_State, false);
                    m_State.motion     = (Motion)clip;
                    m_State.iKOnFeet   = isIKOnFeet;
                    m_State.hideFlags |= HideFlags.HideAndDontSave;
                }
                UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, m_Controller);
#endif
            }

            dg_set_ShowIKOnFeetButton(animator != null && animator.isHuman && clip.isHumanMotion);

            ForceUpdate();
        }
Esempio n. 2
0
        private void PlayableGraphReady(AnimationClip clip)
        {
            if (animator == null)
            {
                return;
            }

            UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, null);

            bool animatesRootTransform   = animator.applyRootMotion;
            bool requiresOffsetPlayable  = va.rootMotionBoneIndex >= 0;
            bool requiresMotionXPlayable = animatesRootTransform;
            bool usesAbsoluteMotion      = true;

            if (va.uAw.GetLinkedWithTimeline())
            {
#if VERYANIMATION_TIMELINE
                va.uAw.GetTimelineAnimationTrackInfo(out animatesRootTransform, out requiresMotionXPlayable, out usesAbsoluteMotion);
                requiresOffsetPlayable = requiresMotionXPlayable;
#else
                Assert.IsTrue(false);
#endif
            }

            if (m_PlayableGraph.IsValid())
            {
                if (m_AnimationClipPlayable.IsValid())
                {
                    if (m_AnimationClipPlayable.GetAnimationClip() == clip &&
                        m_AnimatesRootTransform == animatesRootTransform &&
                        m_RequiresOffsetPlayable == requiresOffsetPlayable &&
                        m_RequiresMotionXPlayable == requiresMotionXPlayable &&
                        m_UsesAbsoluteMotion == usesAbsoluteMotion)
                    {
                        return;
                    }
                }
                m_PlayableGraph.Destroy();
            }
            m_AnimatesRootTransform   = animatesRootTransform;
            m_RequiresOffsetPlayable  = requiresOffsetPlayable;
            m_RequiresMotionXPlayable = requiresMotionXPlayable;
            m_UsesAbsoluteMotion      = usesAbsoluteMotion;

            m_PlayableGraph = PlayableGraph.Create(gameObject.name + "_DummyObject");
            m_PlayableGraph.SetTimeUpdateMode(DirectorUpdateMode.Manual);

            m_AnimationClipPlayable = AnimationClipPlayable.Create(m_PlayableGraph, clip);
            m_AnimationClipPlayable.SetApplyPlayableIK(m_ApplyIK);
            m_AnimationClipPlayable.SetApplyFootIK(false);
            if (m_UAnimationClipPlayable == null)
            {
                m_UAnimationClipPlayable = new UAnimationClipPlayable();
            }
            m_UAnimationClipPlayable.SetRemoveStartOffset(m_AnimationClipPlayable, m_RemoveStartOffset);
#if UNITY_2019_1_OR_NEWER
            m_UAnimationClipPlayable.SetOverrideLoopTime(m_AnimationClipPlayable, true);
            m_UAnimationClipPlayable.SetLoopTime(m_AnimationClipPlayable, false);
#endif

            Playable rootPlayable = m_AnimationClipPlayable;

            if (m_AnimatesRootTransform)
            {
                if (m_RequiresOffsetPlayable)
                {
                    if (m_UAnimationOffsetPlayable == null)
                    {
                        m_UAnimationOffsetPlayable = new UAnimationOffsetPlayable();
                    }
                    m_AnimationOffsetPlayable = m_UAnimationOffsetPlayable.Create(m_PlayableGraph, m_OffsetPosition, m_OffsetRotation, 1);
                    m_AnimationOffsetPlayable.SetInputWeight(0, 1f);
                    m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationOffsetPlayable, 0);
                    rootPlayable = m_AnimationOffsetPlayable;
                }
                if (m_RequiresMotionXPlayable)
                {
                    if (m_UAnimationMotionXToDeltaPlayable == null)
                    {
                        m_UAnimationMotionXToDeltaPlayable = new UAnimationMotionXToDeltaPlayable();
                    }
                    m_AnimationMotionXToDeltaPlayable = m_UAnimationMotionXToDeltaPlayable.Create(m_PlayableGraph);
                    m_UAnimationMotionXToDeltaPlayable.SetAbsoluteMotion(m_AnimationMotionXToDeltaPlayable, m_UsesAbsoluteMotion);
                    m_AnimationMotionXToDeltaPlayable.SetInputWeight(0, 1f);
                    m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationMotionXToDeltaPlayable, 0);
                    rootPlayable = m_AnimationMotionXToDeltaPlayable;
                }
            }

            var playableOutput = AnimationPlayableOutput.Create(m_PlayableGraph, "Animation", animator);
            playableOutput.SetSourcePlayable(rootPlayable);
        }
            public SynchronizeObject(Animator animator, AnimationClip clip)
            {
                this.animator          = animator;
                gameObject             = animator.gameObject;
                transformPoseSave      = new TransformPoseSave(gameObject);
                blendShapeWeightSave   = new BlendShapeWeightSave(gameObject);
                animationClipValueSave = new AnimationClipValueSave(gameObject, clip);
                this.clip = clip;

#if UNITY_2018_3_OR_NEWER
                #region BuildPlayableGraph
                {
                    m_PlayableGraph = PlayableGraph.Create("SynchronizeObject." + gameObject.name);
                    m_PlayableGraph.SetTimeUpdateMode(DirectorUpdateMode.Manual);

                    m_AnimationClipPlayable = AnimationClipPlayable.Create(m_PlayableGraph, clip);
                    m_AnimationClipPlayable.SetApplyPlayableIK(false);
                    m_AnimationClipPlayable.SetApplyFootIK(false);
                    {
                        UAnimationClipPlayable uAnimationClipPlayable = new UAnimationClipPlayable();
                        uAnimationClipPlayable.SetRemoveStartOffset(m_AnimationClipPlayable, true);
                    }
                    Playable rootPlayable = m_AnimationClipPlayable;

#if VERYANIMATION_ANIMATIONRIGGING
                    m_VARigBuilder = gameObject.GetComponent <VeryAnimationRigBuilder>();
                    m_RigBuilder   = gameObject.GetComponent <RigBuilder>();
                    if (m_VARigBuilder != null && m_RigBuilder != null)
                    {
                        m_VARigBuilder.StartPreview();
                        m_RigBuilder.StartPreview();
                        rootPlayable = m_VARigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable);
                        rootPlayable = m_RigBuilder.BuildPreviewGraph(m_PlayableGraph, rootPlayable);
                    }
#endif

                    if (animator.applyRootMotion)
                    {
                        bool hasRootMotionBone = false;
                        if (animator.isHuman)
                        {
                            hasRootMotionBone = true;
                        }
                        else
                        {
                            UAvatar uAvatar = new UAvatar();
                            var     genericRootMotionBonePath = uAvatar.GetGenericRootMotionBonePath(animator.avatar);
                            hasRootMotionBone = !string.IsNullOrEmpty(genericRootMotionBonePath);
                        }
                        if (hasRootMotionBone)
                        {
                            var uAnimationOffsetPlayable = new UAnimationOffsetPlayable();
                            m_AnimationOffsetPlayable = uAnimationOffsetPlayable.Create(m_PlayableGraph, transformPoseSave.startLocalPosition, transformPoseSave.startLocalRotation, 1);
                            m_AnimationOffsetPlayable.SetInputWeight(0, 1f);
                            m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationOffsetPlayable, 0);
                            rootPlayable = m_AnimationOffsetPlayable;
                        }
                        {
                            var uAnimationMotionXToDeltaPlayable = new UAnimationMotionXToDeltaPlayable();
                            m_AnimationMotionXToDeltaPlayable = uAnimationMotionXToDeltaPlayable.Create(m_PlayableGraph);
                            uAnimationMotionXToDeltaPlayable.SetAbsoluteMotion(m_AnimationMotionXToDeltaPlayable, true);
                            m_AnimationMotionXToDeltaPlayable.SetInputWeight(0, 1f);
                            m_PlayableGraph.Connect(rootPlayable, 0, m_AnimationMotionXToDeltaPlayable, 0);
                            rootPlayable = m_AnimationMotionXToDeltaPlayable;
                        }
                    }

                    var playableOutput = AnimationPlayableOutput.Create(m_PlayableGraph, "Animation", animator);
                    playableOutput.SetSourcePlayable(rootPlayable);
                }
                #endregion
#else
                animator.enabled = false;   //In order to avoid the mysterious behavior where an event is called from "UnityEditor.Handles: DrawCameraImpl", it is invalid except when updating
#endif
                #region Save
                saveFireEvents      = animator.fireEvents;
                animator.fireEvents = false;
                #endregion
            }