Skip to content

Commit

Permalink
Sprite.TrySaveToPNG
Browse files Browse the repository at this point in the history
  • Loading branch information
doombubbles committed Oct 24, 2024
1 parent 5797a36 commit 320588d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 deletions.
34 changes: 30 additions & 4 deletions BloonsTD6 Mod Helper/Api/Commands/ExportDisplayCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
using Il2CppAssets.Scripts.Models.GenericBehaviors;
using Il2CppAssets.Scripts.Unity;
using Il2CppAssets.Scripts.Unity.Display;
using Il2CppAssets.Scripts.Unity.Display.Animation;
using Il2CppAssets.Scripts.Unity.UI_New.InGame.TowerSelectionMenu;
using Il2CppNinjaKiwi.Common.ResourceUtils;
using UnityEngine;
namespace BTD_Mod_Helper.Api.Commands;

internal class ExportDisplayCommand : ModCommand<ExportCommand>
{
public override string Command => "display";
public override string Help => "Exports the textures and UnityDisplayNode information for the selected tower / other GUIDs";

public override string Help =>
"Exports the textures and UnityDisplayNode information for the selected tower / other GUIDs";

[Option('o', "open", Default = false, HelpText = "Also open the folder where pngs is exported")]
public bool Open { get; set; }
Expand Down Expand Up @@ -54,10 +58,32 @@ private void Export(UnityDisplayNode node)
{
if (renderer?.material?.mainTexture == null) continue;

var path = Path.Combine(FileIOHelper.sandboxRoot, renderer.name + ".png");
renderer.material.mainTexture.TrySaveToPNG(path);
if (renderer.gameObject.HasComponent(out CustomSpriteFrameAnimator customSpriteFrameAnimator))
{
var i = 0;
foreach (var frame in customSpriteFrameAnimator.frames)
{
var path = Path.Combine(FileIOHelper.sandboxRoot, renderer.name + ".png");
frame.TrySaveToPNG(path);
ModHelper.Msg($"Saved {path}");
i++;
}
}
else
{
var path = Path.Combine(FileIOHelper.sandboxRoot, renderer.name + ".png");
if (renderer.Is(out SpriteRenderer spriteRenderer))
{
spriteRenderer.sprite.texture?.TrySaveToPNG(path);
}
else
{
renderer.material.mainTexture.TrySaveToPNG(path);
}

ModHelper.Msg($"Saved {path}");
}

ModHelper.Msg($"Saved {path}");
}

node.PrintInfo();
Expand Down
31 changes: 27 additions & 4 deletions BloonsTD6 Mod Helper/Extensions/UnityExtensions/RendererExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,39 @@ public static void ApplyOutlineShader(this Renderer renderer)

/// <inheritdoc cref="Texture2DExt.ApplyCustomShader"/>
public static void ApplyCustomShader(this Renderer renderer, CustomShader customShader,
Action<Material> modifyMaterial = null) =>
Action<Material> modifyMaterial = null)
{
if (renderer.material?.mainTexture == null)
{
ModHelper.Warning("Can't ApplyCustomShader, renderer has no material texture");
return;
}
renderer.material.mainTexture = renderer.material.mainTexture.ApplyCustomShader(customShader, modifyMaterial);
}

/// <inheritdoc cref="Texture2DExt.ReplaceColor"/>
public static void ReplaceColor(this Renderer renderer, Color targetColor, Color replacementColor,
float threshold = 0.05f) =>
float threshold = 0.05f)
{
if (renderer.material?.mainTexture == null)
{
ModHelper.Warning("Can't ReplaceColor, renderer has no material texture");
return;
}
renderer.material.mainTexture = renderer.material.mainTexture.ReplaceColor(targetColor, replacementColor, threshold);
}

/// <inheritdoc cref="Texture2DExt.AdjustHSV"/>
public static void AdjustHSV(this Renderer renderer, float hueAdjust, float saturationAdjust, float valueAdjust,
Color? targetColor = null, float threshold = 0.05f) => renderer.material.mainTexture =
renderer.material.mainTexture.AdjustHSV(hueAdjust, saturationAdjust, valueAdjust, targetColor, threshold);
Color? targetColor = null, float threshold = 0.05f)
{
if (renderer.material?.mainTexture == null)
{
ModHelper.Warning("Can't AdjustHSV, renderer has no material texture");
return;
}
renderer.material.mainTexture =
renderer.material.mainTexture.AdjustHSV(hueAdjust, saturationAdjust, valueAdjust, targetColor, threshold);
}

}
36 changes: 33 additions & 3 deletions BloonsTD6 Mod Helper/Extensions/UnityExtensions/SpriteExt.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using UnityEngine;
using System;
using System.IO;
using UnityEngine;
namespace BTD_Mod_Helper.Extensions;

/// <summary>
Expand All @@ -13,7 +15,35 @@ public static class SpriteExt
/// <param name="newTexture"></param>
public static void SetTexture(this Sprite sprite, Texture2D newTexture)
{
var bytes = ImageConversion.EncodeToPNG(newTexture);
ImageConversion.LoadImage(sprite.texture, bytes);
sprite.texture.LoadImage(newTexture.EncodeToPNG());
}

/// <summary>
/// Attempts to save a Sprite to a PNG at the given filePath, even if it isn't marked as readable
/// </summary>
public static void TrySaveToPNG(this Sprite sprite, string filePath)
{
try
{
var texture = sprite.texture;
var spriteRect = sprite.textureRect;
var tmp = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(texture, tmp);
var previous = RenderTexture.active;
RenderTexture.active = tmp;
var myTexture2D = new Texture2D((int) spriteRect.width, (int) spriteRect.height);
myTexture2D.ReadPixels(new Rect(spriteRect.x, spriteRect.y, spriteRect.width, spriteRect.height), 0, 0);
myTexture2D.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(tmp);
var bytes = myTexture2D.EncodeToPNG();
File.WriteAllBytes(filePath, bytes);
}
catch (Exception e)
{
ModHelper.Warning(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public static void TrySaveToPNG(this Texture texture, string filePath)
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
ModHelper.Warning(e);
}
}

Expand Down Expand Up @@ -156,7 +155,7 @@ public static RenderTexture AdjustHSV(this Texture texture, float hueAdjust, flo
material.SetFloat(SaturationAdjust, saturationAdjust);
material.SetFloat(ValueAdjust, valueAdjust);
material.SetColor(TargetColor, targetColor ?? Color.white);
material.SetFloat(Threshold, threshold);
material.SetFloat(Threshold, targetColor == null ? 1 :threshold);
});

}
3 changes: 2 additions & 1 deletion BloonsTD6 Mod Helper/LATEST.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Added a ModBloonOverlay class for making custom Bloon Overlays
- NOTE: Due to a MelonLoader bug, these won't properly display unless you're on ML 0.6.6 or higher
- Accounted for a Unity bug that was affecting the internal durations of embedded AudioClips
- Accounted for a Unity bug that was affecting the internal durations of embedded AudioClips
- Added a Sprite.TrySaveToPNG() extension like the one for Textures except accounting for position and size within a larger texture atlas

0 comments on commit 320588d

Please sign in to comment.