/// <summary> /// Tries to set the region (image) of a renderable attachment. If the attachment is not renderable, nothing is applied.</summary> public static void SetRegion (this Attachment attachment, AtlasRegion region, bool updateOffset = true) { var regionAttachment = attachment as RegionAttachment; if (regionAttachment != null) regionAttachment.SetRegion(region, updateOffset); var meshAttachment = attachment as MeshAttachment; if (meshAttachment != null) meshAttachment.SetRegion(region, updateOffset); }
/// <summary>Sets the region (image) of a RegionAttachment</summary> public static void SetRegion (this RegionAttachment attachment, AtlasRegion region, bool updateOffset = true) { if (region == null) throw new System.ArgumentNullException("region"); // (AtlasAttachmentLoader.cs) attachment.RendererObject = region; attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate); attachment.regionOffsetX = region.offsetX; attachment.regionOffsetY = region.offsetY; attachment.regionWidth = region.width; attachment.regionHeight = region.height; attachment.regionOriginalWidth = region.originalWidth; attachment.regionOriginalHeight = region.originalHeight; if (updateOffset) attachment.UpdateOffset(); }
/// <summary> /// Gets the Rect of an AtlasRegion according to Unity texture coordinates (x-right, y-up). /// This overload relies on region.page.height being correctly set.</summary> static Rect GetUnityRect(this AtlasRegion region) { return(region.GetSpineAtlasRect().SpineUnityFlipRect(region.page.height)); }
static AtlasAsset IngestSpineAtlas(TextAsset atlasText) { if (atlasText == null) { Debug.LogWarning("Atlas source cannot be null!"); return(null); } string primaryName = Path.GetFileNameWithoutExtension(atlasText.name).Replace(".atlas", ""); string assetPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(atlasText)); string atlasPath = assetPath + "/" + primaryName + "_Atlas.asset"; AtlasAsset atlasAsset = (AtlasAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(AtlasAsset)); List <Material> vestigialMaterials = new List <Material>(); if (atlasAsset == null) { atlasAsset = AtlasAsset.CreateInstance <AtlasAsset>(); } else { foreach (Material m in atlasAsset.materials) { vestigialMaterials.Add(m); } } atlasAsset.atlasFile = atlasText; //strip CR string atlasStr = atlasText.text; atlasStr = atlasStr.Replace("\r", ""); string[] atlasLines = atlasStr.Split('\n'); List <string> pageFiles = new List <string>(); for (int i = 0; i < atlasLines.Length - 1; i++) { if (atlasLines[i].Length == 0) { pageFiles.Add(atlasLines[i + 1]); } } atlasAsset.materials = new Material[pageFiles.Count]; for (int i = 0; i < pageFiles.Count; i++) { string texturePath = assetPath + "/" + pageFiles[i]; Texture2D texture = (Texture2D)AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)); TextureImporter texImporter = (TextureImporter)TextureImporter.GetAtPath(texturePath); texImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor; texImporter.mipmapEnabled = false; texImporter.alphaIsTransparency = false; texImporter.maxTextureSize = 2048; EditorUtility.SetDirty(texImporter); AssetDatabase.ImportAsset(texturePath); AssetDatabase.SaveAssets(); string pageName = Path.GetFileNameWithoutExtension(pageFiles[i]); //because this looks silly if (pageName == primaryName && pageFiles.Count == 1) { pageName = "Material"; } string materialPath = assetPath + "/" + primaryName + "_" + pageName + ".mat"; Material mat = (Material)AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)); if (mat == null) { mat = new Material(Shader.Find(defaultShader)); AssetDatabase.CreateAsset(mat, materialPath); } else { vestigialMaterials.Remove(mat); } mat.mainTexture = texture; EditorUtility.SetDirty(mat); AssetDatabase.SaveAssets(); atlasAsset.materials[i] = mat; } for (int i = 0; i < vestigialMaterials.Count; i++) { AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(vestigialMaterials[i])); } if (AssetDatabase.GetAssetPath(atlasAsset) == "") { AssetDatabase.CreateAsset(atlasAsset, atlasPath); } else { atlasAsset.Reset(); } EditorUtility.SetDirty(atlasAsset); AssetDatabase.SaveAssets(); //iterate regions and bake marked Atlas atlas = atlasAsset.GetAtlas(); FieldInfo field = typeof(Atlas).GetField("regions", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.NonPublic); List <AtlasRegion> regions = (List <AtlasRegion>)field.GetValue(atlas); string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset); string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath); string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name); bool hasBakedRegions = false; for (int i = 0; i < regions.Count; i++) { AtlasRegion region = regions[i]; string bakedPrefabPath = Path.Combine(bakedDirPath, SpineEditorUtilities.GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/"); GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject)); if (prefab != null) { BakeRegion(atlasAsset, region, false); hasBakedRegions = true; } } if (hasBakedRegions) { AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } return((AtlasAsset)AssetDatabase.LoadAssetAtPath(atlasPath, typeof(AtlasAsset))); }
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); }
public AtlasSprite(AtlasRegion region) { Region = new AtlasRegion(region); _originalOffsetX = region.OffsetX; _originalOffsetY = region.OffsetY; SetRegion(region); SetOrigin(region.OriginalWidth / 2f, region.OriginalHeight / 2f); int width = region.RegionWidth; int height = region.RegionHeight; if (region.Rotate) { base.Rotate90(true); base.SetBounds(region.OffsetX, region.OffsetY, height, width); } else base.SetBounds(region.OffsetX, region.OffsetY, width, height); Color = new Color(1f, 1f, 1f, 1f); }
private Sprite NewSprite(AtlasRegion region) { if (region.PackedWidth == region.OriginalWidth && region.PackedHeight == region.OriginalHeight) { if (region.Rotate) { Sprite sprite = new Sprite(region); sprite.SetBounds(0, 0, region.RegionHeight, region.RegionWidth); sprite.Rotate90(true); return sprite; } return new Sprite(region); } return new AtlasSprite(region); }
protected override void Draw() { defaultBlendState = PremultipliedAlpha ? BlendState.AlphaBlend : BlendState.NonPremultiplied; float[] vertices = this.vertices; List <Slot> drawOrder = Skeleton.DrawOrder; float x = Skeleton.X, y = Skeleton.Y; CCColor3B color3b = Color; float skeletonR = color3b.R / 255f; float skeletonG = color3b.G / 255f; float skeletonB = color3b.B / 255f; float skeletonA = Opacity / 255f; batcher.Begin(); for (int i = 0, n = drawOrder.Count; i < n; i++) { Slot slot = drawOrder[i]; Attachment attachment = slot.Attachment; if (attachment is RegionAttachment) { RegionAttachment regionAttachment = (RegionAttachment)attachment; BlendState blend = slot.Data.AdditiveBlending ? BlendState.Additive : defaultBlendState; var item = batcher.CreateGeometryInstance(4, 6); item.InstanceAttributes.BlendState = blend; item.GeometryPacket.Indicies = quadTriangles; var itemVertices = item.GeometryPacket.Vertices; AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject; item.GeometryPacket.Texture = (CCTexture2D)region.page.rendererObject; CCColor4B color; float a = skeletonA * slot.A * regionAttachment.A; if (PremultipliedAlpha) { color = new CCColor4B( skeletonR * slot.R * regionAttachment.R * a, skeletonG * slot.G * regionAttachment.G * a, skeletonB * slot.B * regionAttachment.B * a, a); } else { color = new CCColor4B( skeletonR * slot.R * regionAttachment.R, skeletonG * slot.G * regionAttachment.G, skeletonB * slot.B * regionAttachment.B, a); } itemVertices[TL].Colors = color; itemVertices[BL].Colors = color; itemVertices[BR].Colors = color; itemVertices[TR].Colors = color; regionAttachment.ComputeWorldVertices(x, y, slot.Bone, vertices); itemVertices[TL].Vertices.X = vertices[RegionAttachment.X1]; itemVertices[TL].Vertices.Y = vertices[RegionAttachment.Y1]; itemVertices[TL].Vertices.Z = 0; itemVertices[BL].Vertices.X = vertices[RegionAttachment.X2]; itemVertices[BL].Vertices.Y = vertices[RegionAttachment.Y2]; itemVertices[BL].Vertices.Z = 0; itemVertices[BR].Vertices.X = vertices[RegionAttachment.X3]; itemVertices[BR].Vertices.Y = vertices[RegionAttachment.Y3]; itemVertices[BR].Vertices.Z = 0; itemVertices[TR].Vertices.X = vertices[RegionAttachment.X4]; itemVertices[TR].Vertices.Y = vertices[RegionAttachment.Y4]; itemVertices[TR].Vertices.Z = 0; float[] uvs = regionAttachment.UVs; itemVertices[TL].TexCoords.U = uvs[RegionAttachment.X1]; itemVertices[TL].TexCoords.V = uvs[RegionAttachment.Y1]; itemVertices[BL].TexCoords.U = uvs[RegionAttachment.X2]; itemVertices[BL].TexCoords.V = uvs[RegionAttachment.Y2]; itemVertices[BR].TexCoords.U = uvs[RegionAttachment.X3]; itemVertices[BR].TexCoords.V = uvs[RegionAttachment.Y3]; itemVertices[TR].TexCoords.U = uvs[RegionAttachment.X4]; itemVertices[TR].TexCoords.V = uvs[RegionAttachment.Y4]; } else if (attachment is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)attachment; int vertexCount = mesh.Vertices.Length; if (vertices.Length < vertexCount) { vertices = new float[vertexCount]; } mesh.ComputeWorldVertices(x, y, slot, vertices); int[] triangles = mesh.Triangles; var item = batcher.CreateGeometryInstance(vertexCount, triangles.Length); item.GeometryPacket.Indicies = triangles; AtlasRegion region = (AtlasRegion)mesh.RendererObject; item.GeometryPacket.Texture = (CCTexture2D)region.page.rendererObject; CCColor4B color; float a = skeletonA * slot.A * mesh.A; if (PremultipliedAlpha) { color = new CCColor4B( skeletonR * slot.R * mesh.R * a, skeletonG * slot.G * mesh.G * a, skeletonB * slot.B * mesh.B * a, a); } else { color = new CCColor4B( skeletonR * slot.R * mesh.R, skeletonG * slot.G * mesh.G, skeletonB * slot.B * mesh.B, a); } float[] uvs = mesh.UVs; var itemVertices = item.GeometryPacket.Vertices; for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2) { itemVertices[ii].Colors = color; itemVertices[ii].Vertices.X = vertices[v]; itemVertices[ii].Vertices.Y = vertices[v + 1]; itemVertices[ii].Vertices.Z = 0; itemVertices[ii].TexCoords.U = uvs[v]; itemVertices[ii].TexCoords.V = uvs[v + 1]; } } else if (attachment is SkinnedMeshAttachment) { SkinnedMeshAttachment mesh = (SkinnedMeshAttachment)attachment; int vertexCount = mesh.UVs.Length; if (vertices.Length < vertexCount) { vertices = new float[vertexCount]; } mesh.ComputeWorldVertices(x, y, slot, vertices); int[] triangles = mesh.Triangles; var item = batcher.CreateGeometryInstance(vertexCount, triangles.Length); item.GeometryPacket.Indicies = triangles; AtlasRegion region = (AtlasRegion)mesh.RendererObject; item.GeometryPacket.Texture = (CCTexture2D)region.page.rendererObject; CCColor4B color; float a = skeletonA * slot.A * mesh.A; if (PremultipliedAlpha) { color = new CCColor4B( skeletonR * slot.R * mesh.R * a, skeletonG * slot.G * mesh.G * a, skeletonB * slot.B * mesh.B * a, a); } else { color = new CCColor4B( skeletonR * slot.R * mesh.R, skeletonG * slot.G * mesh.G, skeletonB * slot.B * mesh.B, a); } float[] uvs = mesh.UVs; var itemVertices = item.GeometryPacket.Vertices; for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2) { itemVertices[ii].Colors = color; itemVertices[ii].Vertices.X = vertices[v]; itemVertices[ii].Vertices.Y = vertices[v + 1]; itemVertices[ii].Vertices.Z = 0; itemVertices[ii].TexCoords.U = uvs[v]; itemVertices[ii].TexCoords.V = uvs[v + 1]; } } } batcher.End(); if (DebugBones || DebugSlots) { if (DebugSlots) { for (int i = 0; i < Skeleton.Slots.Count; ++i) { var slot = Skeleton.Slots[i]; if (slot.Attachment == null) { continue; } var verticesCount = 0; var worldVertices = new float[1000]; // Max number of vertices per mesh. if (slot.Attachment is RegionAttachment) { var attachment = (RegionAttachment)slot.Attachment; attachment.ComputeWorldVertices(Skeleton.X, Skeleton.Y, slot.bone, worldVertices); verticesCount = 8; } else if (slot.Attachment is MeshAttachment) { var mesh = (MeshAttachment)slot.Attachment; mesh.ComputeWorldVertices(Skeleton.X, Skeleton.Y, slot, worldVertices); verticesCount = mesh.Vertices.Length; } else if (slot.Attachment is SkinnedMeshAttachment) { var mesh = (SkinnedMeshAttachment)slot.Attachment; mesh.ComputeWorldVertices(Skeleton.X, Skeleton.Y, slot, worldVertices); verticesCount = mesh.UVs.Length; } else { continue; } CCPoint[] slotVertices = new CCPoint[verticesCount / 2]; for (int ii = 0, si = 0; ii < verticesCount; ii += 2, si++) { slotVertices[si].X = worldVertices[ii] * ScaleX; slotVertices[si].Y = worldVertices[ii + 1] * ScaleY; } CCDrawingPrimitives.Begin(); CCDrawingPrimitives.DrawPoly(slotVertices, verticesCount / 2, true, DebugSlotColor); CCDrawingPrimitives.End(); } } if (DebugBones) { // Bone lengths. for (int i = 0; i < Skeleton.Bones.Count; i++) { Bone bone = Skeleton.Bones[i]; x = bone.Data.Length * bone.M00 + bone.WorldX; y = bone.Data.Length * bone.M10 + bone.WorldY; CCDrawingPrimitives.Begin(); CCDrawingPrimitives.DrawLine(new CCPoint(bone.WorldX, bone.WorldY), new CCPoint(x, y), DebugJointColor); CCDrawingPrimitives.End(); } // Bone origins. for (int i = 0; i < Skeleton.Bones.Count; i++) { Bone bone = Skeleton.Bones[i]; CCDrawingPrimitives.Begin(); CCDrawingPrimitives.DrawPoint(new CCPoint(bone.WorldX, bone.WorldY), 4, DebugBoneColor); CCDrawingPrimitives.End(); } } } }
// private void SetAtttachment(SkeletonAnimation skeletonrenderer, string resName, string slotStrm) public void SetAttachment(SkeletonAnimation skeletonrenderer, string atlasName, string regionStr, string slotStr, string attachName = "") { // float scale = skeletonrenderer.skeletonDataAsset.scale; Slot slot = skeletonrenderer.skeleton.FindSlot(slotStr); if (slot == null) { return; //没有slot,可能表示没有该细节 } if (atlasName.Equals("")) //直接加载texture { Sprite tex = RoleGenerateManager.instance.LoadTexture(regionStr); if (tex == null) { Debug.LogErrorFormat("<RoleGenerator> 找不到贴图{0}", regionStr); return; } Attachment originalAttachment = slot.Attachment; if (originalAttachment != null) { slot.Attachment = originalAttachment.GetRemappedClone(tex, skeletonrenderer.SkeletonDataAsset.atlasAssets[0].materials[0]); } else { slot.Attachment = tex.ToRegionAttachment(skeletonrenderer.SkeletonDataAsset.atlasAssets[0].materials[0]); } } else { AtlasRegion atlasRegion = null; Atlas atlas = RoleGenerateManager.instance.LoadPartAtlas(atlasName).GetAtlas(); atlasRegion = atlas.FindRegion(regionStr); if (atlasRegion == null) { Debug.LogErrorFormat("<RoleGenerator> 图集:{0}中没有切片{1}", atlasName, regionStr); return; } Attachment originalAttachment = slot.Attachment; if (originalAttachment != null) { slot.Attachment = originalAttachment.GetRemappedClone(atlasRegion); } else { slot.Attachment = atlasRegion.ToRegionAttachment(atlasRegion.name); } } // if (slot!=null) // { // customSkin = customSkin ?? new Skin("custom skin"); // This requires that all customizations are done with skin placeholders defined in Spine. // int slotIndex = skeletonrenderer.skeleton.FindSlotIndex(slotStr); // You can access GetAttachment and SetAttachment via string, but caching the slotIndex is faster. // Skin templateSkin = skeletonrenderer.skeleton.Data.DefaultSkin; // Attachment templateAttachment = templateSkin.GetAttachment(slotIndex, attachName); // Attachment newAttachment = templateAttachment.GetRemappedClone(sprite, sourceMaterial); // customSkin.SetAttachment(slotIndex, attachName, newAttachment); // } }
/// <summary> /// Returns a new linked mesh linked to this MeshAttachment. It will be mapped to the AtlasRegion provided.</summary> public static MeshAttachment GetLinkedMesh(this MeshAttachment o, string newLinkedMeshName, AtlasRegion region) { if (region == null) { throw new System.ArgumentNullException("region"); } MeshAttachment mesh = o.NewLinkedMesh(); mesh.SetRegion(region, false); return(mesh); }
public AtlasSprite(AtlasRegion visual, Vector2 position, Vector2 grid) { Visual = visual; Position = new Vector2(position.X + visual.OffsetX, position.Y + visual.OffsetY); GridPosition = grid; }
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); }
public byte GetAtlasRegionUpgradesByRegion(AtlasRegion region) { return(SocketedWatchstones); }
public extern Sprite(AtlasRegion region, Vector2f size, int zOrder, string name = "");
public extern Sprite(AtlasRegion region, int zOrder, string name = "");
/// <summary> /// Convenience method for getting the main texture of the material of the page of the region.</summary> static Texture2D GetMainTexture(this AtlasRegion region) { var material = (region.page.rendererObject as Material); return(material.mainTexture as Texture2D); }
private Atlas LoadAtlas(UnityEngine.U2D.SpriteAtlas spriteAtlas) { List <AtlasPage> pages = new List <AtlasPage>(); List <AtlasRegion> regions = new List <AtlasRegion>(); Sprite[] sprites = new UnityEngine.Sprite[spriteAtlas.spriteCount]; spriteAtlas.GetSprites(sprites); if (sprites.Length == 0) { return(new Atlas(pages, regions)); } Texture2D texture = null; #if UNITY_EDITOR if (!Application.isPlaying) { texture = AccessPackedTextureEditor(spriteAtlas); } else #endif texture = AccessPackedTexture(sprites); Material material = materials[0]; #if !UNITY_EDITOR material.mainTexture = texture; #endif Spine.AtlasPage page = new AtlasPage(); page.name = spriteAtlas.name; page.width = texture.width; page.height = texture.height; page.format = Spine.Format.RGBA8888; page.minFilter = TextureFilter.Linear; page.magFilter = TextureFilter.Linear; page.uWrap = TextureWrap.ClampToEdge; page.vWrap = TextureWrap.ClampToEdge; page.rendererObject = material; pages.Add(page); sprites = AccessPackedSprites(spriteAtlas); int i = 0; for (; i < sprites.Length; ++i) { var sprite = sprites[i]; AtlasRegion region = new AtlasRegion(); region.name = sprite.name.Replace("(Clone)", ""); region.page = page; region.degrees = sprite.packingRotation == SpritePackingRotation.None ? 0 : 90; region.u2 = 1; region.v2 = 1; region.width = page.width; region.height = page.height; region.originalWidth = page.width; region.originalHeight = page.height; region.index = i; regions.Add(region); } var atlas = new Atlas(pages, regions); AssignRegionsFromSavedRegions(sprites, atlas); return(atlas); }
public void Draw(OpenGL gl, Skeleton skeleton) { if (skeleton == null) { return; } 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++) { float[] vertices = this.vertices; Slot slot = drawOrderItems[i]; Attachment attachment = slot.Attachment; if (attachment is RegionAttachment) { RegionAttachment regionAttachment = (RegionAttachment)attachment; MeshItem item = batcher.NextItem(4, 6); item.triangles = quadTriangles; VertexPositionColorTexture[] itemVertices = item.vertices; AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject; item.texture = (uint)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 = new Vector2(vertices[RegionAttachment.X1], vertices[RegionAttachment.Y1]); itemVertices[BL].position = new Vector2(vertices[RegionAttachment.X2], vertices[RegionAttachment.Y2]); itemVertices[BR].position = new Vector2(vertices[RegionAttachment.X3], vertices[RegionAttachment.Y3]); itemVertices[TR].position = new Vector2(vertices[RegionAttachment.X4], vertices[RegionAttachment.Y4]); float[] uvs = regionAttachment.UVs; itemVertices[TL].textureCoordinate = new Vector2(uvs[RegionAttachment.X1], uvs[RegionAttachment.Y1]); itemVertices[BL].textureCoordinate = new Vector2(uvs[RegionAttachment.X2], uvs[RegionAttachment.Y2]); itemVertices[BR].textureCoordinate = new Vector2(uvs[RegionAttachment.X3], uvs[RegionAttachment.Y3]); itemVertices[TR].textureCoordinate = new Vector2(uvs[RegionAttachment.X4], 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 = (uint)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 = new Vector2(vertices[v], vertices[v + 1]); itemVertices[ii].textureCoordinate = new Vector2(uvs[v], uvs[v + 1]); } } } batcher.Draw(gl); }
public static Skin GetRepackedSkin(this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = TextureFormat.RGBA32, bool mipmaps = false, Material materialPropertySource = null) { Dictionary <Skin.AttachmentKeyTuple, Attachment> attachments = o.Attachments; Skin skin = new Skin(newName); Dictionary <AtlasRegion, int> dictionary = new Dictionary <AtlasRegion, int>(); List <int> list = new List <int>(); List <Attachment> list2 = new List <Attachment>(); List <Texture2D> list3 = new List <Texture2D>(); List <AtlasRegion> list4 = new List <AtlasRegion>(); int num = 0; foreach (KeyValuePair <Skin.AttachmentKeyTuple, Attachment> item2 in attachments) { Attachment clone = item2.Value.GetClone(cloneMeshesAsLinked: true); if (IsRenderable(clone)) { AtlasRegion atlasRegion = clone.GetAtlasRegion(); if (dictionary.TryGetValue(atlasRegion, out int value)) { list.Add(value); } else { list4.Add(atlasRegion); list3.Add(atlasRegion.ToTexture()); dictionary.Add(atlasRegion, num); list.Add(num); num++; } list2.Add(clone); } Skin.AttachmentKeyTuple key = item2.Key; skin.AddAttachment(key.slotIndex, key.name, clone); } Texture2D texture2D = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps); texture2D.mipMapBias = -0.5f; texture2D.anisoLevel = list3[0].anisoLevel; texture2D.name = newName; Rect[] array = texture2D.PackTextures(list3.ToArray(), padding, maxAtlasSize); Material material = new Material(shader); if (materialPropertySource != null) { material.CopyPropertiesFromMaterial(materialPropertySource); material.shaderKeywords = materialPropertySource.shaderKeywords; } material.name = newName; material.mainTexture = texture2D; AtlasPage atlasPage = material.ToSpineAtlasPage(); atlasPage.name = newName; List <AtlasRegion> list5 = new List <AtlasRegion>(); int i = 0; for (int count = list4.Count; i < count; i++) { AtlasRegion atlasRegion2 = list4[i]; AtlasRegion item = UVRectToAtlasRegion(array[i], atlasRegion2.name, atlasPage, atlasRegion2.offsetX, atlasRegion2.offsetY, atlasRegion2.rotate); list5.Add(item); } int j = 0; for (int count2 = list2.Count; j < count2; j++) { Attachment attachment = list2[j]; attachment.SetRegion(list5[list[j]]); } t = texture2D; m = material; return(skin); }
private static Texture2D GetMainTexture(this AtlasRegion region) { Material material = region.page.rendererObject as Material; return(material.mainTexture as Texture2D); }
private void LoadRegions(TextureAtlasData data, Dictionary<TextureAtlasData.Page, TextureContext> pageToTexture) { foreach (var region in data.Regions) { int width = region.Width; int height = region.Height; AtlasRegion atlasRegion = new AtlasRegion(pageToTexture[region.Page], region.Left, region.Top, region.Rotate ? height : width, region.Rotate ? width : height) { Index = region.Index, Name = region.Name, OffsetX = region.OffsetX, OffsetY = region.OffsetY, OriginalWidth = region.OriginalWidth, OriginalHeight = region.OriginalHeight, Rotate = region.Rotate, Splits = region.Splits, Pads = region.Pads, }; if (region.Flip) atlasRegion.Flip(false, true); Regions.Add(atlasRegion); } }
public IntAndAtlasRegionKey(int i, AtlasRegion region) { this.i = i; this.region = region; }
public AtlasRegion(AtlasRegion region) { SetRegion(region); Index = region.Index; Name = region.Name; OffsetX = region.OffsetX; OffsetY = region.OffsetY; PackedWidth = region.PackedWidth; PackedHeight = region.PackedHeight; OriginalWidth = region.OriginalWidth; OriginalHeight = region.OriginalHeight; Rotate = region.Rotate; Splits = region.Splits; }
/// <summary> /// Convenience method for getting any texture of the material of the page of the region by texture property id.</summary> static Texture2D GetTexture(this AtlasRegion region, int texturePropertyId) { var material = (region.page.rendererObject as Material); return(material.GetTexture(texturePropertyId) as Texture2D); }
public byte GetAtlasRegionUpgradesByRegion(AtlasRegion region) { return(M.Read <byte>(Address + ServerDataOffsets.AtlasRegionUpgrades + region.Index)); }
public static MeshAttachment GetLinkedMesh(this MeshAttachment o, string newLinkedMeshName, AtlasRegion region, bool inheritDeform = true) { if (region == null) { throw new ArgumentNullException("region"); } if (o.ParentMesh != null) { o = o.ParentMesh; } MeshAttachment attachment = new MeshAttachment(newLinkedMeshName); attachment.SetRegion(region, false); attachment.Path = newLinkedMeshName; attachment.r = 1f; attachment.g = 1f; attachment.b = 1f; attachment.a = 1f; attachment.inheritDeform = inheritDeform; attachment.ParentMesh = o; attachment.UpdateUVs(); return(attachment); }
public static string GetPathSafeRegionName(AtlasRegion region) { return(region.name.Replace("/", "_")); }
public static Attachment GetRemappedClone(this Attachment o, Sprite sprite, Material sourceMaterial, bool premultiplyAlpha = true, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false) { AtlasRegion atlasRegion = !premultiplyAlpha?sprite.ToAtlasRegion(false) : sprite.ToAtlasRegionPMAClone(sourceMaterial, TextureFormat.RGBA32, false); return(o.GetRemappedClone(atlasRegion, cloneMeshAsLinked, useOriginalRegionSize, (1f / sprite.pixelsPerUnit))); }
public static Sprite ToSprite(this AtlasRegion ar, float pixelsPerUnit = 100) { return(Sprite.Create(ar.GetMainTexture(), ar.GetUnityRect(), new Vector2(0.5f, 0.5f), pixelsPerUnit)); }
public Mesh GenerateMesh(string name, Mesh mesh, out Material material, float scale = 0.01f) { AtlasRegion region = atlas.FindRegion(name); material = null; if (region != null) { if (mesh == null) { mesh = new Mesh(); mesh.name = name; } Vector3[] verts = new Vector3[4]; Vector2[] uvs = new Vector2[4]; Color[] colors = new Color[4] { Color.white, Color.white, Color.white, Color.white }; int[] triangles = new int[6] { 0, 1, 2, 2, 3, 0 }; float left, right, top, bottom; left = region.width / -2f; right = left * -1f; top = region.height / 2f; bottom = top * -1; verts[0] = new Vector3(left, bottom, 0) * scale; verts[1] = new Vector3(left, top, 0) * scale; verts[2] = new Vector3(right, top, 0) * scale; verts[3] = new Vector3(right, bottom, 0) * scale; float u, v, u2, v2; u = region.u; v = region.v; u2 = region.u2; v2 = region.v2; if (!region.rotate) { uvs[0] = new Vector2(u, v2); uvs[1] = new Vector2(u, v); uvs[2] = new Vector2(u2, v); uvs[3] = new Vector2(u2, v2); } else { uvs[0] = new Vector2(u2, v2); uvs[1] = new Vector2(u, v2); uvs[2] = new Vector2(u, v); uvs[3] = new Vector2(u2, v); } mesh.triangles = new int[0]; mesh.vertices = verts; mesh.uv = uvs; mesh.colors = colors; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); material = (Material)region.page.rendererObject; } else { mesh = null; } return(mesh); }
/// <summary> /// Gets the Rect of an AtlasRegion according to Unity texture coordinates (x-right, y-up).</summary> static Rect GetUnityRect(this AtlasRegion region, int textureHeight) { return(region.GetSpineAtlasRect().SpineUnityFlipRect(textureHeight)); }
/// <summary> /// 根据原始附件的类型选择生成不同类型的附件 /// </summary> /// <returns>The attachment by type.</returns> /// <param name="type">Type.</param> /// <param name="attachmentName">Attachment name.</param> /// <param name="atlasRegion">Atlas region.</param> private Attachment GetAttachmentByType(Spine.Unity.SkeletonAnimation skeletonAnim, RoleAttachmentType type, string attachmentName, AtlasRegion atlasRegion) { switch (type) { //暂时只有MeshAttachment和RegionAttachment case RoleAttachmentType.Region: return(SpineCreateAttachment.Instance.CreateRegionAttachmentByAtlasRegion(skeletonAnim, attachmentName, atlasRegion)); case RoleAttachmentType.LinkedMesh: case RoleAttachmentType.Mesh: return(SpineCreateAttachment.Instance.CreateMeshAttachmentByAtlasRegion(skeletonAnim, attachmentName, atlasRegion)); case RoleAttachmentType.BoundingBox: return(SpineCreateAttachment.Instance.CreateBoundingBoxAttachmentByName(skeletonAnim, attachmentName)); case RoleAttachmentType.Clipping: return(SpineCreateAttachment.Instance.CreateClippingAttachmentByName(skeletonAnim, attachmentName)); case RoleAttachmentType.Path: return(SpineCreateAttachment.Instance.CreatePathAttachmentByName(skeletonAnim, attachmentName)); case RoleAttachmentType.Point: return(SpineCreateAttachment.Instance.CreatePointAttachmentByName(skeletonAnim, attachmentName)); default: return(null); } }
/// <summary> /// Returns a new linked mesh linked to this MeshAttachment. It will be mapped to the AtlasRegion provided.</summary> public static MeshAttachment GetLinkedMesh(this MeshAttachment o, string newLinkedMeshName, AtlasRegion region, bool inheritDeform = true) { //if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName cannot be null or empty", "attachmentName"); if (region == null) { throw new System.ArgumentNullException("region"); } // If parentMesh is a linked mesh, create a link to its parent. Preserves Deform animations. if (o.parentMesh != null) { o = o.parentMesh; } // 1. NewMeshAttachment (AtlasAttachmentLoader.cs) var mesh = new MeshAttachment(newLinkedMeshName); mesh.SetRegion(region, false); // 2. (SkeletonJson.cs::ReadAttachment. case: LinkedMesh) mesh.Path = newLinkedMeshName; mesh.r = 1f; mesh.g = 1f; mesh.b = 1f; mesh.a = 1f; //mesh.ParentMesh property call below sets mesh.Width and mesh.Height // 3. Link mesh with parent. (SkeletonJson.cs) mesh.inheritDeform = inheritDeform; mesh.ParentMesh = o; mesh.UpdateUVs(); return(mesh); }
/// <summary> /// 通过AtlasRegion设置卡槽上的Attachment /// </summary> /// <returns><c>true</c>, if mesh attachment by atlas region was set, <c>false</c> otherwise.</returns> /// <param name="slotStr">Slot string.</param> /// <param name="atlasRegion">Atlas region.</param> public bool SetMeshAttachmentByAtlasRegion(SkeletonAnimation skeletonAnim, string slotStr, string attachmentName, AtlasRegion atlasRegion) { Slot slot = skeletonAnim.skeleton.FindSlot(slotStr); if (slot == null && atlasRegion == null) { return(false); } MeshAttachment attachment = SpineCreateAttachment.Instance.CreateMeshAttachmentByAtlasRegion(skeletonAnim, attachmentName, atlasRegion) as MeshAttachment; slot.Attachment = attachment; return(true); }
static AtlasRegion ToAtlasRegion (this Sprite s, bool isolatedTexture = false) { var region = new AtlasRegion(); region.name = s.name; region.index = -1; region.rotate = s.packed && s.packingRotation != SpritePackingRotation.None; // World space units Bounds bounds = s.bounds; Vector2 boundsMin = bounds.min, boundsMax = bounds.max; // Texture space/pixel units Rect spineRect = s.rect.SpineUnityFlipRect(s.texture.height); region.width = (int)spineRect.width; region.originalWidth = (int)spineRect.width; region.height = (int)spineRect.height; region.originalHeight = (int)spineRect.height; region.offsetX = spineRect.width * (0.5f - InverseLerp(boundsMin.x, boundsMax.x, 0)); region.offsetY = spineRect.height * (0.5f - InverseLerp(boundsMin.y, boundsMax.y, 0)); if (isolatedTexture) { region.u = 0; region.v = 1; region.u2 = 1; region.v2 = 0; region.x = 0; region.y = 0; } else { Texture2D tex = s.texture; Rect uvRect = TextureRectToUVRect(s.textureRect, tex.width, tex.height); region.u = uvRect.xMin; region.v = uvRect.yMax; region.u2 = uvRect.xMax; region.v2 = uvRect.yMin; region.x = (int)spineRect.x; region.y = (int)spineRect.y; } return region; }
public Mesh GenerateMesh(string name, Mesh mesh, out Material material, float scale = 0.01f) { AtlasRegion atlasRegion = atlas.FindRegion(name); material = null; if (atlasRegion != null) { if (mesh == null) { mesh = new Mesh(); mesh.name = name; } Vector3[] array = new Vector3[4]; Vector2[] array2 = new Vector2[4]; Color[] colors = new Color[4] { Color.white, Color.white, Color.white, Color.white }; int[] triangles = new int[6] { 0, 1, 2, 2, 3, 0 }; float num = (float)atlasRegion.width / -2f; float x = num * -1f; float num2 = (float)atlasRegion.height / 2f; float y = num2 * -1f; array[0] = new Vector3(num, y, 0f) * scale; array[1] = new Vector3(num, num2, 0f) * scale; array[2] = new Vector3(x, num2, 0f) * scale; array[3] = new Vector3(x, y, 0f) * scale; float u = atlasRegion.u; float v = atlasRegion.v; float u2 = atlasRegion.u2; float v2 = atlasRegion.v2; if (!atlasRegion.rotate) { array2[0] = new Vector2(u, v2); array2[1] = new Vector2(u, v); array2[2] = new Vector2(u2, v); array2[3] = new Vector2(u2, v2); } else { array2[0] = new Vector2(u2, v2); array2[1] = new Vector2(u, v2); array2[2] = new Vector2(u, v); array2[3] = new Vector2(u2, v); } mesh.triangles = new int[0]; mesh.vertices = array; mesh.uv = array2; mesh.colors = colors; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); material = (Material)atlasRegion.page.rendererObject; } else { mesh = null; } return(mesh); }
/// <summary> /// Returns a new linked mesh linked to this MeshAttachment. It will be mapped to the AtlasRegion provided.</summary> public static MeshAttachment GetLinkedMesh (this MeshAttachment o, string newLinkedMeshName, AtlasRegion region, bool inheritDeform = true) { //if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName cannot be null or empty", "attachmentName"); if (region == null) throw new System.ArgumentNullException("region"); // If parentMesh is a linked mesh, create a link to its parent. Preserves Deform animations. if (o.parentMesh != null) o = o.parentMesh; // 1. NewMeshAttachment (AtlasAttachmentLoader.cs) var mesh = new MeshAttachment(newLinkedMeshName); mesh.SetRegion(region, false); // 2. (SkeletonJson.cs::ReadAttachment. case: LinkedMesh) mesh.Path = newLinkedMeshName; mesh.r = 1f; mesh.g = 1f; mesh.b = 1f; mesh.a = 1f; //mesh.ParentMesh property call below sets mesh.Width and mesh.Height // 3. Link mesh with parent. (SkeletonJson.cs) mesh.inheritDeform = inheritDeform; mesh.ParentMesh = o; mesh.UpdateUVs(); return mesh; }
public AtlasRegion AddRegion(string name, TextureContext texture, int x, int y, int width, int height) { Textures.Add(texture); AtlasRegion region = new AtlasRegion(texture, x, y, width, height) { Name = name, OriginalWidth = width, OriginalHeight = height, Index = -1, }; Regions.Add(region); return region; }
/// <summary>Sets the region (image) of a MeshAttachment</summary> public static void SetRegion (this MeshAttachment attachment, AtlasRegion region, bool updateUVs = true) { if (region == null) throw new System.ArgumentNullException("region"); // (AtlasAttachmentLoader.cs) attachment.RendererObject = region; attachment.RegionU = region.u; attachment.RegionV = region.v; attachment.RegionU2 = region.u2; attachment.RegionV2 = region.v2; attachment.RegionRotate = region.rotate; attachment.regionOffsetX = region.offsetX; attachment.regionOffsetY = region.offsetY; attachment.regionWidth = region.width; attachment.regionHeight = region.height; attachment.regionOriginalWidth = region.originalWidth; attachment.regionOriginalHeight = region.originalHeight; if (updateUVs) attachment.UpdateUVs(); }
/// <summary> /// 通过AtlasRegion重建MeshAttachment /// </summary> /// <returns>The mesh attachment by atlas region.</returns> /// <param name="allAttachmentName">Attachment name.</param> /// <param name="atlasRegion">Atlas region.</param> public abstract Attachment CreateMeshAttachmentByAtlasRegion(Spine.Unity.SkeletonAnimation skeletonAnim, string allAttachmentName, AtlasRegion atlasRegion);