private Spine.AtlasRegion CreateRegion(Texture2D texture) { Spine.AtlasRegion region = new Spine.AtlasRegion(); region.width = texture.width; region.height = texture.height; region.originalWidth = texture.width; region.originalHeight = texture.height; region.rotate = false; region.page = new Spine.AtlasPage(); region.page.name = texture.name; region.page.width = texture.width; region.page.height = texture.height; region.page.uWrap = Spine.TextureWrap.ClampToEdge; region.page.vWrap = Spine.TextureWrap.ClampToEdge; return(region); }
public RegionAttachment NewRegionAttachment(Skin skin, string name, string path) { RegionAttachment attachment = new RegionAttachment(name); Texture2D tex = sprite.texture; int instanceId = tex.GetInstanceID(); AtlasRegion atlasRegion; //check cache first if (atlasTable.ContainsKey(instanceId)) { atlasRegion = atlasTable[instanceId]; } else { //Setup new material Material mat = new Material(shader); if (sprite.packed) mat.name = "Unity Packed Sprite Material"; else mat.name = sprite.name + " Sprite Material"; mat.mainTexture = tex; //create faux-region to play nice with SkeletonRenderer atlasRegion = new AtlasRegion(); AtlasPage page = new AtlasPage(); page.rendererObject = mat; atlasRegion.page = page; //cache it atlasTable[instanceId] = atlasRegion; } Rect texRect = sprite.textureRect; //normalize rect to UV space of packed atlas texRect.x = Mathf.InverseLerp(0, tex.width, texRect.x); texRect.y = Mathf.InverseLerp(0, tex.height, texRect.y); texRect.width = Mathf.InverseLerp(0, tex.width, texRect.width); texRect.height = Mathf.InverseLerp(0, tex.height, texRect.height); Bounds bounds = sprite.bounds; Vector3 size = bounds.size; //TODO: make sure this rotation thing actually works bool rotated = false; if (sprite.packed) rotated = sprite.packingRotation == SpritePackingRotation.Any; //do some math and assign UVs and sizes attachment.SetUVs(texRect.xMin, texRect.yMax, texRect.xMax, texRect.yMin, rotated); attachment.RendererObject = atlasRegion; attachment.SetColor(Color.white); attachment.ScaleX = 1; attachment.ScaleY = 1; attachment.RegionOffsetX = sprite.rect.width * (0.5f - Mathf.InverseLerp(bounds.min.x, bounds.max.x, 0)) / sprite.pixelsPerUnit; attachment.RegionOffsetY = sprite.rect.height * (0.5f - Mathf.InverseLerp(bounds.min.y, bounds.max.y, 0)) / sprite.pixelsPerUnit; attachment.Width = size.x; attachment.Height = size.y; attachment.RegionWidth = size.x; attachment.RegionHeight = size.y; attachment.RegionOriginalWidth = size.x; attachment.RegionOriginalHeight = size.y; attachment.UpdateOffset(); return attachment; }
private void Load (TextReader reader, String imagesDir, TextureLoader textureLoader) { if (textureLoader == null) throw new ArgumentNullException("textureLoader cannot be null."); this.textureLoader = textureLoader; String[] tuple = new String[4]; AtlasPage page = null; while (true) { String line = reader.ReadLine(); if (line == null) break; if (line.Trim().Length == 0) page = null; else if (page == null) { page = new AtlasPage(); page.name = line; if (readTuple(reader, tuple) == 2) { // size is only optional for an atlas packed with an old TexturePacker. page.width = int.Parse(tuple[0]); page.height = int.Parse(tuple[1]); readTuple(reader, tuple); } page.format = (Format)Enum.Parse(typeof(Format), tuple[0], false); readTuple(reader, tuple); page.minFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0], false); page.magFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1], false); String direction = readValue(reader); page.uWrap = TextureWrap.ClampToEdge; page.vWrap = TextureWrap.ClampToEdge; if (direction == "x") page.uWrap = TextureWrap.Repeat; else if (direction == "y") page.vWrap = TextureWrap.Repeat; else if (direction == "xy") page.uWrap = page.vWrap = TextureWrap.Repeat; textureLoader.Load(page, Path.Combine(imagesDir, line)); pages.Add(page); } else { AtlasRegion region = new AtlasRegion(); region.name = line; region.page = page; region.rotate = Boolean.Parse(readValue(reader)); readTuple(reader, tuple); int x = int.Parse(tuple[0]); int y = int.Parse(tuple[1]); readTuple(reader, tuple); int width = int.Parse(tuple[0]); int height = int.Parse(tuple[1]); region.u = x / (float)page.width; region.v = y / (float)page.height; if (region.rotate) { region.u2 = (x + height) / (float)page.width; region.v2 = (y + width) / (float)page.height; } else { region.u2 = (x + width) / (float)page.width; region.v2 = (y + height) / (float)page.height; } region.x = x; region.y = y; region.width = Math.Abs(width); region.height = Math.Abs(height); if (readTuple(reader, tuple) == 4) { // split is optional region.splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3])}; if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits region.pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3])}; readTuple(reader, tuple); } } region.originalWidth = int.Parse(tuple[0]); region.originalHeight = int.Parse(tuple[1]); readTuple(reader, tuple); region.offsetX = int.Parse(tuple[0]); region.offsetY = int.Parse(tuple[1]); region.index = int.Parse(readValue(reader)); regions.Add(region); } } }
public static string GetPathSafeRegionName (AtlasRegion region) { return region.name.Replace("/", "_"); }
public static GameObject BakeRegion (AtlasAsset atlasAsset, AtlasRegion region, bool autoSave = true) { Atlas atlas = atlasAsset.GetAtlas(); string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); string bakedPrefabPath = Path.Combine(bakedDirPath, GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/"); GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject)); GameObject root; Mesh mesh; bool isNewPrefab = false; if (!Directory.Exists(bakedDirPath)) Directory.CreateDirectory(bakedDirPath); if (prefab == null) { root = new GameObject("temp", typeof(MeshFilter), typeof(MeshRenderer)); prefab = (GameObject)PrefabUtility.CreatePrefab(bakedPrefabPath, root); isNewPrefab = true; Object.DestroyImmediate(root); } mesh = (Mesh)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(Mesh)); Material mat = null; mesh = atlasAsset.GenerateMesh(region.name, mesh, out mat); if (isNewPrefab) { AssetDatabase.AddObjectToAsset(mesh, prefab); prefab.GetComponent<MeshFilter>().sharedMesh = mesh; } EditorUtility.SetDirty(mesh); EditorUtility.SetDirty(prefab); if (autoSave) { AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } prefab.GetComponent<MeshRenderer>().sharedMaterial = mat; return prefab; }
public void Draw(Skeleton skeleton) { List <Slot> drawOrder = skeleton.DrawOrder; float x = skeleton.X, y = skeleton.Y; float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment; if (regionAttachment != null) { BlendState blend = slot.Data.AdditiveBlending ? BlendState.Additive : defaultBlendState; if (device.BlendState != blend) { End(); device.BlendState = blend; } SpriteBatchItem item = batcher.CreateBatchItem(); AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject; item.Texture = (Texture2D)region.page.rendererObject; Color color; float a = skeletonA * slot.A; if (premultipliedAlpha) { color = new Color(skeletonR * slot.R * a, skeletonG * slot.G * a, skeletonB * slot.B * a, a); } else { color = new Color(skeletonR * slot.R, skeletonG * slot.G, skeletonB * slot.B, a); } item.vertexTL.Color = color; item.vertexBL.Color = color; item.vertexBR.Color = color; item.vertexTR.Color = color; float[] vertices = this.vertices; regionAttachment.ComputeWorldVertices(x, y, slot.Bone, vertices); item.vertexTL.Position.X = vertices[RegionAttachment.X1]; item.vertexTL.Position.Y = vertices[RegionAttachment.Y1]; item.vertexTL.Position.Z = 0; item.vertexBL.Position.X = vertices[RegionAttachment.X2]; item.vertexBL.Position.Y = vertices[RegionAttachment.Y2]; item.vertexBL.Position.Z = 0; item.vertexBR.Position.X = vertices[RegionAttachment.X3]; item.vertexBR.Position.Y = vertices[RegionAttachment.Y3]; item.vertexBR.Position.Z = 0; item.vertexTR.Position.X = vertices[RegionAttachment.X4]; item.vertexTR.Position.Y = vertices[RegionAttachment.Y4]; item.vertexTR.Position.Z = 0; float[] uvs = regionAttachment.UVs; item.vertexTL.TextureCoordinate.X = uvs[RegionAttachment.X1]; item.vertexTL.TextureCoordinate.Y = uvs[RegionAttachment.Y1]; item.vertexBL.TextureCoordinate.X = uvs[RegionAttachment.X2]; item.vertexBL.TextureCoordinate.Y = uvs[RegionAttachment.Y2]; item.vertexBR.TextureCoordinate.X = uvs[RegionAttachment.X3]; item.vertexBR.TextureCoordinate.Y = uvs[RegionAttachment.Y3]; item.vertexTR.TextureCoordinate.X = uvs[RegionAttachment.X4]; item.vertexTR.TextureCoordinate.Y = uvs[RegionAttachment.Y4]; } } }
public void Draw(Skeleton skeleton) { float[] vertices = this.vertices; var drawOrder = skeleton.DrawOrder; var drawOrderItems = skeleton.DrawOrder.Items; float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrderItems[i]; Attachment attachment = slot.Attachment; if (attachment is RegionAttachment) { RegionAttachment regionAttachment = (RegionAttachment)attachment; BlendState blend = slot.Data.BlendMode == BlendMode.additive ? BlendState.Additive : defaultBlendState; if (device.BlendState != blend) { End(); device.BlendState = blend; } MeshItem item = batcher.NextItem(4, 6); item.triangles = quadTriangles; VertexPositionColorTexture[] itemVertices = item.vertices; AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject; item.texture = (Texture2D)region.page.rendererObject; Color color; float a = skeletonA * slot.A * regionAttachment.A; if (premultipliedAlpha) { color = new Color( skeletonR * slot.R * regionAttachment.R * a, skeletonG * slot.G * regionAttachment.G * a, skeletonB * slot.B * regionAttachment.B * a, a); } else { color = new Color( skeletonR * slot.R * regionAttachment.R, skeletonG * slot.G * regionAttachment.G, skeletonB * slot.B * regionAttachment.B, a); } itemVertices[TL].Color = color; itemVertices[BL].Color = color; itemVertices[BR].Color = color; itemVertices[TR].Color = color; regionAttachment.ComputeWorldVertices(slot.Bone, vertices); itemVertices[TL].Position.X = vertices[RegionAttachment.X1]; itemVertices[TL].Position.Y = vertices[RegionAttachment.Y1]; itemVertices[TL].Position.Z = 0; itemVertices[BL].Position.X = vertices[RegionAttachment.X2]; itemVertices[BL].Position.Y = vertices[RegionAttachment.Y2]; itemVertices[BL].Position.Z = 0; itemVertices[BR].Position.X = vertices[RegionAttachment.X3]; itemVertices[BR].Position.Y = vertices[RegionAttachment.Y3]; itemVertices[BR].Position.Z = 0; itemVertices[TR].Position.X = vertices[RegionAttachment.X4]; itemVertices[TR].Position.Y = vertices[RegionAttachment.Y4]; itemVertices[TR].Position.Z = 0; float[] uvs = regionAttachment.UVs; itemVertices[TL].TextureCoordinate.X = uvs[RegionAttachment.X1]; itemVertices[TL].TextureCoordinate.Y = uvs[RegionAttachment.Y1]; itemVertices[BL].TextureCoordinate.X = uvs[RegionAttachment.X2]; itemVertices[BL].TextureCoordinate.Y = uvs[RegionAttachment.Y2]; itemVertices[BR].TextureCoordinate.X = uvs[RegionAttachment.X3]; itemVertices[BR].TextureCoordinate.Y = uvs[RegionAttachment.Y3]; itemVertices[TR].TextureCoordinate.X = uvs[RegionAttachment.X4]; itemVertices[TR].TextureCoordinate.Y = uvs[RegionAttachment.Y4]; } else if (attachment is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)attachment; int vertexCount = mesh.WorldVerticesLength; if (vertices.Length < vertexCount) { vertices = new float[vertexCount]; } mesh.ComputeWorldVertices(slot, vertices); int[] triangles = mesh.Triangles; MeshItem item = batcher.NextItem(vertexCount, triangles.Length); item.triangles = triangles; AtlasRegion region = (AtlasRegion)mesh.RendererObject; item.texture = (Texture2D)region.page.rendererObject; Color color; float a = skeletonA * slot.A * mesh.A; if (premultipliedAlpha) { color = new Color( skeletonR * slot.R * mesh.R * a, skeletonG * slot.G * mesh.G * a, skeletonB * slot.B * mesh.B * a, a); } else { color = new Color( skeletonR * slot.R * mesh.R, skeletonG * slot.G * mesh.G, skeletonB * slot.B * mesh.B, a); } float[] uvs = mesh.UVs; VertexPositionColorTexture[] itemVertices = item.vertices; for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2) { itemVertices[ii].Color = color; itemVertices[ii].Position.X = vertices[v]; itemVertices[ii].Position.Y = vertices[v + 1]; itemVertices[ii].Position.Z = 0; itemVertices[ii].TextureCoordinate.X = uvs[v]; itemVertices[ii].TextureCoordinate.Y = uvs[v + 1]; } } } }
public void Draw(Skeleton skeleton) { var drawOrder = skeleton.DrawOrder; var drawOrderItems = skeleton.DrawOrder.Items; float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A; Color color = new Color(); if (VertexEffect != null) { VertexEffect.Begin(skeleton); } for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrderItems[i]; Attachment attachment = slot.Attachment; float attachmentColorR, attachmentColorG, attachmentColorB, attachmentColorA; Texture2D texture = null; int verticesCount = 0; float[] vertices = this.vertices; int indicesCount = 0; int[] indices = null; float[] uvs = null; if (attachment is RegionAttachment) { RegionAttachment regionAttachment = (RegionAttachment)attachment; attachmentColorR = regionAttachment.R; attachmentColorG = regionAttachment.G; attachmentColorB = regionAttachment.B; attachmentColorA = regionAttachment.A; AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject; texture = (Texture2D)region.page.rendererObject; verticesCount = 4; regionAttachment.ComputeWorldVertices(slot.Bone, vertices, 0, 2); indicesCount = 6; indices = quadTriangles; uvs = regionAttachment.UVs; } else if (attachment is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)attachment; attachmentColorR = mesh.R; attachmentColorG = mesh.G; attachmentColorB = mesh.B; attachmentColorA = mesh.A; AtlasRegion region = (AtlasRegion)mesh.RendererObject; texture = (Texture2D)region.page.rendererObject; int vertexCount = mesh.WorldVerticesLength; if (vertices.Length < vertexCount) { vertices = new float[vertexCount]; } verticesCount = vertexCount >> 1; mesh.ComputeWorldVertices(slot, vertices); indicesCount = mesh.Triangles.Length; indices = mesh.Triangles; uvs = mesh.UVs; } else if (attachment is ClippingAttachment) { ClippingAttachment clip = (ClippingAttachment)attachment; clipper.ClipStart(slot, clip); continue; } else { continue; } // set blend state BlendState blend = slot.Data.BlendMode == BlendMode.Additive ? BlendState.Additive : defaultBlendState; if (device.BlendState != blend) { //End(); //device.BlendState = blend; } // calculate color float a = skeletonA * slot.A * attachmentColorA; if (premultipliedAlpha) { color = new Color( skeletonR * slot.R * attachmentColorR * a, skeletonG * slot.G * attachmentColorG * a, skeletonB * slot.B * attachmentColorB * a, a); } else { color = new Color( skeletonR * slot.R * attachmentColorR, skeletonG * slot.G * attachmentColorG, skeletonB * slot.B * attachmentColorB, a); } Color darkColor = new Color(); if (slot.HasSecondColor) { if (premultipliedAlpha) { darkColor = new Color(slot.R2 * a, slot.G2 * a, slot.B2 * a); } else { darkColor = new Color(slot.R2 * a, slot.G2 * a, slot.B2 * a); } } darkColor.A = premultipliedAlpha ? (byte)255 : (byte)0; // clip if (clipper.IsClipping) { clipper.ClipTriangles(vertices, verticesCount << 1, indices, indicesCount, uvs); vertices = clipper.ClippedVertices.Items; verticesCount = clipper.ClippedVertices.Count >> 1; indices = clipper.ClippedTriangles.Items; indicesCount = clipper.ClippedTriangles.Count; uvs = clipper.ClippedUVs.Items; } if (verticesCount == 0 || indicesCount == 0) { continue; } // submit to batch MeshItem item = batcher.NextItem(verticesCount, indicesCount); item.texture = texture; for (int ii = 0, nn = indicesCount; ii < nn; ii++) { item.triangles[ii] = indices[ii]; } VertexPositionColorTextureColor[] itemVertices = item.vertices; for (int ii = 0, v = 0, nn = verticesCount << 1; v < nn; ii++, v += 2) { itemVertices[ii].Color = color; itemVertices[ii].Color2 = darkColor; itemVertices[ii].Position.X = vertices[v]; itemVertices[ii].Position.Y = vertices[v + 1]; itemVertices[ii].Position.Z = 0; itemVertices[ii].TextureCoordinate.X = uvs[v]; itemVertices[ii].TextureCoordinate.Y = uvs[v + 1]; if (VertexEffect != null) { VertexEffect.Transform(ref itemVertices[ii]); } } clipper.ClipEnd(slot); } clipper.ClipEnd(); if (VertexEffect != null) { VertexEffect.End(); } }
private void Load(TextReader reader, String imagesDir, TextureLoader textureLoader) { if (textureLoader == null) { throw new ArgumentNullException("textureLoader cannot be null."); } this.textureLoader = textureLoader; String[] tuple = new String[4]; AtlasPage page = null; while (true) { String line = reader.ReadLine(); if (line == null) { break; } if (line.Trim().Length == 0) { page = null; } else if (page == null) { page = new AtlasPage(); page.name = line; if (readTuple(reader, tuple) == 2) // size is only optional for an atlas packed with an old TexturePacker. { page.width = int.Parse(tuple[0]); page.height = int.Parse(tuple[1]); readTuple(reader, tuple); } page.format = (Format)Enum.Parse(typeof(Format), tuple[0], false); readTuple(reader, tuple); page.minFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0], false); page.magFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1], false); String direction = readValue(reader); page.uWrap = TextureWrap.ClampToEdge; page.vWrap = TextureWrap.ClampToEdge; if (direction == "x") { page.uWrap = TextureWrap.Repeat; } else if (direction == "y") { page.vWrap = TextureWrap.Repeat; } else if (direction == "xy") { page.uWrap = page.vWrap = TextureWrap.Repeat; } textureLoader.Load(page, Path.Combine(imagesDir, line)); pages.Add(page); } else { AtlasRegion region = new AtlasRegion(); region.name = line; region.page = page; region.rotate = Boolean.Parse(readValue(reader)); readTuple(reader, tuple); int x = int.Parse(tuple[0]); int y = int.Parse(tuple[1]); readTuple(reader, tuple); int width = int.Parse(tuple[0]); int height = int.Parse(tuple[1]); region.u = x / (float)page.width; region.v = y / (float)page.height; if (region.rotate) { region.u2 = (x + height) / (float)page.width; region.v2 = (y + width) / (float)page.height; } else { region.u2 = (x + width) / (float)page.width; region.v2 = (y + height) / (float)page.height; } region.x = x; region.y = y; region.width = Math.Abs(width); region.height = Math.Abs(height); if (readTuple(reader, tuple) == 4) // split is optional { region.splits = new int[] { int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3]) }; if (readTuple(reader, tuple) == 4) // pad is optional, but only present with splits { region.pads = new int[] { int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3]) }; readTuple(reader, tuple); } } region.originalWidth = int.Parse(tuple[0]); region.originalHeight = int.Parse(tuple[1]); readTuple(reader, tuple); region.offsetX = int.Parse(tuple[0]); region.offsetY = int.Parse(tuple[1]); region.index = int.Parse(readValue(reader)); regions.Add(region); } } }
public void Draw(Skeleton skeleton) { List <Slot> drawOrder = skeleton.DrawOrder; for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment; if (regionAttachment != null) { BlendState blend = slot.Data.AdditiveBlending ? BlendState.Additive : BlendState.AlphaBlend; if (device.BlendState != blend) { End(); device.BlendState = blend; } SpriteBatchItem item = batcher.CreateBatchItem(); AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject; item.Texture = (Texture2D)region.page.rendererObject; byte r = (byte)(skeleton.R * slot.R * 255); byte g = (byte)(skeleton.G * slot.G * 255); byte b = (byte)(skeleton.B * slot.B * 255); byte a = (byte)(skeleton.A * slot.A * 255); item.vertexTL.Color.R = r; item.vertexTL.Color.G = g; item.vertexTL.Color.B = b; item.vertexTL.Color.A = a; item.vertexBL.Color.R = r; item.vertexBL.Color.G = g; item.vertexBL.Color.B = b; item.vertexBL.Color.A = a; item.vertexBR.Color.R = r; item.vertexBR.Color.G = g; item.vertexBR.Color.B = b; item.vertexBR.Color.A = a; item.vertexTR.Color.R = r; item.vertexTR.Color.G = g; item.vertexTR.Color.B = b; item.vertexTR.Color.A = a; float[] vertices = this.vertices; regionAttachment.ComputeVertices(skeleton.X, skeleton.Y, slot.Bone, vertices); item.vertexTL.Position.X = vertices[RegionAttachment.X1]; item.vertexTL.Position.Y = vertices[RegionAttachment.Y1]; item.vertexTL.Position.Z = 0; item.vertexBL.Position.X = vertices[RegionAttachment.X2]; item.vertexBL.Position.Y = vertices[RegionAttachment.Y2]; item.vertexBL.Position.Z = 0; item.vertexBR.Position.X = vertices[RegionAttachment.X3]; item.vertexBR.Position.Y = vertices[RegionAttachment.Y3]; item.vertexBR.Position.Z = 0; item.vertexTR.Position.X = vertices[RegionAttachment.X4]; item.vertexTR.Position.Y = vertices[RegionAttachment.Y4]; item.vertexTR.Position.Z = 0; float[] uvs = regionAttachment.UVs; item.vertexTL.TextureCoordinate.X = uvs[RegionAttachment.X1]; item.vertexTL.TextureCoordinate.Y = uvs[RegionAttachment.Y1]; item.vertexBL.TextureCoordinate.X = uvs[RegionAttachment.X2]; item.vertexBL.TextureCoordinate.Y = uvs[RegionAttachment.Y2]; item.vertexBR.TextureCoordinate.X = uvs[RegionAttachment.X3]; item.vertexBR.TextureCoordinate.Y = uvs[RegionAttachment.Y3]; item.vertexTR.TextureCoordinate.X = uvs[RegionAttachment.X4]; item.vertexTR.TextureCoordinate.Y = uvs[RegionAttachment.Y4]; } } }
public void load(StreamReader reader, String imagesDir, ContentManager content) { String[] tuple = new String[4]; AtlasPage page = null; while (true) { String line = reader.ReadLine(); if (line == null) { break; } if (line.Trim().Length == 0) { page = null; } else if (page == null) { page = NewAtlasPage(Path.Combine(imagesDir, Path.GetFileNameWithoutExtension(line)), content); page.Format = (Format)Enum.Parse(typeof(Format), readValue(reader), false); readTuple(reader, tuple); page.MinFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0]); page.MagFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1]); String direction = readValue(reader); page.UWrap = TextureWrap.ClampToEdge; page.VWrap = TextureWrap.ClampToEdge; if (direction == "x") { page.UWrap = TextureWrap.Repeat; } else if (direction == "y") { page.VWrap = TextureWrap.Repeat; } else if (direction == "xy") { page.UWrap = page.VWrap = TextureWrap.Repeat; } pages.Add(page); } else { AtlasRegion region = new AtlasRegion(); region.Name = line; region.Page = page; region.Rotate = Boolean.Parse(readValue(reader)); readTuple(reader, tuple); int x = int.Parse(tuple[0]); int y = int.Parse(tuple[1]); readTuple(reader, tuple); int width = int.Parse(tuple[0]); int height = int.Parse(tuple[1]); float invTexWidth = 1f / page.GetTextureWidth(); float invTexHeight = 1f / page.GetTextureHeight(); region.U = x * invTexWidth; region.V = y * invTexHeight; region.U2 = (x + width) * invTexWidth; region.V2 = (y + height) * invTexHeight; region.Width = Math.Abs(width); region.Height = Math.Abs(height); if (readTuple(reader, tuple) == 4) // split is optional { region.Splits = new int[] { int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3]) }; if (readTuple(reader, tuple) == 4) // pad is optional, but only present with splits { region.Pads = new int[] { int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3]) }; readTuple(reader, tuple); } } region.OriginalWidth = int.Parse(tuple[0]); region.OriginalHeight = int.Parse(tuple[1]); readTuple(reader, tuple); region.OffsetX = int.Parse(tuple[0]); region.OffsetY = int.Parse(tuple[1]); region.Index = int.Parse(readValue(reader)); regions.Add(region); } } }
public void load(StreamReader reader, String imagesDir) { String[] tuple = new String[4]; AtlasPage page = null; while (true) { String line = reader.ReadLine(); if (line == null) break; if (line.Trim().Length == 0) page = null; else if (page == null) { page = NewAtlasPage(Path.Combine(imagesDir, line)); page.Format = (Format)Enum.Parse(typeof(Format), readValue(reader), false); readTuple(reader, tuple); page.MinFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0]); page.MagFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1]); String direction = readValue(reader); page.UWrap = TextureWrap.ClampToEdge; page.VWrap = TextureWrap.ClampToEdge; if (direction == "x") page.UWrap = TextureWrap.Repeat; else if (direction == "y") page.VWrap = TextureWrap.Repeat; else if (direction == "xy") page.UWrap = page.VWrap = TextureWrap.Repeat; pages.Add(page); } else { AtlasRegion region = new AtlasRegion(); region.Name = line; region.Page = page; region.Rotate = Boolean.Parse(readValue(reader)); readTuple(reader, tuple); int x = int.Parse(tuple[0]); int y = int.Parse(tuple[1]); readTuple(reader, tuple); int width = int.Parse(tuple[0]); int height = int.Parse(tuple[1]); float invTexWidth = 1f / page.GetTextureWidth(); float invTexHeight = 1f / page.GetTextureHeight(); region.U = x * invTexWidth; region.V = y * invTexHeight; region.U2 = (x + width) * invTexWidth; region.V2 = (y + height) * invTexHeight; region.Width = Math.Abs(width); region.Height = Math.Abs(height); if (readTuple(reader, tuple) == 4) { // split is optional region.Splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3])}; if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits region.Pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3])}; readTuple(reader, tuple); } } region.OriginalWidth = int.Parse(tuple[0]); region.OriginalHeight = int.Parse(tuple[1]); readTuple(reader, tuple); region.OffsetX = int.Parse(tuple[0]); region.OffsetY = int.Parse(tuple[1]); region.Index = int.Parse(readValue(reader)); regions.Add(region); } } }
public static void DrawMeshAttachmnent(Pipeline pipeline, AtlasRegion region, float[] regionUVs, float[] worldVertices, int[] triangles, Color color, BlendMode blend) { float worldMinX = float.MaxValue; float worldMaxX = float.MinValue; float worldMinY = float.MaxValue; float worldMaxY = float.MinValue; for (int i = 0; i < worldVertices.Length; i += 2) { if (worldMinX > worldVertices[i]) { worldMinX = worldVertices[i]; } if (worldMaxX < worldVertices[i]) { worldMaxX = worldVertices[i]; } } for (int i = 0; i < worldVertices.Length; i += 2) { if (worldMinY > worldVertices[i + 1]) { worldMinY = worldVertices[i + 1]; } if (worldMaxY < worldVertices[i + 1]) { worldMaxY = worldVertices[i + 1]; } } int minX = (int)Math.Round(worldMinX); int maxX = (int)Math.Round(worldMaxX); int minY = (int)Math.Round(worldMinY); int maxY = (int)Math.Round(worldMaxY); Image bitmap = (Image)region.page.rendererObject; //// Extract region Bitmap img = Gdi.ExtractRegion(region, bitmap); Bitmap result = new Bitmap(maxX - minX + 3, maxY - minY + 3, PixelFormat.Format32bppPArgb); BitmapData imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, img.PixelFormat); BitmapData resultData = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.ReadWrite, result.PixelFormat); //Graphics e = Graphics.FromImage(result); //e.Clear(Color.Transparent); for (int i = 0; i < triangles.Length; i += 3) { Point[] srcP = new Point[3]; Point[] destP = new Point[3]; for (int j = 0; j < 3; j++) { srcP[j] = new Point((int)Math.Round(regionUVs[triangles[i + j] * 2] * img.Width), (int)Math.Round(regionUVs[triangles[i + j] * 2 + 1] * img.Height)); destP[j] = new Point((int)Math.Round(worldVertices[triangles[i + j] * 2]) - minX, (int)Math.Round(worldVertices[triangles[i + j] * 2 + 1]) - minY); } //e.DrawPolygon(new Pen(Brushes.Black), destP); Gdi.TriangleTransform(imgData, resultData, srcP, destP); } result.UnlockBits(resultData); img.UnlockBits(imgData); var primitive = pipeline.NewPrimitive(new PointF[] { new PointF(minX, minY), new PointF(maxX, minY), new PointF(minX, maxY) }); primitive.color = color; primitive.image = result; primitive.blend = (int)blend; primitive.texcoords = new RectangleF(0, 0, result.Width, result.Height); pipeline.Marshall(primitive); }
private void Load(TextReader reader, string imagesDir, TextureLoader textureLoader) { if (textureLoader == null) { throw new ArgumentNullException("textureLoader cannot be null."); } this.textureLoader = textureLoader; string[] array = new string[4]; AtlasPage atlasPage = null; for (;;) { string text = reader.ReadLine(); if (text == null) { break; } if (text.Trim().Length == 0) { atlasPage = null; } else if (atlasPage == null) { atlasPage = new AtlasPage(); atlasPage.name = text; if (Atlas.ReadTuple(reader, array) == 2) { atlasPage.width = int.Parse(array[0]); atlasPage.height = int.Parse(array[1]); Atlas.ReadTuple(reader, array); } atlasPage.format = (Format)Enum.Parse(typeof(Format), array[0], false); Atlas.ReadTuple(reader, array); atlasPage.minFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), array[0], false); atlasPage.magFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), array[1], false); string a = Atlas.ReadValue(reader); atlasPage.uWrap = TextureWrap.ClampToEdge; atlasPage.vWrap = TextureWrap.ClampToEdge; if (a == "x") { atlasPage.uWrap = TextureWrap.Repeat; } else if (a == "y") { atlasPage.vWrap = TextureWrap.Repeat; } else if (a == "xy") { atlasPage.uWrap = (atlasPage.vWrap = TextureWrap.Repeat); } textureLoader.Load(atlasPage, Path.Combine(imagesDir, text)); this.pages.Add(atlasPage); } else { AtlasRegion atlasRegion = new AtlasRegion(); atlasRegion.name = text; atlasRegion.page = atlasPage; atlasRegion.rotate = bool.Parse(Atlas.ReadValue(reader)); Atlas.ReadTuple(reader, array); int num = int.Parse(array[0]); int num2 = int.Parse(array[1]); Atlas.ReadTuple(reader, array); int num3 = int.Parse(array[0]); int num4 = int.Parse(array[1]); atlasRegion.u = (float)num / (float)atlasPage.width; atlasRegion.v = (float)num2 / (float)atlasPage.height; if (atlasRegion.rotate) { atlasRegion.u2 = (float)(num + num4) / (float)atlasPage.width; atlasRegion.v2 = (float)(num2 + num3) / (float)atlasPage.height; } else { atlasRegion.u2 = (float)(num + num3) / (float)atlasPage.width; atlasRegion.v2 = (float)(num2 + num4) / (float)atlasPage.height; } atlasRegion.x = num; atlasRegion.y = num2; atlasRegion.width = Math.Abs(num3); atlasRegion.height = Math.Abs(num4); if (Atlas.ReadTuple(reader, array) == 4) { atlasRegion.splits = new int[] { int.Parse(array[0]), int.Parse(array[1]), int.Parse(array[2]), int.Parse(array[3]) }; if (Atlas.ReadTuple(reader, array) == 4) { atlasRegion.pads = new int[] { int.Parse(array[0]), int.Parse(array[1]), int.Parse(array[2]), int.Parse(array[3]) }; Atlas.ReadTuple(reader, array); } } atlasRegion.originalWidth = int.Parse(array[0]); atlasRegion.originalHeight = int.Parse(array[1]); Atlas.ReadTuple(reader, array); atlasRegion.offsetX = (float)int.Parse(array[0]); atlasRegion.offsetY = (float)int.Parse(array[1]); atlasRegion.index = int.Parse(Atlas.ReadValue(reader)); this.regions.Add(atlasRegion); } } }
private void initialize(TextReader reader, Object texture, int textureWidth, int textureHeight) { TextureWidth = textureWidth; TextureHeight = textureHeight; Texture = texture; Regions = new List<AtlasRegion>(); float invTexWidth = 1f / textureWidth; float invTexHeight = 1f / textureHeight; String[] tuple = new String[4]; // Skip past first page name. while (true) { String line = reader.ReadLine(); if (line.Trim().Length != 0) break; } Format = (Format)Enum.Parse(typeof(Format), readValue(reader), false); readTuple(reader, tuple); MinFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0]); MagFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1]); String direction = readValue(reader); UWrap = TextureWrap.ClampToEdge; VWrap = TextureWrap.ClampToEdge; if (direction == "x") UWrap = TextureWrap.Repeat; else if (direction == "y") VWrap = TextureWrap.Repeat; else if (direction == "xy") UWrap = VWrap = TextureWrap.Repeat; while (true) { String line = reader.ReadLine(); if (line == null || line.Trim().Length == 0) break; AtlasRegion region = new AtlasRegion(); region.Atlas = this; region.Name = line; region.Rotate = Boolean.Parse(readValue(reader)); readTuple(reader, tuple); int x = int.Parse(tuple[0]); int y = int.Parse(tuple[1]); readTuple(reader, tuple); int width = int.Parse(tuple[0]); int height = int.Parse(tuple[1]); region.U = x * invTexWidth; region.V = y * invTexHeight; region.U2 = (x + width) * invTexWidth; region.V2 = (y + height) * invTexHeight; region.Width = Math.Abs(width); region.Height = Math.Abs(height); if (readTuple(reader, tuple) == 4) { // split is optional region.Splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3])}; if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits region.Pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]), int.Parse(tuple[2]), int.Parse(tuple[3])}; readTuple(reader, tuple); } } region.OriginalWidth = int.Parse(tuple[0]); region.OriginalHeight = int.Parse(tuple[1]); readTuple(reader, tuple); region.OffsetX = int.Parse(tuple[0]); region.OffsetY = int.Parse(tuple[1]); region.Index = int.Parse(readValue(reader)); Regions.Add(region); } while (true) { String line = reader.ReadLine(); if (line == null) break; if (line.Trim().Length != 0) throw new Exception("An atlas with multiple images is not supported."); } }
public RegionAttachment NewRegionAttachment (Skin skin, string name, string path) { RegionAttachment attachment = new RegionAttachment(name); Texture2D tex = sprite.texture; int instanceId = tex.GetInstanceID(); AtlasRegion atlasRegion; bool cachedMaterialExists = atlasTable.TryGetValue(instanceId, out atlasRegion); if (!cachedMaterialExists) { // Setup new material. var material = new Material(shader); if (sprite.packed) material.name = "Unity Packed Sprite Material"; else material.name = sprite.name + " Sprite Material"; material.mainTexture = tex; // Create faux-region to play nice with SkeletonRenderer. atlasRegion = new AtlasRegion(); var page = new AtlasPage(); page.rendererObject = material; atlasRegion.page = page; // Cache it. atlasTable[instanceId] = atlasRegion; } Rect texRect = sprite.textureRect; // Normalize rect to UV space of packed atlas texRect.x = Mathf.InverseLerp(0, tex.width, texRect.x); texRect.y = Mathf.InverseLerp(0, tex.height, texRect.y); texRect.width = Mathf.InverseLerp(0, tex.width, texRect.width); texRect.height = Mathf.InverseLerp(0, tex.height, texRect.height); Bounds bounds = sprite.bounds; Vector2 boundsMin = bounds.min, boundsMax = bounds.max; Vector2 size = bounds.size; float spriteUnitsPerPixel = 1f / sprite.pixelsPerUnit; bool rotated = false; if (sprite.packed) rotated = sprite.packingRotation == SpritePackingRotation.Any; attachment.SetUVs(texRect.xMin, texRect.yMax, texRect.xMax, texRect.yMin, rotated); attachment.RendererObject = atlasRegion; attachment.SetColor(Color.white); attachment.ScaleX = 1; attachment.ScaleY = 1; attachment.RegionOffsetX = sprite.rect.width * (0.5f - InverseLerp(boundsMin.x, boundsMax.x, 0)) * spriteUnitsPerPixel; attachment.RegionOffsetY = sprite.rect.height * (0.5f - InverseLerp(boundsMin.y, boundsMax.y, 0)) * spriteUnitsPerPixel; attachment.Width = size.x; attachment.Height = size.y; attachment.RegionWidth = size.x; attachment.RegionHeight = size.y; attachment.RegionOriginalWidth = size.x; attachment.RegionOriginalHeight = size.y; attachment.UpdateOffset(); return attachment; }