public void ImportOld(string filename, ModelFile model)
        {
            var importer    = new DaeIMImpoter();
            Root            = importer.Import(filename);

            var skins       = Root.Instances.OfType<SkinDeclaraion>    ().ToArray();
            var morphs      = Root.Instances.OfType<MorphingDeclaraion>().ToArray();
            var geoms       = Root.Instances.OfType<Geometry>          ().ToArray();
            Vertices        = new Dictionary<ModelVertex, int>();
            Indices         = new List<int>();
              //Skins           = new Dictionary<int, ModelSkin>();

            if(morphs.Length > 0)
            {
                var geom    = morphs.First().Source as Geometry;
                var mesh    = geom.Get<Mesh>("Data");
                var posch   = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Position);
                var nrmch   = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Normal);
                var texch   = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.TexCoord);
                var pos     = posch.GetDataAsList<SlimDX.Vector3>();
                var nrm     = nrmch.GetDataAsList<SlimDX.Vector3>();
                var tex     = texch.GetDataAsList<SlimDX.Vector2>();
                var posface = posch.GetIndicesAsList(mesh);
                var nrmface = nrmch.GetIndicesAsList(mesh);
                var texface = texch.GetIndicesAsList(mesh);
                var numidx  = posface.Count;

                for(int i= 0; i < numidx; ++i)
                {
                    var p   = pos[posface[i]];
                    var n   = nrm[nrmface[i]];
                    var t   = tex[texface[i]];
                    var v   = new ModelVertex()
                    {
                        P   = new Vector3(p.X, p.Y, p.Z),
                        N   = new Vector3(n.X, n.Y, n.Z),
                        T   = new Vector2(t.X, t.Y),
                    };

                    int index;

                    if(!Vertices.TryGetValue(v, out index))
                        Vertices.Add(v, index= Vertices.Count);

                    Indices.Add(index);
                }

                var prims   = new List<ModelPrimitive>();
                var start   = 0;

                foreach(var i in mesh.Primitives)
                {
                    var prim        = new ModelPrimitive();
                    prim.Indices    = Indices.Skip(start).Take(i.Count).Select(j => (ushort)j).ToList();
                    prim.NumIndices = Indices.Count;
                    start           +=Indices.Count;

                    prims.Add(prim);
                }

                model.Mesh.Skins        = Vertices.Keys
                    .Select(i => model.Mesh.Skins[FindClosest(model.Mesh.Vertices, i.P)])
                    .ToList();
                model.Mesh.Vertices     = Vertices.Keys.ToList();
                model.Mesh.NumVerts     = model.Mesh.Vertices.Count;
                model.Mesh.Primitives   = prims;
                model.Mesh.NumPrims     = prims.Count;

                // モーフィング
                model.Params    = model.Params.Where(i => !(i is ParamMorph)).ToList();

                foreach(var i in morphs.First().Channels)
                {
                    var mmesh   = i.Geometry.Get<Mesh>("Data");
                    var mposch  = mmesh.Channels.FirstOrDefault(j => j.Semantic == GeometrySemantic.Position);
                    var mpos    = mposch.GetDataAsList<SlimDX.Vector3>();
                    var mposidx = mposch.GetIndicesAsList(mmesh);
                    var dic     = new Dictionary<int, MorphVertex>();
                    var mnrm    = new SlimDX.Vector3[mpos.Count];

                    // 法線計算
                    for(int j= 0; j < mposidx.Count; j+=3)
                    {
                        var a   = mpos[mposidx[j+0]];
                        var b   = mpos[mposidx[j+1]];
                        var c   = mpos[mposidx[j+2]];
                        var ab  = SlimDX.Vector3.Normalize(SlimDX.Vector3.Subtract(b, a));
                        var ac  = SlimDX.Vector3.Normalize(SlimDX.Vector3.Subtract(c, a));
                        var n   = SlimDX.Vector3.Normalize(SlimDX.Vector3.Cross(ab, ac));

                        mnrm[mposidx[j+0]]  = SlimDX.Vector3.Add(mnrm[mposidx[j+0]], n);
                        mnrm[mposidx[j+1]]  = SlimDX.Vector3.Add(mnrm[mposidx[j+1]], n);
                        mnrm[mposidx[j+2]]  = SlimDX.Vector3.Add(mnrm[mposidx[j+2]], n);
                    }

                    for(int j= 0; j < mnrm.Length; ++j)
                        mnrm[j] = SlimDX.Vector3.Normalize(mnrm[j]);

                    for(int j= 0; j < mposidx.Count; ++j)
                    {
                        var ii  = (ushort)Indices[j];
                        var v   = model.Mesh.Vertices[ii];
                        var p   = mpos[mposidx[j]];
                        var n   = mnrm[mposidx[j]];
                        p.X     -=v.P.X;
                        p.Y     -=v.P.Y;
                        p.Z     -=v.P.Z;
                        n.X     -=v.N.X;
                        n.Y     -=v.N.Y;
                        n.Z     -=v.N.Z;
                        dic[ii] = new MorphVertex(ii, p.X, p.Y, p.Z, n.X, n.Y, n.Z);
                    }

                    var morph   = new ParamMorph()
                    {
                        Name        = i.Geometry.Name,
                        Vertices    = dic.OrderBy(j => j.Key).Select(j => j.Value).ToList(),
                        NumVertices = dic.Count,
                    };

                    model.Params.Insert(0, morph);
                }
            } else
            {
                throw new Exception();
            }

            ModelFile.ToFile(model.FileName, model);

            System.Diagnostics.Debug.Print("終了");
        }
        public override void Import(string filename, ModelFile model)
        {
            //MorphRange      = MorphMax - MorphMin;

            var importer    = new DaeIMImpoter();
            Root            = importer.Import(filename);
            var geoms       = Root.Instances.OfType<Geometry>          ().ToArray();
            Vertices        = new Dictionary<ModelVertex, int>();
            Indices         = new List<int>();
            var geom        = geoms.First();
            var mesh        = geom.Get<Mesh>("Data");
            var posch       = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Position);
            var nrmch       = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.Normal);
            var texch       = mesh.Channels.FirstOrDefault(i => i.Semantic == GeometrySemantic.TexCoord);
            var pos         = posch.GetDataAsList<SlimDX.Vector3>();
            var nrm         = nrmch.GetDataAsList<SlimDX.Vector3>();
            var tex         = texch.GetDataAsList<SlimDX.Vector2>();
            var posface     = posch.GetIndicesAsList(mesh);
            var nrmface     = nrmch.GetIndicesAsList(mesh);
            var texface     = texch.GetIndicesAsList(mesh);
            var numidx      = posface.Count;

            for(int i= 0; i < numidx; ++i)
            {
                var p       = pos[posface[i]];
                var n       = nrm[nrmface[i]];
                var t       = tex[texface[i]];
                var v       = new ModelVertex()
                {
                    P       = new Vector3(p.X, p.Y, p.Z),
                    N       = new Vector3(n.X, n.Y, n.Z),
                    T       = new Vector2(t.X, t.Y),
                };

                int index;

                if(!Vertices.TryGetValue(v, out index))
                    Vertices.Add(v, index= Vertices.Count);

                Indices.Add(index);
            }

            var prims   = new List<ModelPrimitive>();
            var start   = 0;

            foreach(var i in mesh.Primitives)
            {
                var prim        = new ModelPrimitive();
                prim.Indices    = Indices.Skip(start).Take(i.Count).Select(j => (ushort)j).ToList();
                prim.NumIndices = Indices.Count;
                start           +=Indices.Count;

                prims.Add(prim);
            }

            model.Mesh.Skins        = Vertices.Keys
                .Select(i => model.Mesh.Skins[FindClosest(model.Mesh.Vertices, i.P)])
                .ToList();
            model.Mesh.Vertices     = Vertices.Keys.ToList();
            model.Mesh.NumVerts     = model.Mesh.Vertices.Count;
            model.Mesh.Primitives   = prims;
            model.Mesh.NumPrims     = prims.Count;

            // モーフィングをボディから反映
            var morphs      = model.Params.OfType<ParamMorph>().ToList();
            var body001     = DataManager.Instance.Body001;

            foreach(var i in morphs)
            {
                var index       = (ushort)0;
                var bodymorph   = body001.Params.FirstOrDefault(j => j.Name == i.Name) as ParamMorph;
                var dic         = bodymorph.Vertices.ToDictionary(j => j.Index);
                var dic2        = new Dictionary<int, MorphVertex>();
                var rr          = Settings.MorphMax - Settings.MorphMin;
                MorphVertex mv;

                foreach(var j in model.Mesh.Vertices)
                {
                    ++index;

                    var near    = FindClosest(body001.Mesh.Vertices, j.P);

                    if(!dic.TryGetValue((ushort)near, out mv))
                        continue;

                    var v       = body001.Mesh.Vertices[near];
                    var x       = v.P.X - j.P.X;
                    var y       = v.P.Y - j.P.Y;
                    var z       = v.P.Z - j.P.Z;
                    var d       = (float)Math.Sqrt(x*x + y*y + z*z);

                    if(d >= Settings.MorphMax)
                        continue;

                    var r       = d <= Settings.MorphMin ? 1.0f : 1.0f - (d-Settings.MorphMin) / rr;
                  //r           = r * MorphScale;
                    var mv2     = new MorphVertex(index-1, mv.X*r, mv.Y*r, mv.Z*r,  mv.NX*r, mv.NY*r, mv.NZ*r);

                    dic2.Add((ushort)(index-1), mv2);
                }

                i.Vertices      = dic2.OrderBy(j => j.Key).Select(j => j.Value).ToList();
                i.NumVertices   = i.Vertices.Count;
            }

            ModelFile.ToFile(model.FileName, model);

            System.Diagnostics.Debug.Print("終了");
        }
        /*
        private void Verify(List<ModelVertex> v1, List<ushort> i1, List<ModelVertex> v2, List<ushort> i2)
        {

        }
        */
        private void ImportOBJ()
        {
            if(null == data)
                return;

            var file    = Path.ChangeExtension(data.FileName, ".obj");
            var obj     = ObjFile.FromFile(file);

            obj.Dump();

            var g       = obj.Groups.FirstOrDefault(i => i.Name == "base");
            var p       = obj.Positions;
            var n       = obj.Normals;
            var t       = obj.TexCoords;
            var remap   = new VertexRemapper();

            foreach(var i in g.Meshes)
            {
                remap.BeginPrimitive();

                for(int j= 0; j < i.FaceCount; ++j)
                {
                    var fp  = i.PositionFaces[j];
                    var fn  = i.NormalFaces[j];
                    var ft  = i.TexCoordFaces[j];

                    remap.AddVertex(p[fp.A], n[fn.A], t[ft.A]);
                    remap.AddVertex(p[fp.B], n[fn.B], t[ft.B]);
                    remap.AddVertex(p[fp.C], n[fn.C], t[ft.C]);
                }

                remap.EndPrimitive();
            }

            //System.Diagnostics.Debug.Assert(remap.Vertices.Count == remap.Vertices.Distinct().Count());

            // ベースメッシュ変更
            var mesh        = Data.Mesh;
            var oldverts    = mesh.Vertices;
            mesh.NumVerts   = remap.Vertices.Count;
            mesh.Vertices   = remap.Vertices;
            mesh.NumPrims   = remap.Primitives.Count;
            mesh.Primitives = remap.Primitives
                .Select(i => new ModelPrimitive() { NumIndices= i.Count, Indices= i })
                .ToList();
            var skins       = new List<ModelSkin>(mesh.NumVerts);

            foreach(var i in mesh.Vertices)
            {
                var index   = FindClosest(oldverts, new Vector3(i.P.X, i.P.Y, i.P.Z));

                skins.Add(mesh.Skins[index]);
            }

            mesh.Skins      = skins;

            // モーフィング
            var morphs  = new List<ParamMorph>();

            foreach(var i in obj.Groups.Where(i => i.Name != "base"))
            {
                var name    = i.Name.Replace("morph_", "");
                var morph   = new Dictionary<int, MorphVertex>();

                #if false
                for(int j= 0; j < mesh.Vertices.Count; ++j)
                    morph[j]    = new MorphVertex(j, Vector3.Zero, Vector3.Zero);
                #else
                var index   = 0;

                foreach(var j in i.Meshes)
                {
                    for(int k= 0; k < j.FaceCount; ++k)
                    {
                        var ni  = remap.FaceRemap[index++];
                        var v0  = mesh.Vertices[ni];
                        var v1  = obj.Positions[j.PositionFaces[k].A];
                        v1      = new Vector3(-v1.X, -v1.Z, v1.Y);

                      //if(v0.X != v1.X || v0.Y != v1.Y || v0.Z != v1.Z)
                        {
                            v1.X    -=v0.P.X;
                            v1.Y    -=v0.P.Y;
                            v1.Z    -=v0.P.Z;
                            var n1  = obj.Normals[j.NormalFaces[k].A];
                            n1      = new Vector3(-n1.X, -n1.Z, n1.Y);
                            n1.X    -=v0.N.X;
                            n1.Y    -=v0.N.Y;
                            n1.Z    -=v0.N.Z;
                            morph[ni]= new MorphVertex(ni, v1, n1);
                        }

                        ni      = remap.FaceRemap[index++];
                        v0      = mesh.Vertices[ni];
                        v1      = obj.Positions[j.PositionFaces[k].B];
                        v1      = new Vector3(-v1.X, -v1.Z, v1.Y);

                      //if(v0.X != v1.X || v0.Y != v1.Y || v0.Z != v1.Z)
                        {
                            v1.X    -=v0.P.X;
                            v1.Y    -=v0.P.Y;
                            v1.Z    -=v0.P.Z;
                            var n1  = obj.Normals[j.NormalFaces[k].B];
                            n1      = new Vector3(-n1.X, -n1.Z, n1.Y);
                            n1.X    -=v0.N.X;
                            n1.Y    -=v0.N.Y;
                            n1.Z    -=v0.N.Z;
                            morph[ni]= new MorphVertex(ni, v1, n1);
                        }

                        ni      = remap.FaceRemap[index++];
                        v0      = mesh.Vertices[ni];
                        v1      = obj.Positions[j.PositionFaces[k].C];
                        v1      = new Vector3(-v1.X, -v1.Z, v1.Y);

                      //if(v0.X != v1.X || v0.Y != v1.Y || v0.Z != v1.Z)
                        {
                            v1.X    -=v0.P.X;
                            v1.Y    -=v0.P.Y;
                            v1.Z    -=v0.P.Z;
                            var n1  = obj.Normals[j.NormalFaces[k].C];
                            n1      = new Vector3(-n1.X, -n1.Z, n1.Y);
                            n1.X    -=v0.N.X;
                            n1.Y    -=v0.N.Y;
                            n1.Z    -=v0.N.Z;
                            morph[ni]= new MorphVertex(ni, v1, n1);
                        }
                    }
                }

                System.Diagnostics.Debug.Assert(remap.FaceRemap.Count == index);
                #endif

                morphs.Add(new ParamMorph()
                {
                    Name        = name,
                    NumVertices = morph.Count,
                    Vertices    = morph.ToArray().OrderBy(j => j.Key).Select(j => j.Value).ToList(),
                });
            }

            Data.Params = Data.Params.Where(i => !(i is ParamMorph)).ToList();
            Data.Params.InsertRange(0, morphs.Cast<Param>());

            /*
            var bak     = Path.ChangeExtension(Data.FileName, "*.@model");

            if(!File.Exists(bak))
                File.Copy(Data.FileName, bak);
            */

            ModelFile.ToFile(Data.FileName, Data);
              //ModelFile.ToFile(Path.ChangeExtension(Data.FileName, ".model"), Data);
        }