Ejemplo n.º 1
0
        /// <summary>
        /// start play a new animation
        /// </summary>
        /// <param name="skeleton"></param>
        /// <param name="action"></param>
        /// <returns>anim handle or null</returns>
        public AnimHandle Play(Skeleton skeleton, AnimType.ActionData action)
        {
            if (m_isPlaying)
            {
                Debug.Fail("old animation still playing");
                return(null);
            }

            if (skeleton.GetBoneCount() == 0)
            {
                // ignore
                return(null);
            }

            m_isPlaying = true;
            m_dt        = 0.0f;
            m_skeleton  = skeleton;
            m_animData  = action;

            // initialize mapper
            m_boneIndex2groupIndexMapper = new int[m_skeleton.GetBoneCount()];
            m_samplers = new AnimSampler[m_skeleton.GetBoneCount()];

            for (int boneIndex = 0; boneIndex < m_skeleton.GetBoneCount(); ++boneIndex)
            {
                string boneName   = m_skeleton.GetBoneName(boneIndex);
                int    groupIndex = m_animData.Groups.Select((g, index) => new { g, index }).First(v => v.g.BoneName == boneName).index;
                m_boneIndex2groupIndexMapper[boneIndex] = groupIndex;

                m_samplers[boneIndex] = new AnimSampler(m_animData.Groups[groupIndex]);
            }

            // compute last frame
            int lastFrame = 0;

            for (int boneIndex = 0; boneIndex < m_skeleton.GetBoneCount(); ++boneIndex)
            {
                lastFrame = Math.Max(lastFrame, m_samplers[boneIndex].LastFrame);
            }
            m_lastFrame = lastFrame;

            return(new AnimHandle(action.Name, 1.0f, 1.0f));
        }
Ejemplo n.º 2
0
        public AnimHandle Play(AnimType.ActionData action)
        {
            var player = new AnimPlayer();
            var handle = player.Play(m_skeleton, action);

            if (handle == null)
            {
                return(null);
            }

            var entry = new _Entry()
            {
                Player = player,
                Handle = handle,
            };

            m_entryList.Add(entry);

            return(handle);
        }
Ejemplo n.º 3
0
        private void _LoadAction(BlendTypeRepository repository, BlendValueCapsule bAnimAction, ref List <AnimType.ActionData> animActionList)
        {
            var groupList = new List <AnimType.ActionGroupData>();
            var bGroup    = bAnimAction.GetMember("groups").GetMember("first").GetRawValue <BlendAddress>().DereferenceOne();

            while (bGroup != null)
            {
                var groupData = new AnimType.ActionGroupData();
                groupData.BoneName = bGroup.GetMember("name").GetAllValueAsString();
                groupData.Location = AnimType.ChannelData <Vector3> .Empty();

                groupData.Rotation = AnimType.ChannelData <Quaternion> .Empty();

                groupData.Scale = AnimType.ChannelData <Vector3> .Empty();

                //Console.WriteLine("    found anim action group : " + groupData.BoneName);

                var channelList = new List <AnimType.ChannelData <float> >();
                var bChannel    = bGroup.GetMember("channels").GetMember("first").GetRawValue <BlendAddress>().DereferenceOne();
                while (bChannel != null)
                {
                    string boneName     = "";
                    string propertyName = "";
                    var    bRnaPath     = bChannel.GetMember("rna_path").GetRawValue <BlendAddress>().DereferenceAll(Blender.BlendPrimitiveType.Char());
                    string rnaPath      = Blender.ConvertUtil.CharArray2String(bRnaPath.Select(c => (object)c.GetRawValue <char>()));
                    if (!BlenderUtil.ParseRnaPath(rnaPath, ref boneName, ref propertyName))
                    {
                        Debug.Fail("Failed to parse rna path(" + rnaPath + ")");
                        return;
                    }
                    int arrayIndex = bChannel.GetMember("array_index").GetRawValue <int>();

                    if (boneName == groupData.BoneName)
                    {
                        //Console.WriteLine(String.Format("        {0}.{1}[{2}]", boneName, propertyName, arrayIndex));

                        var bBeztList = bChannel.GetMember("bezt").GetRawValue <BlendAddress>().DereferenceAll();
                        var channel   = new AnimType.ChannelData <float>();
                        channel.KeyFrames = new AnimType.KeyData <float> [bBeztList.Count()];

                        foreach (var bBezt in bBeztList.Select((value, index) => new { value, index }))
                        {
                            float frame = bBezt.value.GetMember("vec").GetAt(1, 0).GetRawValue <float>();
                            float value = bBezt.value.GetMember("vec").GetAt(1, 1).GetRawValue <float>();

                            channel.KeyFrames[bBezt.index] = new AnimType.KeyData <float>((int)frame, value);
                        }

                        channelList.Add(channel);
                    }

                    bChannel = bChannel.GetMember("next").GetRawValue <BlendAddress>().DereferenceOne();
                }                       // while

                if (channelList.Count() == 10)
                {
                    // channel type convertion
                    // location : floatx3 to Vector3
                    // rotation : floatx4 to Quatanion
                    // scale : floatx3 to Vector3
                    groupData.Location.KeyFrames
                        = channelList[0].KeyFrames
                          .Select((key, index) => new AnimType.KeyData <Vector3>(key.Frame, new Vector3(key.Value, channelList[1].KeyFrames[index].Value, channelList[2].KeyFrames[index].Value)))
                          .Select(key => { key.Value = BlenderUtil.ChangeCoordsSystem(key.Value); return(key); })
                          //.Select(key => { key.Frame--; return key; })	// blender frame index starts from 1
                          .ToArray();
                    groupData.Rotation.KeyFrames
                        = channelList[3].KeyFrames
                          .Select((key, index) => new AnimType.KeyData <Quaternion>(key.Frame, new Quaternion(channelList[4].KeyFrames[index].Value, channelList[5].KeyFrames[index].Value, channelList[6].KeyFrames[index].Value, key.Value)))
                          .Select(key => { key.Value = BlenderUtil.ChangeCoordsSystem(key.Value); return(key); })
                          //.Select(key => { key.Frame--; return key; })	// blender frame index starts from 1
                          .ToArray();
                    groupData.Scale.KeyFrames
                        = channelList[7].KeyFrames
                          .Select((key, index) => new AnimType.KeyData <Vector3>(key.Frame, new Vector3(key.Value, channelList[8].KeyFrames[index].Value, channelList[9].KeyFrames[index].Value)))
                          .Select(key => { key.Value = BlenderUtil.ChangeCoordsSystem(key.Value); return(key); })
                          //.Select(key => { key.Frame--; return key; })	// blender frame index starts from 1
                          .ToArray();
                    groupList.Add(groupData);

                    bGroup = bGroup.GetMember("next").GetRawValue <BlendAddress>().DereferenceOne();
                }
                else
                {
                    Debug.Fail("unexpected the number of channels.");
                    return;
                }
            }

            if (groupList.Count != 0)
            {
                var actionData = new AnimType.ActionData();
                var actionName = bAnimAction.GetMember("id").GetMember("name").GetAllValueAsString();
                actionName = actionName.Substring(2, actionName.Length - 2);                //  ACArmatureAction => ArmatureAction

                actionData.Name   = actionName;
                actionData.Groups = groupList.ToArray();
                animActionList.Add(actionData);
            }
        }