Exemplo n.º 1
0
        public void DebugMesh(ISpriteMeshData spriteMeshData, Vector2[] vertices, Edge[] edges, Vector2[] controlPoints, Edge[] bones, int[] pins)
        {
            var boneSamples  = SampleBones(controlPoints, bones, kNumSamples);
            var verticesList = new List <Vector2>(vertices.Length + controlPoints.Length + boneSamples.Length);
            var edgesList    = new List <Edge>(edges);
            var indicesList  = new List <int>();

            verticesList.AddRange(vertices);
            verticesList.AddRange(controlPoints);
            verticesList.AddRange(boneSamples);

            TriangulationUtility.Triangulate(verticesList, edgesList, indicesList, Allocator.Temp);

            spriteMeshData.Clear();

            verticesList.ForEach(v => spriteMeshData.AddVertex(v, new BoneWeight()));
            spriteMeshData.edges.AddRange(edgesList);
            spriteMeshData.indices.AddRange(indicesList);
        }
Exemplo n.º 2
0
 public void Triangulate(IList <Vector2> vertices, IList <Edge> edges, IList <int> indices)
 {
     TriangulationUtility.Triangulate(vertices, edges, indices);
 }
        public BoneWeight[] Calculate(Vector2[] vertices, Edge[] edges, Vector2[] controlPoints, Edge[] bones, int[] pins)
        {
            var weights = new BoneWeight[vertices.Length];

            for (var i = 0; i < weights.Length; ++i)
            {
                weights[i] = defaultWeight;
            }

            var indices = new List <int>(vertices.Length);

            TriangulationUtility.Triangulate(vertices, edges, indices);

            if (indices.Count < 3)
            {
                return(weights);
            }

            var boneSamples  = SampleBones(controlPoints, bones, kNumSamples);
            var verticesList = new List <Vector2>(vertices.Length + controlPoints.Length + boneSamples.Length);
            var edgesList    = new List <Edge>(edges);
            var indicesList  = new List <int>();

            verticesList.AddRange(vertices);
            verticesList.AddRange(controlPoints);
            verticesList.AddRange(boneSamples);

            try
            {
                TriangulationUtility.Tessellate(kMinAngle, 0f, kMeshAreaFactor, kLargestTriangleAreaFactor, 0, verticesList, edgesList, indicesList);
            }
            catch (Exception)
            {
                return(weights);
            }

            var tessellatedVertices = verticesList.ToArray();
            var tessellatedIndices  = indicesList.ToArray();

            GCHandle verticesHandle      = GCHandle.Alloc(tessellatedVertices, GCHandleType.Pinned);
            GCHandle indicesHandle       = GCHandle.Alloc(tessellatedIndices, GCHandleType.Pinned);
            GCHandle controlPointsHandle = GCHandle.Alloc(controlPoints, GCHandleType.Pinned);
            GCHandle bonesHandle         = GCHandle.Alloc(bones, GCHandleType.Pinned);
            GCHandle pinsHandle          = GCHandle.Alloc(pins, GCHandleType.Pinned);
            GCHandle weightsHandle       = GCHandle.Alloc(weights, GCHandleType.Pinned);

            Bbw(kNumIterations,
                verticesHandle.AddrOfPinnedObject(), tessellatedVertices.Length, vertices.Length,
                indicesHandle.AddrOfPinnedObject(), tessellatedIndices.Length,
                controlPointsHandle.AddrOfPinnedObject(), controlPoints.Length,
                bonesHandle.AddrOfPinnedObject(), bones.Length,
                pinsHandle.AddrOfPinnedObject(), pins.Length,
                weightsHandle.AddrOfPinnedObject());

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

            for (var i = 0; i < weights.Length; ++i)
            {
                var weight = weights[i];

                if (weight.Sum() == 0f)
                {
                    weights[i] = defaultWeight;
                }
            }

            return(weights);
        }