public DTSweepDebugContext(DTSweepContext tcx)
     : base(tcx)
 {
 }
Esempio n. 2
0
    /// <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)
    {
        Profiler.BeginSample("Poly2Mesh.CreateMesh");

        // Check for the easy case (a triangle)
        if (polygon.holes.Count == 0 && (polygon.outside.Count == 3 ||
                                         (polygon.outside.Count == 4 && polygon.outside[3] == polygon.outside[0])))
        {
            return(CreateTriangle(polygon));
        }

        // Ensure we have the rotation properly calculated, and have a valid normal
        if (polygon.rotation == Quaternion.identity)
        {
            polygon.CalcRotation();
        }
        if (polygon.planeNormal == Vector3.zero)
        {
            return(null);                                                       // bad data
        }
        // Rotate 1 point and note where it ends up in Z
        float z = (polygon.rotation * polygon.outside[0]).z;

        // Prepare a map from vertex codes to 3D positions.
        Dictionary <uint, Vector3> codeToPosition = new Dictionary <uint, Vector3>();

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

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

        // Triangulate it!  Note that this may throw an exception if the data is bogus.
        try {
            DTSweepContext tcx = new DTSweepContext();
            tcx.PrepareTriangulation(poly);
            DTSweep.Triangulate(tcx);
            tcx = null;
        } catch (System.Exception e) {
            Profiler.EndSample();
            throw e;
        }

        // Now, to get back to our original positions, use our code-to-position map.  We do
        // this instead of un-rotating to be a little more robust about noncoplanar polygons.

        // Create the Vector3 vertices (undoing the rotation),
        // and also build a map of vertex codes to indices
        Quaternion?            invRot      = null;
        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;
                if (!codeToPosition.TryGetValue(p.VertexCode, out pos))
                {
                    // This can happen in rare cases when we're hitting limits of floating-point precision.
                    // Rather than fail, let's just do the inverse rotation.
                    Debug.Log("Vertex code lookup failed; using inverse rotation.");
                    if (!invRot.HasValue)
                    {
                        invRot = Quaternion.Inverse(polygon.rotation);
                    }
                    pos = invRot.Value * new Vector3(p.Xf, p.Yf, z);
                }
                vertexList.Add(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
        Vector2[] uv = null;
        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
        Mesh msh = new Mesh();

        msh.vertices  = vertexList.ToArray();
        msh.triangles = indices;
        msh.uv        = uv;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        Profiler.EndSample();
        return(msh);
    }
    public static Mesh CreateMesh(Polygon polygon)
    {
        if (polygon.holes.Count == 0 && (polygon.outside.Count == 3 || (polygon.outside.Count == 4 && polygon.outside[3] == polygon.outside[0])))
        {
            return(CreateTriangle(polygon));
        }

        Dictionary <uint, Vector2> codeToPosition = new Dictionary <uint, Vector2>();

        Polygon2DTriangulation.Polygon poly = new Polygon2DTriangulation.Polygon(ConvertPoints(polygon.outside, codeToPosition));

        foreach (List <Vector2> hole in polygon.holes)
        {
            poly.AddHole(new Polygon2DTriangulation.Polygon(ConvertPoints(hole, codeToPosition)));
        }

        try {
            DTSweepContext tcx = new DTSweepContext();
            tcx.PrepareTriangulation(poly);
            DTSweep.Triangulate(tcx);
            tcx = null;
        } catch (System.Exception e) {
            throw(e);
        }

        Dictionary <uint, int> codeToIndex = new Dictionary <uint, int>();
        List <Vector2>         vertexList  = new List <Vector2>();

        foreach (DelaunayTriangle t in poly.Triangles)
        {
            foreach (var p in t.Points)
            {
                if (codeToIndex.ContainsKey(p.VertexCode))
                {
                    continue;
                }

                codeToIndex[p.VertexCode] = vertexList.Count;

                Vector2 pos;
                if (!codeToPosition.TryGetValue(p.VertexCode, out pos))
                {
                    pos = new Vector2(p.Xf, -p.Yf);
                }

                vertexList.Add(pos);
            }
        }

        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];
            }
        }

        Vector2[] uv = null;
        if (polygon.outsideUVs != null)
        {
            uv = new Vector2[vertexList.Count];
            for (int i = 0; i < vertexList.Count; i++)
            {
                uv[i] = polygon.ClosestUV(vertexList[i]);
            }
        }

        return(CreateMesh(vertexList.ToArray(), indices, uv));
    }
