Exemplo n.º 1
0
 public void SetSpriteMesh(SpriteMesh _spriteMesh, SpriteMeshInstance _spriteMeshInstance)
 {
     spriteMesh         = _spriteMesh;
     spriteMeshInstance = _spriteMeshInstance;
     spriteMeshData     = SpriteMeshUtils.LoadSpriteMeshData(_spriteMesh);
     RevertChanges();
 }
Exemplo n.º 2
0
        public void RevertChanges()
        {
            Clear("");

            if (spriteMesh && spriteMeshData)
            {
                pivotPoint = spriteMeshData.pivotPoint;
                rect       = SpriteMeshUtils.GetRect(spriteMesh.sprite);

                m_TexVertices = spriteMeshData.vertices.ToList();
                nodes         = m_TexVertices.ConvertAll(v => Node.Create(m_TexVertices.IndexOf(v)));
                boneWeights   = spriteMeshData.boneWeights.ToList();
                edges         = spriteMeshData.edges.ToList().ConvertAll(e => Edge.Create(nodes[e.index1], nodes[e.index2]));
                holes         = spriteMeshData.holes.ToList().ConvertAll(h => new Hole(h));
                indices       = spriteMeshData.indices.ToList();
                bindPoses     = spriteMeshData.bindPoses.ToList().ConvertAll(b => b.Clone() as BindInfo);

                CreateBlendShapeCache(spriteMeshData);

                if (boneWeights.Count != nodes.Count)
                {
                    boneWeights = nodes.ConvertAll(n => BoneWeight.Create());
                }

                m_DirtyVertices = true;
            }
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
        public void ApplyChanges()
        {
            if (spriteMeshData)
            {
                spriteMeshData.vertices    = m_TexVertices.ToArray();
                spriteMeshData.boneWeights = boneWeights.ToArray();
                spriteMeshData.edges       = indexedEdges.ToArray();
                spriteMeshData.holes       = holes.ConvertAll(h => h.vertex).ToArray();
                spriteMeshData.indices     = indices.ToArray();
                spriteMeshData.bindPoses   = bindPoses.ToArray();
                spriteMeshData.pivotPoint  = pivotPoint;

                SpriteMeshUtils.DestroyBlendShapes(spriteMeshData, false, "");

                SetBlendShapesFromCache();

                EditorUtility.SetDirty(spriteMeshData);

                string spriteAssetPath = AssetDatabase.GetAssetPath(spriteMesh.sprite);
                SpriteMeshUtils.UpdateAssets(spriteMesh, spriteMeshData);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                TextureImporter textureImporter = AssetImporter.GetAtPath(spriteAssetPath) as TextureImporter;
                textureImporter.userData = textureImporter.assetTimeStamp.ToString();
                AssetDatabase.StartAssetEditing();
                AssetDatabase.ImportAsset(spriteAssetPath);
                AssetDatabase.StopAssetEditing();
                isDirty = false;
            }

            if (spriteMeshInstance)
            {
                SpriteMeshUtils.UpdateRenderer(spriteMeshInstance, false);
            }
        }
        public static SpriteMeshInstance CreateSpriteMeshInstance(SpriteMesh spriteMesh, GameObject gameObject,
                                                                  bool undo = true)
        {
            SpriteMeshInstance spriteMeshInstance = null;

            if (spriteMesh && gameObject)
            {
                if (undo)
                {
                    spriteMeshInstance = Undo.AddComponent <SpriteMeshInstance>(gameObject);
                }
                else
                {
                    spriteMeshInstance = gameObject.AddComponent <SpriteMeshInstance>();
                }

                spriteMeshInstance.spriteMesh     = spriteMesh;
                spriteMeshInstance.sharedMaterial = defaultMaterial;

                SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh);

                List <Bone2D> bones = new List <Bone2D>();
                List <string> paths = new List <string>();

                Vector4 zero = new Vector4(0f, 0f, 0f, 1f);

                foreach (BindInfo bindInfo in spriteMeshData.bindPoses)
                {
                    Matrix4x4 m = spriteMeshInstance.transform.localToWorldMatrix * bindInfo.bindPose.inverse;

                    GameObject bone = new GameObject(bindInfo.name);

                    if (undo)
                    {
                        Undo.RegisterCreatedObjectUndo(bone, Undo.GetCurrentGroupName());
                    }

                    Bone2D boneComponent = bone.AddComponent <Bone2D>();

                    boneComponent.localLength = bindInfo.boneLength;
                    bone.transform.position   = m * zero;
                    bone.transform.rotation   = m.GetRotation();
                    bone.transform.parent     = gameObject.transform;

                    bones.Add(boneComponent);
                    paths.Add(bindInfo.path);
                }

                BoneUtils.ReconstructHierarchy(bones, paths);

                spriteMeshInstance.bones = bones;

                SpriteMeshUtils.UpdateRenderer(spriteMeshInstance, undo);

                EditorUtility.SetDirty(spriteMeshInstance);
            }

            return(spriteMeshInstance);
        }
