From d42a10568720dbf3bbb9582c3534b6aa4c65711f Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Sat, 17 Jun 2023 01:20:59 +0200 Subject: [PATCH] Add some quick convert buttons to texture editing. --- Penumbra/Import/Textures/CombinedTexture.cs | 4 ++- Penumbra/Import/Textures/Texture.cs | 31 +++++++++++++---- .../AdvancedWindow/ModEditWindow.Textures.cs | 34 +++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/Penumbra/Import/Textures/CombinedTexture.cs b/Penumbra/Import/Textures/CombinedTexture.cs index fcfbc3ee..bf017048 100644 --- a/Penumbra/Import/Textures/CombinedTexture.cs +++ b/Penumbra/Import/Textures/CombinedTexture.cs @@ -4,7 +4,6 @@ using System.Numerics; using Dalamud.Interface; using Lumina.Data.Files; using OtterTex; -using Penumbra.Services; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.PixelFormats; @@ -41,6 +40,9 @@ public partial class CombinedTexture : IDisposable public bool IsLoaded => _mode != Mode.Empty; + + public bool IsLeftCopy + => _mode == Mode.LeftCopy; public Exception? SaveException { get; private set; } = null; diff --git a/Penumbra/Import/Textures/Texture.cs b/Penumbra/Import/Textures/Texture.cs index 77412e92..ef8e16fc 100644 --- a/Penumbra/Import/Textures/Texture.cs +++ b/Penumbra/Import/Textures/Texture.cs @@ -54,6 +54,22 @@ public sealed class Texture : IDisposable public bool IsLoaded => TextureWrap != null; + public DXGIFormat Format + => BaseImage switch + { + ScratchImage s => s.Meta.Format, + TexFile t => t.Header.Format.ToDXGI(), + _ => DXGIFormat.Unknown, + }; + + public int MipMaps + => BaseImage switch + { + ScratchImage s => s.Meta.MipLevels, + TexFile t => t.Header.MipLevels, + _ => 1, + }; + public void Draw(Vector2 size) { if (TextureWrap != null) @@ -151,6 +167,13 @@ public sealed class Texture : IDisposable } } + public void Reload(DalamudServices dalamud) + { + var path = Path; + Path = string.Empty; + Load(dalamud, path); + } + private bool LoadDds(DalamudServices dalamud) { Type = FileType.Dds; @@ -180,7 +203,7 @@ public sealed class Texture : IDisposable using var stream = OpenTexStream(dalamud.GameData); var scratch = TexFileParser.Parse(stream); BaseImage = scratch; - var rgba = scratch.GetRGBA(out var f).ThrowIfError(f); + var rgba = scratch.GetRGBA(out var f).ThrowIfError(f); RGBAPixels = rgba.Pixels[..(f.Meta.Width * f.Meta.Height * (f.Meta.Format.BitsPerPixel() / 8))].ToArray(); CreateTextureWrap(dalamud.UiBuilder, scratch.Meta.Width, scratch.Meta.Height); return true; @@ -268,10 +291,6 @@ public sealed class Texture : IDisposable if (ImGuiUtil.DrawDisabledButton(FontAwesomeIcon.Recycle.ToIconString(), new Vector2(ImGui.GetFrameHeight()), "Reload the currently selected path.", false, true)) - { - var path = Path; - Path = string.Empty; - Load(dalamud, path); - } + Reload(dalamud); } } diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs index 24ab9b90..f96ab4da 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Textures.cs @@ -5,6 +5,7 @@ using System.Numerics; using ImGuiNET; using OtterGui; using OtterGui.Raii; +using OtterTex; using Penumbra.Import.Textures; namespace Penumbra.UI.AdvancedWindow; @@ -133,6 +134,39 @@ public partial class ModEditWindow _forceTextureStartPath = false; } + if (_left.Type is Texture.FileType.Tex && _center.IsLeftCopy) + { + var buttonSize = new Vector2((ImGui.GetContentRegionAvail().X - ImGui.GetStyle().ItemSpacing.X * 2) / 3, 0); + if (ImGuiUtil.DrawDisabledButton("Convert to BC7", buttonSize, + "This converts the texture to BC7 format in place. This is not revertible.", + _left.Format is DXGIFormat.BC7Typeless or DXGIFormat.BC7UNorm or DXGIFormat.BC7UNormSRGB)) + { + _center.SaveAsTex(_left.Path, CombinedTexture.TextureSaveType.BC7, _left.MipMaps > 1); + _left.Reload(_dalamud); + } + + ImGui.SameLine(); + if (ImGuiUtil.DrawDisabledButton("Convert to BC3", buttonSize, + "This converts the texture to BC3 format in place. This is not revertible.", + _left.Format is DXGIFormat.BC3Typeless or DXGIFormat.BC3UNorm or DXGIFormat.BC3UNormSRGB)) + { + _center.SaveAsTex(_left.Path, CombinedTexture.TextureSaveType.BC3, _left.MipMaps > 1); + _left.Reload(_dalamud); + } + + ImGui.SameLine(); + if (ImGuiUtil.DrawDisabledButton("Convert to RGBA", buttonSize, + "This converts the texture to RGBA format in place. This is not revertible.", + _left.Format is DXGIFormat.B8G8R8A8UNorm or DXGIFormat.B8G8R8A8Typeless or DXGIFormat.B8G8R8A8UNormSRGB)) + { + _center.SaveAsTex(_left.Path, CombinedTexture.TextureSaveType.Bitmap, _left.MipMaps > 1); + _left.Reload(_dalamud); + } + } + else + { + ImGui.NewLine(); + } ImGui.NewLine(); }