Esempio n. 4
0
    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);
    }
Esempio n. 5
0
    public static Mesh CreateXYMesh(Polygon polygon)
    {
        //AML Maybe pass this as arg and reduce coords to XY
        float z = polygon.outside[0].z;

        // Check for the easy case (a triangle)
        if (polygon.holes.Count == 0 && (polygon.outside.Count == 3 ||
                                         (polygon.outside.Count == 4 && polygon.outside[3] == polygon.outside[0])))
        {
            return(CreateTriangle(polygon));
        }
        // Convert the outside points (throwing out Z at this point)
        Poly2Tri.Polygon poly = new Poly2Tri.Polygon(ConvertPoints(polygon.outside));
        // Convert each of the holes
        foreach (List <Vector3> hole in polygon.holes)
        {
            poly.AddHole(new Poly2Tri.Polygon(ConvertPoints(hole)));
        }
        // Triangulate it!  Note that this may throw an exception if the data is bogus.
        try {
            DTSweepContext tcx = new DTSweepContext();
            tcx.PrepareTriangulation(poly);
            DTSweep.Triangulate(tcx);
            tcx = null;
        } catch (System.Exception e) {
            //Profiler.Exit(profileID);
            throw e;
        }
        // Create the indices array
        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;
                vertexList.Add(new Vector3(p.Xf, p.Yf, z));
            }
        }
        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
        Vector2[] uv = null;
        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
        Mesh msh = new Mesh();

        msh.vertices  = vertexList.ToArray();
        msh.triangles = indices;
        msh.uv        = uv;
        msh.RecalculateNormals();
        msh.RecalculateBounds();
        return(msh);
    }
Esempio n. 6
0
 public DTSweepDebugContext(DTSweepContext tcx) : base(tcx)
 {
 }
