mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Clean up generated textures
This commit is contained in:
parent
58f12976ef
commit
931403566b
@ -8,6 +8,44 @@ namespace NewHorizons.Utility
|
|||||||
{
|
{
|
||||||
static class ImageUtilities
|
static class ImageUtilities
|
||||||
{
|
{
|
||||||
|
private static Dictionary<string, Texture2D> _loadedTextures = new Dictionary<string, Texture2D>();
|
||||||
|
private static List<Texture2D> _generatedTextures = new List<Texture2D>();
|
||||||
|
|
||||||
|
public static Texture2D GetTexture(IModBehaviour mod, string filename)
|
||||||
|
{
|
||||||
|
// Copied from OWML but without the print statement lol
|
||||||
|
var path = mod.ModHelper.Manifest.ModFolderPath + filename;
|
||||||
|
if (_loadedTextures.ContainsKey(path))
|
||||||
|
{
|
||||||
|
Logger.Log($"Already loaded image at path: {path}");
|
||||||
|
return _loadedTextures[path];
|
||||||
|
}
|
||||||
|
Logger.Log($"Loading image at path: {path}");
|
||||||
|
var data = File.ReadAllBytes(path);
|
||||||
|
var texture = new Texture2D(2, 2);
|
||||||
|
texture.name = Path.GetFileNameWithoutExtension(path);
|
||||||
|
texture.LoadImage(data);
|
||||||
|
_loadedTextures.Add(path, texture);
|
||||||
|
return texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ClearCache()
|
||||||
|
{
|
||||||
|
foreach (var texture in _loadedTextures.Values)
|
||||||
|
{
|
||||||
|
if (texture == null) continue;
|
||||||
|
UnityEngine.Object.Destroy(texture);
|
||||||
|
}
|
||||||
|
_loadedTextures.Clear();
|
||||||
|
|
||||||
|
foreach(var texture in _generatedTextures)
|
||||||
|
{
|
||||||
|
if (texture == null) continue;
|
||||||
|
UnityEngine.Object.Destroy(texture);
|
||||||
|
}
|
||||||
|
_generatedTextures.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
public static Texture2D MakeOutline(Texture2D texture, Color color, int thickness)
|
public static Texture2D MakeOutline(Texture2D texture, Color color, int thickness)
|
||||||
{
|
{
|
||||||
var outline = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
|
var outline = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
|
||||||
@ -32,6 +70,8 @@ namespace NewHorizons.Utility
|
|||||||
outline.SetPixels(outlinePixels);
|
outline.SetPixels(outlinePixels);
|
||||||
outline.Apply();
|
outline.Apply();
|
||||||
|
|
||||||
|
_generatedTextures.Add(outline);
|
||||||
|
|
||||||
return outline;
|
return outline;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,6 +107,9 @@ namespace NewHorizons.Utility
|
|||||||
newImage.name = image.name + "Tinted";
|
newImage.name = image.name + "Tinted";
|
||||||
newImage.SetPixels(pixels);
|
newImage.SetPixels(pixels);
|
||||||
newImage.Apply();
|
newImage.Apply();
|
||||||
|
|
||||||
|
_generatedTextures.Add(newImage);
|
||||||
|
|
||||||
return newImage;
|
return newImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,15 +127,10 @@ namespace NewHorizons.Utility
|
|||||||
newImage.name = image.name + "LerpedGrayscale";
|
newImage.name = image.name + "LerpedGrayscale";
|
||||||
newImage.SetPixels(pixels);
|
newImage.SetPixels(pixels);
|
||||||
newImage.Apply();
|
newImage.Apply();
|
||||||
return newImage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Texture2D LoadImage(string filepath)
|
_generatedTextures.Add(newImage);
|
||||||
{
|
|
||||||
Texture2D tex = new Texture2D(2, 2);
|
return newImage;
|
||||||
tex.name = Path.GetFileNameWithoutExtension(filepath);
|
|
||||||
tex.LoadRawTextureData(File.ReadAllBytes(filepath));
|
|
||||||
return tex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Texture2D ClearTexture(int width, int height)
|
public static Texture2D ClearTexture(int width, int height)
|
||||||
@ -107,6 +145,9 @@ namespace NewHorizons.Utility
|
|||||||
}
|
}
|
||||||
tex.SetPixels(fillPixels);
|
tex.SetPixels(fillPixels);
|
||||||
tex.Apply();
|
tex.Apply();
|
||||||
|
|
||||||
|
_generatedTextures.Add(tex);
|
||||||
|
|
||||||
return tex;
|
return tex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,39 +165,12 @@ namespace NewHorizons.Utility
|
|||||||
}
|
}
|
||||||
tex.SetPixels(fillPixels);
|
tex.SetPixels(fillPixels);
|
||||||
tex.Apply();
|
tex.Apply();
|
||||||
|
|
||||||
|
_generatedTextures.Add(tex);
|
||||||
|
|
||||||
return tex;
|
return tex;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<string, Texture2D> _loadedTextures = new Dictionary<string, Texture2D>();
|
|
||||||
|
|
||||||
public static Texture2D GetTexture(IModBehaviour mod, string filename)
|
|
||||||
{
|
|
||||||
// Copied from OWML but without the print statement lol
|
|
||||||
var path = mod.ModHelper.Manifest.ModFolderPath + filename;
|
|
||||||
if (_loadedTextures.ContainsKey(path))
|
|
||||||
{
|
|
||||||
Logger.Log($"Already loaded image at path: {path}");
|
|
||||||
return _loadedTextures[path];
|
|
||||||
}
|
|
||||||
Logger.Log($"Loading image at path: {path}");
|
|
||||||
var data = File.ReadAllBytes(path);
|
|
||||||
var texture = new Texture2D(2, 2);
|
|
||||||
texture.name = Path.GetFileNameWithoutExtension(path);
|
|
||||||
texture.LoadImage(data);
|
|
||||||
_loadedTextures.Add(path, texture);
|
|
||||||
return texture;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void ClearCache()
|
|
||||||
{
|
|
||||||
foreach(var texture in _loadedTextures.Values)
|
|
||||||
{
|
|
||||||
if (texture == null) continue;
|
|
||||||
UnityEngine.Object.Destroy(texture);
|
|
||||||
}
|
|
||||||
_loadedTextures.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Color GetAverageColor(Texture2D src)
|
public static Color GetAverageColor(Texture2D src)
|
||||||
{
|
{
|
||||||
var pixels = src.GetPixels32();
|
var pixels = src.GetPixels32();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user