static Rect GetAltasTextureRect(Sprite sp, Texture2D atlasTexture) { Rect textureRect = new Rect(); Vector2[] uvs = SpriteUtility.GetSpriteUVs(sp, false); Vector2[] altasUvs = SpriteUtility.GetSpriteUVs(sp, true); int count = uvs.Length; int compare = 0; for (int i = 1; i < count; i++) { if (uvs[i].x != uvs[0].x && uvs[i].y != uvs[0].y) { compare = i; break; } } textureRect.width = (altasUvs[0].x - altasUvs[compare].x) / (uvs[0].x - uvs[compare].x); textureRect.height = (altasUvs[0].y - altasUvs[compare].y) / (uvs[0].y - uvs[compare].y); textureRect.x = altasUvs[0].x - textureRect.width * uvs[0].x; textureRect.y = altasUvs[0].y - textureRect.height * uvs[0].y; textureRect.x *= atlasTexture.width; textureRect.y *= atlasTexture.height; textureRect.width *= atlasTexture.width; textureRect.height *= atlasTexture.height; return(textureRect); }
public Mesh ConvertSprite(SpriteRenderer spriteRenderer) { Sprite sprite = spriteRenderer.sprite; spriteRenderer.transform.position = new Vector3(spriteRenderer.transform.position.x, spriteRenderer.transform.position.y, spriteRenderer.transform.position.z + (spriteRenderer.sortingOrder * sTarget.sortingOrderToZPositionMultiplier + sTarget.sortingOrderToZPositionOffset)); Mesh newMesh = new Mesh(); Vector3[] meshVerts = new Vector3[sprite.vertices.Length]; Color[] colors = new Color[sprite.vertices.Length]; Vector3[] normals = new Vector3[sprite.vertices.Length]; for (int i = 0; i < sprite.vertices.Length; i++) { meshVerts[i] = sprite.vertices[i]; } newMesh.vertices = meshVerts; for (int i = 0; i < sprite.vertices.Length; i++) { colors[i] = spriteRenderer.color; normals[i] = new Vector3(0, 0, -1); } newMesh.colors = colors; newMesh.normals = normals; newMesh.uv = SpriteUtility.GetSpriteUVs(sprite, false); int[] meshIndicies = new int[sprite.triangles.Length]; for (int i = 0; i < meshIndicies.Length; i++) { meshIndicies[i] = sprite.triangles[i]; } newMesh.SetIndices(meshIndicies, MeshTopology.Triangles, 0); newMesh.hideFlags = HideFlags.HideAndDontSave; return(newMesh); }
private static bool tryGetAtlasedUvsEditor(Sprite sprite, out Vector2[] uvs) { try { uvs = SpriteUtility.GetSpriteUVs(sprite, getAtlasData: true); return(true); } catch (Exception) { uvs = null; return(false); } }
static void SaveSubTextureInfo() { Texture2D altasTexture = SpriteUtility.GetSpriteTexture(spriteList[0], true); string filename = Application.dataPath + "/Resources/" + MapPrefabDef.MapTerrainSprData + ".txt"; StreamWriter sw; FileInfo t = new FileInfo(filename); if (!t.Exists) { //如果此文件不存在则创建 sw = t.CreateText(); } else { //如果此文件存在则打开 t.Delete(); sw = t.CreateText(); } byte[] altasdata = altasTexture.GetRawTextureData(); System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.AppendFormat(" {0}", altasTexture.name); sb.AppendFormat(" {0}", altasTexture.width); sb.AppendFormat(" {0}", altasTexture.height); sb.AppendFormat(" {0}", (int)altasTexture.format); sb.AppendFormat(" {0}", altasdata.Length); sb.AppendFormat(" {0}", altasTexture.mipmapCount); sb.Remove(0, 1); sw.WriteLine(sb.ToString()); List <Vector4> TArray = new List <Vector4>(); for (int i = 0; i < spriteList.Count; i++) { Vector2[] data = SpriteUtility.GetSpriteUVs(spriteList[i], true); Vector2 ms = new Vector2(altasTexture.width, altasTexture.height); //左上 Vector2 p0 = new Vector2(data[0].x * ms.x, data[0].y * ms.y); //右下 Vector2 p1 = new Vector2(data[1].x * ms.x, data[1].y * ms.y); //右上 Vector2 p2 = new Vector2(data[2].x * ms.x, data[2].y * ms.y); //左下 Vector2 p3 = new Vector2(data[3].x * ms.x, data[3].y * ms.y); var sp = p3; var ss = new Vector2(p1.x - p3.x, p0.y - p3.y); Vector4 pos = new Vector4(sp.x / ms.x, sp.y / ms.y, ss.x / ms.x, ss.y / ms.y); sw.WriteLine(spriteList[i].name + " " + pos.x + " " + pos.y + " " + pos.z + " " + pos.w + "\n"); } sw.Close(); //销毁流 sw.Dispose(); AssetDatabase.Refresh(); }
public static void WritePrefabPathToCSharp() { Debug.Log(string.Join(",", Packer.atlasNames)); Texture2D[] texts = Packer.GetTexturesForAtlas("1"); Sprite sprite0 = Resources.Load <Sprite>("Sprites/car02"); Sprite sprite1 = Resources.Load <Sprite>("Sprites/car0_scaled"); Vector2[] points0 = SpriteUtility.GetSpriteUVs(sprite0, true); Vector2[] points1 = SpriteUtility.GetSpriteUVs(sprite1, true); Debug.Log("tEXT"); }
private Mesh SpriteToMesh(SpriteRenderer spriteRenderer) { var sprite = spriteRenderer.sprite; Mesh result = new Mesh(); result.hideFlags = HideFlags.HideAndDontSave; result.Clear(); //verts var verts = sprite.vertices; var verts3d = new Vector3[verts.Length]; for (int i = 0; i < verts.Length; i++) { verts3d [i] = verts [i]; } result.vertices = verts3d; // uvs result.uv = SpriteUtility.GetSpriteUVs(sprite, false); // colors and normals var colors = new Color[result.vertices.Length]; var normals = new Vector3[result.vertices.Length]; for (int i = 0; i < colors.Length; i++) { colors [i] = spriteRenderer.color; normals [i] = Vector3.back; } result.colors = colors; result.normals = normals; // indicies var indicies2d = sprite.triangles; var indicies = new int[indicies2d.Length]; for (int i = 0; i < indicies.Length; i++) { indicies [i] = indicies2d [i]; } // finish it up result.SetIndices(indicies, MeshTopology.Triangles, 0); return(result); }
public static Mesh CreateSpriteMesh(Transform owner, Sprite sprite) { if (owner != null && sprite != null) { // Unparent the skin temporarily before adding the mesh Transform parent = owner.parent; owner.parent = null; // Reset the rotation before creating the mesh so the UV's will align properly Quaternion localRotation = owner.localRotation; owner.localRotation = Quaternion.identity; // Reset the scale before creating the mesh so the UV's will align properly Vector3 localScale = owner.localScale; owner.localScale = Vector3.one; Vector2[] vertices2D = SpriteUtility.GetSpriteMesh(sprite, false); int[] indices = SpriteUtility.GetSpriteIndices(sprite, false).Select(element => (int)element).ToArray(); // Create the Vector3 vertices Vector3[] vertices = new Vector3[vertices2D.Length]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0); } Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = indices; Vector2[] uvs = SpriteUtility.GetSpriteUVs(sprite, false); mesh.uv = uvs; mesh.RecalculateNormals(); mesh.RecalculateBounds(); // Reset the rotations of the object owner.localRotation = localRotation; owner.localScale = localScale; owner.parent = parent; return(mesh); } return(null); }
public static void GetSpriteData(Sprite sprite, out Vector2[] vertices, out IndexedEdge[] edges, out int[] indices, out Vector2 pivotPoint) { int width = 0; int height = 0; GetSpriteTextureSize(sprite, ref width, ref height); pivotPoint = Vector2.zero; Vector2[] uvs = SpriteUtility.GetSpriteUVs(sprite, false); vertices = new Vector2[uvs.Length]; for (int i = 0; i < uvs.Length; ++i) { vertices[i] = new Vector2(uvs[i].x * width, uvs[i].y * height); } ushort[] l_indices = sprite.triangles; indices = new int[l_indices.Length]; for (int i = 0; i < l_indices.Length; ++i) { indices[i] = (int)l_indices[i]; } HashSet <IndexedEdge> edgesSet = new HashSet <IndexedEdge>(); for (int i = 0; i < indices.Length; i += 3) { int index1 = indices[i]; int index2 = indices[i + 1]; int index3 = indices[i + 2]; IndexedEdge edge1 = new IndexedEdge(index1, index2); IndexedEdge edge2 = new IndexedEdge(index2, index3); IndexedEdge edge3 = new IndexedEdge(index1, index3); if (edgesSet.Contains(edge1)) { edgesSet.Remove(edge1); } else { edgesSet.Add(edge1); } if (edgesSet.Contains(edge2)) { edgesSet.Remove(edge2); } else { edgesSet.Add(edge2); } if (edgesSet.Contains(edge3)) { edgesSet.Remove(edge3); } else { edgesSet.Add(edge3); } } edges = new IndexedEdge[edgesSet.Count]; int edgeIndex = 0; foreach (IndexedEdge edge in edgesSet) { edges[edgeIndex] = edge; ++edgeIndex; } pivotPoint = GetPivotPoint(sprite); }
private static Vector2[] getAtlasedUvsEditor(Sprite sprite) { return(SpriteUtility.GetSpriteUVs(sprite, getAtlasData: true)); }
public static Dictionary <string, Texture2D> CreateAlphaAtlasTexture() { Dictionary <string, Texture2D> result = new Dictionary <string, Texture2D>(); List <Entry> entries = new List <Entry>(); Material mat = new Material(Shader.Find("Unlit/Transparent")); List <UnityEngine.Object> objects = new List <UnityEngine.Object>(); foreach (string path in AssetDatabase.FindAssets("t:sprite")) { objects.AddRange(AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GUIDToAssetPath(path))); } Sprite[] sprites = objects.Distinct() .Select(x => x as Sprite) .Where(x => x != null && x.packed) .ToArray(); foreach (Sprite sprite in sprites) { string atlasName; Texture2D atlasTexture; Packer.GetAtlasDataForSprite(sprite, out atlasName, out atlasTexture); Texture2D texture = SpriteUtility.GetSpriteTexture(sprite, false); if (atlasTexture != null && texture != null && texture.format == TextureFormat.RGBA32) { entries.Add(new Entry() { sprite = sprite, atlasName = atlasName, texture = texture, atlasTexture = atlasTexture, uvs = SpriteUtility.GetSpriteUVs(sprite, false), atlasUvs = SpriteUtility.GetSpriteUVs(sprite, true), }); } } var atlasGroups = from e in entries group e by e.atlasTexture; foreach (var atlasGroup in atlasGroups) { Texture tex = atlasGroup.Key; RenderTexture rt = RenderTexture.GetTemporary(tex.width, tex.height, 0, RenderTextureFormat.ARGB32); Graphics.SetRenderTarget(rt); GL.Clear(true, true, Color.clear); GL.PushMatrix(); GL.LoadOrtho(); foreach (var entry in atlasGroup) { mat.mainTexture = entry.texture; mat.SetPass(0); GL.Begin(GL.TRIANGLES); var tris = entry.sprite.triangles; foreach (int index in tris) { GL.TexCoord(entry.uvs[index]); GL.Vertex(entry.atlasUvs[index]); } GL.End(); } GL.PopMatrix(); Texture2D tex2 = new Texture2D(tex.width, tex.height, TextureFormat.RGBA32, false); tex2.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); var colors = tex2.GetPixels32(); int count = colors.Length; Color32[] newColors = new Color32[count]; for (int i = 0; i < count; i++) { byte alpha = colors[i].a; newColors[i] = new Color32(alpha, alpha, alpha, 255); } tex2.SetPixels32(newColors); tex2.Apply(); string texName = tex.name; texName = tex.name.Substring(0, texName.LastIndexOf("-")) + "-fmt32"; result.Add(texName, tex2); RenderTexture.ReleaseTemporary(rt); } return(result); }
public static void GetInfoFromSprite(Sprite sprite, List <Vertex> texVertices, List <IndexedEdge> indexedEdges, List <int> indices) { texVertices.Clear(); indices.Clear(); indexedEdges.Clear(); if (sprite) { Vector2[] uvs = SpriteUtility.GetSpriteUVs(sprite, false); for (int i = 0; i < uvs.Length; ++i) { Vector2 texCoord = new Vector2(uvs[i].x * sprite.texture.width, uvs[i].y * sprite.texture.height); texVertices.Add(new Vertex(texCoord)); } ushort[] l_indices = sprite.triangles; for (int i = 0; i < l_indices.Length; ++i) { indices.Add((int)l_indices[i]); } HashSet <Edge> edgesSet = new HashSet <Edge>(); for (int i = 0; i < indices.Count; i += 3) { int index1 = indices[i]; int index2 = indices[i + 1]; int index3 = indices[i + 2]; Edge edge1 = new Edge(texVertices[index1], texVertices[index2]); Edge edge2 = new Edge(texVertices[index2], texVertices[index3]); Edge edge3 = new Edge(texVertices[index1], texVertices[index3]); if (edgesSet.Contains(edge1)) { edgesSet.Remove(edge1); } else { edgesSet.Add(edge1); } if (edgesSet.Contains(edge2)) { edgesSet.Remove(edge2); } else { edgesSet.Add(edge2); } if (edgesSet.Contains(edge3)) { edgesSet.Remove(edge3); } else { edgesSet.Add(edge3); } } foreach (Edge edge in edgesSet) { indexedEdges.Add(new IndexedEdge(texVertices.IndexOf(edge.vertex1), texVertices.IndexOf(edge.vertex2))); } } }
void OnWizardCreate() { mySprites = Selection.GetFiltered <SpriteRenderer>(SelectionMode.Unfiltered); //EndObject part var go = GameObject.CreatePrimitive(PrimitiveType.Quad); go.name = "TileGroup"; DestroyImmediate(go.GetComponent <MeshCollider>()); var filter = go.GetComponent <MeshFilter>(); var renderer = go.GetComponent <MeshRenderer>(); /* esta parte da warnings, paso... * var mat = AssetDatabase.GetBuiltinExtraResource<Material>("Sprites-Default.mat"); * renderer.sharedMaterial = maty; * renderer.sharedMaterial.enableInstancing = true; */ Mesh finalMesh = new Mesh(); var combineList = new CombineInstance[mySprites.Length]; for (int i = 0; i < mySprites.Length; i++) { var spr = mySprites[i].sprite; Mesh result = new Mesh(); //verts var verts = spr.vertices; var verts3d = new Vector3[verts.Length]; for (int j = 0; j < verts.Length; j++) { verts3d[j] = verts[j]; } result.vertices = verts3d; // uvs result.uv = SpriteUtility.GetSpriteUVs(spr, false); // colors and normals var colors = new Color[result.vertices.Length]; var normals = new Vector3[result.vertices.Length]; for (int j = 0; j < colors.Length; j++) { colors[j] = mySprites[i].color; normals[j] = Vector3.back; } result.colors = colors; result.normals = normals; // indicies var indicies2d = spr.triangles; var indicies = new int[indicies2d.Length]; for (int j = 0; j < indicies.Length; j++) { indicies[j] = indicies2d[j]; } // finish it up result.SetIndices(indicies, MeshTopology.Triangles, 0); // combineList[i].mesh = result; } finalMesh.CombineMeshes(combineList); }
private void FindAllEntries(BuildTarget buildTarget, List <SpriteEntry> spriteEntries, List <AtlasEntry> atlasEntries) { var platformString = GetPlatformString(buildTarget); foreach (var guid in AssetDatabase.FindAssets("t:Texture")) { var path = AssetDatabase.GUIDToAssetPath(guid); var importer = AssetImporter.GetAtPath(path) as TextureImporter; if (importer == null) { continue; } // 获取sprite列表 var sprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(path) .Distinct() .OfType <Sprite>() .Where(x => x.packed) .ToArray(); for (var i = 0; i < sprites.Length; ++i) { var sprite = sprites[i]; Texture2D atlasTexture; string atlasName; Packer.GetAtlasDataForSprite(sprite, out atlasName, out atlasTexture); if (atlasTexture != null) { var entry = new SpriteEntry { Path = path, Sprite = sprite, Importer = importer, Texture = SpriteUtility.GetSpriteTexture(sprite, false), AtlasName = atlasName, Uvs = SpriteUtility.GetSpriteUVs(sprite, false), AtlasUvs = SpriteUtility.GetSpriteUVs(sprite, true), AtlasTexture = atlasTexture, }; spriteEntries.Add(entry); } } } // 获取atlas列表 var atlasGroups = from e in spriteEntries group e by e.AtlasTexture; foreach (var atlasGroup in atlasGroups) { var tex = atlasGroup.Key; var texName = tex.name; // 检查是否需要分离alpha通道 var atlasName = string.Empty; var needSeparateAlpha = false; foreach (var spriteEntry in atlasGroup) { var importer = spriteEntry.Importer; atlasName = importer.spritePackingTag; if (!string.IsNullOrEmpty(atlasName)) { var settings = importer.GetPlatformTextureSettings(platformString); var format = settings.format; if (format == TextureImporterFormat.Automatic) { format = importer.GetAutomaticFormat(platformString); } needSeparateAlpha = TextureUtility.IsTransparent(format); } } if (CustomAtlasConfig.ShouldKeepAlpha(atlasName)) { needSeparateAlpha = false; } var entry = new AtlasEntry { Name = texName, Texture = tex, SpriteEntries = atlasGroup.ToList(), NeedSeparateAlpha = needSeparateAlpha, }; atlasEntries.Add(entry); } }