Exemple #1
0
        public void InitFromOutline(float detail, float alphaTolerance, bool holeDetection, float tessellation, string undoName)
        {
            Clear(undoName);

            float   pixelsPerUnit = SpriteMeshUtils.GetSpritePixelsPerUnit(spriteMesh.sprite);
            float   factor        = pixelsPerUnit / spriteMesh.sprite.pixelsPerUnit;
            Vector2 position      = rect.position / factor;
            Vector2 size          = rect.size / factor;

            Rect l_rect = new Rect(position.x, position.y, size.x, size.y);

            Texture2D texture     = SpriteUtility.GetSpriteTexture(spriteMesh.sprite, false);
            Rect      clampedRect = MathUtils.ClampRect(MathUtils.OrderMinMax(l_rect), new Rect(0f, 0f, texture.width, texture.height));

            List <Vector2>     l_texcoords;
            List <IndexedEdge> l_indexedEdges;
            List <int>         l_indices;

            SpriteMeshUtils.InitFromOutline(texture, clampedRect, detail, alphaTolerance, holeDetection, out l_texcoords, out l_indexedEdges, out l_indices);
            SpriteMeshUtils.Tessellate(l_texcoords, l_indexedEdges, holes, l_indices, tessellation * 10f);

            nodes         = l_texcoords.ConvertAll(v => Node.Create(l_texcoords.IndexOf(v)));
            edges         = l_indexedEdges.ConvertAll(e => Edge.Create(nodes[e.index1], nodes[e.index2]));
            m_TexVertices = l_texcoords.ConvertAll(v => v * factor);
            boneWeights   = l_texcoords.ConvertAll(v => BoneWeight.Create());
            indices       = l_indices;

            isDirty = true;

            m_DirtyVertices = true;
        }
Exemple #2
0
        public static void CalculateBbw(Vector2[] vertices, IndexedEdge[] edges, Vector2[] controlPoints, IndexedEdge[] controlPointEdges, out float[,] weights)
        {
            Vector2[] sampledEdges = SampleEdges(controlPoints, controlPointEdges, 10);

            List <Vector2> verticesAndSamplesList = new List <Vector2>(vertices.Length + sampledEdges.Length);

            verticesAndSamplesList.AddRange(vertices);
            verticesAndSamplesList.AddRange(sampledEdges);

            List <IndexedEdge> edgesList   = new List <IndexedEdge>(edges);
            List <Hole>        holes       = new List <Hole>();
            List <int>         indicesList = new List <int>();

            SpriteMeshUtils.Tessellate(verticesAndSamplesList, edgesList, holes, indicesList, 4f);

            Vector2[] verticesAndSamples = verticesAndSamplesList.ToArray();
            int[]     indices            = indicesList.ToArray();

            weights = new float[controlPointEdges.Length, vertices.Length];

            GCHandle verticesHandle      = GCHandle.Alloc(verticesAndSamples, GCHandleType.Pinned);
            GCHandle indicesHandle       = GCHandle.Alloc(indices, GCHandleType.Pinned);
            GCHandle edgesHandle         = GCHandle.Alloc(edges, GCHandleType.Pinned);
            GCHandle controlPointsHandle = GCHandle.Alloc(controlPoints, GCHandleType.Pinned);
            GCHandle boneEdgesHandle     = GCHandle.Alloc(controlPointEdges, GCHandleType.Pinned);
            GCHandle weightsHandle       = GCHandle.Alloc(weights, GCHandleType.Pinned);

            /*
             * SaveData(verticesHandle.AddrOfPinnedObject(), vertices.Length,
             * indicesHandle.AddrOfPinnedObject(), indices.Length,
             * edgesHandle.AddrOfPinnedObject(),edges.Length,
             * controlPointsHandle.AddrOfPinnedObject(), controlPoints.Length,
             *       boneEdgesHandle.AddrOfPinnedObject(), controlPointEdges.Length);
             */

            Bbw(verticesHandle.AddrOfPinnedObject(), verticesAndSamples.Length, vertices.Length,
                indicesHandle.AddrOfPinnedObject(), indices.Length,
                edgesHandle.AddrOfPinnedObject(), edges.Length,
                controlPointsHandle.AddrOfPinnedObject(), controlPoints.Length,
                boneEdgesHandle.AddrOfPinnedObject(), controlPointEdges.Length,
                weightsHandle.AddrOfPinnedObject());

            verticesHandle.Free();
            indicesHandle.Free();
            edgesHandle.Free();
            controlPointsHandle.Free();
            boneEdgesHandle.Free();
            weightsHandle.Free();
        }
Exemple #3
0
        public static UnityEngine.BoneWeight[] CalculateBbw(Vector2[] vertices, IndexedEdge[] edges,
                                                            Vector2[] controlPoints, IndexedEdge[] controlPointEdges, int[] pins)
        {
            Vector2[] sampledEdges = SampleEdges(controlPoints, controlPointEdges, 10);

            List <Vector2> verticesAndSamplesList = new List <Vector2>(vertices.Length + sampledEdges.Length);

            verticesAndSamplesList.AddRange(vertices);
            verticesAndSamplesList.AddRange(controlPoints);
            verticesAndSamplesList.AddRange(sampledEdges);

            List <IndexedEdge> edgesList   = new List <IndexedEdge>(edges);
            List <Hole>        holes       = new List <Hole>();
            List <int>         indicesList = new List <int>();

            SpriteMeshUtils.Tessellate(verticesAndSamplesList, edgesList, holes, indicesList, 4f);

            Vector2[] verticesAndSamples = verticesAndSamplesList.ToArray();
            int[]     indices            = indicesList.ToArray();

            UnityEngine.BoneWeight[] weights = new UnityEngine.BoneWeight[vertices.Length];

            GCHandle verticesHandle      = GCHandle.Alloc(verticesAndSamples, GCHandleType.Pinned);
            GCHandle indicesHandle       = GCHandle.Alloc(indices, GCHandleType.Pinned);
            GCHandle controlPointsHandle = GCHandle.Alloc(controlPoints, GCHandleType.Pinned);
            GCHandle boneEdgesHandle     = GCHandle.Alloc(controlPointEdges, GCHandleType.Pinned);
            GCHandle pinsHandle          = GCHandle.Alloc(pins, GCHandleType.Pinned);
            GCHandle weightsHandle       = GCHandle.Alloc(weights, GCHandleType.Pinned);

            Bbw(-1,
                verticesHandle.AddrOfPinnedObject(), verticesAndSamples.Length, vertices.Length,
                indicesHandle.AddrOfPinnedObject(), indices.Length,
                controlPointsHandle.AddrOfPinnedObject(), controlPoints.Length,
                boneEdgesHandle.AddrOfPinnedObject(), controlPointEdges.Length,
                pinsHandle.AddrOfPinnedObject(), pins.Length,
                weightsHandle.AddrOfPinnedObject());

            verticesHandle.Free();
            indicesHandle.Free();
            controlPointsHandle.Free();
            boneEdgesHandle.Free();
            pinsHandle.Free();
            weightsHandle.Free();

            return(weights);
        }