public ImportedObject[] Import(string path, ImportType type)
        {
            ImportedObject obj = new ImportedObject()
            {
                Result = Result.Failed,
                Type   = ImportedObjectType.Motion,
                Path   = path,
            };

            VmdImportResult res;
            {
                var importer = new VmdMotionImporter.VmdMotionImporter();
                res = importer.Import(path, type == ImportType.Full);
                if (res == null || res.result != VmdImportResult.Result.Success)
                {
                    return(new ImportedObject[] { obj });
                }
            }

            var vmd = res.vmd;

            obj.Result   = Result.Success;
            obj.Name     = Path.GetFileNameWithoutExtension(path);
            obj.Property = new ImportedProperty()
            {
                Name = vmd.Header.ModelName
            };

            if (type == ImportType.OverviewOnly)
            {
                obj.Result = Result.Success;
                return(new ImportedObject[] { obj });
            }

            var anim = new Motion();

            anim.Name = obj.Name;
            var max = 0;

            // bone
            {
                var dic = new Dictionary <string, BoneMotion>();
                foreach (var m in vmd.MotionList.Motions)
                {
                    BoneMotion bm = null;
                    if (!dic.ContainsKey(m.Name))
                    {
                        dic.Add(m.Name, bm = new BoneMotion()
                        {
                            BoneName = m.Name,
                            Keys     = new List <KeyFrame <BoneMotionValue> >(),
                        });
                    }
                    bm = dic[m.Name];

                    var key = new KeyFrame <BoneMotionValue>();
                    key.FrameNo = (int)m.FrameNo;
                    if (key.FrameNo > max)
                    {
                        max = key.FrameNo;
                    }
                    key.Value = new BoneMotionValue()
                    {
                        location = m.Location.ToVec3(true) * ImportScale,
                        rotation = m.Rotation.ToQuaternion(),
                        scale    = OpenTK.Vector3.One,
                    };

                    key.Interpolate = new BezierInterpolate()
                    {
                        p1 = new OpenTK.Vector2(m.BezierX1.W, m.BezierY1.W),
                        p2 = new OpenTK.Vector2(m.BezierX2.W, m.BezierY2.W),
                    };

                    bm.Keys.Add(key);
                }
                foreach (var bm in dic.Values)
                {
                    bm.Keys.Sort((k1, k2) => { return(k1.FrameNo - k2.FrameNo); });
                }
                anim.BoneMotions = dic;
            }

            // skin
            {
                var dic = new Dictionary <string, SkinMotion>();
                foreach (var s in vmd.SkinList.Skins)
                {
                    SkinMotion sm = null;
                    if (!dic.ContainsKey(s.Name))
                    {
                        dic.Add(s.Name, sm = new SkinMotion()
                        {
                            MorphName = s.Name,
                            Keys      = new List <KeyFrame <float> >(),
                        });
                    }
                    sm = dic[s.Name];

                    var key = new KeyFrame <float>();
                    key.FrameNo = (int)s.FrameNo;
                    if (key.FrameNo > max)
                    {
                        max = key.FrameNo;
                    }
                    key.Value = s.Weight;

                    key.Interpolate = Interpolates.Smoothstep;

                    sm.Keys.Add(key);
                }
                foreach (var sm in dic.Values)
                {
                    sm.Keys.Sort((k1, k2) => { return(k1.FrameNo - k2.FrameNo); });
                }
                anim.SkinMotions = dic;
            }

            anim.FrameNoMax = max;

            obj.Motions = new Motion[] { anim };
            obj.Result  = Result.Success;
            return(new ImportedObject[] { obj });
        }
Exemple #2
0
        public static Motion FromNwMotion(NwMotion motion)
        {
            var m = new Motion();

            m.Name = motion.Key;

            if (motion.BoneMotion != null)
            {
                m.BoneMotions = new Dictionary <string, BoneMotion>();
                foreach (var bm in motion.BoneMotion.Values)
                {
                    var bmv = new BoneMotion();
                    bmv.BoneName = bm.BoneName;
                    bmv.Keys     = new List <KeyFrame <BoneMotionValue> >();
                    foreach (var key in bm.Keys)
                    {
                        var f = new KeyFrame <BoneMotionValue>();
                        f.FrameNo = key.FrameNo;
                        if (key.FrameNo > m.FrameNoMax)
                        {
                            m.FrameNoMax = key.FrameNo;
                        }
                        f.Interpolate = new BezierInterpolate()
                        {
                            p1 = key.Interpolate.P1.FromVec2f(), p2 = key.Interpolate.P2.FromVec2f()
                        };
                        var r = key.Rotation.FromVec4f();
                        f.Value = new BoneMotionValue()
                        {
                            location = key.Location.FromVec3f(),
                            rotation = new Quaternion(r.X, r.Y, r.Z, r.W),
                            scale    = key.Scale.FromVec3f(),
                        };
                        bmv.Keys.Add(f);
                    }

                    m.BoneMotions.Add(bm.BoneName, bmv);
                }
            }

            if (motion.MorphMotion != null)
            {
                m.SkinMotions = new Dictionary <string, SkinMotion>();
                foreach (var mm in motion.MorphMotion.Values)
                {
                    var smv = new SkinMotion();
                    smv.MorphName = mm.MorphName;
                    smv.Keys      = new List <KeyFrame <float> >();
                    foreach (var key in mm.Keys)
                    {
                        var f = new KeyFrame <float>();
                        f.FrameNo = key.FrameNo;
                        if (key.FrameNo > m.FrameNoMax)
                        {
                            m.FrameNoMax = key.FrameNo;
                        }
                        f.Interpolate = new BezierInterpolate()
                        {
                            p1 = key.Interpolate.P1.FromVec2f(), p2 = key.Interpolate.P2.FromVec2f()
                        };
                        f.Value = key.Rate;
                        smv.Keys.Add(f);
                    }

                    m.SkinMotions.Add(mm.MorphName, smv);
                }
            }

            return(m);
        }