Пример #1
0
    /// <summary>
    /// Align all transforms rotation with one Transform, or on mini/max values of boundaries
    /// </summary>
    /// <param name="referenceTransform">
    /// A <see cref="Transform"/> which all other transforms will be aligned with, if alignType is "mean"
    /// </param>
    /// <param name="transformList">
    /// The <see cref="Transform[]"/> of all objects to be aligned
    /// </param>
    /// <param name="axis">
    /// The axis to align on : <see cref="Vector3.one"/> to align all axis, <see cref="Vector3.right"/> to align on the X axis, <see cref="Vector3.up"/> to align on the Y axis, <see cref="Vector3.forward"/> to align on the Z axis
    /// </param>
    /// <param name="alignType">
    /// Witch type of alignement we do, from the <see cref="Landmark"/> enum
    /// </param>
    public static void AlignRotation(Transform referenceTransform, Transform[] transformList, Vector3 axis, Landmark alignType)
    {
        // Get the rotation from the active selected object
        Vector3 activeRotation = referenceTransform.eulerAngles;

        // If alignment is not the mean one, search the min or max oriented object
        Vector3 markRotation = referenceTransform.eulerAngles;

        if (alignType == Landmark.minimum)
        {
            markRotation = BasicExtents.GetMinMarkRotation(referenceTransform.eulerAngles, transformList);
        }
        else if (alignType == Landmark.maximum)
        {
            markRotation = BasicExtents.GetMaxMarkRotation(referenceTransform.eulerAngles, transformList);
        }
        activeRotation = markRotation;

        foreach (Transform nextTransform in transformList)
        {
            Vector3 newRot;
            // Apply the angles changes from the axis to align with
            newRot.x = (axis == Vector3.one || axis == Vector3.right) ? activeRotation.x : nextTransform.eulerAngles.x;
            newRot.y = (axis == Vector3.one || axis == Vector3.up) ? activeRotation.y : nextTransform.eulerAngles.y;
            newRot.z = (axis == Vector3.one || axis == Vector3.forward) ? activeRotation.z : nextTransform.eulerAngles.z;
            nextTransform.rotation = Quaternion.Euler(newRot);
        }
    }
    /// <summary>
    /// Linear Rotation distribution
    /// </summary>
    /// <param name="activeTransform">
    /// The <see cref="Transform"/> to use as the first transform
    /// </param>
    /// <param name="transformList">
    /// All <see cref="Transform[]"/> to be distributed
    /// </param>
    /// <param name="sortBy">
    /// The axis which is used to sort the transform list, <see cref="SortAxis"/>
    /// </param>
    /// <param name="axis">
    /// The axis to distribute the transforms on, using the same <see cref="Vector3"/> format
    /// </param>
    public static void DistributeRotation(Transform activeTransform, Transform[] transformList, SortAxis sortBy, Vector3 axis)
    {
        if (transformList.Length == 0)
        {
            return;
        }

        // Get the min and max marks
        Vector3 minMarkRotation = BasicExtents.GetMinMarkRotation(activeTransform.eulerAngles, transformList);
        Vector3 maxMarkRotation = BasicExtents.GetMaxMarkRotation(activeTransform.eulerAngles, transformList);

        // Interval
        Vector3 distanceBetween = (maxMarkRotation - minMarkRotation) / (transformList.Length - 1);
        // Delta from minMark
        Vector3 delta = Vector3.zero;

        // List of selected objects, to sort from the min position to the max position
        List <Transform> list = new List <Transform>(transformList);

        // Sort the selected objects as requested
        switch (sortBy)
        {
        case SortAxis.Z:
            list.Sort(ByVector3PositionZ);
            break;

        case SortAxis.Y:
            list.Sort(ByVector3PositionY);
            break;

        default:
            list.Sort(ByVector3PositionX);
            break;
        }

        foreach (Transform nextTransform in list)
        {
            Vector3 newPos;
            newPos.x = (axis == Vector3.one || axis == Vector3.right) ? minMarkRotation.x + delta.x : nextTransform.eulerAngles.x;
            newPos.y = (axis == Vector3.one || axis == Vector3.up) ? minMarkRotation.y + delta.y : nextTransform.eulerAngles.y;
            newPos.z = (axis == Vector3.one || axis == Vector3.forward) ? minMarkRotation.z + delta.z : nextTransform.eulerAngles.z;
            nextTransform.rotation = Quaternion.Euler(newPos);
            delta += distanceBetween;
        }
    }