Fix atlas breaking if it had a blank slide, cache solid colour texture

This commit is contained in:
Nick 2024-06-17 21:11:25 -04:00
parent 45c7971347
commit 96cebc3476
2 changed files with 13 additions and 1 deletions

View File

@ -511,6 +511,7 @@ namespace NewHorizons.Builder.Props
if (useAtlasCache && cacheExists)
{
NHLogger.LogVerbose($"The atlas cache for slide reel containing [{slides.FirstOrDefault(x => !string.IsNullOrEmpty(x.imagePath))?.imagePath}] is {atlasKey}");
// Load the atlas texture used to draw onto the physical slide reel object
atlasImageLoader.PathsToLoad.Add((0, Path.Combine(mod.ModHelper.Manifest.ModFolderPath, ATLAS_SLIDE_CACHE_FOLDER, $"{atlasKey}.png")));
}
@ -523,7 +524,12 @@ namespace NewHorizons.Builder.Props
if (string.IsNullOrEmpty(slideInfo.imagePath))
{
imageLoader.imageLoadedEvent?.Invoke(Texture2D.blackTexture, i, null);
// Delay at least one frame to let things subscribe to the event before it fires
int index = i;
Delay.FireOnNextUpdate(() =>
{
imageLoader.imageLoadedEvent?.Invoke(ImageUtilities.MakeSolidColorTexture(256, 256, Color.black), index, null);
});
}
else
{

View File

@ -425,6 +425,9 @@ namespace NewHorizons.Utility.Files
}
public static Texture2D MakeSolidColorTexture(int width, int height, Color color)
{
var key = $"{color} {width} {height}";
if (_textureCache.TryGetValue(key, out var existingTexture)) return (Texture2D)existingTexture;
var pixels = new Color[width * height];
for (int i = 0; i < pixels.Length; i++)
@ -435,6 +438,9 @@ namespace NewHorizons.Utility.Files
var newTexture = new Texture2D(width, height);
newTexture.SetPixels(pixels);
newTexture.Apply();
_textureCache.Add(key, newTexture);
return newTexture;
}