Esempio n. 7
0
    /// <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 bool CreateMesh(List <Poly2Mesh.Polygon> polygons, ref Mesh dstMesh)
    {
        // TODO: use vertex indices instead of actual vertices to find original vertices

        var poly2TriPolygons = new List <Poly2Tri.Polygon>();
        var codeToPositions  = new List <Dictionary <uint, Vector3> >();
        var zs = new List <float>();

        // Ensure we have the rotation properly calculated, and have a valid normal
        for (int p = 0; p < polygons.Count; p++)
        {
            if (polygons[p].rotation == Quaternion.identity)
            {
                polygons[p].CalcRotation();
            }
            if (polygons[p].planeNormal == Vector3.zero)
            {
                Debug.Log("polygons[p].planeNormal == Vector3.zero");
                return(false);       // bad data
            }

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

            // Prepare a map from vertex codes to 3D positions.
            Dictionary <uint, Vector3> codeToPosition = new Dictionary <uint, Vector3>();

            // Convert the outside points (throwing out Z at this point)
            Poly2Tri.Polygon poly = new Poly2Tri.Polygon(ConvertPoints(polygons[p].outside, polygons[p].rotation, codeToPosition));


            // Convert each of the holes
            if (polygons[p].holes != null)
            {
                foreach (List <Vector3> hole in polygons[p].holes)
                {
                    poly.AddHole(new Poly2Tri.Polygon(ConvertPoints(hole, polygons[p].rotation, codeToPosition)));
                }
            }

            codeToPositions.Add(codeToPosition);
            poly2TriPolygons.Add(poly);
            zs.Add(z);
        }

        // Triangulate it!  Note that this may throw an exception if the data is bogus.
        for (int p = 0; p < poly2TriPolygons.Count; p++)
        {
            try
            {
                DTSweepContext tcx = new DTSweepContext();
                tcx.PrepareTriangulation(poly2TriPolygons[p]);
                DTSweep.Triangulate(tcx);
                tcx = null;
            } catch (System.Exception e) {
                //Profiler.Exit(profileID);
                Debug.LogException(e);
                //throw e;
            }
        }

        // Now, to get back to our original positions, use our code-to-position map.  We do
        // this instead of un-rotating to be a little more robust about noncoplanar polygons.

        // Create the Vector3 vertices (undoing the rotation),
        // and also build a map of vertex codes to indices
        Quaternion?            invRot      = null;
        Dictionary <uint, int> codeToIndex = new Dictionary <uint, int>();
        List <Vector3>         vertexList  = new List <Vector3>();
        List <int>             indexList   = new List <int>();
        int triangleCount = 0;

        for (int p = 0; p < polygons.Count; p++)
        {
            var poly           = poly2TriPolygons[p];
            var polygon        = polygons[p];
            var z              = zs[p];
            var codeToPosition = codeToPositions[p];
            triangleCount += poly.Triangles.Count;
            codeToIndex.Clear();
            foreach (DelaunayTriangle t in poly.Triangles)
            {
                foreach (var point in t.Points)
                {
                    if (codeToIndex.ContainsKey(point.VertexCode))
                    {
                        continue;
                    }
                    codeToIndex[point.VertexCode] = vertexList.Count;
                    Vector3 pos;
                    if (!codeToPosition.TryGetValue(point.VertexCode, out pos))
                    {
                        // This can happen in rare cases when we're hitting limits of floating-point precision.
                        // Rather than fail, let's just do the inverse rotation.
                        Debug.LogWarning("Vertex code lookup failed; using inverse rotation.");
                        if (!invRot.HasValue)
                        {
                            invRot = Quaternion.Inverse(polygon.rotation);
                        }
                        pos = invRot.Value * new Vector3(point.Xf, point.Yf, z);
                    }
                    vertexList.Add(pos);
                }
            }
            if (polygon.inverse)
            {
                foreach (DelaunayTriangle t in poly.Triangles)
                {
                    indexList.Add(codeToIndex[t.Points[2].VertexCode]);
                    indexList.Add(codeToIndex[t.Points[1].VertexCode]);
                    indexList.Add(codeToIndex[t.Points[0].VertexCode]);
                }
            }
            else
            {
                foreach (DelaunayTriangle t in poly.Triangles)
                {
                    indexList.Add(codeToIndex[t.Points[0].VertexCode]);
                    indexList.Add(codeToIndex[t.Points[1].VertexCode]);
                    indexList.Add(codeToIndex[t.Points[2].VertexCode]);
                }
            }
        }

        // Create the indices array
        var indices = indexList.ToArray();

        // Create the mesh
        dstMesh.vertices  = vertexList.ToArray();
        dstMesh.triangles = indices;
        dstMesh.RecalculateNormals();
        return(true);
    }
Esempio n. 8
0
    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);
    }
