Material editor: Allow negatives again with R²G²B²

There seems to be people using it.
This commit is contained in:
Exter-N 2023-08-28 03:06:18 +02:00
parent afd7aab37d
commit ffb8f0e8d3
2 changed files with 27 additions and 7 deletions

View file

@ -483,13 +483,13 @@ public partial class ModEditWindow
private static bool ColorPicker( string label, string tooltip, Vector3 input, Action< Vector3 > setter, string letter = "" ) private static bool ColorPicker( string label, string tooltip, Vector3 input, Action< Vector3 > setter, string letter = "" )
{ {
var ret = false; var ret = false;
var inputSqrt = Vector3.SquareRoot( input ); var inputSqrt = PseudoSqrtRgb( input );
var tmp = inputSqrt; var tmp = inputSqrt;
if( ImGui.ColorEdit3( label, ref tmp, 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 ) && tmp != inputSqrt )
{ {
setter( tmp * tmp ); setter( PseudoSquareRgb( tmp ) );
ret = true; ret = true;
} }
@ -505,4 +505,24 @@ public partial class ModEditWindow
return ret; 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);
} }

View file

@ -147,11 +147,11 @@ public partial class ModEditWindow
ImGui.SetNextItemWidth(editorWidth); ImGui.SetNextItemWidth(editorWidth);
var value = new Vector3(values); var value = new Vector3(values);
if (_squaredRgb) if (_squaredRgb)
value = Vector3.SquareRoot(value); value = PseudoSqrtRgb(value);
if (ImGui.ColorEdit3("##0", ref value, ImGuiColorEditFlags.Float | (_clamped ? 0 : ImGuiColorEditFlags.HDR)) && !disabled) if (ImGui.ColorEdit3("##0", ref value, ImGuiColorEditFlags.Float | (_clamped ? 0 : ImGuiColorEditFlags.HDR)) && !disabled)
{ {
if (_squaredRgb) if (_squaredRgb)
value *= value; value = PseudoSquareRgb(value);
if (_clamped) if (_clamped)
value = Vector3.Clamp(value, Vector3.Zero, Vector3.One); value = Vector3.Clamp(value, Vector3.Zero, Vector3.One);
value.CopyTo(values); value.CopyTo(values);
@ -165,11 +165,11 @@ public partial class ModEditWindow
ImGui.SetNextItemWidth(editorWidth); ImGui.SetNextItemWidth(editorWidth);
var value = new Vector4(values); var value = new Vector4(values);
if (_squaredRgb) 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 (ImGui.ColorEdit4("##0", ref value, ImGuiColorEditFlags.Float | ImGuiColorEditFlags.AlphaPreviewHalf | (_clamped ? 0 : ImGuiColorEditFlags.HDR)) && !disabled)
{ {
if (_squaredRgb) if (_squaredRgb)
value *= new Vector4(value.X, value.Y, value.Z, 1.0f); value = PseudoSquareRgb(value);
if (_clamped) if (_clamped)
value = Vector4.Clamp(value, Vector4.Zero, Vector4.One); value = Vector4.Clamp(value, Vector4.Zero, Vector4.One);
value.CopyTo(values); value.CopyTo(values);