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 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 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));
        }
        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;
            ;
        }
        internal static Color[] GetColors(BaseVectorDirection direction)
        {
            Color[] c = new Color[2];
            switch (direction)
            {
            case BaseVectorDirection.right:
                c [0] = new Color32(240, 1, 1, 255);
                c [1] = new Color32(255, 128, 0, 255);
                break;

            case BaseVectorDirection.up:
                c [0] = new Color32(0, 199, 29, 255);
                c [1] = new Color32(255, 128, 0, 255);
                break;

            case BaseVectorDirection.forward:
                c [0] = new Color32(30, 44, 255, 255);
                c [1] = new Color32(255, 128, 0, 255);
                break;

            case (BaseVectorDirection)5:
                c [0] = new Color32(240, 1, 1, 255);
                c [1] = new Color32(30, 44, 255, 255);
                break;

            case (BaseVectorDirection)6:
                c [0] = new Color32(240, 1, 1, 255);
                c [1] = new Color32(0, 199, 29, 255);
                break;

            case (BaseVectorDirection)7:
                c [0] = new Color32(240, 1, 1, 255);
                c [1] = new Color32(127, 0, 255, 255);
                break;

            default:
                c [0] = new Color32(127, 0, 255, 255);
                c [1] = new Color32(255, 128, 0, 255);
                break;
            }
            return(c);
        }
        internal static Vector3 GetBaseDirection(BaseVectorDirection direction)
        {
            Vector3 v = new Vector3();

            switch (direction)
            {
            case BaseVectorDirection.right:
                v = Vector3.right;
                break;

            case BaseVectorDirection.up:
                v = Vector3.up;
                break;

            case BaseVectorDirection.forward:
                v = Vector3.forward;
                break;
            }
            return(v);
        }
        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;
            ;
        }
Ejemplo n.º 12
0
        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);
        }
        static void AngleAxisRaw(Vector3 origin, float angle, Vector3 axis, Quaternion startRotation, BaseVectorDirection builtinDirection, float halfAxisLenght, Vector3 direction, bool showVectorLabel = !default(bool))
        {
            Color temp = Gizmos.color;

            direction.Normalize();
            Gizmos.color = new Color32(162, 0, 255, 255);
            Gizmos.DrawRay(origin - axis.normalized * halfAxisLenght, axis.normalized * halfAxisLenght * 2);
            GizmosForVector.DrawVector(origin - axis.normalized * halfAxisLenght,
                                       axis.normalized, halfAxisLenght * 2, Gizmos.color);
            Quaternion endRotation = Quaternion.AngleAxis(angle, axis);

            endRotation = endRotation * startRotation;
            Slerp(origin, startRotation, endRotation, builtinDirection, halfAxisLenght, direction, showVectorLabel);

            Quaternion middle = Quaternion.Slerp(startRotation, endRotation, 0.5f);

//		DrawQuaternion (origin, middle, Color.yellow, 6, direction);////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            GizmosForVector.ShowLabel(origin + middle * direction * halfAxisLenght,              //* startRotation
//				System.Math.Round (Quaternion.Angle (startRotation, endRotation), 0) + "\xB0", Color.magenta);
                                      System.Math.Round((startRotation * Quaternion.Inverse(endRotation)).SignedAngle()) + "\xB0", Color.magenta);

            //first * Quaternion.Inverse (second)
            Gizmos.color = temp;
        }
 static void Slerp(Vector3 origin, Quaternion startRotation, Quaternion endRotation,
                   BaseVectorDirection builtinDirection,
                   float lenght, Vector3 customDirection, bool showVectorLabel = !default(bool), string name1 = "", string name2 = "")
 {
     InterpolateQuaternions(Quaternion.Slerp, origin, startRotation, endRotation, builtinDirection, lenght, customDirection, showVectorLabel, name1, name2);
 }