示例#1
0
        static void InitFromSprite(SpriteMeshData spriteMeshData, Sprite sprite, Vector2 pivot)
        {
            Vector2[]     vertices;
            IndexedEdge[] edges;
            int[]         indices;

            if (sprite)
            {
                GetSpriteData(sprite, out vertices, out edges, out indices);

                spriteMeshData.vertices   = vertices;
                spriteMeshData.edges      = edges;
                spriteMeshData.indices    = indices;
                spriteMeshData.pivotPoint = pivot;
            }
        }
示例#2
0
        public static RuntimeSpriteMesh CreateSpriteMesh(Sprite sprite, Vector2 pivot)
        {
            if (spriteMeshes.ContainsKey(sprite))
            {
                return(spriteMeshes[sprite]);
            }
            else
            {
                RuntimeSpriteMesh spriteMesh = ScriptableObject.CreateInstance <RuntimeSpriteMesh>();
                InitFromSprite(spriteMesh, sprite);

                SpriteMeshData spriteMeshData = ScriptableObject.CreateInstance <SpriteMeshData>();
                spriteMeshData.name      = spriteMesh.name + "_Data";
                spriteMeshData.hideFlags = HideFlags.HideInHierarchy;
                InitFromSprite(spriteMeshData, sprite, pivot);

                spriteMesh.spriteMeshData = spriteMeshData;

                UpdateAssets(spriteMesh, spriteMeshData);

                return(spriteMesh);
            }
        }
示例#3
0
        public static Vector3[] GetMeshVertices(Sprite sprite, SpriteMeshData spriteMeshData)
        {
            float pixelsPerUnit = sprite.pixelsPerUnit;

            return((new List <Vector2>(spriteMeshData.vertices)).ConvertAll(v => TexCoordToVertex(spriteMeshData.pivotPoint, v, pixelsPerUnit)).ToArray());
        }