Esempio n. 9
0
    public static MeshProperties[] CreateMesh(List <BoundaryPoint> genPoly, Transform objTrans, float spriteSquareSize)
    {
        const int   _FRONTOFFSET     = 3;
        const float _BACKFACE_OFFSET = 0.5f;
        const float _SCALE_FACTOR    = 1.1f;

        MeshProperties _generatedMesh;
        MeshProperties _frontFaceMesh;

        Vector2[] _genPolyArrFront = new Vector2[genPoly.Count];

        List <VertexProperties> _verts = new List <VertexProperties>();
        List <VertexProperties> _frontFaceVerticies = new List <VertexProperties>();

        List <int> _indicies          = new List <int>();
        List <int> _frontFaceIndicies = new List <int>();

        //ConvertingToArray
        for (int i = 0; i < genPoly.Count; i++)
        {
            _genPolyArrFront[i] = objTrans.TransformPoint(genPoly[i].Pos);
        }

        Vector3 vecSum = Vector3.zero;

        //VerticiesFront
        for (int i = 0; i < genPoly.Count; i++)
        {
            Vector3 _position = new Vector3(_genPolyArrFront[i].x, _genPolyArrFront[i].y, 0.0f);

            vecSum += _position;

            _verts.Add(new VertexProperties {
                position = _position
            });
            _verts.Add(new VertexProperties {
                position = _position
            });
            _verts.Add(new VertexProperties {
                position = _position
            });

            _frontFaceVerticies.Add(new VertexProperties {
                position = _position
            });
        }

        //Calculating the center of the unscaled polygon
        Vector3 polygonCenter = vecSum / genPoly.Count;

        polygonCenter.z = objTrans.position.z;

        Matrix4x4 scaleMatrix = BlastProof.Mathematics.ScaleMatrix(_SCALE_FACTOR);

        Vector3 vecSum2 = Vector3.zero;

        //VerticiesBack
        for (int i = 0; i < genPoly.Count; i++)
        {
            Vector3 _position = scaleMatrix.MultiplyPoint(new Vector3(_genPolyArrFront[i].x, _genPolyArrFront[i].y, _BACKFACE_OFFSET));
            vecSum2 += _position;

            _verts.Add(new VertexProperties {
                position = _position
            });
            _verts.Add(new VertexProperties {
                position = _position
            });
            _verts.Add(new VertexProperties {
                position = _position
            });
        }

        Vector3 scaledPolyCenter = vecSum2 / genPoly.Count;

        scaledPolyCenter.z = objTrans.position.z;

        //Caching how much should the polygon move on axis so it matches the original scale polygon
        Vector3 translVec = polygonCenter - scaledPolyCenter;

        Matrix4x4 transMatrix = BlastProof.Mathematics.TranslateMatrix(translVec);

        //Multiplying each backface polygon position with the translation matrix so the center of backface polygon and frontface polygon matches
        for (int i = _verts.Count / 2; i < _verts.Count; i++)
        {
            _verts[i].position = transMatrix.MultiplyPoint(_verts[i].position);
        }

        var newGenPolyArrFront = new List <Poly2Tri.PolygonPoint>();

        for (int i = 0; i < _genPolyArrFront.Length; i++)
        {
            var point = new Poly2Tri.PolygonPoint(_genPolyArrFront[i].x, _genPolyArrFront[i].y);
            point.index = i;
            newGenPolyArrFront.Add(point);
        }

        Poly2Tri.Polygon poly = new Poly2Tri.Polygon(newGenPolyArrFront);

        DTSweepContext tcx = new DTSweepContext();

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

        List <int> indiciesFromTriangulator = new List <int>();

        foreach (var triangle in poly.Triangles)
        {
            foreach (var point in triangle.Points)
            {
                indiciesFromTriangulator.Add(point.index);
            }
        }

        indiciesFromTriangulator.Reverse();

        Triangulator tri = new Triangulator(_genPolyArrFront);

        int[] triangledPoly = indiciesFromTriangulator.ToArray();

        //FrontFaceIndicies
        for (int i = 0; i < triangledPoly.Length; i++)
        {
            _indicies.Add(triangledPoly[i] * _FRONTOFFSET);
            _frontFaceIndicies.Add(triangledPoly[i]);
        }

        //BackFaceIndicies
        for (int i = triangledPoly.Length - 1; i >= 0; i--)
        {
            _indicies.Add(triangledPoly[i] * _FRONTOFFSET + (_verts.Count / 2));
        }

        //Front-Back Faces normals
        for (int i = 0; i < _indicies.Count / 2; i += 3)
        {
            int[] v1 = { _indicies[i], _indicies[(i + 1) % (_indicies.Count / 2)], _indicies[(i + 2) % (_indicies.Count / 2)] };
            int[] v2 = { _indicies[i + (_indicies.Count / 2)], _indicies[(i + 1) % (_indicies.Count / 2) + (_indicies.Count / 2)], _indicies[(i + 2) % (_indicies.Count / 2) + (_indicies.Count / 2)] };

            GetNormalsForVerts(_verts, v1);
            GetNormalsForVerts(_verts, v2);
            GetUVsWithSize(_verts, v1, Faces.forward, spriteSquareSize);
            GetUVsWithSize(_verts, v2, Faces.forward, spriteSquareSize);
        }

        //Generating Side Triangles
        for (int i = 1; i < _verts.Count / 2; i += 6)
        {
            int[] frontFaceVerts = { i, (i + 3) % (_verts.Count / 2) };
            int[] backFaceVerts  = { (i + (_verts.Count / 2)), (i + 3) % (_verts.Count / 2) + (_verts.Count / 2) };

            //verts pos are used as uvs
            int[] uvCoord = { i, (i + 3) % (_verts.Count / 2), (i + (_verts.Count / 2)), (i + 3) % (_verts.Count / 2) + (_verts.Count / 2) };

            GetQuadIndicies(frontFaceVerts, backFaceVerts, _indicies, _verts);

            GetUVsWithSize(_verts, uvCoord, Faces.left, spriteSquareSize);
        }

        //Generate Up-Down Verts
        for (int i = 5; i < _verts.Count / 2; i += 6)
        {
            int[] frontFaceVerts = { i % (_verts.Count / 2), (i + 3) % (_verts.Count / 2) };
            int[] backFaceVerts  = { (i % (_verts.Count / 2) + (_verts.Count / 2)),
                                     (i + 3) % (_verts.Count / 2) + (_verts.Count / 2) };

            //verts pos are used as uvs
            int[] uvCoord = { i % (_verts.Count / 2), (i + 3) % (_verts.Count / 2), (i % (_verts.Count / 2) + (_verts.Count / 2)), (i + 3) % (_verts.Count / 2) + (_verts.Count / 2) };

            GetQuadIndicies(frontFaceVerts, backFaceVerts, _indicies, _verts);
            GetUVsWithSize(_verts, uvCoord, Faces.up, spriteSquareSize);
        }

        _generatedMesh             = new MeshProperties(_verts);
        _generatedMesh.mesh_center = polygonCenter;
        _generatedMesh.SetIndicies(_indicies.ToArray());

        _frontFaceMesh             = new MeshProperties(_frontFaceVerticies);
        _frontFaceMesh.mesh_center = polygonCenter;
        _frontFaceMesh.SetIndicies(_frontFaceIndicies.ToArray());

        return(new MeshProperties[] { _generatedMesh, _frontFaceMesh });
    }
