Created
December 4, 2024 09:07
-
-
Save egemenertugrul/edc3db96dbd0e390748dd52e041f9eff to your computer and use it in GitHub Desktop.
Remap Unit for Unity Visual Scripting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Unity.VisualScripting; | |
public static class MathExtensions | |
{ | |
public static float Remap(this float value, float fromMin, float fromMax, float toMin, float toMax, bool isClamped = false) | |
{ | |
return isClamped | |
? Mathf.Clamp01((value - fromMin) / (fromMax - fromMin)) * (toMax - toMin) + toMin | |
: (value - fromMin) / (fromMax - fromMin) * (toMax - toMin) + toMin; | |
} | |
} | |
[UnitCategory("Math/Float")] | |
public class FloatRemapUnit : RemapUnit<float> | |
{ | |
public override float Operation(float value, float fromMin, float fromMax, float toMin, float toMax) | |
{ | |
return value.Remap(fromMin, fromMax, toMin, toMax, isClamped); | |
} | |
} | |
[UnitCategory("Math/Vector 2")] | |
public class Vector2RemapUnit : RemapUnit<Vector2> | |
{ | |
public override Vector2 Operation(Vector2 value, Vector2 fromMin, Vector2 fromMax, Vector2 toMin, Vector2 toMax) | |
{ | |
return new Vector2( | |
value.x.Remap(fromMin.x, fromMax.x, toMin.x, toMax.x, isClamped), | |
value.y.Remap(fromMin.y, fromMax.y, toMin.y, toMax.y, isClamped) | |
); | |
} | |
} | |
[UnitCategory("Math/Vector 3")] | |
public class Vector3RemapUnit : RemapUnit<Vector3> | |
{ | |
public override Vector3 Operation(Vector3 value, Vector3 fromMin, Vector3 fromMax, Vector3 toMin, Vector3 toMax) | |
{ | |
return new Vector3( | |
value.x.Remap(fromMin.x, fromMax.x, toMin.x, toMax.x, isClamped), | |
value.y.Remap(fromMin.y, fromMax.y, toMin.y, toMax.y, isClamped), | |
value.z.Remap(fromMin.z, fromMax.z, toMin.z, toMax.z, isClamped) | |
); | |
} | |
} | |
[UnitCategory("Math/Vector 4")] | |
public class Vector4RemapUnit : RemapUnit<Vector4> | |
{ | |
public override Vector4 Operation(Vector4 value, Vector4 fromMin, Vector4 fromMax, Vector4 toMin, Vector4 toMax) | |
{ | |
return new Vector4( | |
value.x.Remap(fromMin.x, fromMax.x, toMin.x, toMax.x, isClamped), | |
value.y.Remap(fromMin.y, fromMax.y, toMin.y, toMax.y, isClamped), | |
value.z.Remap(fromMin.z, fromMax.z, toMin.z, toMax.z, isClamped), | |
value.w.Remap(fromMin.w, fromMax.w, toMin.w, toMax.w, isClamped) | |
); | |
} | |
} | |
[UnitTitle("Remap")] | |
[TypeIcon(typeof(Mathf))] | |
public abstract class RemapUnit<T> : Unit | |
{ | |
[Serialize] | |
[Inspectable] | |
protected bool isClamped; | |
[DoNotSerialize] | |
private ValueInput valueInput; | |
[DoNotSerialize] | |
private ValueInput fromMinInput; | |
[DoNotSerialize] | |
private ValueInput fromMaxInput; | |
[DoNotSerialize] | |
private ValueInput toMinInput; | |
[DoNotSerialize] | |
private ValueInput toMaxInput; | |
[DoNotSerialize] | |
private ValueOutput resultOutput; | |
[DoNotSerialize] | |
[PortLabelHidden] | |
protected ControlInput controlInput; | |
[DoNotSerialize] | |
[PortLabelHidden] | |
protected ControlOutput controlOutput; | |
protected override void Definition() | |
{ | |
valueInput = ValueInput<T>("Value", default(T)); | |
fromMinInput = ValueInput<T>("From Min", default(T)); | |
fromMaxInput = ValueInput<T>("From Max", default(T)); | |
toMinInput = ValueInput<T>("To Min", default(T)); | |
toMaxInput = ValueInput<T>("To Max", default(T)); | |
controlInput = ControlInput("", (flow) => controlOutput); | |
controlOutput = ControlOutput(""); | |
resultOutput = ValueOutput<T>("Result", (flow) => Operation(flow)); | |
Assignment(controlInput, resultOutput); | |
Succession(controlInput, controlOutput); | |
Requirement(valueInput, controlInput); | |
Requirement(fromMinInput, controlInput); | |
Requirement(fromMaxInput, controlInput); | |
Requirement(toMinInput, controlInput); | |
Requirement(toMaxInput, controlInput); | |
} | |
private T Operation(Flow flow) | |
{ | |
return Operation(flow.GetValue<T>(valueInput), flow.GetValue<T>(fromMinInput), flow.GetValue<T>(fromMaxInput), flow.GetValue<T>(toMinInput), flow.GetValue<T>(toMaxInput)); | |
} | |
public abstract T Operation(T value, T fromMin, T fromMax, T toMin, T toMax); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment