Пример #1
0
        private Type _Sample <Type>(float frame, AnimType.ChannelData <Type> channel, Type defaultValue, _IpoType <Type> ipo)
        {
            if (channel.KeyFrames.Count() == 0)
            {
                return(defaultValue);
            }
            else if (frame < channel.KeyFrames[0].Frame)
            {
                float t = frame / (float)channel.KeyFrames[0].Frame;
                return(ipo(defaultValue, channel.KeyFrames[0].Value, t));
            }

            int count = channel.KeyFrames.Count();

            for (int index = 0; index < count; ++index)
            {
                if (frame < channel.KeyFrames[index].Frame)
                {
                    float t = (frame - (float)channel.KeyFrames[index - 1].Frame) / ((float)channel.KeyFrames[index].Frame - (float)channel.KeyFrames[index - 1].Frame);
                    return(ipo(channel.KeyFrames[index - 1].Value, channel.KeyFrames[index].Value, t));
                }
            }

            // last frame
            return(channel.KeyFrames[count - 1].Value);
        }
Пример #2
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);
            }
        }