/// <summary> /// Visualizes the rotate towards with posibility to show one of default axis. /// </summary> /// <param name="origin">Origin of rotating object</param> /// <param name="current">Current direction of rotating object.</param> /// <param name="target">Target direction.</param> /// <param name="lenght">Lenght of vectors.</param> /// <param name="showPlane">If set to <c>true</c> show plane in which are current and target direction are.</param> public static void VisualizeRotateTowards(Vector3 origin, Vector3 current, Vector3 target, float lenght = 6, bool showPlane = default(bool)) { Vector3 direction = ColorsAndDirections.GetBaseDirection(default(BaseVectorDirection)); VisualizeRotateTowardsRaw(origin, current, target, default(BaseVectorDirection), lenght, current); }
public static void AngleAxis(Vector3 origin, float angle, Vector3 axis, Quaternion startRotation, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float halfAxisLenght = 3, bool showVectorLabel = !default(bool)) { Vector3 direction = ColorsAndDirections.GetBaseDirection(builtinDirection); AngleAxisRaw(origin, angle, axis, startRotation, builtinDirection, halfAxisLenght, direction, showVectorLabel); }
public static void RotateTowards(Vector3 origin, Quaternion from, Quaternion to, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = lenght6, bool showVectorLabel = !default(bool)) { Vector3 direction = ColorsAndDirections.GetBaseDirection(builtinDirection); Slerp(origin, from, to, builtinDirection, lenght, direction, showVectorLabel, "from", "to"); }
public static void Slerp(Vector3 origin, Quaternion startRotation, Quaternion endRotation, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = lenght6, bool showVectorLabel = !default(bool)) { Vector3 direction = ColorsAndDirections.GetBaseDirection(builtinDirection); InterpolateQuaternions(Quaternion.Slerp, origin, startRotation, endRotation, builtinDirection, lenght, direction, showVectorLabel); }
public static void SlerpUnclamped(Vector3 origin, Quaternion startRotation, Quaternion endRotation, float howFuther, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = lenght6, bool showVectorLabel = !default(bool)) { Vector3 direction = ColorsAndDirections.GetBaseDirection(builtinDirection); SlerpUnclampedRaw(origin, startRotation, endRotation, howFuther, builtinDirection, lenght, direction, showVectorLabel); }
public static void Angle(Vector3 origin, Quaternion startRotation, Quaternion endRotation, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = lenght6, bool showVectorLabel = !default(bool)) { Vector3 direction = ColorsAndDirections.GetBaseDirection(builtinDirection); InterpolateQuaternions(Quaternion.Slerp, origin, startRotation, endRotation, builtinDirection, lenght, direction, showVectorLabel); GizmosForVector.ShowLabel(origin, "angle: " + System.Math.Round(Quaternion.Angle(startRotation, endRotation), 0) + "\xB0", new Color32(127, 0, 255, 255)); }
static void DrawRotateGizmo(Transform objectToRotate, Vector3 point, float angle, Vector3 axis, float halfAxisLenght, float offset, bool showPoints) { if (!objectToRotate) { return; } Vector3 binormal = Vector3.zero; Vector3 tangent = Vector3.zero; Vector3.OrthoNormalize(ref axis, ref binormal, ref tangent); Vector3 directionFromPointToObject = point - objectToRotate.position; Vector3 distanceBetweenPointAndPivotBelongAxis = Vector3.Project(directionFromPointToObject, axis.normalized); Vector3 pivot = point - distanceBetweenPointAndPivotBelongAxis; Quaternion pendicularQ0 = Quaternion.AngleAxis(0, -axis.normalized); Quaternion pendicularQ90 = Quaternion.AngleAxis(90, -axis.normalized); float radius = (objectToRotate.position - pivot).magnitude + offset; Vector3 dir = axis.normalized; Color startColor = ColorsAndDirections.GetColors((BaseVectorDirection)0) [0]; Color endColor = ColorsAndDirections.GetColors((BaseVectorDirection)7) [1]; int iterations = 200; List <Quaternion> points = new List <Quaternion> (iterations); List <Color> colors = new List <Color> (iterations); Vector3 direction = binormal; Gizmos.DrawRay(pivot - axis.normalized * halfAxisLenght * halfAxisLenght, axis.normalized * halfAxisLenght * halfAxisLenght * 2); for (int i = 0; i < iterations; i++) { float t = i * 4 / (float)iterations; points.Add(Quaternion.SlerpUnclamped(pendicularQ0, pendicularQ0 * pendicularQ90, t)); colors.Add(Color.Lerp(startColor, endColor, t * 0.25f)); } for (int i = 0; i < iterations - 1; i++) { if (i % 2 == 0) { Gizmos.color = colors [i]; Gizmos.DrawLine(pivot + points [i] * direction * radius, pivot + points [i + 1] * direction * radius); } } if (showPoints) { GizmosForVector.ShowVectorValue(point, "point", point, Color.red); GizmosForVector.ShowVectorValue(pivot, "pivot", pivot, Color.green); Gizmos.color = Color.red; Gizmos.DrawSphere(point, 0.2f); Gizmos.color = Color.green; Gizmos.DrawSphere(pivot, 0.2f); } }
public static void FromToRotation(Vector3 origin, Vector3 fromDirection, Vector3 toDirection, float lenght = lenght6, bool showVectorLabel = !default(bool)) { Quaternion startRotation = Quaternion.FromToRotation(Vector3.right, fromDirection); Quaternion endRotation = Quaternion.FromToRotation(Vector3.right, toDirection); //Slerp (origin, startRotation, endRotation, BaseVectorDirection.right, lenght, showVectorLabel); Vector3 direction = ColorsAndDirections.GetBaseDirection(BaseVectorDirection.right); InterpolateQuaternions(Quaternion.Slerp, origin, startRotation, endRotation, BaseVectorDirection.right, lenght, direction, showVectorLabel); }
public static void ToAngleAxis(Vector3 origin, Quaternion rotation, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float halfAxisLenght = 3, bool showVectorLabel = !default(bool)) { float angle; Vector3 axis; rotation.ToAngleAxis(out angle, out axis); Vector3 direction = ColorsAndDirections.GetBaseDirection(builtinDirection); Quaternion startRotation = Quaternion.FromToRotation(Vector3.right, direction); AngleAxisRaw(origin, angle, axis, startRotation, builtinDirection, halfAxisLenght, Vector3.right, showVectorLabel); }
public static void SlerpUnclampedRaw(Vector3 origin, Quaternion startRotation, Quaternion endRotation, float howFuther, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = lenght6, Vector3 direction = default(Vector3), bool showVectorLabel = !default(bool)) { float angle; Vector3 axis; // Quaternion sub = startRotation.Substraction (endRotation); Quaternion sub = startRotation * Quaternion.Inverse(endRotation); sub.ToAngleAxis(out angle, out axis); float halfAxisLenght = lenght * 0.5f; Gizmos.DrawRay(origin - axis.normalized * halfAxisLenght, axis.normalized * halfAxisLenght * 2); //Vector3 direction = ColorsAndDirections.GetBaseDirection (builtinDirection); // directionn = (directionn == Vector3.zero) ? Vector3.right : directionn.normalized;///w bok direction = (direction == Vector3.zero) ? Vector3.right : direction.normalized; Color startColor = ColorsAndDirections.GetColors(builtinDirection) [0]; Color endColor = ColorsAndDirections.GetColors(builtinDirection) [1]; DrawQuaternion(origin, startRotation, startColor, lenght, direction, showVectorLabel, "start"); DrawQuaternion(origin, endRotation, endColor, lenght, direction, showVectorLabel, "end"); Color finishColor = (builtinDirection == customVectorDirection) ? (Color) new Color32(0, 174, 219, 255) : ColorsAndDirections.GetColors((BaseVectorDirection)7) [1]; DrawQuaternion(origin, Quaternion.SlerpUnclamped(startRotation, endRotation, howFuther), finishColor, lenght, direction, showVectorLabel, "howFuther: " + howFuther + "\nfinal Position: "); Quaternion pendicularQ0 = Quaternion.AngleAxis(0, axis); Quaternion pendicularQ90 = Quaternion.AngleAxis(90, axis); pendicularQ0 *= startRotation; pendicularQ90 *= startRotation; int iterations = 200; List <Quaternion> points = new List <Quaternion> (iterations); List <Color> colors = new List <Color> (iterations); for (int i = 0; i < iterations; i++) { float t = i * 4 / (float)iterations; points.Add(Quaternion.SlerpUnclamped(pendicularQ0, pendicularQ90, t)); colors.Add(Color.Lerp(startColor, endColor, t * 0.25f)); } for (int i = 0; i < iterations - 1; i++) { if (i % 2 == 0) { Gizmos.color = colors [i]; Gizmos.DrawLine(points [i] * direction * lenght + origin, points [i + 1] * direction * lenght + origin); } } }
static void InterpolateQuaternions(System.Func <Quaternion, Quaternion, float, Quaternion> func, Vector3 origin, Quaternion startRotation, Quaternion endRotation, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = lenght6, Vector3 direction = default(Vector3), bool showVectorLabel = default(bool), string name1 = "", string name2 = "") { Color temp = Gizmos.color; int iterations = 100; direction = (direction == Vector3.zero) ? Vector3.right : direction.normalized; Color startColor = ColorsAndDirections.GetColors(builtinDirection) [0]; Color endColor = ColorsAndDirections.GetColors(builtinDirection) [1]; DrawQuaternion(origin, startRotation, startColor, lenght, direction, showVectorLabel, name1); DrawQuaternion(origin, endRotation, endColor, lenght, direction, showVectorLabel, name2); DrawDotsConnectingQuaternions(func, origin, startRotation, endRotation, lenght, direction, iterations, startColor, endColor); Gizmos.color = temp; ; }
static void InterpolateQuaternionsForLerp(System.Func <Quaternion, Quaternion, float, Quaternion> func, Vector3 origin, Quaternion startRotation, Quaternion endRotation, float t, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = lenght6, Vector3 direction = default(Vector3), bool showVectorLabel = default(bool), string name1 = "", string name2 = "") { Color temp = Gizmos.color; int iterations = 100; direction = (direction == Vector3.zero) ? Vector3.right : direction.normalized; Color startColor = ColorsAndDirections.GetColors(builtinDirection)[0]; Color endColor = ColorsAndDirections.GetColors(builtinDirection)[1]; Quaternion middleRotation = func(startRotation, endRotation, t); DrawQuaternion(origin, startRotation, startColor, lenght, direction, false, "a:"); DrawQuaternion(origin, middleRotation, Color.magenta, lenght, direction, false, "Interpolated for t = " + System.Math.Round(t, 2) + " :"); DrawQuaternion(origin, endRotation, endColor, lenght, direction, false, "b:"); DrawDotsConnectingQuaternions(func, origin, startRotation, middleRotation, lenght, direction, iterations, Color.magenta, Color.magenta); DrawDotsConnectingQuaternions(func, origin, middleRotation, endRotation, lenght, direction, iterations, startColor, endColor); Gizmos.color = temp; ; }
static void VisualizeRotateTowardsRaw(Vector3 origin, Vector3 current, Vector3 target, BaseVectorDirection builtinDirection = default(BaseVectorDirection), float lenght = 6, Vector3 direction = default(Vector3), bool showPlane = default(bool)) { Color temp = Gizmos.color; direction = (direction == Vector3.zero) ? Vector3.right : direction.normalized; Color startColor = ColorsAndDirections.GetColors((BaseVectorDirection)4) [0]; Color endColor = ColorsAndDirections.GetColors((BaseVectorDirection)4) [1]; int iterations = 100; List <Vector3> points = new List <Vector3> (iterations); List <Color> colors = new List <Color> (iterations); for (int i = 0; i < iterations; i++) { float t = i / (float)iterations; points.Add(Vector3.Slerp(current.normalized * lenght, target.normalized * lenght, t)); colors.Add(Color.Lerp(startColor, endColor, t)); } for (int i = 0; i < iterations - 1; i++) { if (i % 2 == 0) { Gizmos.color = colors [i]; Gizmos.DrawLine(points [i] + origin, points [i + 1] + origin); } } if (showPlane) { Vector3 normal = Vector3.Cross(current, target); if (normal.magnitude > 0f) { DrawPlane(origin, normal, lenght); } } DrawVector(origin, current, lenght, startColor); DrawVector(origin, target, lenght, endColor); }