AtlasUtilities.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using UnityEngine;
  30. using System.Collections.Generic;
  31. using System;
  32. namespace Spine.Unity.AttachmentTools {
  33. public static class AtlasUtilities {
  34. internal const TextureFormat SpineTextureFormat = TextureFormat.RGBA32;
  35. internal const float DefaultMipmapBias = -0.5f;
  36. internal const bool UseMipMaps = false;
  37. internal const float DefaultScale = 0.01f;
  38. const int NonrenderingRegion = -1;
  39. public static AtlasRegion ToAtlasRegion (this Texture2D t, Material materialPropertySource, float scale = DefaultScale) {
  40. return t.ToAtlasRegion(materialPropertySource.shader, scale, materialPropertySource);
  41. }
  42. public static AtlasRegion ToAtlasRegion (this Texture2D t, Shader shader, float scale = DefaultScale, Material materialPropertySource = null) {
  43. var material = new Material(shader);
  44. if (materialPropertySource != null) {
  45. material.CopyPropertiesFromMaterial(materialPropertySource);
  46. material.shaderKeywords = materialPropertySource.shaderKeywords;
  47. }
  48. material.mainTexture = t;
  49. var page = material.ToSpineAtlasPage();
  50. float width = t.width;
  51. float height = t.height;
  52. var region = new AtlasRegion();
  53. region.name = t.name;
  54. region.index = -1;
  55. region.rotate = false;
  56. // World space units
  57. Vector2 boundsMin = Vector2.zero, boundsMax = new Vector2(width, height) * scale;
  58. // Texture space/pixel units
  59. region.width = (int)width;
  60. region.originalWidth = (int)width;
  61. region.height = (int)height;
  62. region.originalHeight = (int)height;
  63. region.offsetX = width * (0.5f - InverseLerp(boundsMin.x, boundsMax.x, 0));
  64. region.offsetY = height * (0.5f - InverseLerp(boundsMin.y, boundsMax.y, 0));
  65. // Use the full area of the texture.
  66. region.u = 0;
  67. region.v = 1;
  68. region.u2 = 1;
  69. region.v2 = 0;
  70. region.x = 0;
  71. region.y = 0;
  72. region.page = page;
  73. return region;
  74. }
  75. /// <summary>
  76. /// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate of the Sprite's texture data.</summary>
  77. public static AtlasRegion ToAtlasRegionPMAClone (this Texture2D t, Material materialPropertySource, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) {
  78. return t.ToAtlasRegionPMAClone(materialPropertySource.shader, textureFormat, mipmaps, materialPropertySource);
  79. }
  80. /// <summary>
  81. /// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate of the Sprite's texture data.</summary>
  82. public static AtlasRegion ToAtlasRegionPMAClone (this Texture2D t, Shader shader, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, Material materialPropertySource = null) {
  83. var material = new Material(shader);
  84. if (materialPropertySource != null) {
  85. material.CopyPropertiesFromMaterial(materialPropertySource);
  86. material.shaderKeywords = materialPropertySource.shaderKeywords;
  87. }
  88. var newTexture = t.GetClone(textureFormat, mipmaps, applyPMA : true);
  89. newTexture.name = t.name + "-pma-";
  90. material.name = t.name + shader.name;
  91. material.mainTexture = newTexture;
  92. var page = material.ToSpineAtlasPage();
  93. var region = newTexture.ToAtlasRegion(shader);
  94. region.page = page;
  95. return region;
  96. }
  97. /// <summary>
  98. /// Creates a new Spine.AtlasPage from a UnityEngine.Material. If the material has a preassigned texture, the page width and height will be set.</summary>
  99. public static AtlasPage ToSpineAtlasPage (this Material m) {
  100. var newPage = new AtlasPage {
  101. rendererObject = m,
  102. name = m.name
  103. };
  104. var t = m.mainTexture;
  105. if (t != null) {
  106. newPage.width = t.width;
  107. newPage.height = t.height;
  108. }
  109. return newPage;
  110. }
  111. /// <summary>
  112. /// Creates a Spine.AtlasRegion from a UnityEngine.Sprite.</summary>
  113. public static AtlasRegion ToAtlasRegion (this Sprite s, AtlasPage page) {
  114. if (page == null) throw new System.ArgumentNullException("page", "page cannot be null. AtlasPage determines which texture region belongs and how it should be rendered. You can use material.ToSpineAtlasPage() to get a shareable AtlasPage from a Material, or use the sprite.ToAtlasRegion(material) overload.");
  115. var region = s.ToAtlasRegion();
  116. region.page = page;
  117. return region;
  118. }
  119. /// <summary>
  120. /// Creates a Spine.AtlasRegion from a UnityEngine.Sprite. This creates a new AtlasPage object for every AtlasRegion you create. You can centralize Material control by creating a shared atlas page using Material.ToSpineAtlasPage and using the sprite.ToAtlasRegion(AtlasPage) overload.</summary>
  121. public static AtlasRegion ToAtlasRegion (this Sprite s, Material material) {
  122. var region = s.ToAtlasRegion();
  123. region.page = material.ToSpineAtlasPage();
  124. return region;
  125. }
  126. public static AtlasRegion ToAtlasRegionPMAClone (this Sprite s, Material materialPropertySource, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) {
  127. return s.ToAtlasRegionPMAClone(materialPropertySource.shader, textureFormat, mipmaps, materialPropertySource);
  128. }
  129. /// <summary>
  130. /// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate of the Sprite's texture data.</summary>
  131. public static AtlasRegion ToAtlasRegionPMAClone (this Sprite s, Shader shader, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, Material materialPropertySource = null) {
  132. var material = new Material(shader);
  133. if (materialPropertySource != null) {
  134. material.CopyPropertiesFromMaterial(materialPropertySource);
  135. material.shaderKeywords = materialPropertySource.shaderKeywords;
  136. }
  137. var tex = s.ToTexture(textureFormat, mipmaps, applyPMA : true);
  138. tex.name = s.name + "-pma-";
  139. material.name = tex.name + shader.name;
  140. material.mainTexture = tex;
  141. var page = material.ToSpineAtlasPage();
  142. var region = s.ToAtlasRegion(true);
  143. region.page = page;
  144. return region;
  145. }
  146. internal static AtlasRegion ToAtlasRegion (this Sprite s, bool isolatedTexture = false) {
  147. var region = new AtlasRegion();
  148. region.name = s.name;
  149. region.index = -1;
  150. region.rotate = s.packed && s.packingRotation != SpritePackingRotation.None;
  151. // World space units
  152. Bounds bounds = s.bounds;
  153. Vector2 boundsMin = bounds.min, boundsMax = bounds.max;
  154. // Texture space/pixel units
  155. Rect spineRect = s.rect.SpineUnityFlipRect(s.texture.height);
  156. region.width = (int)spineRect.width;
  157. region.originalWidth = (int)spineRect.width;
  158. region.height = (int)spineRect.height;
  159. region.originalHeight = (int)spineRect.height;
  160. region.offsetX = spineRect.width * (0.5f - InverseLerp(boundsMin.x, boundsMax.x, 0));
  161. region.offsetY = spineRect.height * (0.5f - InverseLerp(boundsMin.y, boundsMax.y, 0));
  162. if (isolatedTexture) {
  163. region.u = 0;
  164. region.v = 1;
  165. region.u2 = 1;
  166. region.v2 = 0;
  167. region.x = 0;
  168. region.y = 0;
  169. } else {
  170. Texture2D tex = s.texture;
  171. Rect uvRect = TextureRectToUVRect(s.textureRect, tex.width, tex.height);
  172. region.u = uvRect.xMin;
  173. region.v = uvRect.yMax;
  174. region.u2 = uvRect.xMax;
  175. region.v2 = uvRect.yMin;
  176. region.x = (int)spineRect.x;
  177. region.y = (int)spineRect.y;
  178. }
  179. return region;
  180. }
  181. #region Runtime Repacking
  182. /// <summary>
  183. /// Fills the outputAttachments list with new attachment objects based on the attachments in sourceAttachments,
  184. /// but mapped to a new single texture using the same material.</summary>
  185. /// <remarks>Returned <c>Material</c> and <c>Texture</c> behave like <c>new Texture2D()</c>, thus you need to call <c>Destroy()</c>
  186. /// to free resources.</remarks>
  187. /// <param name="sourceAttachments">The list of attachments to be repacked.</param>
  188. /// <param name = "outputAttachments">The List(Attachment) to populate with the newly created Attachment objects.</param>
  189. ///
  190. /// <param name="materialPropertySource">May be null. If no Material property source is provided, no special </param>
  191. public static void GetRepackedAttachments (List<Attachment> sourceAttachments, List<Attachment> outputAttachments, Material materialPropertySource, out Material outputMaterial, out Texture2D outputTexture, int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps, string newAssetName = "Repacked Attachments", bool clearCache = false, bool useOriginalNonrenderables = true) {
  192. if (sourceAttachments == null) throw new System.ArgumentNullException("sourceAttachments");
  193. if (outputAttachments == null) throw new System.ArgumentNullException("outputAttachments");
  194. // Use these to detect and use shared regions.
  195. var existingRegions = new Dictionary<AtlasRegion, int>();
  196. var regionIndexes = new List<int>();
  197. var texturesToPack = new List<Texture2D>();
  198. var originalRegions = new List<AtlasRegion>();
  199. outputAttachments.Clear();
  200. outputAttachments.AddRange(sourceAttachments);
  201. int newRegionIndex = 0;
  202. for (int i = 0, n = sourceAttachments.Count; i < n; i++) {
  203. var originalAttachment = sourceAttachments[i];
  204. if (IsRenderable(originalAttachment)) {
  205. var newAttachment = originalAttachment.GetCopy(true);
  206. var region = newAttachment.GetRegion();
  207. int existingIndex;
  208. if (existingRegions.TryGetValue(region, out existingIndex)) {
  209. regionIndexes.Add(existingIndex); // Store the region index for the eventual new attachment.
  210. } else {
  211. originalRegions.Add(region);
  212. texturesToPack.Add(region.ToTexture(textureFormat, mipmaps)); // Add the texture to the PackTextures argument
  213. existingRegions.Add(region, newRegionIndex); // Add the region to the dictionary of known regions
  214. regionIndexes.Add(newRegionIndex); // Store the region index for the eventual new attachment.
  215. newRegionIndex++;
  216. }
  217. outputAttachments[i] = newAttachment;
  218. } else {
  219. outputAttachments[i] = useOriginalNonrenderables ? originalAttachment : originalAttachment.GetCopy(true);
  220. regionIndexes.Add(NonrenderingRegion); // Output attachments pairs with regionIndexes list 1:1. Pad with a sentinel if the attachment doesn't have a region.
  221. }
  222. }
  223. // Fill a new texture with the collected attachment textures.
  224. var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps);
  225. newTexture.mipMapBias = AtlasUtilities.DefaultMipmapBias;
  226. newTexture.name = newAssetName;
  227. // Copy settings
  228. if (texturesToPack.Count > 0) {
  229. var sourceTexture = texturesToPack[0];
  230. newTexture.CopyTextureAttributesFrom(sourceTexture);
  231. }
  232. var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize);
  233. // Rehydrate the repacked textures as a Material, Spine atlas and Spine.AtlasAttachments
  234. Shader shader = materialPropertySource == null ? Shader.Find("Spine/Skeleton") : materialPropertySource.shader;
  235. var newMaterial = new Material(shader);
  236. if (materialPropertySource != null) {
  237. newMaterial.CopyPropertiesFromMaterial(materialPropertySource);
  238. newMaterial.shaderKeywords = materialPropertySource.shaderKeywords;
  239. }
  240. newMaterial.name = newAssetName;
  241. newMaterial.mainTexture = newTexture;
  242. var page = newMaterial.ToSpineAtlasPage();
  243. page.name = newAssetName;
  244. var repackedRegions = new List<AtlasRegion>();
  245. for (int i = 0, n = originalRegions.Count; i < n; i++) {
  246. var oldRegion = originalRegions[i];
  247. var newRegion = UVRectToAtlasRegion(rects[i], oldRegion, page);
  248. repackedRegions.Add(newRegion);
  249. }
  250. // Map the cloned attachments to the repacked atlas.
  251. for (int i = 0, n = outputAttachments.Count; i < n; i++) {
  252. var a = outputAttachments[i];
  253. if (IsRenderable(a))
  254. a.SetRegion(repackedRegions[regionIndexes[i]]);
  255. }
  256. // Clean up.
  257. if (clearCache)
  258. AtlasUtilities.ClearCache();
  259. outputTexture = newTexture;
  260. outputMaterial = newMaterial;
  261. }
  262. /// <summary>
  263. /// Creates and populates a duplicate skin with cloned attachments that are backed by a new packed texture atlas
  264. /// comprised of all the regions from the original skin.</summary>
  265. /// <remarks>GetRepackedSkin is an expensive operation, preferably call it at level load time.
  266. /// No Spine.Atlas object is created so there is no way to find AtlasRegions except through the Attachments using them.
  267. /// Returned <c>Material</c> and <c>Texture</c> behave like <c>new Texture2D()</c>, thus you need to call <c>Destroy()</c>
  268. /// to free resources.</remarks>
  269. /// <param name="additionalTexturePropertyIDsToCopy">Optional additional textures (such as normal maps) to copy while repacking.
  270. /// To copy e.g. the main texture and normal maps, pass 'new int[] { Shader.PropertyToID("_BumpMap") }' at this parameter.</param>
  271. /// <param name="additionalOutputTextures">When <c>additionalTexturePropertyIDsToCopy</c> is non-null,
  272. /// this array will be filled with the resulting repacked texture for every property,
  273. /// just as the main repacked texture is assigned to <c>outputTexture</c>.</param>
  274. /// <param name="additionalTextureFormats">When <c>additionalTexturePropertyIDsToCopy</c> is non-null,
  275. /// this array will be used as <c>TextureFormat</c> at the Texture at the respective property.
  276. /// When <c>additionalTextureFormats</c> is <c>null</c> or when its array size is smaller,
  277. /// <c>textureFormat</c> is used where there exists no corresponding array item.</param>
  278. /// <param name="additionalTextureIsLinear">When <c>additionalTexturePropertyIDsToCopy</c> is non-null,
  279. /// this array will be used to determine whether <c>linear</c> or <c>sRGB</c> color space is used at the
  280. /// Texture at the respective property. When <c>additionalTextureIsLinear</c> is <c>null</c>, <c>linear</c> color space
  281. /// is assumed at every additional Texture element.
  282. /// When e.g. packing the main texture and normal maps, pass 'new bool[] { true }' at this parameter, because normal maps use
  283. /// linear color space.</param>
  284. public static Skin GetRepackedSkin (this Skin o, string newName, Material materialPropertySource, out Material outputMaterial, out Texture2D outputTexture,
  285. int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps,
  286. bool useOriginalNonrenderables = true, bool clearCache = false,
  287. int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null,
  288. TextureFormat[] additionalTextureFormats = null, bool[] additionalTextureIsLinear = null) {
  289. return GetRepackedSkin(o, newName, materialPropertySource.shader, out outputMaterial, out outputTexture,
  290. maxAtlasSize, padding, textureFormat, mipmaps, materialPropertySource,
  291. clearCache, useOriginalNonrenderables, additionalTexturePropertyIDsToCopy, additionalOutputTextures,
  292. additionalTextureFormats, additionalTextureIsLinear);
  293. }
  294. /// <summary>
  295. /// Creates and populates a duplicate skin with cloned attachments that are backed by a new packed texture atlas
  296. /// comprised of all the regions from the original skin.</summary>
  297. /// <remarks>GetRepackedSkin is an expensive operation, preferably call it at level load time.
  298. /// No Spine.Atlas object is created so there is no way to find AtlasRegions except through the Attachments using them.
  299. /// Returned <c>Material</c> and <c>Texture</c> behave like <c>new Texture2D()</c>, thus you need to call <c>Destroy()</c>
  300. /// to free resources.</remarks>
  301. public static Skin GetRepackedSkin (this Skin o, string newName, Shader shader, out Material outputMaterial, out Texture2D outputTexture,
  302. int maxAtlasSize = 1024, int padding = 2, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps,
  303. Material materialPropertySource = null, bool clearCache = false, bool useOriginalNonrenderables = true,
  304. int[] additionalTexturePropertyIDsToCopy = null, Texture2D[] additionalOutputTextures = null,
  305. TextureFormat[] additionalTextureFormats = null, bool[] additionalTextureIsLinear = null) {
  306. outputTexture = null;
  307. if (additionalTexturePropertyIDsToCopy != null && additionalTextureIsLinear == null) {
  308. additionalTextureIsLinear = new bool[additionalTexturePropertyIDsToCopy.Length];
  309. for (int i = 0; i < additionalTextureIsLinear.Length; ++i) {
  310. additionalTextureIsLinear[i] = true;
  311. }
  312. }
  313. if (o == null) throw new System.NullReferenceException("Skin was null");
  314. var skinAttachments = o.Attachments;
  315. var newSkin = new Skin(newName);
  316. newSkin.bones.AddRange(o.bones);
  317. newSkin.constraints.AddRange(o.constraints);
  318. // Use these to detect and use shared regions.
  319. var existingRegions = new Dictionary<AtlasRegion, int>();
  320. var regionIndexes = new List<int>();
  321. // Collect all textures from the attachments of the original skin.
  322. var repackedAttachments = new List<Attachment>();
  323. int numTextureParamsToRepack = 1 + (additionalTexturePropertyIDsToCopy == null ? 0 : additionalTexturePropertyIDsToCopy.Length);
  324. additionalOutputTextures = (additionalTexturePropertyIDsToCopy == null ? null : new Texture2D[additionalTexturePropertyIDsToCopy.Length]);
  325. List<Texture2D>[] texturesToPackAtParam = new List<Texture2D>[numTextureParamsToRepack];
  326. for (int i = 0; i < numTextureParamsToRepack; ++i) {
  327. texturesToPackAtParam[i] = new List<Texture2D>();
  328. }
  329. var originalRegions = new List<AtlasRegion>();
  330. int newRegionIndex = 0;
  331. foreach (var skinEntry in skinAttachments) {
  332. var originalKey = skinEntry.Key;
  333. var originalAttachment = skinEntry.Value;
  334. Attachment newAttachment;
  335. if (IsRenderable(originalAttachment)) {
  336. newAttachment = originalAttachment.GetCopy(true);
  337. var region = newAttachment.GetRegion();
  338. int existingIndex;
  339. if (existingRegions.TryGetValue(region, out existingIndex)) {
  340. regionIndexes.Add(existingIndex); // Store the region index for the eventual new attachment.
  341. } else {
  342. originalRegions.Add(region);
  343. for (int i = 0; i < numTextureParamsToRepack; ++i) {
  344. Texture2D regionTexture = (i == 0 ?
  345. region.ToTexture(textureFormat, mipmaps) :
  346. region.ToTexture((additionalTextureFormats != null && i - 1 < additionalTextureFormats.Length) ?
  347. additionalTextureFormats[i - 1] : textureFormat,
  348. mipmaps, additionalTexturePropertyIDsToCopy[i - 1], additionalTextureIsLinear[i - 1]));
  349. texturesToPackAtParam[i].Add(regionTexture); // Add the texture to the PackTextures argument
  350. }
  351. existingRegions.Add(region, newRegionIndex); // Add the region to the dictionary of known regions
  352. regionIndexes.Add(newRegionIndex); // Store the region index for the eventual new attachment.
  353. newRegionIndex++;
  354. }
  355. repackedAttachments.Add(newAttachment);
  356. newSkin.SetAttachment(originalKey.SlotIndex, originalKey.Name, newAttachment);
  357. } else {
  358. newSkin.SetAttachment(originalKey.SlotIndex, originalKey.Name, useOriginalNonrenderables ? originalAttachment : originalAttachment.GetCopy(true));
  359. }
  360. }
  361. // Rehydrate the repacked textures as a Material, Spine atlas and Spine.AtlasAttachments
  362. var newMaterial = new Material(shader);
  363. if (materialPropertySource != null) {
  364. newMaterial.CopyPropertiesFromMaterial(materialPropertySource);
  365. newMaterial.shaderKeywords = materialPropertySource.shaderKeywords;
  366. }
  367. newMaterial.name = newName;
  368. Rect[] rects = null;
  369. for (int i = 0; i < numTextureParamsToRepack; ++i) {
  370. // Fill a new texture with the collected attachment textures.
  371. var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize,
  372. (i > 0 && additionalTextureFormats != null && i - 1 < additionalTextureFormats.Length) ?
  373. additionalTextureFormats[i - 1] : textureFormat,
  374. mipmaps,
  375. (i > 0) ? additionalTextureIsLinear[i - 1] : false);
  376. newTexture.mipMapBias = AtlasUtilities.DefaultMipmapBias;
  377. var texturesToPack = texturesToPackAtParam[i];
  378. if (texturesToPack.Count > 0) {
  379. var sourceTexture = texturesToPack[0];
  380. newTexture.CopyTextureAttributesFrom(sourceTexture);
  381. }
  382. newTexture.name = newName;
  383. var rectsForTexParam = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize);
  384. if (i == 0) {
  385. rects = rectsForTexParam;
  386. newMaterial.mainTexture = newTexture;
  387. outputTexture = newTexture;
  388. }
  389. else {
  390. newMaterial.SetTexture(additionalTexturePropertyIDsToCopy[i - 1], newTexture);
  391. additionalOutputTextures[i - 1] = newTexture;
  392. }
  393. }
  394. var page = newMaterial.ToSpineAtlasPage();
  395. page.name = newName;
  396. var repackedRegions = new List<AtlasRegion>();
  397. for (int i = 0, n = originalRegions.Count; i < n; i++) {
  398. var oldRegion = originalRegions[i];
  399. var newRegion = UVRectToAtlasRegion(rects[i], oldRegion, page);
  400. repackedRegions.Add(newRegion);
  401. }
  402. // Map the cloned attachments to the repacked atlas.
  403. for (int i = 0, n = repackedAttachments.Count; i < n; i++) {
  404. var a = repackedAttachments[i];
  405. if (IsRenderable(a))
  406. a.SetRegion(repackedRegions[regionIndexes[i]]);
  407. }
  408. // Clean up.
  409. if (clearCache)
  410. AtlasUtilities.ClearCache();
  411. outputMaterial = newMaterial;
  412. return newSkin;
  413. }
  414. public static Sprite ToSprite (this AtlasRegion ar, float pixelsPerUnit = 100) {
  415. return Sprite.Create(ar.GetMainTexture(), ar.GetUnityRect(), new Vector2(0.5f, 0.5f), pixelsPerUnit);
  416. }
  417. struct IntAndAtlasRegionKey {
  418. int i;
  419. AtlasRegion region;
  420. public IntAndAtlasRegionKey(int i, AtlasRegion region) {
  421. this.i = i;
  422. this.region = region;
  423. }
  424. public override int GetHashCode () {
  425. return i.GetHashCode() * 23 ^ region.GetHashCode();
  426. }
  427. }
  428. static Dictionary<IntAndAtlasRegionKey, Texture2D> CachedRegionTextures = new Dictionary<IntAndAtlasRegionKey, Texture2D>();
  429. static List<Texture2D> CachedRegionTexturesList = new List<Texture2D>();
  430. public static void ClearCache () {
  431. foreach (var t in CachedRegionTexturesList) {
  432. UnityEngine.Object.Destroy(t);
  433. }
  434. CachedRegionTextures.Clear();
  435. CachedRegionTexturesList.Clear();
  436. }
  437. /// <summary>Creates a new Texture2D object based on an AtlasRegion.
  438. /// If applyImmediately is true, Texture2D.Apply is called immediately after the Texture2D is filled with data.</summary>
  439. public static Texture2D ToTexture (this AtlasRegion ar, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps,
  440. int texturePropertyId = 0, bool linear = false, bool applyPMA = false) {
  441. Texture2D output;
  442. IntAndAtlasRegionKey cacheKey = new IntAndAtlasRegionKey(texturePropertyId, ar);
  443. CachedRegionTextures.TryGetValue(cacheKey, out output);
  444. if (output == null) {
  445. Texture2D sourceTexture = texturePropertyId == 0 ? ar.GetMainTexture() : ar.GetTexture(texturePropertyId);
  446. Rect r = ar.GetUnityRect();
  447. int width = (int)r.width;
  448. int height = (int)r.height;
  449. output = new Texture2D(width, height, textureFormat, mipmaps, linear) { name = ar.name };
  450. output.CopyTextureAttributesFrom(sourceTexture);
  451. if (applyPMA)
  452. AtlasUtilities.CopyTextureApplyPMA(sourceTexture, r, output);
  453. else
  454. AtlasUtilities.CopyTexture(sourceTexture, r, output);
  455. CachedRegionTextures.Add(cacheKey, output);
  456. CachedRegionTexturesList.Add(output);
  457. }
  458. return output;
  459. }
  460. static Texture2D ToTexture (this Sprite s, TextureFormat textureFormat = SpineTextureFormat,
  461. bool mipmaps = UseMipMaps, bool linear = false, bool applyPMA = false) {
  462. var spriteTexture = s.texture;
  463. Rect r;
  464. if (!s.packed || s.packingMode == SpritePackingMode.Rectangle) {
  465. r = s.textureRect;
  466. }
  467. else {
  468. r = new Rect();
  469. r.xMin = Math.Min(s.uv[0].x, s.uv[1].x) * spriteTexture.width;
  470. r.xMax = Math.Max(s.uv[0].x, s.uv[1].x) * spriteTexture.width;
  471. r.yMin = Math.Min(s.uv[0].y, s.uv[2].y) * spriteTexture.height;
  472. r.yMax = Math.Max(s.uv[0].y, s.uv[2].y) * spriteTexture.height;
  473. #if UNITY_EDITOR
  474. if (s.uv.Length > 4) {
  475. Debug.LogError("When using a tightly packed SpriteAtlas with Spine, you may only access Sprites that are packed as 'FullRect' from it! " +
  476. "You can either disable 'Tight Packing' at the whole SpriteAtlas, or change the single Sprite's TextureImporter Setting 'MeshType' to 'Full Rect'." +
  477. "Sprite Asset: " + s.name, s);
  478. }
  479. #endif
  480. }
  481. var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps, linear);
  482. newTexture.CopyTextureAttributesFrom(spriteTexture);
  483. if (applyPMA)
  484. AtlasUtilities.CopyTextureApplyPMA(spriteTexture, r, newTexture);
  485. else
  486. AtlasUtilities.CopyTexture(spriteTexture, r, newTexture);
  487. return newTexture;
  488. }
  489. static Texture2D GetClone (this Texture2D t, TextureFormat textureFormat = SpineTextureFormat,
  490. bool mipmaps = UseMipMaps, bool linear = false, bool applyPMA = false) {
  491. var newTexture = new Texture2D((int)t.width, (int)t.height, textureFormat, mipmaps, linear);
  492. newTexture.CopyTextureAttributesFrom(t);
  493. if (applyPMA)
  494. AtlasUtilities.CopyTextureApplyPMA(t, new Rect(0, 0, t.width, t.height), newTexture);
  495. else
  496. AtlasUtilities.CopyTexture(t, new Rect(0, 0, t.width, t.height), newTexture);
  497. return newTexture;
  498. }
  499. static void CopyTexture (Texture2D source, Rect sourceRect, Texture2D destination) {
  500. if (SystemInfo.copyTextureSupport == UnityEngine.Rendering.CopyTextureSupport.None) {
  501. // GetPixels fallback for old devices.
  502. Color[] pixelBuffer = source.GetPixels((int)sourceRect.x, (int)sourceRect.y, (int)sourceRect.width, (int)sourceRect.height);
  503. destination.SetPixels(pixelBuffer);
  504. destination.Apply();
  505. } else {
  506. Graphics.CopyTexture(source, 0, 0, (int)sourceRect.x, (int)sourceRect.y, (int)sourceRect.width, (int)sourceRect.height, destination, 0, 0, 0, 0);
  507. }
  508. }
  509. static void CopyTextureApplyPMA (Texture2D source, Rect sourceRect, Texture2D destination) {
  510. Color[] pixelBuffer = source.GetPixels((int)sourceRect.x, (int)sourceRect.y, (int)sourceRect.width, (int)sourceRect.height);
  511. for (int i = 0, n = pixelBuffer.Length; i < n; i++) {
  512. Color p = pixelBuffer[i];
  513. float a = p.a;
  514. p.r = p.r * a;
  515. p.g = p.g * a;
  516. p.b = p.b * a;
  517. pixelBuffer[i] = p;
  518. }
  519. destination.SetPixels(pixelBuffer);
  520. destination.Apply();
  521. }
  522. static bool IsRenderable (Attachment a) {
  523. return a is IHasRendererObject;
  524. }
  525. /// <summary>
  526. /// Get a rect with flipped Y so that a Spine atlas rect gets converted to a Unity Sprite rect and vice versa.</summary>
  527. static Rect SpineUnityFlipRect (this Rect rect, int textureHeight) {
  528. rect.y = textureHeight - rect.y - rect.height;
  529. return rect;
  530. }
  531. /// <summary>
  532. /// Gets the Rect of an AtlasRegion according to Unity texture coordinates (x-right, y-up).
  533. /// This overload relies on region.page.height being correctly set.</summary>
  534. static Rect GetUnityRect (this AtlasRegion region) {
  535. return region.GetSpineAtlasRect().SpineUnityFlipRect(region.page.height);
  536. }
  537. /// <summary>
  538. /// Gets the Rect of an AtlasRegion according to Unity texture coordinates (x-right, y-up).</summary>
  539. static Rect GetUnityRect (this AtlasRegion region, int textureHeight) {
  540. return region.GetSpineAtlasRect().SpineUnityFlipRect(textureHeight);
  541. }
  542. /// <summary>
  543. /// Returns a Rect of the AtlasRegion according to Spine texture coordinates. (x-right, y-down)</summary>
  544. static Rect GetSpineAtlasRect (this AtlasRegion region, bool includeRotate = true) {
  545. if (includeRotate && region.rotate)
  546. return new Rect(region.x, region.y, region.height, region.width);
  547. else
  548. return new Rect(region.x, region.y, region.width, region.height);
  549. }
  550. /// <summary>
  551. /// Denormalize a uvRect into a texture-space Rect.</summary>
  552. static Rect UVRectToTextureRect (Rect uvRect, int texWidth, int texHeight) {
  553. uvRect.x *= texWidth;
  554. uvRect.width *= texWidth;
  555. uvRect.y *= texHeight;
  556. uvRect.height *= texHeight;
  557. return uvRect;
  558. }
  559. /// <summary>
  560. /// Normalize a texture Rect into UV coordinates.</summary>
  561. static Rect TextureRectToUVRect (Rect textureRect, int texWidth, int texHeight) {
  562. textureRect.x = Mathf.InverseLerp(0, texWidth, textureRect.x);
  563. textureRect.y = Mathf.InverseLerp(0, texHeight, textureRect.y);
  564. textureRect.width = Mathf.InverseLerp(0, texWidth, textureRect.width);
  565. textureRect.height = Mathf.InverseLerp(0, texHeight, textureRect.height);
  566. return textureRect;
  567. }
  568. /// <summary>
  569. /// Creates a new Spine AtlasRegion according to a Unity UV Rect (x-right, y-up, uv-normalized).</summary>
  570. static AtlasRegion UVRectToAtlasRegion (Rect uvRect, AtlasRegion referenceRegion, AtlasPage page) {
  571. var tr = UVRectToTextureRect(uvRect, page.width, page.height);
  572. var rr = tr.SpineUnityFlipRect(page.height);
  573. int x = (int)rr.x, y = (int)rr.y;
  574. int w, h;
  575. if (referenceRegion.rotate) {
  576. w = (int)rr.height;
  577. h = (int)rr.width;
  578. } else {
  579. w = (int)rr.width;
  580. h = (int)rr.height;
  581. }
  582. int originalW = Mathf.RoundToInt((float)w * ((float)referenceRegion.originalWidth / (float)referenceRegion.width));
  583. int originalH = Mathf.RoundToInt((float)h * ((float)referenceRegion.originalHeight / (float)referenceRegion.height));
  584. int offsetX = Mathf.RoundToInt((float)referenceRegion.offsetX * ((float)w / (float)referenceRegion.width));
  585. int offsetY = Mathf.RoundToInt((float)referenceRegion.offsetY * ((float)h / (float)referenceRegion.height));
  586. return new AtlasRegion {
  587. page = page,
  588. name = referenceRegion.name,
  589. u = uvRect.xMin,
  590. u2 = uvRect.xMax,
  591. v = uvRect.yMax,
  592. v2 = uvRect.yMin,
  593. index = -1,
  594. width = w,
  595. originalWidth = originalW,
  596. height = h,
  597. originalHeight = originalH,
  598. offsetX = offsetX,
  599. offsetY = offsetY,
  600. x = x,
  601. y = y,
  602. rotate = referenceRegion.rotate
  603. };
  604. }
  605. /// <summary>
  606. /// Convenience method for getting the main texture of the material of the page of the region.</summary>
  607. static Texture2D GetMainTexture (this AtlasRegion region) {
  608. var material = (region.page.rendererObject as Material);
  609. return material.mainTexture as Texture2D;
  610. }
  611. /// <summary>
  612. /// Convenience method for getting any texture of the material of the page of the region by texture property name.</summary>
  613. static Texture2D GetTexture (this AtlasRegion region, string texturePropertyName) {
  614. var material = (region.page.rendererObject as Material);
  615. return material.GetTexture(texturePropertyName) as Texture2D;
  616. }
  617. /// <summary>
  618. /// Convenience method for getting any texture of the material of the page of the region by texture property id.</summary>
  619. static Texture2D GetTexture (this AtlasRegion region, int texturePropertyId) {
  620. var material = (region.page.rendererObject as Material);
  621. return material.GetTexture(texturePropertyId) as Texture2D;
  622. }
  623. static void CopyTextureAttributesFrom(this Texture2D destination, Texture2D source) {
  624. destination.filterMode = source.filterMode;
  625. destination.anisoLevel = source.anisoLevel;
  626. #if UNITY_EDITOR
  627. destination.alphaIsTransparency = source.alphaIsTransparency;
  628. #endif
  629. destination.wrapModeU = source.wrapModeU;
  630. destination.wrapModeV = source.wrapModeV;
  631. destination.wrapModeW = source.wrapModeW;
  632. }
  633. #endregion
  634. static float InverseLerp (float a, float b, float value) {
  635. return (value - a) / (b - a);
  636. }
  637. }
  638. }