Exemplo n.º 1
0
        public Transition CreateDefaultTransition(State source)
        {
            // default transition is to crossfade to every state
            Transition t = new CrossfadeTransition(source, null, 0.1f, new string[0] {
            });

            return(t);
        }
Exemplo n.º 2
0
        public static Mixamo.AnimationGraph FromJson( Hashtable json_graph , AnimationStateMachine asm )
        {
            Mixamo.AnimationGraph graph = new Mixamo.AnimationGraph();
            graph.name = (string) json_graph["name"];
            graph.rootPath = (string) json_graph["root_path"];

            graph.rmSetup = new Mixamo.RootMotionSetup( asm.transform , asm.Target.transform.Find( graph.rootPath ) , asm.Target );

            ArrayList json_clips = (ArrayList) json_graph["clips"];
            graph.clips = new Mixamo.Clip[json_clips.Count];

            foreach( AnimationState s in asm.Target.animation ) {
                s.weight = 0f;
            }

            Dictionary<int , List<Clip>> sync_groups = new Dictionary<int, List<Clip>>();

            // Clips
            for( int i=0;i<json_clips.Count;i++ ) {
                Hashtable json_clip = (Hashtable)json_clips[i];
                Mixamo.Clip c = (graph.clips[i] = new Mixamo.Clip() );
                c.name = (string) json_clip["name"];
                c.type = (string) json_clip["type"];

                if( json_clip.ContainsKey("sync_clip_group" ) ) {
                    c.sync_clip_group = int.Parse( json_clip["sync_clip_group"].ToString() );
                }
                c.anim_name = (string) json_clip["anim_name"];
                //c.layer = 0;
                if( json_clip.ContainsKey( "layer" ) ) {
                    c.layer = int.Parse( json_clip["layer"].ToString() );
                } else {
                    c.layer = 0;
                }
                c.parent = graph;

                if( json_clip.ContainsKey( "root_motion_translation" ) ) {
                    switch( json_clip["root_motion_translation"].ToString() ) {
                    case "z":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.Z;
                        break;
                    case "x":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.X;
                        break;
                    case "y":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.Y;
                        break;
                    case "xz":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.XZ;
                        break;
                    case "xy":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.XY;
                        break;
                    case "yz":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.YZ;
                        break;
                    case "xyz":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.XYZ;
                        break;
                    case "":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.None;
                        break;
                    default:
                        throw new System.Exception( "Invalid Root Motion Translation Type: '" + json_clip["root_motion_translation"].ToString() + "'! Valid types: x,y,z,xy,xz,yz,xyz" );
                    }
                }

                if( json_clip.ContainsKey( "root_motion_rotation" ) && json_clip["root_motion_rotation"].ToString()  != "" ) {

                    switch( json_clip["root_motion_rotation"].ToString() ) {
                    case "x":
                        c.RootMotion.RotExtractionType = RootMotionRotExtractionType.X;
                        break;
                    case "y":
                        c.RootMotion.RotExtractionType = RootMotionRotExtractionType.Y;
                        break;
                    case "z":
                        c.RootMotion.RotExtractionType = RootMotionRotExtractionType.Z;
                        break;
                    default:
                        throw new System.Exception( "Invalid Root Motion Rotation Type: " + json_clip["root_motion_rotation"].ToString() + "! Valid types: xyz" );
                    }
                } else {
                    c.RootMotion.RotExtractionType = RootMotionRotExtractionType.None;
                }

                // setup animation state clip
                AnimationState s = asm.Target.animation[c.anim_name];
                s.wrapMode = WrapMode.Loop;
                if( c.type == "additive" )
                    s.blendMode = AnimationBlendMode.Additive;
                else
                    s.blendMode = AnimationBlendMode.Blend;
                s.layer = c.layer;
                s.weight = 0f;
                if( json_clip.ContainsKey("mixing_transform") ) {
                    Transform t = asm.Target.transform.Find( json_clip["mixing_transform"].ToString() );
                    s.AddMixingTransform( t , true);
                }
                s.enabled = true;
                c.anim_state = s;

                if( c.sync_clip_group != null ) {
                    int grp = (int)c.sync_clip_group;
                    if( !sync_groups.ContainsKey( grp ) ) {
                        sync_groups.Add( grp , new List<Clip>() );
                    }
                    sync_groups[ grp ].Add( c );
                }
            }

            // sync any layers that need to be synced together
            /*foreach( List<Clip> lst in sync_groups.Values ) {
                float sum_speed = 0;
                foreach( Clip c in lst ) {
                    sum_speed += c.anim_state.normalizedSpeed;
                }

                float avg_ns = sum_speed / lst.Count;
                foreach( Clip c in lst ) {
                    c.anim_state.normalizedSpeed = avg_ns;
                }
            }*/

            for(int i=0;i<graph.clips.Length;i++) {
                graph.clips[i].SetupRootMotionDeltas();
            }

            List<AnimationClip> clipsToDelete = new List<AnimationClip>();
            foreach( AnimationState s in asm.Target.animation ) {
                if( graph.GetClipByAnimName( s.name )  == null ) {
                    Debug.LogWarning( "Did not load the animation clip: " + s.name + ". Removing it!" );
                    clipsToDelete.Add( s.clip );
                }
            }

            foreach( AnimationClip c in clipsToDelete ) {
                asm.Target.animation.RemoveClip( c );
            }

            // Layers
            ArrayList json_layers = (ArrayList)json_graph["layers"];
            //please support more than one with this line removed!
            /*			if( json_layers.Count != 1 ) {
                throw new System.Exception( "Only supports 1 layer thus far!" );
            }*/
            graph.layers = new Mixamo.Layer[json_layers.Count];
            for( int i=0;i<json_layers.Count;i++) {
                // Layer
                Hashtable json_layer = (Hashtable)json_layers[i];
                Mixamo.Layer layer = (graph.layers[i] = new Mixamo.Layer());
                layer.name = (string)json_layer["name"];
                layer.priority = int.Parse( json_layer["priority"].ToString() );
                layer.graph = graph;

                // States
                ArrayList json_states = (ArrayList)json_layer["states"];
                layer.states = new State[json_states.Count];
                for(int j=0;j<json_states.Count;j++) {
                    // State
                    Hashtable json_state = (Hashtable)json_states[j];
                    State state = (layer.states[j] = new State());
                    state.name = (string)json_state["name"];
                    state.layer = layer;
                    state.IsLooping = json_state.ContainsKey("is_looping") ? bool.Parse( json_state["is_looping"].ToString() ) : true;
                    state.root = graph.JsonToTreeNode( (Hashtable)json_state["tree"] , state );
                }

                // Transitions
                for(int j=0;j<json_states.Count;j++ ) {
                    Hashtable json_state = (Hashtable)json_states[j];
                    State s = layer.states[j];
                    if( json_state.ContainsKey( "transitions" ) ) {
                        ArrayList json_transitions = (ArrayList) json_state["transitions"];
                        s.transitions = new Transition[ json_transitions.Count];
                        for( int k=0; k < json_transitions.Count;k++) {
                            Hashtable json_transition = (Hashtable)json_transitions[k];
                            Transition t;
                            State dest = ( json_transition["destination"].ToString() == "*" ? null : layer.GetStateByName( json_transition["destination"].ToString() ) );
                            string[] guards;
                            if( json_transition.ContainsKey( "guards" ) ) {
                                ArrayList arr = (ArrayList) json_transition["guards"];
                                guards = new string[arr.Count];
                                for(int a=0;a<arr.Count;++a) { guards[a] = arr[a].ToString(); }
                            } else {
                                guards = new string[0] {};
                            }
                            if( json_transition["type"].ToString() == "crossfade" ) {
                                t = new CrossfadeTransition( s , dest , (float)(double)json_transition["duration"] , guards );
                            } else if( json_transition["type"].ToString() == "clip" ) {
                                t = new ClipTransition( graph.GetClipByNameAndMarkInUse( json_transition["clip"].ToString() , null ) , s , dest , (float)(double) json_transition["duration_in"] , float.Parse(json_transition["duration_out"].ToString() ) , guards );
                            } else {
                                throw new System.Exception( "Transition type not supported: " + json_transition["type"] );
                            }
                            if( !s.IsLooping )
                                t.WaitTillEnd = true;
                            s.transitions[k] = t;
                        }
                    } else {
                        s.transitions = new Transition[1];
                        s.transitions[0] = layer.CreateDefaultTransition( s );
                        if( !s.IsLooping )
                            s.transitions[0].WaitTillEnd = true;
                    }
                }
            }

            graph.Init();
            return graph;
        }
