Exemple #1
0
        /**
         * Generate a triagle mesh for the hexagon
         */
        public ITriangleMesh GenerateMesh()
        {
            ITriangleMesh mesh = new TriangleMesh();

            Vector2[] texCoords      = new Vector2[7];
            Vector2   texCoordCenter = new Vector2(0.2f, 0.2f);

            for (int i = 0; i < 6; i++)
            {
                Vector3 v = GetCorner(i);
                mesh.AddVertex(v);
                texCoords[i] = getTexCoord(0.2f, texCoordCenter, i);
            }
            mesh.AddVertex(pos);
            texCoords[6] = texCoordCenter;

            int texCoordStartIndex = mesh.GetNumberOfTexCoords();

            for (int i = 0; i < 7; i++)
            {
                mesh.AddTextureCoordinate(texCoords[i]);
            }

            for (int i = 0; i < 6; i++)
            {
                Triangle t = new Triangle(i, 6, (i + 1) % 6, texCoordStartIndex + i, texCoordStartIndex + 6, texCoordStartIndex + (i + 1) % 6);
                mesh.AddTriangle(t);
            }

            mesh.ComputeTriangleNormals();
            return(mesh);
        }
Exemple #2
0
        public static ITriangleMesh Snap(ITriangleMesh mesh, float epsilon)
        {
            Dictionary <int, int> vertexMapping = new Dictionary <int, int>();
            ITriangleMesh         result        = new TriangleMesh();

            for (int i = 0; i < mesh.GetNumberOfVertices(); i++)
            {
                Vector3 vi    = mesh.GetVertex(i);
                bool    found = false;
                for (int j = 0; j < result.GetNumberOfVertices(); j++)
                {
                    Vector3 vj = result.GetVertex(j);
                    if (Vector3.Subtract(vi, vj).Length < epsilon)
                    {
                        vertexMapping[i] = j;
                        found            = true;
                    }
                }
                if (!found)
                {
                    int idx = result.AddVertex(vi);
                    vertexMapping[i] = idx;
                }
            }

            for (int i = 0; i < mesh.GetNumberOfTexCoords(); i++)
            {
                result.AddTextureCoordinate(mesh.GetTextureCoordinate(i));
            }

            for (int i = 0; i < mesh.GetNumberOfTriangles(); i++)
            {
                Triangle t = mesh.GetTriangle(i).Clone();
                t.A = vertexMapping[t.A];
                t.B = vertexMapping[t.B];
                t.C = vertexMapping[t.C];
                result.AddTriangle(t);
            }

            Console.WriteLine("Mesh snapping: " + mesh.GetNumberOfVertices() + " -> " + result.GetNumberOfVertices() + " verts.");
            return(result);
        }
        private ITriangleMesh CreateBoundary(ITriangleMesh mesh, float height, float borderHeightTop, float borderHeightBottom, Vector3 offset, Vector2[] cornerTexCoords)
        {
            List <TriangleEdge> border       = mesh.GetBorder();
            int           vertsPerBorderEdge = 3;
            ITriangleMesh borderMesh         = new TriangleMesh();

            for (int i = 0; i < border.Count; i++)
            {
                borderMesh.AddVertex(mesh.GetVertex(border[i].A));
            }
            for (int i = 0; i < border.Count; i++)
            {
                TriangleEdge borderEdge = border[i];
                TriangleEdge prevEdge   = border[(i + border.Count - 1) % border.Count];
                Vector3      v          = borderMesh.GetVertex(i);

                // v0
                Vector3 edgeNormal     = ComputeBorderEdgeNormal(borderEdge, mesh);
                Vector3 prevEdgeNormal = ComputeBorderEdgeNormal(prevEdge, mesh);
                Vector3 borderNormal   = Vector3.Add(edgeNormal, prevEdgeNormal);
                borderNormal.Normalize();
                borderNormal = Vector3.Multiply(borderNormal, Math.Max(borderHeightTop, borderHeightBottom) / (float)Math.Sin(30.0 / 180.0 * Math.PI));
                Vector3 v0 = Vector3.Add(Vector3.Add(v, borderNormal), new Vector3(0, 0, -borderHeightTop));
                borderMesh.AddVertex(v0);

                // v1
                Vector3 v1 = Vector3.Add(v0, Vector3.Multiply(Vector3.UnitZ, -(height - (borderHeightTop + borderHeightBottom))));
                borderMesh.AddVertex(v1);

                // v2
                Vector3 v2 = Vector3.Add(v, Vector3.Multiply(Vector3.UnitZ, -height));
                borderMesh.AddVertex(v2);
            }

            float alpha = (float)(borderHeightTop * Math.Sqrt(2) / (borderHeightTop * Math.Sqrt(2) + borderHeightBottom * Math.Sqrt(2) + height));
            float beta  = 1 - (float)(borderHeightBottom * Math.Sqrt(2) / (borderHeightTop * Math.Sqrt(2) + borderHeightBottom * Math.Sqrt(2) + height));
            int   ti0   = borderMesh.AddTextureCoordinate(cornerTexCoords[0]);
            int   ti1   = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[0], 1 - alpha), Vector2.Multiply(cornerTexCoords[1], alpha)));
            int   ti2   = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[0], 1 - beta), Vector2.Multiply(cornerTexCoords[1], beta)));
            int   ti3   = borderMesh.AddTextureCoordinate(cornerTexCoords[1]);
            int   ti_0  = borderMesh.AddTextureCoordinate(cornerTexCoords[2]);
            int   ti_1  = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[2], 1 - alpha), Vector2.Multiply(cornerTexCoords[3], alpha)));
            int   ti_2  = borderMesh.AddTextureCoordinate(Vector2.Add(Vector2.Multiply(cornerTexCoords[2], 1 - beta), Vector2.Multiply(cornerTexCoords[3], beta)));
            int   ti_3  = borderMesh.AddTextureCoordinate(cornerTexCoords[3]);

            // Triangles
            for (int i = 0; i < border.Count; i++)
            {
                TriangleEdge borderEdge = border[i];
                int          vi0        = border.Count + vertsPerBorderEdge * i + 0;
                int          vi1        = border.Count + vertsPerBorderEdge * i + 1;
                int          vi2        = border.Count + vertsPerBorderEdge * i + 2;
                int          vi_0       = border.Count + vertsPerBorderEdge * ((i + 1) % border.Count) + 0;
                int          vi_1       = border.Count + vertsPerBorderEdge * ((i + 1) % border.Count) + 1;
                int          vi_2       = border.Count + vertsPerBorderEdge * ((i + 1) % border.Count) + 2;
                // A
                Triangle t = new Triangle(i, vi0, (i + 1) % border.Count, ti0, ti1, ti_1);
                borderMesh.AddTriangle(t);
                // B
                t = new Triangle((i + 1) % border.Count, vi0, vi_0, ti_0, ti1, ti_1);
                borderMesh.AddTriangle(t);
                // C
                t = new Triangle(vi0, vi1, vi_0, ti1, ti2, ti_1);
                borderMesh.AddTriangle(t);
                // D
                t = new Triangle(vi1, vi_1, vi_0, ti2, ti_2, ti_1);
                borderMesh.AddTriangle(t);
                // E
                t = new Triangle(vi1, vi2, vi_1, ti2, ti3, ti_2);
                borderMesh.AddTriangle(t);
                // F
                t = new Triangle(vi2, vi_2, vi_1, ti3, ti_3, ti_2);
                borderMesh.AddTriangle(t);
            }

            return(borderMesh);
        }