[unity] Optional repacking texture format params.

This commit is contained in:
John 2017-03-16 22:03:08 +08:00 committed by GitHub
parent 98ab69a61c
commit 3e4722cb56

View File

@ -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,7 +301,7 @@ 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);
@ -329,7 +332,8 @@ namespace Spine.Unity.Modules.AttachmentTools {
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);
@ -358,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);
@ -372,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)