Esempio n. 10
0
    public void CreateMesh(Vector2[] vertsToCopy, Transform transform)
    {
        List <Vector3> resultsLocal                   = new List <Vector3>();
        List <int>     resultsTriIndexesLocal         = new List <int>();
        List <int>     resultsTriIndexesReversedLocal = new List <int>();
        List <Vector2> uvsLocal     = new List <Vector2>();
        List <Vector3> normalsLocal = new List <Vector3>();

        Sprite  spr   = transform.GetComponent <SpriteRenderer>().sprite;
        Rect    rec   = spr.rect;
        Vector3 bound = transform.GetComponent <Renderer>().bounds.max - transform.GetComponent <Renderer>().bounds.min;


        TextureImporter textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(spr)) as TextureImporter;


        int i = 0;

        Polygon poly = new Polygon();

        for (i = 0; i < vertsToCopy.Length; i++)
        {
            poly.Points.Add(new TriangulationPoint(vertsToCopy[i].x, vertsToCopy[i].y));
        }

        DTSweepContext tcx = new DTSweepContext();

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


        int  indexNumber  = 0;
        bool multiSprites = false;

        foreach (DelaunayTriangle triangle in poly.Triangles)
        {
            Vector3 v = new Vector3();
            foreach (TriangulationPoint p in triangle.Points)
            {
                v = new Vector3((float)p.X, (float)p.Y, 0);
                if (!resultsLocal.Contains(v))
                {
                    resultsLocal.Add(v);
                    resultsTriIndexesLocal.Add(indexNumber);
                    Vector2 newUv = new Vector2((v.x / bound.x) + 0.5f, (v.y / bound.y) + 0.5f);

                    newUv.x *= rec.width / spr.texture.width;
                    newUv.y *= rec.height / spr.texture.height;
                    //Debug.Log(spr.textureRectOffset);
                    newUv.x += (rec.x) / spr.texture.width;
                    newUv.y += (rec.y) / spr.texture.height;

                    //Debug.Log(Application.unityVersion);

                    SpriteMetaData[] smdArray = textureImporter.spritesheet;
                    Vector2          pivot    = new Vector2(.0f, .0f);;

                    for (int j = 0; j < smdArray.Length; j++)
                    {
                        if (smdArray[j].name == spr.name)
                        {
                            switch (smdArray[j].alignment)
                            {
                            case (0):
                                smdArray[j].pivot = Vector2.zero;
                                break;

                            case (1):
                                smdArray[j].pivot = new Vector2(0f, 1f) - new Vector2(.5f, .5f);
                                break;

                            case (2):
                                smdArray[j].pivot = new Vector2(0.5f, 1f) - new Vector2(.5f, .5f);
                                break;

                            case (3):
                                smdArray[j].pivot = new Vector2(1f, 1f) - new Vector2(.5f, .5f);
                                break;

                            case (4):
                                smdArray[j].pivot = new Vector2(0f, .5f) - new Vector2(.5f, .5f);
                                break;

                            case (5):
                                smdArray[j].pivot = new Vector2(1f, .5f) - new Vector2(.5f, .5f);
                                break;

                            case (6):
                                smdArray[j].pivot = new Vector2(0f, 0f) - new Vector2(.5f, .5f);
                                break;

                            case (7):
                                smdArray[j].pivot = new Vector2(0.5f, 0f) - new Vector2(.5f, .5f);
                                break;

                            case (8):
                                smdArray[j].pivot = new Vector2(1f, 0f) - new Vector2(.5f, .5f);
                                break;

                            case (9):
                                smdArray[j].pivot -= new Vector2(.5f, .5f);
                                break;
                            }
                            pivot = smdArray[j].pivot;
                        }
                    }
                    if (textureImporter.spriteImportMode == SpriteImportMode.Single)
                    {
                        pivot = textureImporter.spritePivot - new Vector2(.5f, .5f);
                    }
                    newUv.x += ((pivot.x) * rec.width) / spr.texture.width;
                    newUv.y += ((pivot.y) * rec.height) / spr.texture.height;

                    /*
                     * if(Application.unityVersion != "4.3.0f4")
                     * {
                     *
                     *      Debug.Log(spr.textureRectOffset.x);
                     *      newUv.x += (spr.textureRectOffset.x)/ spr.texture.width;
                     *      newUv.y += (spr.textureRectOffset.y)/ spr.texture.height;
                     * }
                     */
                    uvsLocal.Add(newUv);
                    normalsLocal.Add(new Vector3(0, 0, -1));


                    indexNumber++;
                }
                else
                {
                    resultsTriIndexesLocal.Add(resultsLocal.LastIndexOf(v));
                }
            }
        }
        if (!multiSprites)
        {
            Debug.Log("Tip: Sprite Pivot should be set to Center, with no custom pivot before conversion");
        }

        for (int j = resultsTriIndexesLocal.Count - 1; j >= 0; j--)
        {
            resultsTriIndexesReversedLocal.Add(resultsTriIndexesLocal[j]);
        }

        results.AddRange(resultsLocal);
        resultsTriIndexes.AddRange(resultsTriIndexesLocal);
        resultsTriIndexesReversed.AddRange(resultsTriIndexesReversedLocal);
        uvs.AddRange(uvsLocal);
        normals.AddRange(normalsLocal);

        resultsLocal.Clear();
        resultsTriIndexesLocal.Clear();
        resultsTriIndexesReversedLocal.Clear();
        uvsLocal.Clear();
        normalsLocal.Clear();

        finalVertices = results.ToArray();

        finalNormals = normals.ToArray();
        finalUvs     = uvs.ToArray();

        finalTriangles = resultsTriIndexesReversed.ToArray();

        //results.Clear();
        //resultsTriIndexesReversed.Clear();
    }