Exemplo n.º 6
0
        static int FindBindInfo(BindInfo bindInfo, SpriteMeshInstance spriteMeshInstance)
        {
            if (spriteMeshInstance)
            {
                return(FindBindInfo(bindInfo, SpriteMeshUtils.LoadSpriteMeshData(spriteMeshInstance.spriteMesh)));
            }

            return(-1);
        }
Exemplo n.º 7
0
        void UpdateSpriteMeshData()
        {
            m_SpriteMeshData = null;

            if (m_SpriteMeshProperty != null && m_SpriteMeshProperty.objectReferenceValue)
            {
                m_SpriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(m_SpriteMeshProperty.objectReferenceValue as SpriteMesh);
            }
        }
Exemplo n.º 8
0
        override public void OnInspectorGUI()
        {
            Sprite oldSprite = m_SpriteProperty.objectReferenceValue as Sprite;

            EditorGUI.BeginChangeCheck();

            serializedObject.Update();

            EditorGUILayout.PropertyField(m_SpriteProperty);

            serializedObject.ApplyModifiedProperties();

            if (EditorGUI.EndChangeCheck())
            {
                Sprite sprite = m_SpriteProperty.objectReferenceValue as Sprite;

                SpriteMeshUtils.UpdateAssets(target as SpriteMesh);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                AssetDatabase.StartAssetEditing();

                if (oldSprite)
                {
                    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(oldSprite));
                }

                AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(sprite));
                AssetDatabase.StopAssetEditing();
            }

            serializedObject.Update();

            EditorGUI.BeginDisabledGroup(true);

            EditorGUILayout.PropertyField(m_SharedMeshProperty);
            EditorGUILayout.PropertyField(m_SharedMaterialsProperty, true);

            EditorGUI.EndDisabledGroup();

            EditorGUILayout.BeginHorizontal();

            GUILayout.FlexibleSpace();

            if (GUILayout.Button("Edit Sprite Mesh", GUILayout.Width(150f)))
            {
                SpriteMeshEditorWindow window = SpriteMeshEditorWindow.GetWindow();
                window.UpdateFromSelection();
            }

            GUILayout.FlexibleSpace();

            EditorGUILayout.EndHorizontal();

            serializedObject.ApplyModifiedProperties();
        }
Exemplo n.º 9
0
        void DoUpdateRenderer()
        {
            SpriteMeshUtils.UpdateRenderer(m_SpriteMeshInstance);

#if UNITY_5_5_OR_NEWER
#else
            EditorUtility.SetSelectedWireframeHidden(m_SpriteMeshInstance.cachedRenderer, !m_SpriteMeshInstance.cachedSkinnedRenderer);
#endif

            Undo.CollapseUndoOperations(m_UndoGroup);
            SceneView.RepaintAll();
        }
Exemplo n.º 10
0
        void OnPreprocessTexture()
        {
            if (!s_Initialized)
            {
                return;
            }

            string guid = AssetDatabase.AssetPathToGUID(assetPath);

            if (s_SpriteMeshToTextureCache.ContainsValue(guid))
            {
                TextureImporter    textureImporter        = (TextureImporter)assetImporter;
                SerializedObject   textureImporterSO      = new SerializedObject(textureImporter);
                SerializedProperty textureImporterSprites = textureImporterSO.FindProperty("m_SpriteSheet.m_Sprites");

                foreach (KeyValuePair <string, string> pair in s_SpriteMeshToTextureCache)
                {
                    if (pair.Value == guid)
                    {
                        SpriteMesh     spriteMesh     = LoadSpriteMesh(AssetDatabase.GUIDToAssetPath(pair.Key));
                        SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh);

                        if (spriteMesh && spriteMeshData && spriteMesh.sprite && spriteMeshData.vertices.Length > 0)
                        {
                            SerializedProperty spriteProp = null;
                            int    i    = 0;
                            string name = "";

                            while (i < textureImporterSprites.arraySize && name != spriteMesh.sprite.name)
                            {
                                spriteProp = textureImporterSprites.GetArrayElementAtIndex(i);
                                name       = spriteProp.FindPropertyRelative("m_Name").stringValue;

                                ++i;
                            }

                            if (name == spriteMesh.sprite.name)
                            {
                                Rect textureRect = SpriteMeshUtils.CalculateSpriteRect(spriteMesh, 5);
                                spriteProp.FindPropertyRelative("m_Rect").rectValue     = textureRect;
                                spriteProp.FindPropertyRelative("m_Alignment").intValue = 9;
                                spriteProp.FindPropertyRelative("m_Pivot").vector2Value = Vector2.Scale(spriteMeshData.pivotPoint - textureRect.position, new Vector2(1f / textureRect.size.x, 1f / textureRect.size.y));
                                textureImporter.userData = textureImporter.assetTimeStamp.ToString();
                            }
                        }
                    }
                }

                textureImporterSO.ApplyModifiedProperties();
            }
        }
Exemplo n.º 11
0
        static void CreateSpriteMesh(MenuCommand menuCommand)
        {
            List <Texture2D> selectedTextures = Selection.objects.ToList().Where(o => o is Texture2D).ToList().ConvertAll(o => o as Texture2D);

            foreach (Texture2D texture in selectedTextures)
            {
                SpriteMeshUtils.CreateSpriteMesh(texture);
            }

            if (selectedTextures.Count == 0)
            {
                SpriteMeshUtils.CreateSpriteMesh(Selection.activeObject as Sprite);
            }
        }
        static void ContextCreateSpriteMesh(MenuCommand menuCommand)
        {
            GameObject     spriteRendererGO = Selection.activeGameObject;
            SpriteRenderer spriteRenderer   = null;
            SpriteMesh     spriteMesh       = null;

            int sortingLayerID = 0;
            int sortingOrder   = 0;

            if (spriteRendererGO)
            {
                spriteRenderer = spriteRendererGO.GetComponent <SpriteRenderer>();
            }

            if (spriteRenderer &&
                spriteRenderer.sprite)
            {
                sortingLayerID = spriteRenderer.sortingLayerID;
                sortingOrder   = spriteRenderer.sortingOrder;

                SpriteMesh overrideSpriteMesh = SpriteMeshPostprocessor.GetSpriteMeshFromSprite(spriteRenderer.sprite);

                if (overrideSpriteMesh)
                {
                    spriteMesh = overrideSpriteMesh;
                }
                else
                {
                    spriteMesh = SpriteMeshUtils.CreateSpriteMesh(spriteRenderer.sprite);
                }
            }

            if (spriteMesh)
            {
                Undo.SetCurrentGroupName("create SpriteMeshInstance");
                Undo.DestroyObjectImmediate(spriteRenderer);
                SpriteMeshInstance spriteMeshInstance =
                    SpriteMeshUtils.CreateSpriteMeshInstance(spriteMesh, spriteRendererGO, true);

                spriteMeshInstance.sortingLayerID = sortingLayerID;
                spriteMeshInstance.sortingOrder   = sortingOrder;

                Selection.activeGameObject = spriteRendererGO;
            }
            else
            {
                Debug.Log("Select a SpriteRenderer with a Sprite to convert to SpriteMesh");
            }
        }