示例#4
0
        public static void UpdateAssets(RuntimeSpriteMesh spriteMesh, SpriteMeshData spriteMeshData)
        {
            if (spriteMesh && spriteMeshData)
            {
                //string spriteMeshPath = AssetDatabase.GetAssetPath(spriteMesh);

                //SerializedObject spriteMeshSO = new SerializedObject(spriteMesh);
                //SerializedProperty sharedMeshProp = spriteMeshSO.FindProperty("m_SharedMesh");
                //SerializedProperty sharedMaterialsProp = spriteMeshSO.FindProperty("m_SharedMaterials");

                if (!spriteMesh.sharedMesh)
                {
                    Mesh mesh = new Mesh();
                    mesh.hideFlags = HideFlags.HideInHierarchy;
                    //AssetDatabase.AddObjectToAsset(mesh, spriteMeshPath);

                    //spriteMeshSO.Update();
                    //sharedMeshProp.objectReferenceValue = mesh;
                    //spriteMeshSO.ApplyModifiedProperties();
                    //EditorUtility.SetDirty(mesh);
                }

                spriteMesh.sharedMesh.name = spriteMesh.name;

                if (spriteMesh.sharedMaterials.Length == 0)
                {
                    //Material material = new Material(Shader.Find("Sprites/Default"));
                    //material.mainTexture = SpriteUtility.GetSpriteTexture(spriteMesh.sprite, false);

                    //AssetDatabase.AddObjectToAsset(material, spriteMeshPath);

                    //spriteMeshSO.Update();
                    //sharedMaterialsProp.arraySize = 1;
                    //sharedMaterialsProp.GetArrayElementAtIndex(0).objectReferenceValue = material;
                    //spriteMeshSO.ApplyModifiedProperties();
                }

                for (int i = 0; i < spriteMesh.sharedMaterials.Length; i++)
                {
                    Material material = spriteMesh.sharedMaterials[i];

                    if (material)
                    {
                        if (spriteMesh.sprite)
                        {
                            //material.mainTexture = SpriteUtility.GetSpriteTexture(spriteMesh.sprite, false);
                        }

                        material.name      = spriteMesh.name + "_" + i;
                        material.hideFlags = HideFlags.HideInHierarchy;
                        //EditorUtility.SetDirty(material);
                    }
                }

                spriteMeshData.hideFlags = HideFlags.HideInHierarchy;
                //EditorUtility.SetDirty(spriteMeshData);

                int width  = 0;
                int height = 0;

                GetSpriteTextureSize(spriteMesh.sprite, ref width, ref height);

                Vector3[] vertices = GetMeshVertices(spriteMesh.sprite, spriteMeshData);

                Vector2 textureWidthHeightInv = new Vector2(1f / width, 1f / height);

                Vector2[] uvs = (new List <Vector2>(spriteMeshData.vertices)).ConvertAll(v => Vector2.Scale(v, textureWidthHeightInv)).ToArray();

                Vector3[] normals = (new List <Vector3>(vertices)).ConvertAll(v => Vector3.back).ToArray();

                BoneWeight[] boneWeightsData = spriteMeshData.boneWeights;

                if (boneWeightsData.Length != spriteMeshData.vertices.Length)
                {
                    boneWeightsData = new BoneWeight[spriteMeshData.vertices.Length];
                }

                List <UnityEngine.BoneWeight> boneWeights = new List <UnityEngine.BoneWeight>(boneWeightsData.Length);

                List <float> verticesOrder = new List <float>(spriteMeshData.vertices.Length);

                for (int i = 0; i < boneWeightsData.Length; i++)
                {
                    BoneWeight boneWeight = boneWeightsData[i];

                    List <KeyValuePair <int, float> > pairs = new List <KeyValuePair <int, float> >();
                    pairs.Add(new KeyValuePair <int, float>(boneWeight.boneIndex0, boneWeight.weight0));
                    pairs.Add(new KeyValuePair <int, float>(boneWeight.boneIndex1, boneWeight.weight1));
                    pairs.Add(new KeyValuePair <int, float>(boneWeight.boneIndex2, boneWeight.weight2));
                    pairs.Add(new KeyValuePair <int, float>(boneWeight.boneIndex3, boneWeight.weight3));

                    pairs = pairs.OrderByDescending(s => s.Value).ToList();

                    UnityEngine.BoneWeight boneWeight2 = new UnityEngine.BoneWeight();
                    boneWeight2.boneIndex0 = Mathf.Max(0, pairs[0].Key);
                    boneWeight2.boneIndex1 = Mathf.Max(0, pairs[1].Key);
                    boneWeight2.boneIndex2 = Mathf.Max(0, pairs[2].Key);
                    boneWeight2.boneIndex3 = Mathf.Max(0, pairs[3].Key);
                    boneWeight2.weight0    = pairs[0].Value;
                    boneWeight2.weight1    = pairs[1].Value;
                    boneWeight2.weight2    = pairs[2].Value;
                    boneWeight2.weight3    = pairs[3].Value;

                    boneWeights.Add(boneWeight2);

                    float vertexOrder = i;

                    if (spriteMeshData.bindPoses.Length > 0)
                    {
                        vertexOrder = spriteMeshData.bindPoses[boneWeight2.boneIndex0].zOrder * boneWeight2.weight0 +
                                      spriteMeshData.bindPoses[boneWeight2.boneIndex1].zOrder * boneWeight2.weight1 +
                                      spriteMeshData.bindPoses[boneWeight2.boneIndex2].zOrder * boneWeight2.weight2 +
                                      spriteMeshData.bindPoses[boneWeight2.boneIndex3].zOrder * boneWeight2.weight3;
                    }

                    verticesOrder.Add(vertexOrder);
                }

                List <WeightedTriangle> weightedTriangles = new List <WeightedTriangle>(spriteMeshData.indices.Length / 3);

                for (int i = 0; i < spriteMeshData.indices.Length; i += 3)
                {
                    int p1 = spriteMeshData.indices[i];
                    int p2 = spriteMeshData.indices[i + 1];
                    int p3 = spriteMeshData.indices[i + 2];

                    weightedTriangles.Add(new WeightedTriangle(p1, p2, p3,
                                                               verticesOrder[p1],
                                                               verticesOrder[p2],
                                                               verticesOrder[p3]));
                }

                weightedTriangles = weightedTriangles.OrderBy(t => t.weight).ToList();

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

                for (int i = 0; i < weightedTriangles.Count; ++i)
                {
                    WeightedTriangle t = weightedTriangles[i];
                    indices.Add(t.p1);
                    indices.Add(t.p2);
                    indices.Add(t.p3);
                }

                List <Matrix4x4> bindposes = (new List <BindInfo>(spriteMeshData.bindPoses)).ConvertAll(p => p.bindPose);

                for (int i = 0; i < bindposes.Count; i++)
                {
                    Matrix4x4 bindpose = bindposes[i];

                    bindpose.m23 = 0f;

                    bindposes[i] = bindpose;
                }

                spriteMesh.sharedMesh.Clear();
                spriteMesh.sharedMesh.vertices    = vertices;
                spriteMesh.sharedMesh.uv          = uvs;
                spriteMesh.sharedMesh.triangles   = indices.ToArray();
                spriteMesh.sharedMesh.normals     = normals;
                spriteMesh.sharedMesh.boneWeights = boneWeights.ToArray();
                spriteMesh.sharedMesh.bindposes   = bindposes.ToArray();
                spriteMesh.sharedMesh.RecalculateBounds();

                //RebuildBlendShapes(spriteMesh);
            }
        }