當前位置: 首頁>>代碼示例>>C#>>正文


C# DTSweepContext.PrepareTriangulation方法代碼示例

本文整理匯總了C#中Poly2Tri.DTSweepContext.PrepareTriangulation方法的典型用法代碼示例。如果您正苦於以下問題:C# DTSweepContext.PrepareTriangulation方法的具體用法?C# DTSweepContext.PrepareTriangulation怎麽用?C# DTSweepContext.PrepareTriangulation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Poly2Tri.DTSweepContext的用法示例。


在下文中一共展示了DTSweepContext.PrepareTriangulation方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: CreateMesh

    private void CreateMesh() {
        Sprite sprite = spriteRenderer.sprite;

        Rect bounds = GetBounds(polygon);

        DTSweepContext ctx = new DTSweepContext();
        Polygon poly = new Polygon(polygon.Select(p => new PolygonPoint(p.x, p.y)));

        ctx.PrepareTriangulation(poly);
        DTSweep.Triangulate(ctx);

        List<Vector2> verts = new List<Vector2>();
        List<int> tris = new List<int>();

        foreach (DelaunayTriangle tri in poly.Triangles) {
            verts.AddRange(tri.Points.Reverse().Select(p => new Vector2(p.Xf, p.Yf)));
            for (int i = 0; i < 3; i++) {
                tris.Add(tris.Count);
            }
        }

        Mesh mesh = new Mesh();
        mesh.vertices = verts.Select(x => (Vector3)x).ToArray();
        mesh.triangles = tris.ToArray();

        List<Vector2> uv = new List<Vector2>();

        Vector3 lower = new Vector3(bounds.x, bounds.y);
        Vector3 size = new Vector3(bounds.xMax, bounds.yMax) - lower;

        Rect uv_bounds = new Rect(sprite.rect.x / sprite.texture.width, sprite.rect.y / sprite.texture.height, sprite.rect.width / sprite.texture.width, sprite.rect.height / sprite.texture.height);

        float scalex = sprite.bounds.size.x / bounds.width;
        float scaley = sprite.bounds.size.y / bounds.height;

        Vector3[] scaled = mesh.vertices;

        for (int i = 0; i < mesh.vertices.Length; i++) {
            Vector3 v = scaled[i];
            Vector3 rel = v - lower;
            uv.Add(new Vector2(rel.x / size.x * uv_bounds.width, rel.y / size.y * uv_bounds.height) + new Vector2(uv_bounds.x, uv_bounds.y));

            scaled[i] = new Vector3(v.x * scalex, v.y * scaley, v.z) - ((Vector3)bounds.center * scalex) + sprite.bounds.center;
        }

        mesh.vertices = scaled;
        mesh.uv = uv.ToArray();
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mesh.Optimize();

        //GameObject go = new GameObject();
        //MeshFilter mf = go.AddComponent<MeshFilter>();
        //mf.sharedMesh = mesh;
        //MeshRenderer mr = go.AddComponent<MeshRenderer>();
        //mr.sharedMaterial = spriteRenderer.sharedMaterial;

        ScriptableObjectUtility.CreateAsset(mesh);
    }
開發者ID:joaogameprog,項目名稱:CamoStealth_Unity,代碼行數:59,代碼來源:SkinMesh.cs

