Exemplo n.º 1
0
    /// <summary>
    /// Draws controls of selected mode.
    /// </summary>
    void DrawControls()
    {
        for (int i = 0; i < _transforms.Length; i++)
        {
            Transform transform = _transforms[i];
            //if(transform.GetComponent<Rigidbody>()==null) continue;
            Vector3 pos  = AutoRagdollUtility.GetRotatorPosition(transform);
            float   size = HandleUtility.GetHandleSize(pos);

            if (Handles.Button(pos, Quaternion.identity, size / 6f, size / 6f, Handles.SphereHandleCap))
            {
                _curPointIndex = i;

                Quaternion rotatorRotation2 = AutoRagdollUtility.GetRotatorRotation(transform);

                if (!_buttonPressed)
                {
                    _lastRotation  = rotatorRotation2;
                    _buttonPressed = true;
                }
                //Selection.activeGameObject = transform.gameObject;
            }
            else
            {
                _buttonPressed = false;
            }

            if (_curPointIndex != i)
            {
                continue;
            }

            // if current point controll was selected
            // draw other controls over it
            Quaternion rotatorRotation = AutoRagdollUtility.GetRotatorRotation(transform);
            switch (_selectedMode)
            {
            case SelectedMode.ColliderRotate:
                ProcessRotation(rotatorRotation, transform, pos);
                break;

            case SelectedMode.ColliderMove:
                ProcessColliderMove(rotatorRotation, transform, pos);
                break;

            case SelectedMode.ColliderScale:
                ProcessColliderScale(rotatorRotation, transform, pos);
                break;
            }
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// Resize collider though controls
    /// </summary>
    /// <param name="transform">The node the collider is attached to</param>
    static void ProcessColliderMove(Quaternion rotatorRotation, Transform transform, Vector3 pos)
    {
        if (Tools.pivotRotation == PivotRotation.Global)
        {
            rotatorRotation = Quaternion.identity;
        }

        Vector3 newPosition = Handles.PositionHandle(pos, rotatorRotation);
        Vector3 translateBy = newPosition - pos;

        if (translateBy != Vector3.zero)
        {
            AutoRagdollUtility.SetColliderPosition(transform, newPosition);
        }
    }
Exemplo n.º 3
0
    /// <summary>
    /// Rotate node's colider though controls
    /// </summary>
    void ProcessRotation(Quaternion rotatorRotation, Transform transform, Vector3 pos)
    {
        Quaternion newRotation;
        bool       changed;

        if (Tools.pivotRotation == PivotRotation.Global)
        {
            Quaternion fromStart = rotatorRotation * Quaternion.Inverse(_lastRotation);
            newRotation = Handles.RotationHandle(fromStart, pos);
            changed     = fromStart != newRotation;
            newRotation = newRotation * _lastRotation;
        }
        else
        {
            newRotation = Handles.RotationHandle(rotatorRotation, pos);
            changed     = rotatorRotation != newRotation;
        }

        if (changed)
        {
            transform = AutoRagdollUtility.GetRotatorTransform(transform);
            AutoRagdollUtility.RotateCollider(transform, newRotation);
        }
    }
Exemplo n.º 4
0
    /// <summary>
    /// Move collider though controls
    /// </summary>
    void ProcessColliderScale(Quaternion rotatorRotation, Transform transform, Vector3 pos)
    {
        float size     = HandleUtility.GetHandleSize(pos);
        var   collider = AutoRagdollUtility.GetCollider(transform);

        // process each collider type in its own way
        CapsuleCollider cCollider = collider as CapsuleCollider;
        BoxCollider     bCollider = collider as BoxCollider;
        SphereCollider  sCollider = collider as SphereCollider;

        if (cCollider != null)
        {
            // for capsule collider draw circle and two dot controllers
            Vector3 direction = DirectionIntToVector(cCollider.direction);

            var t = Quaternion.LookRotation(cCollider.transform.TransformDirection(direction));

            // method "Handles.ScaleValueHandle" multiplies size on 0.15f
            // so to send exact size to "Handles.CircleCap",
            // I needed to multiply size on 1f/0.15f
            // Then to get a size a little bigger (to 130%) than
            // collider (for nice looking purpose), I multiply size by 1.3f
            const float magicNumber = 1f / 0.15f * 1.3f;

            // draw radius controll

            float radius        = Handles.ScaleValueHandle(cCollider.radius, pos, t, cCollider.radius * magicNumber, Handles.CircleHandleCap, 0);
            bool  radiusChanged = cCollider.radius != radius;

            Vector3 scaleHeightShift = cCollider.transform.TransformDirection(direction * cCollider.height / 2);

            // draw height controlls
            Vector3 heightControl1Pos = pos + scaleHeightShift;
            Vector3 heightControl2Pos = pos - scaleHeightShift;

            float height1   = Handles.ScaleValueHandle(cCollider.height, heightControl1Pos, t, size * 0.5f, Handles.DotHandleCap, 0);
            float height2   = Handles.ScaleValueHandle(cCollider.height, heightControl2Pos, t, size * 0.5f, Handles.DotHandleCap, 0);
            float newHeight = 0f;

            bool moved             = false;
            bool firstCtrlSelected = false;
            if (height1 != cCollider.height)
            {
                moved             = true;
                firstCtrlSelected = true;
                newHeight         = height1;
            }
            else if (height2 != cCollider.height)
            {
                moved     = true;
                newHeight = height2;
            }

            if (moved | radiusChanged)
            {
                Undo.RecordObject(cCollider, "Resize capsule collider");

                bool upperSelected = false;
                if (moved)
                {
                    if (newHeight < 0.01f)
                    {
                        newHeight = 0.01f;
                    }

                    bool firstIsUpper = FirstIsUpper(cCollider.transform, heightControl1Pos, heightControl2Pos);
                    upperSelected = firstIsUpper == firstCtrlSelected;

                    cCollider.center += direction * (newHeight - cCollider.height) / 2 * (firstCtrlSelected ? 1 : -1);
                    cCollider.height  = newHeight;
                }
                if (radiusChanged)
                {
                    cCollider.radius = radius;
                }

                // resize symmetric colliders too
                Transform symBone;
                if (_symmetricBones != null && _symmetricBones.TryGetValue(transform.name, out symBone))
                {
                    var symCapsule = AutoRagdollUtility.GetCollider(symBone) as CapsuleCollider;
                    if (symCapsule == null)
                    {
                        return;
                    }

                    Undo.RecordObject(symCapsule, "Resize symetric capsule collider");

                    if (moved)
                    {
                        Vector3 direction2 = DirectionIntToVector(symCapsule.direction);

                        Vector3 scaleHeightShift2 = symCapsule.transform.TransformDirection(direction2 * symCapsule.height / 2);
                        Vector3 pos2 = AutoRagdollUtility.GetRotatorPosition(symCapsule.transform);

                        Vector3 heightControl1Pos2 = pos2 + scaleHeightShift2;
                        Vector3 heightControl2Pos2 = pos2 - scaleHeightShift2;

                        bool firstIsUpper2 = FirstIsUpper(symCapsule.transform, heightControl1Pos2, heightControl2Pos2);

                        symCapsule.center += direction2 * (newHeight - symCapsule.height) / 2
                                             * (upperSelected ? 1 : -1)
                                             * (firstIsUpper2 ? 1 : -1);

                        symCapsule.height = cCollider.height;
                    }
                    if (radiusChanged)
                    {
                        symCapsule.radius = cCollider.radius;
                    }
                }
            }
        }
        else if (bCollider != null)
        {
            // resize Box collider

            var newSize = Handles.ScaleHandle(bCollider.size, pos, rotatorRotation, size);
            if (bCollider.size != newSize)
            {
                Undo.RecordObject(bCollider, "Resize box collider");
                bCollider.size = newSize;
            }
        }
        else if (sCollider != null)
        {
            // resize Sphere collider
            var newRadius = Handles.RadiusHandle(rotatorRotation, pos, sCollider.radius, true);
            if (sCollider.radius != newRadius)
            {
                Undo.RecordObject(sCollider, "Resize sphere collider");
                sCollider.radius = newRadius;
            }
        }
        else
        {
            throw new InvalidOperationException("Unsupported Collider type: " + collider.GetType().FullName);
        }
    }