Exemplo n.º 3
0
        public static Mixamo.AnimationGraph FromJson(Hashtable json_graph, AnimationStateMachine asm)
        {
            Mixamo.AnimationGraph graph = new Mixamo.AnimationGraph();
            graph.name     = (string)json_graph["name"];
            graph.rootPath = (string)json_graph["root_path"];

            graph.rmSetup = new Mixamo.RootMotionSetup(asm.transform, asm.Target.transform.Find(graph.rootPath), asm.Target);

            ArrayList json_clips = (ArrayList)json_graph["clips"];

            graph.clips = new Mixamo.Clip[json_clips.Count];

            foreach (AnimationState s in asm.Target.GetComponent <Animation>())
            {
                s.weight = 0f;
            }

            Dictionary <int, List <Clip> > sync_groups = new Dictionary <int, List <Clip> >();

            // Clips
            for (int i = 0; i < json_clips.Count; i++)
            {
                Hashtable   json_clip = (Hashtable)json_clips[i];
                Mixamo.Clip c         = (graph.clips[i] = new Mixamo.Clip());
                c.name = (string)json_clip["name"];
                c.type = (string)json_clip["type"];

                if (json_clip.ContainsKey("sync_clip_group"))
                {
                    c.sync_clip_group = int.Parse(json_clip["sync_clip_group"].ToString());
                }
                c.anim_name = (string)json_clip["anim_name"];
                //c.layer = 0;
                if (json_clip.ContainsKey("layer"))
                {
                    c.layer = int.Parse(json_clip["layer"].ToString());
                }
                else
                {
                    c.layer = 0;
                }
                c.parent = graph;

                if (json_clip.ContainsKey("root_motion_translation"))
                {
                    switch (json_clip["root_motion_translation"].ToString())
                    {
                    case "z":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.Z;
                        break;

                    case "x":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.X;
                        break;

                    case "y":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.Y;
                        break;

                    case "xz":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.XZ;
                        break;

                    case "xy":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.XY;
                        break;

                    case "yz":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.YZ;
                        break;

                    case "xyz":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.XYZ;
                        break;

                    case "":
                        c.RootMotion.ExtractionType = RootMotionExtractionType.None;
                        break;

                    default:
                        throw new System.Exception("Invalid Root Motion Translation Type: '" + json_clip["root_motion_translation"].ToString() + "'! Valid types: x,y,z,xy,xz,yz,xyz");
                    }
                }

                if (json_clip.ContainsKey("root_motion_rotation") && json_clip["root_motion_rotation"].ToString() != "")
                {
                    switch (json_clip["root_motion_rotation"].ToString())
                    {
                    case "x":
                        c.RootMotion.RotExtractionType = RootMotionRotExtractionType.X;
                        break;

                    case "y":
                        c.RootMotion.RotExtractionType = RootMotionRotExtractionType.Y;
                        break;

                    case "z":
                        c.RootMotion.RotExtractionType = RootMotionRotExtractionType.Z;
                        break;

                    default:
                        throw new System.Exception("Invalid Root Motion Rotation Type: " + json_clip["root_motion_rotation"].ToString() + "! Valid types: xyz");
                    }
                }
                else
                {
                    c.RootMotion.RotExtractionType = RootMotionRotExtractionType.None;
                }

                // setup animation state clip
                AnimationState s = asm.Target.GetComponent <Animation>()[c.anim_name];
                s.wrapMode = WrapMode.Loop;
                if (c.type == "additive")
                {
                    s.blendMode = AnimationBlendMode.Additive;
                }
                else
                {
                    s.blendMode = AnimationBlendMode.Blend;
                }
                s.layer  = c.layer;
                s.weight = 0f;
                if (json_clip.ContainsKey("mixing_transform"))
                {
                    Transform t = asm.Target.transform.Find(json_clip["mixing_transform"].ToString());
                    s.AddMixingTransform(t, true);
                }
                s.enabled    = true;
                c.anim_state = s;

                if (c.sync_clip_group != null)
                {
                    int grp = (int)c.sync_clip_group;
                    if (!sync_groups.ContainsKey(grp))
                    {
                        sync_groups.Add(grp, new List <Clip>());
                    }
                    sync_groups[grp].Add(c);
                }
            }

            // sync any layers that need to be synced together

            /*foreach( List<Clip> lst in sync_groups.Values ) {
             *      float sum_speed = 0;
             *      foreach( Clip c in lst ) {
             *              sum_speed += c.anim_state.normalizedSpeed;
             *      }
             *
             *      float avg_ns = sum_speed / lst.Count;
             *      foreach( Clip c in lst ) {
             *              c.anim_state.normalizedSpeed = avg_ns;
             *      }
             * }*/


            for (int i = 0; i < graph.clips.Length; i++)
            {
                graph.clips[i].SetupRootMotionDeltas();
            }

            List <AnimationClip> clipsToDelete = new List <AnimationClip>();

            foreach (AnimationState s in asm.Target.GetComponent <Animation>())
            {
                if (graph.GetClipByAnimName(s.name) == null)
                {
                    Debug.LogWarning("Did not load the animation clip: " + s.name + ". Removing it!");
                    clipsToDelete.Add(s.clip);
                }
            }


            foreach (AnimationClip c in clipsToDelete)
            {
                asm.Target.GetComponent <Animation>().RemoveClip(c);
            }


            // Layers
            ArrayList json_layers = (ArrayList)json_graph["layers"];

            //please support more than one with this line removed!

/*			if( json_layers.Count != 1 ) {
 *                              throw new System.Exception( "Only supports 1 layer thus far!" );
 *                      }*/
            graph.layers = new Mixamo.Layer[json_layers.Count];
            for (int i = 0; i < json_layers.Count; i++)
            {
                // Layer
                Hashtable    json_layer = (Hashtable)json_layers[i];
                Mixamo.Layer layer      = (graph.layers[i] = new Mixamo.Layer());
                layer.name     = (string)json_layer["name"];
                layer.priority = int.Parse(json_layer["priority"].ToString());
                layer.graph    = graph;

                // States
                ArrayList json_states = (ArrayList)json_layer["states"];
                layer.states = new State[json_states.Count];
                for (int j = 0; j < json_states.Count; j++)
                {
                    // State
                    Hashtable json_state = (Hashtable)json_states[j];
                    State     state      = (layer.states[j] = new State());
                    state.name      = (string)json_state["name"];
                    state.layer     = layer;
                    state.IsLooping = json_state.ContainsKey("is_looping") ? bool.Parse(json_state["is_looping"].ToString()) : true;
                    state.root      = graph.JsonToTreeNode((Hashtable)json_state["tree"], state);
                }

                // Transitions
                for (int j = 0; j < json_states.Count; j++)
                {
                    Hashtable json_state = (Hashtable)json_states[j];
                    State     s          = layer.states[j];
                    if (json_state.ContainsKey("transitions"))
                    {
                        ArrayList json_transitions = (ArrayList)json_state["transitions"];
                        s.transitions = new Transition[json_transitions.Count];
                        for (int k = 0; k < json_transitions.Count; k++)
                        {
                            Hashtable  json_transition = (Hashtable)json_transitions[k];
                            Transition t;
                            State      dest = (json_transition["destination"].ToString() == "*" ? null : layer.GetStateByName(json_transition["destination"].ToString()));
                            string[]   guards;
                            if (json_transition.ContainsKey("guards"))
                            {
                                ArrayList arr = (ArrayList)json_transition["guards"];
                                guards = new string[arr.Count];
                                for (int a = 0; a < arr.Count; ++a)
                                {
                                    guards[a] = arr[a].ToString();
                                }
                            }
                            else
                            {
                                guards = new string[0] {
                                };
                            }
                            if (json_transition["type"].ToString() == "crossfade")
                            {
                                t = new CrossfadeTransition(s, dest, (float)(double)json_transition["duration"], guards);
                            }
                            else if (json_transition["type"].ToString() == "clip")
                            {
                                t = new ClipTransition(graph.GetClipByNameAndMarkInUse(json_transition["clip"].ToString(), null), s, dest, (float)(double)json_transition["duration_in"], float.Parse(json_transition["duration_out"].ToString()), guards);
                            }
                            else
                            {
                                throw new System.Exception("Transition type not supported: " + json_transition["type"]);
                            }
                            if (!s.IsLooping)
                            {
                                t.WaitTillEnd = true;
                            }
                            s.transitions[k] = t;
                        }
                    }
                    else
                    {
                        s.transitions    = new Transition[1];
                        s.transitions[0] = layer.CreateDefaultTransition(s);
                        if (!s.IsLooping)
                        {
                            s.transitions[0].WaitTillEnd = true;
                        }
                    }
                }
            }

            graph.Init();
            return(graph);
        }
Exemplo n.º 4
0
 public Transition CreateDefaultTransition(  State source  )
 {
     // default transition is to crossfade to every state
     Transition t = new CrossfadeTransition( source , null , 0.1f , new string[0] {} );
     return t;
 }