From 298e392ccb1967036b60abc151ec3093304ab00e Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Thu, 30 Jul 2026 10:51:51 -0400 Subject: Improve LUT feature, add HDR params Signed-off-by: Andrew Opalach --- LUT.cs | 243 +++++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 132 insertions(+), 111 deletions(-) (limited to 'LUT.cs') diff --git a/LUT.cs b/LUT.cs index 1181216..f38010c 100644 --- a/LUT.cs +++ b/LUT.cs @@ -2,57 +2,23 @@ using System.Text; using System.Numerics; -using SharpPluginLoader.Core; +using System.Runtime.InteropServices; + +using ImGuiNET; using SharpPluginLoader.Core.Memory; -using SharpPluginLoader.Core.Rendering; +using SharpPluginLoader.Core.Resources; +using SharpPluginLoader.Core.IO; namespace WorldTuningTool { - public class WorldLUT + public class WorldLUTs { - private const string lutsPath = "nativePC/plugins/CSharp/LUT"; - - public WorldLUT() { } - - private string[] lutList = []; - - public string[] GetLUTs() - { - return lutList; - } + private const string addPath = "nativePC/plugins/CSharp/LUT"; - public void ReloadLUTs() - { - if (Directory.Exists(lutsPath)) - { - lutList = Directory.GetFiles(lutsPath); - } - } - - public static nint ProbeD3DResource(nint lightingObject) - { - nint lutTexture = MemoryUtil.Read(lightingObject + 0x1C0); - nint d3dResource = 0x0; - if (lutTexture != 0x0) - { - if (Renderer.IsDirectX12) - { - d3dResource = MemoryUtil.Read(lutTexture + 0x10); - } - else - { - d3dResource = MemoryUtil.Read(lutTexture + 0x60); - if (d3dResource != 0x0) - { - d3dResource = MemoryUtil.Read(d3dResource + 0x18); - } - } - } - return d3dResource; - } + public WorldLUTs() { } // https://github.com/AsteriskAmpersand/CrappyLUTStudio--CLUTS-/blob/5af10daaa5f295b106cc5db09530a6fe5a4320d4/LUT.py#L35 - public byte[]? parseWorldLUT(BinaryReader reader) + private byte[]? parseWorldLUT(BinaryReader reader) { byte[] signature = reader.ReadBytes(4); if (!(signature[0] == 0x54 && signature[1] == 0x45 && @@ -87,12 +53,11 @@ namespace WorldTuningTool // https://drive.google.com/file/d/143Eh08ZYncCAMwJ1q4gWxVOqR_OSWYvs/view // When does DOMAIN_MIN/MAX apply? - public byte[]? parseCubeLUT(StreamReader reader) + private byte[]? parseCubeLUT(StreamReader reader) { string? line; - Vector3[,,]? parsed = null; - int dim = 0, last = 0; - int x = 0, y = 0, z = 0; + Vector3[]? parsed = null; + int dim = 0, idx = 0; while ((line = reader.ReadLine()) != null) { if (line.StartsWith("LUT_3D_SIZE")) @@ -113,8 +78,7 @@ namespace WorldTuningTool { return null; } - parsed = new Vector3[dim,dim,dim]; - last = dim - 1; + parsed = new Vector3[dim * dim * dim]; while ((line = reader.ReadLine()) != null) { if (line == "") continue; @@ -132,7 +96,7 @@ namespace WorldTuningTool { try { - parsed[x,y,z] = Plugin.StringToVector3(line, ' '); + parsed[idx] = Plugin.StringToVector3(line, ' '); } catch (FormatException) { @@ -142,23 +106,7 @@ namespace WorldTuningTool { return null; } - if (x == last) - { - if (y == last) - { - z++; - y = 0; - } - else - { - y++; - } - x = 0; - } - else - { - x++; - } + idx++; } } if (parsed == null) @@ -166,71 +114,144 @@ namespace WorldTuningTool return null; } const int lutDataSize = 32 * 32 * 32 * 8; - byte[] data = new byte[lutDataSize], bytes; + byte[] data = new byte[lutDataSize]; float scale = MathF.Pow(2, 14) - 1; - int idx = 0; - for (z = 0; z < 32; z++) + for (idx = 0; idx < lutDataSize; idx+=8) { - for (y = 0; y < 32; y++) + Vector3 v = parsed[idx/8]; + ushort normX = (ushort)Math.Round(v.X * scale); + ushort normY = (ushort)Math.Round(v.Y * scale); + ushort normZ = (ushort)Math.Round(v.Z * scale); + byte[] bytes = BitConverter.GetBytes(normX); + data[idx] = bytes[0]; + data[idx + 1] = bytes[1]; + bytes = BitConverter.GetBytes(normY); + data[idx + 2] = bytes[0]; + data[idx + 3] = bytes[1]; + bytes = BitConverter.GetBytes(normZ); + data[idx + 4] = bytes[0]; + data[idx + 5] = bytes[1]; + bytes = BitConverter.GetBytes(0x2B88); // Alpha constant. + data[idx + 6] = bytes[0]; + data[idx + 7] = bytes[1]; + } + return data; + } + + private unsafe bool overwriteFromStream(nint texture, Stream stream, bool cube = false) + { + byte[]? data = null; + if (cube) + { + using (StreamReader reader = new StreamReader(stream)) { - for (x = 0; x < 32; x++) - { - Vector3 v = parsed[x,y,z]; - ushort normX = (ushort)Math.Round(v.X * scale); - ushort normY = (ushort)Math.Round(v.Y * scale); - ushort normZ = (ushort)Math.Round(v.Z * scale); - bytes = BitConverter.GetBytes(normX); - data[idx] = bytes[0]; - data[idx + 1] = bytes[1]; - bytes = BitConverter.GetBytes(normY); - data[idx + 2] = bytes[0]; - data[idx + 3] = bytes[1]; - bytes = BitConverter.GetBytes(normZ); - data[idx + 4] = bytes[0]; - data[idx + 5] = bytes[1]; - bytes = BitConverter.GetBytes(0x2B88); // Alpha constant. - data[idx + 6] = bytes[0]; - data[idx + 7] = bytes[1]; - idx += 8; - } + data = parseCubeLUT(reader); } } - return data; + else + { + using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, false)) + { + data = parseWorldLUT(reader); + } + } + if (data == null) + { + return false; + } +#if TEXTURE_FEATURE + fixed (byte *dataPointer = (byte[])data) + { + // DXGI_FORMAT_R16G16B16A16_FLOAT = 10 + return new Texture(texture).Replace(dataPointer, 10, 32, 32, 32); + } +#else + return true; +#endif } - public unsafe bool OverwriteFromFile(nint resource, string path) + private unsafe bool overwriteFromFile(nint texture, string path) { + string ext = path.Split('.').Last().ToLower(); using (FileStream stream = File.Open(path, FileMode.Open)) { - byte[]? data = null; - string ext = path.Split('.').Last().ToLower(); - if (ext == "tex") + return overwriteFromStream(texture, stream, ext == "cube"); + } + } + + private void reloadBlendLUT(nint lightingObject) + { + MemoryUtil.GetRef(lightingObject + 0x1D4) = MemoryUtil.GetRef(lightingObject + 0x1D0) + 1.0f; + } + + private string[] fileList = []; + private static string[] selectedFile = [ "", "" ]; + private static bool[] selectionStarted = [ false, false ]; + private static bool[] selectedInvalid = [ false, false ]; + + public void DrawLUT(nint texture, nint lightingObject, int i, float width) + { + ImGui.PushID(i); + string lutPath = Marshal.PtrToStringAnsi(texture + 0xC)!; + ImGui.Text($"LUT {i}: {lutPath}"); +#if TEXTURE_FEATURE + if (ImGui.Button("Reset")) + { + MtFileStream? file = MtFileStream.FromPath($"native:\\{lutPath}.tex", OpenMode.Read, false); + if (file != null) { - using (BinaryReader reader = new BinaryReader(stream, Encoding.ASCII, false)) + if (overwriteFromStream(texture, new MemoryStream(file.Read(file.Length)))) { - data = parseWorldLUT(reader); + reloadBlendLUT(lightingObject); } + file.Close(); } - else if (ext == "cube") + selectedFile[i] = ""; + selectedInvalid[i] = false; + } + ImGui.SameLine(); + ImGui.SetNextItemWidth(width * 0.6f); + bool prevWasInvalid = selectedInvalid[i]; + if (prevWasInvalid) + { + ImGui.PushStyleColor(ImGuiCol.Text, 0xFF0000FF); + } + if (ImGui.BeginCombo("##LUT Files", selectedFile[i], ImGuiComboFlags.HeightLarge)) + { + if (prevWasInvalid) { - using (StreamReader reader = new StreamReader(stream)) - { - data = parseCubeLUT(reader); - } + ImGui.PopStyleColor(); } - if (data == null) + if (!selectionStarted[i]) { - return false; + fileList = Directory.Exists(addPath) ? Directory.GetFiles(addPath) : []; + selectionStarted[i] = true; } - fixed (byte *dataPointer = (byte[])data) + foreach (string filePath in fileList) { -#if TEXTURE_FEATURE - // DXGI_FORMAT_R16G16B16A16_FLOAT = 10 - Renderer.ReplaceTexture(resource, dataPointer, 10, 32, 32, 32); -#endif + string fileName = Path.GetFileName(filePath); + bool isSelected = selectedFile[i] == fileName; + if (ImGui.Selectable(fileName, isSelected)) + { + selectedFile[i] = fileName; + if (!(selectedInvalid[i] = !overwriteFromFile(texture, filePath))) + { + reloadBlendLUT(lightingObject); + } + } } - return true; + ImGui.EndCombo(); } + else + { + if (prevWasInvalid) + { + ImGui.PopStyleColor(); + } + selectionStarted[i] = false; + } +#endif + ImGui.PopID(); } } } -- cgit v1.2.3-101-g0448