Exemple #1
0
    public Shape OnConvert(Mesh mesh)
    {
        MeshNormalizer             normalizer = new MeshNormalizer(mesh);
        List <UnityEngine.Vector3> vertices   = normalizer.UniqueVertices;
        List <int> triangles = normalizer.UniqueTriangles;

        List <Subdivision.Core.Point> points = new List <Subdivision.Core.Point>();

        foreach (var v in normalizer.UniqueVertices)
        {
            points.Add(new Subdivision.Core.Point(v.x, v.y, v.z));
        }

        List <Subdivision.Core.Edge> edges = new List <Subdivision.Core.Edge>();

        Shape shape = new Shape();

        for (int i = 0; i < normalizer.UniqueTriangles.Count / 3; i++)
        {
            Subdivision.Core.Point p0 = points[triangles[3 * i]];
            Subdivision.Core.Point p1 = points[triangles[3 * i + 1]];
            Subdivision.Core.Point p2 = points[triangles[3 * i + 2]];

            Subdivision.Core.Edge e0 = new Subdivision.Core.Edge(p0, p1);
            Subdivision.Core.Edge e1 = new Subdivision.Core.Edge(p1, p2);
            Subdivision.Core.Edge e2 = new Subdivision.Core.Edge(p2, p0);

            bool b0 = true;
            bool b1 = true;
            bool b2 = true;
            foreach (Subdivision.Core.Edge ee in edges)
            {
                if (e0.IsMatchFor(ee))
                {
                    e0 = ee; b0 = false;
                }
                if (e1.IsMatchFor(ee))
                {
                    e1 = ee; b1 = false;
                }
                if (e2.IsMatchFor(ee))
                {
                    e2 = ee; b2 = false;
                }
            }

            if (b0)
            {
                edges.Add(e0);
            }
            if (b1)
            {
                edges.Add(e1);
            }
            if (b2)
            {
                edges.Add(e2);
            }

            shape.AddFace(new Face(e0, e1, e2));
        }

        return(shape);
    }
        private void Subdivide()
        {
            // TODO: Save TCs and work with them properly.
            Shape p = new Shape();
            Dictionary <Location, Point> ps = new Dictionary <Location, Point>();

            for (int i = 0; i < BSSD.Verts[0].Count; i++)
            {
                Location t = new Location(BSSD.Verts[0][i]);
                if (!ps.ContainsKey(t))
                {
                    ps.Add(t, new Point(BSSD.Verts[0][i]));
                }
            }
            for (int i = 0; i < BSSD.Verts[0].Count; i += 3)
            {
                Point a = ps[new Location(BSSD.Verts[0][i])];
                Point b = ps[new Location(BSSD.Verts[0][i + 1])];
                Point c = ps[new Location(BSSD.Verts[0][i + 2])];
                if (i + 3 < BSSD.Verts[0].Count)
                {
                    Point a2 = ps[new Location(BSSD.Verts[0][i + 3])];
                    Point b2 = ps[new Location(BSSD.Verts[0][i + 4])];
                    Point c2 = ps[new Location(BSSD.Verts[0][i + 5])];
                    bool  ac = a2 == a || a2 == b || a2 == c;
                    bool  bc = b2 == a || b2 == b || b2 == c;
                    bool  cc = c2 == a || c2 == b || c2 == c;
                    if (ac && bc && cc)
                    {
                        SysConsole.Output(OutputType.WARNING, this + " has weird setup: " + a + ", " + b + ", " + c);
                        p.AddFace(SubdivisionUtilities.CreateFaceF(p.AllEdges, a, b, c));
                    }
                    else if (ac && cc)
                    {
                        p.AddFace(SubdivisionUtilities.CreateFaceF(p.AllEdges, a, b, b2, c));
                        i += 3;
                    }
                    else if (ac && bc)
                    {
                        p.AddFace(SubdivisionUtilities.CreateFaceF(p.AllEdges, a, b, c, c2));
                        i += 3;
                    }
                    else if (bc && cc)
                    {
                        p.AddFace(SubdivisionUtilities.CreateFaceF(p.AllEdges, a, b, c, a2));
                        i += 3;
                    }
                    else
                    {
                        p.AddFace(SubdivisionUtilities.CreateFaceF(p.AllEdges, a, b, c));
                    }
                }
                else
                {
                    p.AddFace(SubdivisionUtilities.CreateFaceF(p.AllEdges, a, b, c));
                }
            }
            CatmullClarkSubdivider cmcs = new CatmullClarkSubdivider();
            Shape          res          = cmcs.Subdivide(p);
            List <Vector3> vecs         = new List <Vector3>();
            List <Vector3> norms        = new List <Vector3>();
            List <Vector3> Tcs          = new List <Vector3>();

            foreach (Face face in res.Faces)
            {
                for (int i = 0; i < 3; i++)
                {
                    vecs.Add(face.AllPoints[i].Position);
                    norms.Add(face.Normal);
                    Tcs.Add(new Vector3(0, 0, BSSD.TCrds[0][0].Z));
                }
            }
            BSSD = new BlockShapeSubDetails();
            Vector3[] tcrds = Tcs.ToArray();
            for (int i = 0; i < BSSD.Verts.Length; i++)
            {
                BSSD.Verts[i] = vecs;
                BSSD.Norms[i] = norms;
                BSSD.TCrds[i] = tcrds;
            }
        }