Exemplo n.º 13
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();
        }
Exemplo n.º 14
0
        void SetBlendShapesFromCache()
        {
            if (spriteMesh)
            {
                foreach (BlendShape blendshape in blendshapes)
                {
                    BlendShape newBlendshape = SpriteMeshUtils.CreateBlendShape(spriteMesh, blendshape.name);

                    foreach (BlendShapeFrame frame in blendshape.frames)
                    {
                        SpriteMeshUtils.CreateBlendShapeFrame(newBlendshape, frame.weight, frame.vertices.ToArray());
                    }
                }
            }
        }
Exemplo n.º 15
0
        static void Upgrade_003(SerializedObject spriteMeshSO)
        {
            SpriteMesh     spriteMesh     = spriteMeshSO.targetObject as SpriteMesh;
            SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh);

            if (spriteMesh.sprite && spriteMeshData)
            {
                TextureImporter textureImporter =
                    AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(spriteMesh.sprite)) as TextureImporter;

                float maxImporterSize = textureImporter.maxTextureSize;

                int width  = 1;
                int height = 1;

                SpriteMeshUtils.GetWidthAndHeight(textureImporter, ref width, ref height);

                int maxSize = Mathf.Max(width, height);

                float factor = maxSize / maxImporterSize;

                if (factor > 1f)
                {
                    SerializedObject   spriteMeshDataSO  = new SerializedObject(spriteMeshData);
                    SerializedProperty smdPivotPointProp = spriteMeshDataSO.FindProperty("m_PivotPoint");
                    SerializedProperty smdVerticesProp   = spriteMeshDataSO.FindProperty("m_Vertices");
                    SerializedProperty smdHolesProp      = spriteMeshDataSO.FindProperty("m_Holes");

                    spriteMeshDataSO.Update();

                    smdPivotPointProp.vector2Value = spriteMeshData.pivotPoint * factor;

                    for (int i = 0; i < spriteMeshData.vertices.Length; ++i)
                    {
                        smdVerticesProp.GetArrayElementAtIndex(i).vector2Value = spriteMeshData.vertices[i] * factor;
                    }

                    for (int i = 0; i < spriteMeshData.holes.Length; ++i)
                    {
                        smdHolesProp.GetArrayElementAtIndex(i).vector2Value = spriteMeshData.holes[i] * factor;
                    }

                    spriteMeshDataSO.ApplyModifiedProperties();

                    EditorUtility.SetDirty(spriteMeshData);
                }
            }
        }
Exemplo n.º 16
0
        static void CreateInstance()
        {
            instance = SpriteMeshUtils.CreateSpriteMeshInstance(spriteMesh, false);

            if (instance)
            {
                s_InstanceBones = instance.bones;

                instance.transform.parent = parentTransform;

                if (parentTransform)
                {
                    instance.transform.localPosition = Vector3.zero;
                }
            }
        }
Exemplo n.º 17
0
        static void CreateSpriteMesh(MenuCommand menuCommand)
        {
            List <Texture2D> selectedTextures = Selection.objects.ToList().Where(o => o is Texture2D).ToList().ConvertAll(o => o as Texture2D);

            foreach (Texture2D texture in selectedTextures)
            {
                Debug.Log("Texture path");
                SpriteMeshUtils.CreateSpriteMesh(texture);
            }

            if (selectedTextures.Count == 0)
            {
                // Yar, this be the one we want
                SpriteMeshUtils.CreateSpriteMesh(Selection.activeObject as Sprite);
            }
        }
