Beispiel #1
0
        public static void AddPolygon(this IMeshBuilder meshBuilder, Vector2[] poly)
        {
            Triangulator triangulator = new Triangulator(poly);

            int[] tris = triangulator.Triangulate();

            for (int i = 0; i < poly.Length; i++)
            {
                var v = new Vector3(poly[i].x, 0f, poly[i].y);
                meshBuilder.AddVertex(v);
                meshBuilder.UVs.Add(new Vector2(v.x, v.z));
            }

            meshBuilder.SetTriangles(tris);
        }
Beispiel #2
0
        public static void BuildIcosahedron(this IMeshBuilder meshBuilder, float radius = 1f)
        {
            // create 12 vertices of a icosahedron
            float t = (1f + Mathf.Sqrt(5f)) / 2f;

            meshBuilder.AddVertex(new Vector3(-1f, t, 0f).normalized *radius);
            meshBuilder.AddVertex(new Vector3(1f, t, 0f).normalized *radius);
            meshBuilder.AddVertex(new Vector3(-1f, -t, 0f).normalized *radius);
            meshBuilder.AddVertex(new Vector3(1f, -t, 0f).normalized *radius);

            meshBuilder.AddVertex(new Vector3(0f, -1f, t).normalized *radius);
            meshBuilder.AddVertex(new Vector3(0f, 1f, t).normalized *radius);
            meshBuilder.AddVertex(new Vector3(0f, -1f, -t).normalized *radius);
            meshBuilder.AddVertex(new Vector3(0f, 1f, -t).normalized *radius);

            meshBuilder.AddVertex(new Vector3(t, 0f, -1f).normalized *radius);
            meshBuilder.AddVertex(new Vector3(t, 0f, 1f).normalized *radius);
            meshBuilder.AddVertex(new Vector3(-t, 0f, -1f).normalized *radius);
            meshBuilder.AddVertex(new Vector3(-t, 0f, 1f).normalized *radius);

            // create 20 triangles of the icosahedron

            // 5 faces around point 0
            meshBuilder.AddTriangle(0, 11, 5);
            meshBuilder.AddTriangle(0, 5, 1);
            meshBuilder.AddTriangle(0, 1, 7);
            meshBuilder.AddTriangle(0, 7, 10);
            meshBuilder.AddTriangle(0, 10, 11);

            // 5 adjacent faces
            meshBuilder.AddTriangle(1, 5, 9);
            meshBuilder.AddTriangle(5, 11, 4);
            meshBuilder.AddTriangle(11, 10, 2);
            meshBuilder.AddTriangle(10, 7, 6);
            meshBuilder.AddTriangle(7, 1, 8);

            // 5 faces around point 3
            meshBuilder.AddTriangle(3, 9, 4);
            meshBuilder.AddTriangle(3, 4, 2);
            meshBuilder.AddTriangle(3, 2, 6);
            meshBuilder.AddTriangle(3, 6, 8);
            meshBuilder.AddTriangle(3, 8, 9);

            // 5 adjacent faces
            meshBuilder.AddTriangle(4, 9, 5);
            meshBuilder.AddTriangle(2, 4, 11);
            meshBuilder.AddTriangle(6, 2, 10);
            meshBuilder.AddTriangle(8, 6, 7);
            meshBuilder.AddTriangle(9, 8, 1);
        }
Beispiel #3
0
        public static IMeshSelection ExtrudeEdge(
            this IMeshBuilder meshBuilder,
            IMeshSelection edge,
            Func <int, Vector3, Vector3> extrudeTranslationFunc)
        {
            Log.Trace("Will extrude edge {0}. Total vertices in mesh = {1}", edge, meshBuilder.Vertices.Count);

            var lowerEdge    = new MeshSelection(edge);
            var extrudedEdge = new MeshSelection();

            // Check if we're dealing with a closed selection loop.
            var isClosed = lowerEdge.IsClosed;

            if (isClosed)
            {
                Log.Trace("The edge has the same start and end point {0}", lowerEdge[0]);
            }

            // Extrude new vertices from the current source vertices.
            for (int i = 0; i < lowerEdge.Count; i++)
            {
                var extrudeIdx = lowerEdge[i];
                var p          = meshBuilder.Vertices[extrudeIdx];

                Log.Trace("extrudeIdx = {0}", extrudeIdx);
                // TODO: Need to skip duplicate indexes when the start/end range is wrapped.

                var translation  = extrudeTranslationFunc(i, p);
                var newVertexIdx = meshBuilder.AddVertex(p + translation);
                extrudedEdge.Add(newVertexIdx);

                // TODO: This currently assumes the normal won't be changed by the translation. Need fix
                // support rotation as well.
                meshBuilder.Normals.Add(meshBuilder.Normals[extrudeIdx]);
            }

            if (isClosed)
            {
                extrudedEdge.Add(extrudedEdge[0]);
            }

            Log.Trace("Building {0} quads", edge.Count);

            Assert.AreEqual(lowerEdge.Count, extrudedEdge.Count, "Number of extruded indices should match the number of lower indices");

            Log.Trace("lowerEdge = {0}", lowerEdge);
            Log.Trace("extrudedEdge = {0}", extrudedEdge);

            // Build quads between the start vertices and the extruded vertices, using the virtual
            // selection which includes the closing vertex index.
            for (int i = 0; i < lowerEdge.VirtualCount - 1; i++)
            {
                var bl = lowerEdge.GetAtVirtualIndex(i);
                var tl = extrudedEdge.GetAtVirtualIndex(i);
                var tr = extrudedEdge.GetAtVirtualIndex(i + 1);
                var br = lowerEdge.GetAtVirtualIndex(i + 1);

                Log.Trace("Building quad: verts=[{0},{1},{2},{3}], indices=[{4},{5},{6},{7}]",
                          meshBuilder.Vertices[bl],
                          meshBuilder.Vertices[tl],
                          meshBuilder.Vertices[tr],
                          meshBuilder.Vertices[br],
                          bl,
                          tl,
                          tr,
                          br);

                meshBuilder.AddTriangle(bl, tl, br);
                meshBuilder.AddTriangle(br, tl, tr);
            }

            return(extrudedEdge);
        }