示例2: CreateMesh

    /// <summary>
    /// Create a Mesh from a given Polygon.
    /// </summary>
    /// <returns>The freshly minted mesh.</returns>
    /// <param name="polygon">Polygon you want to triangulate.</param>
    public static Mesh CreateMesh(Polygon polygon)
    {
        // Ensure we have the rotation properly calculated
        if (polygon.rotation == Quaternion.identity) polygon.CalcRotation();

        // Rotate 1 point and note where it ends up in Z
        float z = (polygon.rotation * polygon.outside[0]).z;

        // Convert the outside points (throwing out Z at this point)
        Poly2Tri.Polygon poly = new Poly2Tri.Polygon(ConvertPoints(polygon.outside, polygon.rotation));

        // Convert each of the holes
        foreach (List<Vector3> hole in polygon.holes) {
            poly.AddHole(new Poly2Tri.Polygon(ConvertPoints(hole, polygon.rotation)));
        }

        // Triangulate it!  Note that this may throw an exception if the data is bogus.
        DTSweepContext tcx = new DTSweepContext();
        tcx.PrepareTriangulation(poly);
        DTSweep.Triangulate(tcx);
        tcx = null;

        // Create the Vector3 vertices (undoing the rotation),
        // and also build a map of vertex codes to indices
        Quaternion invRot = Quaternion.Inverse(polygon.rotation);
        Dictionary<uint, int> codeToIndex = new Dictionary<uint, int>();
        List<Vector3> vertexList = new List<Vector3>();
        foreach (DelaunayTriangle t in poly.Triangles) {
            foreach (var p in t.Points) {
                if (codeToIndex.ContainsKey(p.VertexCode)) continue;
                codeToIndex[p.VertexCode] = vertexList.Count;
                Vector3 pos = new Vector3(p.Xf, p.Yf, z);	// (restore the Z we saved earlier)
                vertexList.Add(invRot * pos);
            }
        }

        // Create the indices array
        int[] indices = new int[poly.Triangles.Count * 3];
        {
            int i = 0;
            foreach (DelaunayTriangle t in poly.Triangles) {
                indices[i++] = codeToIndex[t.Points[0].VertexCode];
                indices[i++] = codeToIndex[t.Points[1].VertexCode];
                indices[i++] = codeToIndex[t.Points[2].VertexCode];
            }
        }

        // Create the UV list, by looking up the closest point for each in our poly
        Mesh msh = new Mesh();
        msh.vertices = vertexList.ToArray();
        Vector2[] uvs = new Vector2[msh.vertices.Length];
        for (int i=0; i < uvs.Length; i++) {
            uvs[i] = new Vector2(msh.vertices[i].x, msh.vertices[i].y);
        }
        msh.uv = uvs;
        /*if (polygon.OutsideUVs != null) {
            uv = new Vector2[vertexList.Count];
            for (int i=0; i<vertexList.Count; i++) {
                uv[i] = polygon.ClosestUV(vertexList[i]);
            }
        }*/

        // Create the mesh

        msh.triangles = indices;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        return msh;
    }
開發者ID:Ruixel,項目名稱:CYplayer,代碼行數:74,代碼來源:Poly2Mesh.cs

示例3: CreateMesh

    private static Mesh CreateMesh(Sprite sprite, Vector2[] polygon)
    {
        if (sprite != null && polygon != null) {
            Rect bounds = GetBounds(polygon);

            DTSweepContext ctx = new DTSweepContext();
            Polygon poly = new Polygon(polygon.Select(p => new PolygonPoint(p.x, p.y)));

            ctx.PrepareTriangulation(poly);
            DTSweep.Triangulate(ctx);

            List<Vector2> verts = new List<Vector2>();
            List<int> tris = new List<int>();

            foreach (DelaunayTriangle tri in poly.Triangles) {
                verts.AddRange(tri.Points.Reverse().Select(p => new Vector2(p.Xf, p.Yf)));
                for (int i = 0; i < 3; i++) {
                    tris.Add(tris.Count);
                }
            }

            Mesh mesh = new Mesh();
            mesh.vertices = verts.Select(x => (Vector3)x).ToArray();
            mesh.triangles = tris.ToArray();

            List<Vector2> uv = new List<Vector2>();

            Vector3 lower = new Vector3(bounds.x, bounds.y);
            Vector3 size = new Vector3(bounds.xMax, bounds.yMax) - lower;

            Rect uvBounds = new Rect(sprite.rect.x / sprite.texture.width, sprite.rect.y / sprite.texture.height,
                sprite.rect.width / sprite.texture.width, sprite.rect.height / sprite.texture.height);

            float scalex = sprite.bounds.size.x / bounds.width;
            float scaley = sprite.bounds.size.y / bounds.height;

            Vector3[] scaled = mesh.vertices;

            for (int i = 0; i < mesh.vertices.Length; i++) {
                Vector3 v = scaled[i];
                Vector3 rel = v - lower;
                uv.Add(new Vector2(rel.x / size.x * uvBounds.width, rel.y / size.y * uvBounds.height) +
                       new Vector2(uvBounds.x, uvBounds.y));

                scaled[i] = new Vector3(v.x * scalex, v.y * scaley, v.z) - ((Vector3)bounds.center * scalex) +
                            sprite.bounds.center;
            }

            mesh.vertices = scaled;
            mesh.uv = uv.ToArray();
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
            mesh.Optimize();

            return mesh;
        }

        return null;
    }
開發者ID:shkilevk,項目名稱:Jury-rig-a-2D-game-workshop,代碼行數:59,代碼來源:SpriteMesh.cs


注:本文中的Poly2Tri.DTSweepContext.PrepareTriangulation方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。