Exemplo n.º 18
0
        static void DoSpriteOverride(SpriteMesh spriteMesh, Sprite sprite)
        {
            SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh);

            if (spriteMeshData)
            {
                Rect rect       = SpriteMeshUtils.CalculateSpriteRect(spriteMesh, 5);
                Rect spriteRect = sprite.rect;

                Vector2 factor = new Vector2(spriteRect.width / rect.width, spriteRect.height / rect.height);

                Vector2[] newVertices = new List <Vector2>(spriteMeshData.vertices).ConvertAll(v => MathUtils.ClampPositionInRect(Vector2.Scale(v, factor), spriteRect) - spriteRect.position).ToArray();
                ushort[]  newIndices  = new List <int>(spriteMeshData.indices).ConvertAll <ushort>(i => (ushort)i).ToArray();

                sprite.OverrideGeometry(newVertices, newIndices);
            }
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
0
        static void ContextCreateSpriteMesh(MenuCommand menuCommand)
        {
            GameObject     spriteRendererGO = Selection.activeGameObject;
            SpriteRenderer spriteRenderer   = null;
            SpriteMesh     spriteMesh       = null;

            if (spriteRendererGO)
            {
                spriteRenderer = spriteRendererGO.GetComponent <SpriteRenderer>();
            }

            if (spriteRenderer)
            {
                spriteMesh = SpriteMeshUtils.CreateSpriteMesh(spriteRenderer.sprite);
            }

            if (spriteMesh)
            {
                Undo.DestroyObjectImmediate(spriteRenderer);

                string spriteMeshPath = AssetDatabase.GetAssetPath(spriteMesh);

                Mesh     mesh     = AssetDatabase.LoadAssetAtPath(spriteMeshPath, typeof(Mesh)) as Mesh;
                Material material = AssetDatabase.LoadAssetAtPath(spriteMeshPath, typeof(Material)) as Material;

                SpriteMeshInstance spriteMeshRenderer = spriteRendererGO.AddComponent <SpriteMeshInstance>();
                spriteMeshRenderer.spriteMesh = spriteMesh;

                MeshFilter meshFilter = spriteRendererGO.AddComponent <MeshFilter>();
                meshFilter.sharedMesh = mesh;

                MeshRenderer renderer = spriteRendererGO.AddComponent <MeshRenderer>();
                renderer.sharedMaterial = material;

                Undo.RegisterCreatedObjectUndo(meshFilter, "Create SpriteMesh");
                Undo.RegisterCreatedObjectUndo(renderer, "Create SpriteMesh");

                Selection.activeGameObject = spriteRendererGO;
            }
            else
            {
                Debug.Log("Select a SpriteRenderer with a Sprite to convert to SpriteMesh");
            }
        }
Exemplo n.º 21
0
        void DoSpriteOverride(SpriteMesh spriteMesh, Sprite sprite)
        {
            TextureImporter textureImporter = (TextureImporter)assetImporter;

#if UNITY_5_1 || UNITY_5_2 || UNITY_5_3_OR_NEWER
            Debug.Assert(textureImporter.spriteImportMode == SpriteImportMode.Single ||
                         textureImporter.spriteImportMode == SpriteImportMode.Multiple,
                         "Incompatible Sprite Mode. Use Single or Multiple.");
#endif

            SpriteMeshData spriteMeshData = SpriteMeshUtils.LoadSpriteMeshData(spriteMesh);

            if (spriteMeshData)
            {
                Vector2 factor           = Vector2.one;
                Rect    spriteRect       = sprite.rect;
                Rect    rectTextureSpace = new Rect();

                if (textureImporter.spriteImportMode == SpriteImportMode.Single)
                {
                    int width  = 0;
                    int height = 0;

                    SpriteMeshUtils.GetSpriteTextureSize(spriteMesh.sprite, ref width, ref height);
                    rectTextureSpace = new Rect(0, 0, width, height);
                }
                else if (textureImporter.spriteImportMode == SpriteImportMode.Multiple)
                {
                    rectTextureSpace = SpriteMeshUtils.CalculateSpriteRect(spriteMesh, 5);
                }

                factor = new Vector2(spriteRect.width / rectTextureSpace.width,
                                     spriteRect.height / rectTextureSpace.height);

                Vector2[] newVertices = new List <Vector2>(spriteMeshData.vertices).ConvertAll(v =>
                                                                                               MathUtils.ClampPositionInRect(Vector2.Scale(v, factor), spriteRect) - spriteRect.position)
                                        .ToArray();
                ushort[] newIndices =
                    new List <int>(spriteMeshData.indices).ConvertAll <ushort>(i => (ushort)i).ToArray();

                sprite.OverrideGeometry(newVertices, newIndices);
            }
        }
