Beispiel #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.setDefaultMaterial();
                    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.PreRender();

            return(n);
        }
Beispiel #2
0
        public NUD toNUD()
        {
            NUD nud = new NUD();
            int j   = 0;

            foreach (Mesh m in mesh)
            {
                NUD.Mesh n_mesh = new NUD.Mesh();
                nud.mesh.Add(n_mesh);
                n_mesh.Text = "Mesh_" + j++;
                foreach (List <int> i in m.faces)
                {
                    NUD.Polygon poly = new NUD.Polygon();
                    n_mesh.polygons.Add(poly);
                    poly.setDefaultMaterial();

                    List <Vertex> indexSim = new List <Vertex>();

                    foreach (int index in i)
                    {
                        Vertex v = vertices[index];

                        if (!indexSim.Contains(v))
                        {
                            indexSim.Add(v);
                            NUD.Vertex vert = new NUD.Vertex();
                            vert.pos = v.pos;
                            vert.nrm = v.nrm;
                            vert.col = v.col;
                            List <Vector2> uvs = new List <Vector2>();
                            uvs.Add(new Vector2(v.tx[0].X, 1 - v.tx[0].Y));
                            vert.tx = uvs;
                            if (vert.weight.Count < 4)
                            {
                                v.weight.Add(0f);
                                v.weight.Add(0f);
                            }
                            vert.weight = v.weight;
                            List <int> nodez = new List <int>();
                            nodez.Add(m.nodeList[0][v.node[0]]);
                            nodez.Add(m.nodeList[0][v.node[1]]);
                            nodez.Add(0);
                            nodez.Add(0);
                            vert.node = nodez;
                            poly.AddVertex(vert);
                        }

                        poly.faces.Add(indexSim.IndexOf(v));
                    }
                }
            }
            return(nud);
        }
Beispiel #3
0
        public NUD toNUD()
        {
            NUD nud = new NUD();
            int j   = 0;

            foreach (Mesh m in mesh)
            {
                NUD.Mesh n_mesh = new NUD.Mesh();
                nud.mesh.Add(n_mesh);
                n_mesh.Text = "Mesh_" + j++;
                foreach (List <int> i in m.faces)
                {
                    NUD.Polygon poly = new NUD.Polygon();
                    n_mesh.polygons.Add(poly);
                    poly.setDefaultMaterial();

                    List <Vertex> indexSim = new List <Vertex>();

                    foreach (int index in i)
                    {
                        Vertex v = vertices[index];

                        if (!indexSim.Contains(v))
                        {
                            indexSim.Add(v);
                            NUD.Vertex vert = new NUD.Vertex();
                            vert.pos    = v.pos;
                            vert.nrm    = v.nrm;
                            vert.col    = v.col;
                            vert.tx     = v.tx;
                            vert.weight = v.weight;
                            vert.node   = v.node;
                            poly.AddVertex(vert);
                        }

                        poly.faces.Add(indexSim.IndexOf(v));
                    }
                }
            }
            return(nud);
        }
Beispiel #4
0
        public static NUD.Polygon readPoly(string input)
        {
            NUD.Polygon poly = new NUD.Polygon();
            poly.setDefaultMaterial();

            string[] lines = input.Replace("  ", " ").Split('\n');

            int vi = 0;

            NUD.Vertex v;
            for (int i = 0; i < lines.Length; i++)
            {
                string[] args = lines[i].Split(' ');
                switch (args[0])
                {
                case "v":
                    v       = new NUD.Vertex();
                    v.pos.X = float.Parse(args[1]);
                    v.pos.Y = float.Parse(args[2]);
                    v.pos.Z = float.Parse(args[3]);
                    v.node.Add(-1);
                    v.weight.Add(1);
                    poly.vertices.Add(v);
                    break;

                case "vt":
                    v = poly.vertices[vi++];
                    v.tx.Add(new Vector2(float.Parse(args[1]), float.Parse(args[2])));
                    break;

                case "f":
                    poly.faces.Add(int.Parse(args[1].Split('/')[0]) - 1);
                    poly.faces.Add(int.Parse(args[2].Split('/')[0]) - 1);
                    poly.faces.Add(int.Parse(args[3].Split('/')[0]) - 1);
                    break;
                }
            }

            return(poly);
        }
Beispiel #5
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.node.Add(int.Parse(args[w++]));
                            vert.weight.Add(float.Parse(args[w++]));
                        }

                        NUD.Mesh mes = null;
                        foreach (NUD.Mesh m in nud.meshes)
                        {
                            if (m.Text.Equals(meshName))
                            {
                                mes = m;
                            }
                        }
                        if (mes == null)
                        {
                            mes = new NUD.Mesh();
                            mes.Text = meshName;
                            nud.meshes.Add(mes);
                        }
                        if (mes.Nodes.Count == 0)
                        {
                            NUD.Polygon poly = new NUD.Polygon();
                            poly.setDefaultMaterial();
                            mes.Nodes.Add(poly);
                        }
                        bool found = false;
                        foreach (NUD.Vertex nv in ((NUD.Polygon)mes.Nodes[0]).vertices)
                        {
                            if (nv.pos.Equals(vert.pos))
                            {
                                ((NUD.Polygon)mes.Nodes[0]).faces.Add(((NUD.Polygon)mes.Nodes[0]).vertices.IndexOf(nv));
                                found = true;
                                break;
                            }
                        }
                        if (!found)
                        {
                            ((NUD.Polygon)mes.Nodes[0]).faces.Add(((NUD.Polygon)mes.Nodes[0]).vertices.Count);
                            ((NUD.Polygon)mes.Nodes[0]).vertices.Add(vert);
                        }
                    }
                }
            }

            nud.PreRender();
            return nud;
        }