public void CalculateAutomaticWeights(List <Node> targetNodes)
        {
            float pixelsPerUnit = SpriteMeshUtils.GetSpritePixelsPerUnit(spriteMesh.sprite);

            if (nodes.Count <= 0)
            {
                Debug.Log("Cannot calculate automatic weights from a SpriteMesh with no vertices.");
                return;
            }

            if (bindPoses.Count <= 0)
            {
                Debug.Log("Cannot calculate automatic weights. Specify bones to the SpriteMeshInstance.");
                return;
            }

            if (spriteMesh && bindPoses.Count > 1)
            {
                List <Vector2>     controlPoints     = new List <Vector2>(bindPoses.Count * 2);
                List <IndexedEdge> controlPointEdges = new List <IndexedEdge>(bindPoses.Count);

                foreach (BindInfo bindInfo in bindPoses)
                {
                    Vector2 tip  = SpriteMeshUtils.VertexToTexCoord(spriteMesh, pivotPoint, bindInfo.position, pixelsPerUnit);
                    Vector2 tail = SpriteMeshUtils.VertexToTexCoord(spriteMesh, pivotPoint, bindInfo.endPoint, pixelsPerUnit);

                    int index1 = -1;

                    if (!ContainsVector(tip, controlPoints, 0.01f, out index1))
                    {
                        index1 = controlPoints.Count;
                        controlPoints.Add(tip);
                    }

                    int index2 = -1;

                    if (!ContainsVector(tail, controlPoints, 0.01f, out index2))
                    {
                        index2 = controlPoints.Count;
                        controlPoints.Add(tail);
                    }

                    IndexedEdge edge = new IndexedEdge(index1, index2);
                    controlPointEdges.Add(edge);
                }

                float[,] weightArray;

                BbwPlugin.CalculateBbw(m_TexVertices.ToArray(),
                                       indexedEdges.ToArray(),
                                       controlPoints.ToArray(),
                                       controlPointEdges.ToArray(),
                                       out weightArray);

                FillBoneWeights(targetNodes, weightArray);

                isDirty = true;
            }
            else
            {
                BoneWeight boneWeight = BoneWeight.Create();
                boneWeight.boneIndex0 = 0;
                boneWeight.weight0    = 1f;

                foreach (Node node in targetNodes)
                {
                    SetBoneWeight(node, boneWeight);
                }
            }
        }
Beispiel #2
0
        public void CalculateAutomaticWeights(List <Node> targetNodes)
        {
            float pixelsPerUnit = SpriteMeshUtils.GetSpritePixelsPerUnit(spriteMesh.sprite);

            if (nodes.Count <= 0)
            {
                Debug.Log("Cannot calculate automatic weights from a SpriteMesh with no vertices.");
                return;
            }

            if (bindPoses.Count <= 0)
            {
                Debug.Log("Cannot calculate automatic weights. Specify bones to the SpriteMeshInstance.");
                return;
            }

            if (!spriteMesh)
            {
                return;
            }

            List <Vector2>     controlPoints     = new List <Vector2>();
            List <IndexedEdge> controlPointEdges = new List <IndexedEdge>();
            List <int>         pins = new List <int>();

            foreach (BindInfo bindInfo in bindPoses)
            {
                Vector2 tip  = SpriteMeshUtils.VertexToTexCoord(spriteMesh, pivotPoint, bindInfo.position, pixelsPerUnit);
                Vector2 tail = SpriteMeshUtils.VertexToTexCoord(spriteMesh, pivotPoint, bindInfo.endPoint, pixelsPerUnit);

                if (bindInfo.boneLength <= 0f)
                {
                    int index = controlPoints.Count;
                    controlPoints.Add(tip);
                    pins.Add(index);

                    continue;
                }

                int index1 = -1;

                if (!ContainsVector(tip, controlPoints, 0.01f, out index1))
                {
                    index1 = controlPoints.Count;
                    controlPoints.Add(tip);
                }

                int index2 = -1;

                if (!ContainsVector(tail, controlPoints, 0.01f, out index2))
                {
                    index2 = controlPoints.Count;
                    controlPoints.Add(tail);
                }

                IndexedEdge edge = new IndexedEdge(index1, index2);
                controlPointEdges.Add(edge);
            }

            UnityEngine.BoneWeight[] boneWeights = BbwPlugin.CalculateBbw(m_TexVertices.ToArray(), indexedEdges.ToArray(), controlPoints.ToArray(), controlPointEdges.ToArray(), pins.ToArray());

            foreach (Node node in targetNodes)
            {
                UnityEngine.BoneWeight unityBoneWeight = boneWeights[node.index];

                SetBoneWeight(node, CreateBoneWeightFromUnityBoneWeight(unityBoneWeight));
            }

            isDirty = true;
        }
Beispiel #3
0
        public void CalculateAutomaticWeights(List <Vertex> targetVertices)
        {
            if (texVertices.Count <= 0)
            {
                Debug.Log("Cannot calculate automatic weights from a SpriteMesh with no vertices.");
                return;
            }

            if (bindPoses.Count <= 0)
            {
                Debug.Log("Cannot calculate automatic weights. Specify bones to the SpriteMeshInstance.");
                return;
            }

            if (spriteMesh)
            {
                List <Vector3> vertices = new List <Vector3>(texVertices.Count);

                for (int i = 0; i < texVertices.Count; i++)
                {
                    Vertex vertex = texVertices[i];
                    vertices.Add(vertex.vertex);
                }

                List <Vector3>     controlPoints     = new List <Vector3>(bindPoses.Count * 2);
                List <IndexedEdge> controlPointEdges = new List <IndexedEdge>(bindPoses.Count);

                foreach (BindInfo bindInfo in bindPoses)
                {
                    Vector3 tip  = SpriteMeshUtils.VertexToTexCoord(spriteMesh, bindInfo.position);
                    Vector3 tail = SpriteMeshUtils.VertexToTexCoord(spriteMesh, bindInfo.endPoint);

                    int index1 = -1;

                    if (!ContainsVector(tip, controlPoints, 0.01f, out index1))
                    {
                        index1 = controlPoints.Count;
                        controlPoints.Add(tip);
                    }

                    int index2 = -1;

                    if (!ContainsVector(tail, controlPoints, 0.01f, out index2))
                    {
                        index2 = controlPoints.Count;
                        controlPoints.Add(tail);
                    }

                    IndexedEdge edge = new IndexedEdge(index1, index2);
                    controlPointEdges.Add(edge);
                }

                float[,] weightArray;

                UpdateIndexedEdges();

                BbwPlugin.CalculateBbw(vertices.ToArray(),
                                       indices.ToArray(),
                                       indexedEdges.ToArray(),
                                       controlPoints.ToArray(),
                                       controlPointEdges.ToArray(),
                                       out weightArray);

                FillBoneWeights(targetVertices, weightArray);

                isDirty = true;
            }
        }