Exemplo n.º 22
0
        static bool Bindeable(SpriteMeshInstance targetSpriteMeshInstance,
                              SpriteMeshInstance destinationSpriteMeshInstance)
        {
            bool bindeable = false;

            if (targetSpriteMeshInstance &&
                destinationSpriteMeshInstance &&
                targetSpriteMeshInstance.spriteMesh &&
                destinationSpriteMeshInstance.spriteMesh &&
                targetSpriteMeshInstance.spriteMesh != destinationSpriteMeshInstance.spriteMesh &&
                destinationSpriteMeshInstance.cachedSkinnedRenderer)
            {
                SpriteMeshData targetData      = SpriteMeshUtils.LoadSpriteMeshData(targetSpriteMeshInstance.spriteMesh);
                SpriteMeshData destinationData =
                    SpriteMeshUtils.LoadSpriteMeshData(destinationSpriteMeshInstance.spriteMesh);

                bindeable = true;

                if (destinationData.bindPoses.Length >= targetData.bindPoses.Length)
                {
                    for (int i = 0; i < targetData.bindPoses.Length; ++i)
                    {
                        if (bindeable)
                        {
                            BindInfo bindInfo = targetData.bindPoses[i];

                            if (FindBindInfo(bindInfo, destinationData) < 0)
                            {
                                bindeable = false;
                            }
                        }
                    }
                }
                else
                {
                    bindeable = false;
                }
            }

            return(bindeable);
        }
Exemplo n.º 23
0
        static void Initialize()
        {
            s_SpriteMeshToTextureCache.Clear();

            string[] spriteMeshGUIDs = AssetDatabase.FindAssets("t:SpriteMesh");

            List <string> needsOverride = new List <string>();

            foreach (string guid in spriteMeshGUIDs)
            {
                SpriteMesh spriteMesh = LoadSpriteMesh(AssetDatabase.GUIDToAssetPath(guid));

                if (spriteMesh)
                {
                    UpdateCachedSpriteMesh(spriteMesh);
                    UpgradeSpriteMesh(spriteMesh);

                    if (s_SpriteMeshToTextureCache.ContainsKey(guid) &&
                        SpriteMeshUtils.NeedsOverride(spriteMesh))
                    {
                        needsOverride.Add(s_SpriteMeshToTextureCache[guid]);
                    }
                }
            }

            s_Initialized = true;

            needsOverride = needsOverride.Distinct().ToList();

            AssetDatabase.StartAssetEditing();

            foreach (string textureGuid in needsOverride)
            {
                AssetDatabase.ImportAsset(AssetDatabase.GUIDToAssetPath(textureGuid));
            }

            AssetDatabase.StopAssetEditing();
        }
