From ffb8f0e8d3530e24a4406ebb44e459bb07cfc56e Mon Sep 17 00:00:00 2001 From: Exter-N Date: Mon, 28 Aug 2023 03:06:18 +0200 Subject: [PATCH] =?UTF-8?q?Material=20editor:=20Allow=20negatives=20again?= =?UTF-8?q?=20with=20R=C2=B2G=C2=B2B=C2=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There seems to be people using it. --- .../ModEditWindow.Materials.ColorSet.cs | 26 ++++++++++++++++--- .../ModEditWindow.Materials.ConstantEditor.cs | 8 +++--- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorSet.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorSet.cs index 798939ca..daca1098 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorSet.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ColorSet.cs @@ -483,13 +483,13 @@ public partial class ModEditWindow private static bool ColorPicker( string label, string tooltip, Vector3 input, Action< Vector3 > setter, string letter = "" ) { var ret = false; - var inputSqrt = Vector3.SquareRoot( input ); + var inputSqrt = PseudoSqrtRgb( input ); var tmp = inputSqrt; if( ImGui.ColorEdit3( label, ref tmp, - ImGuiColorEditFlags.NoInputs | ImGuiColorEditFlags.DisplayRGB | ImGuiColorEditFlags.InputRGB | ImGuiColorEditFlags.NoTooltip ) + ImGuiColorEditFlags.NoInputs | ImGuiColorEditFlags.DisplayRGB | ImGuiColorEditFlags.InputRGB | ImGuiColorEditFlags.NoTooltip | ImGuiColorEditFlags.HDR ) && tmp != inputSqrt ) { - setter( tmp * tmp ); + setter( PseudoSquareRgb( tmp ) ); ret = true; } @@ -505,4 +505,24 @@ public partial class ModEditWindow return ret; } + + // Functions to deal with squared RGB values without making negatives useless. + + private static float PseudoSquareRgb(float x) + => x < 0.0f ? -(x * x) : (x * x); + + private static Vector3 PseudoSquareRgb(Vector3 vec) + => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z)); + + private static Vector4 PseudoSquareRgb(Vector4 vec) + => new(PseudoSquareRgb(vec.X), PseudoSquareRgb(vec.Y), PseudoSquareRgb(vec.Z), vec.W); + + private static float PseudoSqrtRgb(float x) + => x < 0.0f ? -MathF.Sqrt(-x) : MathF.Sqrt(x); + + private static Vector3 PseudoSqrtRgb(Vector3 vec) + => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z)); + + private static Vector4 PseudoSqrtRgb(Vector4 vec) + => new(PseudoSqrtRgb(vec.X), PseudoSqrtRgb(vec.Y), PseudoSqrtRgb(vec.Z), vec.W); } \ No newline at end of file diff --git a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs index f7ea317d..8a104145 100644 --- a/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs +++ b/Penumbra/UI/AdvancedWindow/ModEditWindow.Materials.ConstantEditor.cs @@ -147,11 +147,11 @@ public partial class ModEditWindow ImGui.SetNextItemWidth(editorWidth); var value = new Vector3(values); if (_squaredRgb) - value = Vector3.SquareRoot(value); + value = PseudoSqrtRgb(value); if (ImGui.ColorEdit3("##0", ref value, ImGuiColorEditFlags.Float | (_clamped ? 0 : ImGuiColorEditFlags.HDR)) && !disabled) { if (_squaredRgb) - value *= value; + value = PseudoSquareRgb(value); if (_clamped) value = Vector3.Clamp(value, Vector3.Zero, Vector3.One); value.CopyTo(values); @@ -165,11 +165,11 @@ public partial class ModEditWindow ImGui.SetNextItemWidth(editorWidth); var value = new Vector4(values); if (_squaredRgb) - value = new Vector4(MathF.Sqrt(value.X), MathF.Sqrt(value.Y), MathF.Sqrt(value.Z), value.W); + value = PseudoSqrtRgb(value); if (ImGui.ColorEdit4("##0", ref value, ImGuiColorEditFlags.Float | ImGuiColorEditFlags.AlphaPreviewHalf | (_clamped ? 0 : ImGuiColorEditFlags.HDR)) && !disabled) { if (_squaredRgb) - value *= new Vector4(value.X, value.Y, value.Z, 1.0f); + value = PseudoSquareRgb(value); if (_clamped) value = Vector4.Clamp(value, Vector4.Zero, Vector4.One); value.CopyTo(values);