public static void Create(GameObject gameObject)
        {
            var rigBuilder = gameObject.GetComponent <RigBuilder>();

            if (rigBuilder == null)
            {
                rigBuilder = Undo.AddComponent <RigBuilder>(gameObject);
            }

            var vaRigBuilder = gameObject.GetComponent <VeryAnimationRigBuilder>();

            if (vaRigBuilder == null)
            {
                vaRigBuilder = Undo.AddComponent <VeryAnimationRigBuilder>(gameObject);
            }

            //Must be in order before RigBuilder
            {
                var components        = vaRigBuilder.GetComponents <MonoBehaviour>();
                var indexRigBuilder   = ArrayUtility.FindIndex(components, x => x.GetType() == typeof(RigBuilder));
                var indexVARigBuilder = ArrayUtility.FindIndex(components, x => x.GetType() == typeof(VeryAnimationRigBuilder));
                if (indexRigBuilder >= 0 && indexVARigBuilder >= 0)
                {
                    for (int i = 0; i < indexVARigBuilder - indexRigBuilder; i++)
                    {
                        ComponentUtility.MoveComponentUp(vaRigBuilder);
                    }
                }
            }

            var vaRig = GetVeryAnimationRig(gameObject);

            if (vaRig == null)
            {
                var rigObj = new GameObject(AnimationRiggingRigName);
                rigObj.transform.SetParent(gameObject.transform);
                rigObj.transform.localPosition = Vector3.zero;
                rigObj.transform.localRotation = Quaternion.identity;
                rigObj.transform.localScale    = Vector3.one;
                Undo.RegisterCreatedObjectUndo(rigObj, "");
                var rig = Undo.AddComponent <Rig>(rigObj);
                vaRig = Undo.AddComponent <VeryAnimationRig>(rigObj);
                Undo.RecordObject(rigBuilder, "");
#if UNITY_2020_1_OR_NEWER
                var rigLayer = new RigLayer(rig);            //version 0.3
#else
                var rigLayer = new RigBuilder.RigLayer(rig); //version 0.2
#endif
                rigBuilder.layers.Add(rigLayer);
                Selection.activeGameObject = rigObj;
            }
        }
Exemple #2
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();
        }