Example #1
0
        public NUD toNUD()
        {
            NUD n = new NUD();

            n.hasBones = false;

            foreach (OBJObject o in objects)
            {
                NUD.Mesh m = new NUD.Mesh();
                m.Text       = o.name;
                m.singlebind = -1;
                m.boneflag   = 0x08;

                foreach (OBJGroup g in o.groups)
                {
                    if (g.v.Count == 0)
                    {
                        continue;
                    }

                    NUD.Polygon p = new NUD.Polygon();
                    m.Nodes.Add(p);
                    m.Nodes.Add(p);
                    p.AddDefaultMaterial();
                    p.vertSize = 0x06;
                    p.UVSize   = 0x10;
                    p.polflag  = 0x00;

                    Dictionary <int, int> collected = new Dictionary <int, int>();

                    for (int i = 0; i < g.v.Count; i++)
                    {
                        p.faces.Add(p.vertices.Count);
                        NUD.Vertex v = new NUD.Vertex();
                        p.vertices.Add(v);
                        if (g.v.Count > i)
                        {
                            v.pos = this.v[g.v[i]] + Vector3.Zero;
                        }
                        if (g.vn.Count > i)
                        {
                            v.nrm = vn[g.vn[i]] + Vector3.Zero;
                        }
                        if (g.vt.Count > i)
                        {
                            v.uv.Add(vt[g.vt[i]] + Vector2.Zero);
                        }
                    }
                }
                if (m.Nodes.Count > 0)
                {
                    n.Nodes.Add(m);
                }
            }

            n.OptimizeFileSize();
            n.UpdateVertexData();

            return(n);
        }
Example #2
0
        public static NUD toNUD(string fname)
        {
            StreamReader reader = File.OpenText(fname);
            string       line;

            string current = "";

            NUD nud = new NUD();

            while ((line = reader.ReadLine()) != null)
            {
                line = Regex.Replace(line, @"\s+", " ");
                string[] args = line.Replace(";", "").TrimStart().Split(' ');

                if (args[0].Equals("triangles") || args[0].Equals("end"))
                {
                    current = args[0];
                    continue;
                }

                if (current.Equals("triangles"))
                {
                    string meshName = args[0];
                    if (args[0].Equals(""))
                    {
                        continue;
                    }
                    for (int j = 0; j < 3; j++)
                    {
                        line = reader.ReadLine();
                        line = Regex.Replace(line, @"\s+", " ");
                        args = line.Replace(";", "").TrimStart().Split(' ');
                        // read triangle strip
                        int        parent = int.Parse(args[0]);
                        NUD.Vertex vert   = new NUD.Vertex();
                        vert.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
                        vert.nrm = new Vector3(float.Parse(args[4]), float.Parse(args[5]), float.Parse(args[6]));
                        vert.uv.Add(new Vector2(float.Parse(args[7]), float.Parse(args[8])));
                        int wCount = int.Parse(args[9]);
                        int w      = 10;
                        for (int i = 0; i < wCount; i++)
                        {
                            vert.boneIds.Add(int.Parse(args[w++]));
                            vert.boneWeights.Add(float.Parse(args[w++]));
                        }

                        NUD.Mesh mes = null;
                        foreach (NUD.Mesh m in nud.Nodes)
                        {
                            if (m.Text.Equals(meshName))
                            {
                                mes = m;
                            }
                        }
                        if (mes == null)
                        {
                            mes      = new NUD.Mesh();
                            mes.Text = meshName;
                            nud.Nodes.Add(mes);
                        }
                        if (mes.Nodes.Count == 0)
                        {
                            NUD.Polygon poly = new NUD.Polygon();
                            poly.AddDefaultMaterial();
                            mes.Nodes.Add(poly);
                        }
                        {
                            ((NUD.Polygon)mes.Nodes[0]).faces.Add(((NUD.Polygon)mes.Nodes[0]).vertices.Count);
                            ((NUD.Polygon)mes.Nodes[0]).vertices.Add(vert);
                        }
                    }
                }
            }

            nud.OptimizeFileSize();
            nud.UpdateVertexData();
            return(nud);
        }