|
@@ -1,4 +1,4 @@
|
|
|
-/******************************************************************************
|
|
|
+/******************************************************************************
|
|
|
* Spine Runtimes Software License v2.5
|
|
|
*
|
|
|
* Copyright (c) 2013-2016, Esoteric Software
|
|
@@ -129,10 +129,10 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
|
|
|
/// <summary>
|
|
|
/// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate texture of the Sprite's texture data. Returns a RegionAttachment that uses it. Use this if you plan to use a premultiply alpha shader such as "Spine/Skeleton"</summary>
|
|
|
- public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Shader shader) {
|
|
|
+ public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Shader shader, TextureFormat textureFormat = SpriteAtlasRegionExtensions.SpineTextureFormat, bool mipmaps = SpriteAtlasRegionExtensions.UseMipMaps) {
|
|
|
if (sprite == null) throw new System.ArgumentNullException("sprite");
|
|
|
if (shader == null) throw new System.ArgumentNullException("shader");
|
|
|
- var region = sprite.ToAtlasRegionPMAClone(shader);
|
|
|
+ var region = sprite.ToAtlasRegionPMAClone(shader, textureFormat, mipmaps);
|
|
|
var unitsPerPixel = 1f / sprite.pixelsPerUnit;
|
|
|
return region.ToRegionAttachment(sprite.name, unitsPerPixel);
|
|
|
}
|
|
@@ -201,6 +201,9 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
}
|
|
|
|
|
|
public static class SpriteAtlasRegionExtensions {
|
|
|
+ internal const TextureFormat SpineTextureFormat = TextureFormat.RGBA32;
|
|
|
+ internal const bool UseMipMaps = false;
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 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>
|
|
|
public static AtlasPage ToSpineAtlasPage (this Material m) {
|
|
@@ -237,9 +240,9 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
|
|
|
/// <summary>
|
|
|
/// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate of the Sprite's texture data.</summary>
|
|
|
- public static AtlasRegion ToAtlasRegionPMAClone (this Sprite s, Shader shader) {
|
|
|
+ public static AtlasRegion ToAtlasRegionPMAClone (this Sprite s, Shader shader, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) {
|
|
|
var material = new Material(shader);
|
|
|
- var tex = s.ToTexture(false);
|
|
|
+ var tex = s.ToTexture(false, textureFormat, mipmaps);
|
|
|
tex.ApplyPMA(true);
|
|
|
|
|
|
tex.name = s.name + "-pma-";
|
|
@@ -298,23 +301,39 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
/// <summary>
|
|
|
/// Creates and populates a duplicate skin with cloned attachments that are backed by a new packed texture atlas comprised of all the regions from the original skin.</summary>
|
|
|
/// <remarks>No Spine.Atlas object is created so there is no way to find AtlasRegions except through the Attachments using them.</remarks>
|
|
|
- public static Skin GetRepackedSkin (this Skin o, string newName, Shader shader, out Material m, out Texture2D t, int maxAtlasSize = 1024, int padding = 2) {
|
|
|
+ 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 = SpineTextureFormat, bool mipmaps = UseMipMaps) {
|
|
|
var skinAttachments = o.Attachments;
|
|
|
var newSkin = new Skin(newName);
|
|
|
|
|
|
+ var existingRegions = new Dictionary<AtlasRegion, int>();
|
|
|
+ var textureIndexes = new List<int>();
|
|
|
+
|
|
|
var repackedAttachments = new List<Attachment>();
|
|
|
var texturesToPack = new List<Texture2D>();
|
|
|
+ int newRegionIndex = 0;
|
|
|
foreach (var kvp in skinAttachments) {
|
|
|
var newAttachment = kvp.Value.GetClone(true);
|
|
|
if (IsRenderable(newAttachment)) {
|
|
|
- texturesToPack.Add(newAttachment.GetAtlasRegion().ToTexture());
|
|
|
+
|
|
|
+ var region = newAttachment.GetAtlasRegion();
|
|
|
+ int existingIndex;
|
|
|
+ if (existingRegions.TryGetValue(region, out existingIndex)) {
|
|
|
+ textureIndexes.Add(existingIndex); // Store the region index for the eventual new attachment.
|
|
|
+ } else {
|
|
|
+ texturesToPack.Add(region.ToTexture()); // Add the texture to the PackTextures argument
|
|
|
+ existingRegions.Add(region, newRegionIndex); // Add the region to the dictionary of known regions
|
|
|
+ textureIndexes.Add(newRegionIndex); // Store the region index for the eventual new attachment.
|
|
|
+ newRegionIndex++;
|
|
|
+ }
|
|
|
+
|
|
|
repackedAttachments.Add(newAttachment);
|
|
|
}
|
|
|
var key = kvp.Key;
|
|
|
newSkin.AddAttachment(key.slotIndex, key.name, newAttachment);
|
|
|
}
|
|
|
|
|
|
- var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize);
|
|
|
+ var newTexture = new Texture2D(maxAtlasSize, maxAtlasSize, textureFormat, mipmaps);
|
|
|
+ newTexture.anisoLevel = texturesToPack[0].anisoLevel;
|
|
|
newTexture.name = newName;
|
|
|
var rects = newTexture.PackTextures(texturesToPack.ToArray(), padding, maxAtlasSize);
|
|
|
|
|
@@ -326,7 +345,7 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
|
|
|
for (int i = 0, n = repackedAttachments.Count; i < n; i++) {
|
|
|
var a = repackedAttachments[i];
|
|
|
- var r = rects[i];
|
|
|
+ var r = rects[textureIndexes[i]];
|
|
|
var oldRegion = a.GetAtlasRegion();
|
|
|
var newRegion = UVRectToAtlasRegion(r, oldRegion.name, page, oldRegion.offsetX, oldRegion.offsetY, oldRegion.rotate);
|
|
|
a.SetRegion(newRegion);
|
|
@@ -343,10 +362,10 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
|
|
|
/// <summary>Creates a new Texture2D object based on an AtlasRegion.
|
|
|
/// If applyImmediately is true, Texture2D.Apply is called immediately after the Texture2D is filled with data.</summary>
|
|
|
- public static Texture2D ToTexture (this AtlasRegion ar, bool applyImmediately = true) {
|
|
|
+ public static Texture2D ToTexture (this AtlasRegion ar, bool applyImmediately = true, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) {
|
|
|
Texture2D sourceTexture = ar.GetMainTexture();
|
|
|
Rect r = ar.GetUnityRect(sourceTexture.height);
|
|
|
- Texture2D output = new Texture2D((int)r.width, (int)r.height);
|
|
|
+ Texture2D output = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps);
|
|
|
output.name = ar.name;
|
|
|
Color[] pixelBuffer = sourceTexture.GetPixels((int)r.x, (int)r.y, (int)r.width, (int)r.height);
|
|
|
output.SetPixels(pixelBuffer);
|
|
@@ -357,11 +376,11 @@ namespace Spine.Unity.Modules.AttachmentTools {
|
|
|
return output;
|
|
|
}
|
|
|
|
|
|
- static Texture2D ToTexture (this Sprite s, bool applyImmediately = true) {
|
|
|
+ static Texture2D ToTexture (this Sprite s, bool applyImmediately = true, TextureFormat textureFormat = SpineTextureFormat, bool mipmaps = UseMipMaps) {
|
|
|
var spriteTexture = s.texture;
|
|
|
var r = s.textureRect;
|
|
|
var spritePixels = spriteTexture.GetPixels((int)r.x, (int)r.y, (int)r.width, (int)r.height);
|
|
|
- var newTexture = new Texture2D((int)r.width, (int)r.height);
|
|
|
+ var newTexture = new Texture2D((int)r.width, (int)r.height, textureFormat, mipmaps);
|
|
|
newTexture.SetPixels(spritePixels);
|
|
|
|
|
|
if (applyImmediately)
|