Exemplo n.º 24
0
        static void Upgrade_001(SerializedObject spriteMeshSO)
        {
            spriteMeshSO.Update();

            SerializedProperty bindPosesProp = spriteMeshSO.FindProperty("bindPoses");

            for (int i = 0; i < bindPosesProp.arraySize; ++i)
            {
                SerializedProperty bindPoseProp = bindPosesProp.GetArrayElementAtIndex(i);
                bindPoseProp.FindPropertyRelative("color").colorValue = ColorRing.GetColor(i);
            }

            SerializedProperty pivotPointProp = spriteMeshSO.FindProperty("pivotPoint");
            SerializedProperty spriteProp     = spriteMeshSO.FindProperty("m_Sprite");
            Sprite             sprite         = spriteProp.objectReferenceValue as Sprite;

            if (sprite)
            {
                pivotPointProp.vector2Value = SpriteMeshUtils.GetPivotPoint(sprite);
            }

            spriteMeshSO.ApplyModifiedProperties();
        }
Exemplo n.º 25
0
 public static void UpdateAssets(SpriteMesh spriteMesh)
 {
     UpdateAssets(spriteMesh, SpriteMeshUtils.LoadSpriteMeshData(spriteMesh));
 }
Exemplo n.º 26
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;
        }
Exemplo n.º 27
0
        public void Triangulate()
        {
            SpriteMeshUtils.Triangulate(m_TexVertices, indexedEdges, holes, ref indices);

            isDirty = true;
        }
Exemplo n.º 28
0
        Vector3 ToVertex(Vector2 v)
        {
            float pixelsPerUnit = SpriteMeshUtils.GetSpritePixelsPerUnit(spriteMesh.sprite);

            return(ToVertex(v, pixelsPerUnit));
        }
Exemplo n.º 29
0
        List <Vector2> GetBlendshapePositions(BlendShape blendshape, float weight)
        {
            weight = Mathf.Clamp(weight, 0f, weight);

            List <Vector2> result = new List <Vector2>(m_TexVertices.Count);

            if (blendshape)
            {
                BlendShapeFrame prevFrame = null;
                BlendShapeFrame nextFrame = null;

                foreach (BlendShapeFrame frame in blendshape.frames)
                {
                    if (frame && frame.weight < weight)
                    {
                        prevFrame = frame;
                    }
                    else if (frame && nextFrame == null)
                    {
                        nextFrame = frame;

                        break;
                    }
                }

                Vector3[] prevFrameVertices = null;
                Vector3[] nextFrameVertices = null;

                float prevWeight = 0f;
                float nextWeight = 0f;

                if (prevFrame)
                {
                    prevFrameVertices = prevFrame.vertices;
                    prevWeight        = prevFrame.weight;
                }
                else
                {
                    prevFrameVertices = ToVertices(m_TexVertices).ToArray();
                }

                if (nextFrame)
                {
                    nextFrameVertices = nextFrame.vertices;
                    nextWeight        = nextFrame.weight;
                }
                else if (prevFrameVertices != null)
                {
                    nextFrameVertices = prevFrameVertices;
                    nextWeight        = prevWeight;
                }

                if (prevFrameVertices != null &&
                    nextFrameVertices != null &&
                    prevFrameVertices.Length == nextFrameVertices.Length)
                {
                    int   count         = prevFrameVertices.Length;
                    float pixelsPerUnit = SpriteMeshUtils.GetSpritePixelsPerUnit(spriteMesh.sprite);

                    float t = 0f;

                    float weightDelta = (nextWeight - prevWeight);

                    if (weightDelta > 0f)
                    {
                        t = (weight - prevWeight) / weightDelta;
                    }

                    for (int i = 0; i < count; ++i)
                    {
                        Vector3 v = Vector3.Lerp(prevFrameVertices[i], nextFrameVertices[i], t);

                        result.Add(SpriteMeshUtils.VertexToTexCoord(spriteMesh, pivotPoint, v, pixelsPerUnit));
                    }
                }
            }

            return(result);
        }
Exemplo n.º 30
0
 Vector3 ToVertex(Vector2 v, float pixelsPerUnit)
 {
     return(SpriteMeshUtils.TexCoordToVertex(pivotPoint, v, pixelsPerUnit));
 }