public EvaluationGraph(RigBuilder rigBuilder, AnimationClip clip)
        {
            m_SyncSceneToStreamLayer = new SyncSceneToStreamLayer();

            var layers = rigBuilder.layers;

            m_RigLayers = new List <IRigLayer>(layers.Count);
            for (int i = 0; i < layers.Count; ++i)
            {
                if (!layers[i].active)
                {
                    continue;
                }

                m_RigLayers.Add(new RigLayer(layers[i].rig));
            }

            m_Graph = PlayableGraph.Create("Evaluation-Graph");
            m_Graph.SetTimeUpdateMode(DirectorUpdateMode.Manual);

            var animator = rigBuilder.GetComponent <Animator>();

            var      clipPlayable  = AnimationClipPlayable.Create(m_Graph, clip);
            Playable inputPlayable = clipPlayable;

            var playableChains = RigBuilderUtils.BuildPlayables(animator, m_Graph, m_RigLayers, m_SyncSceneToStreamLayer);

            foreach (var chain in playableChains)
            {
                if (!chain.IsValid())
                {
                    continue;
                }

                chain.playables[0].AddInput(inputPlayable, 0, 1);
                inputPlayable = chain.playables[chain.playables.Length - 1];
            }


            m_Output = AnimationPlayableOutput.Create(m_Graph, "Evaluation-Graph-Output", animator);
            m_Output.SetSourcePlayable(inputPlayable);
        }
Exemplo n.º 2
0
            public EvaluationGraph(RigBuilder rigBuilder, AnimationClip clip, AnimationClip defaultPoseClip, IReadOnlyDictionary <IRigConstraint, IRigConstraint> overrides, IRigConstraint lastConstraint = null)
            {
                m_SyncSceneToStreamLayer = new SyncSceneToStreamLayer();

                bool stopBuilding = false;

                var layers = rigBuilder.layers;

                m_RigLayers = new List <IRigLayer>(layers.Count);
                for (int i = 0; i < layers.Count; ++i)
                {
                    if (stopBuilding == true)
                    {
                        break;
                    }

                    if (layers[i].rig == null || !layers[i].active)
                    {
                        continue;
                    }

                    IRigConstraint[] constraints = RigUtils.GetConstraints(layers[i].rig);
                    if (constraints == null || constraints.Length == 0)
                    {
                        continue;
                    }

                    var newConstraints = new List <IRigConstraint>(constraints.Length);
                    foreach (IRigConstraint constraint in constraints)
                    {
                        if (overrides.TryGetValue(constraint, out IRigConstraint newConstraint))
                        {
                            if (newConstraint != null)
                            {
                                newConstraints.Add(newConstraint);
                            }
                        }
                        else
                        {
                            newConstraints.Add(constraint);
                        }

                        if (constraint == lastConstraint)
                        {
                            stopBuilding = true;
                            break;
                        }
                    }

                    m_RigLayers.Add(new OverrideRigLayer(layers[i].rig, newConstraints.ToArray()));
                }

                m_Graph = PlayableGraph.Create("Evaluation-Graph");
                m_Graph.SetTimeUpdateMode(DirectorUpdateMode.Manual);

                var animator = rigBuilder.GetComponent <Animator>();

                m_Clip = clip;

                var settings = AnimationUtility.GetAnimationClipSettings(m_Clip);

                m_ClipLoopTime = settings.loopTime;

                // Override loop time in clip asset.
                settings.loopTime = false;
                AnimationUtility.SetAnimationClipSettings(m_Clip, settings);

                var defaultPosePlayable = AnimationClipPlayable.Create(m_Graph, defaultPoseClip);
                var clipPlayable        = AnimationClipPlayable.Create(m_Graph, m_Clip);

                defaultPosePlayable.SetApplyFootIK(false);
                clipPlayable.SetApplyFootIK(false);

                AnimationLayerMixerPlayable mixer = AnimationLayerMixerPlayable.Create(m_Graph, 2);

                mixer.ConnectInput(0, defaultPosePlayable, 0, 1.0f);
                mixer.ConnectInput(1, clipPlayable, 0, 1.0f);

                Playable inputPlayable = mixer;

                var playableChains = RigBuilderUtils.BuildPlayables(animator, m_Graph, m_RigLayers, m_SyncSceneToStreamLayer);

                foreach (var chain in playableChains)
                {
                    if (!chain.IsValid())
                    {
                        continue;
                    }

                    chain.playables[0].AddInput(inputPlayable, 0, 1);
                    inputPlayable = chain.playables[chain.playables.Length - 1];
                }

                var output = AnimationPlayableOutput.Create(m_Graph, "bake-output", animator);

                output.SetSourcePlayable(inputPlayable);
            }