示例#1
0
    private void Update()
    {
        if (UI.Core.instance.layoutSystem.activeScreen == UI.Screen.center)
        {
            InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
            if (inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.Mouse)
            {
                // Let mouse handle zooming:
                if (!UI.Core.instance.pointerIsOverPlatformUIObject)
                {
                    if (Input.GetAxis("Mouse ScrollWheel") != 0)
                    {
                        float inputScroll = Input.GetAxis("Mouse ScrollWheel");

                        float zoom = transform.localScale.x + inputScroll / (1 / zoomingSpeed);

                        zoom = Mathf.Clamp(zoom, minZoom, maxZoom);

                        transform.localScale = new Vector3(zoom, zoom, zoom);
                        targetZoom           = transform.localScale;
                    }
                }
            }
            else if (inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.ViveController)
            {
                // Let left Vive controller handle zooming:
                LeftController lc = InputDeviceManager.instance.leftController;
                if (lc != null)
                {
                    UnityEngine.EventSystems.PointerEventData.FramePressState triggerState = lc.triggerButtonState;
                    if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Pressed && zooming == false)
                    {
                        zooming       = true;
                        originalDist  = (lc.transform.position - transform.position).magnitude;
                        mOriginalZoom = transform.localScale;
                    }
                    else if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Released && zooming == true)
                    {
                        zooming = false;
                    }

                    if (zooming)
                    {
                        float dist = (lc.transform.position - transform.position).magnitude;

                        float distDiff = dist - originalDist;

                        Vector3 newScale = mOriginalZoom + mOriginalZoom * distDiff;

                        setTargetZoom(newScale);
                    }
                }
            }
        }

        // Auto-Zoom to target, if given:
        transform.localScale = Vector3.SmoothDamp(transform.localScale, targetZoom, ref zoomVelocity, scaleTime);
    }
示例#2
0
    private void Update()
    {
        if (UI.Core.instance.layoutSystem.activeScreen == UI.Screen.center)
        {
            InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
            if (inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.Mouse)
            {
                // Let mouse handle rotation:
                if (Input.GetMouseButton(1) || Input.GetKey(KeyCode.M))
                {
                    float inputH = -Input.GetAxis("Mouse X");
                    float inputV = -Input.GetAxis("Mouse Y");

                    Vector3 upVector    = Camera.main.transform.up;
                    Vector3 rightVector = Camera.main.transform.right;
                    transform.RotateAround(transform.position, upVector, inputH * rotationSpeedMouse);
                    transform.RotateAround(transform.position, rightVector, -inputV * rotationSpeedMouse);

                    targetRotation = transform.localRotation;                           // Make sure it doesn't auto-rotate back.
                }
            }
            else if (inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.ViveController)
            {
                // Let left Vive controller handle rotation:
                LeftController lc = InputDeviceManager.instance.leftController;
                if (lc != null)
                {
                    UnityEngine.EventSystems.PointerEventData.FramePressState triggerState = lc.triggerButtonState;
                    if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Pressed)
                    {
                        rotating        = true;
                        previousVivePos = lc.transform.localPosition;
                    }
                    else if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Released)
                    {
                        rotating        = false;
                        previousVivePos = new Vector3(0, 0, 0);
                    }
                    if (rotating)
                    {
                        Vector3 upVector    = Camera.main.transform.up;
                        Vector3 rightVector = Camera.main.transform.right;
                        transform.RotateAround(transform.position, upVector, (previousVivePos.x - lc.transform.localPosition.x) * rotationSpeedVive);
                        transform.RotateAround(transform.position, rightVector, -(previousVivePos.y - lc.transform.localPosition.y) * rotationSpeedVive);
                        targetRotation  = transform.localRotation;                              // Make sure it doesn't auto-rotate back.
                        previousVivePos = lc.transform.localPosition;
                    }
                }
            }
        }

        // Slowly rotate towards target, if any:
        //float step =  Time.time;
        transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, (Time.time - rotationStartTime) / rotationTime);
    }
示例#3
0
    private void Update()
    {
        if (UI.Core.instance.layoutSystem.activeScreen == UI.Screen.center)
        {
            InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
            if (inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.Mouse)
            {
                // Let mouse handle rotation:
                if (Input.GetMouseButton(0) || Input.GetKey(KeyCode.M))
                {
                    float inputH = -Input.GetAxis("Mouse X");
                    float inputV = -Input.GetAxis("Mouse Y");

                    if (mouseRotationMode == RotationMode.MODE_FIXED_UP_AXIS)
                    {
                        // Fixed-Up-Axis Mode maps up-down movement to the model's tilt value. Also has a maximum tilt rotation of
                        // +- 85 degrees

                        leftRightAng = leftRightAng + inputH * rotationSpeedMouse;

                        Quaternion leftRightQuat = Quaternion.AngleAxis(leftRightAng, new Vector3(0, 1, 0));
                        transform.rotation = leftRightQuat * Quaternion.AngleAxis(90f, new Vector3(1, 0, 0));

                        // Decide in which direction to rotate by checking if the Y-Vector is facing towards the camera:
                        float dist1 = Vector3.Distance(transform.TransformPoint(new Vector3(0, 1, 0)), Camera.main.transform.position);
                        float dist2 = Vector3.Distance(transform.TransformPoint(new Vector3(0, -1, 0)), Camera.main.transform.position);
                        if (dist1 > dist2)
                        {
                            inputV = -inputV;
                        }
                        upDownAng = Mathf.Clamp(upDownAng + inputV * rotationSpeedMouse, -85, 85);
                        Quaternion upDownQuat = Quaternion.AngleAxis(upDownAng, new Vector3(1, 0, 0));
                        transform.localRotation *= upDownQuat;
                    }
                    else
                    {
                        // Standard Mode maps Controller left-right movement to rotation around the global "up" vector
                        // and up-down movement to a global axis from left to right, so it ignores the model's current orientation.

                        Vector3 upVector    = Camera.main.transform.up;
                        Vector3 rightVector = Camera.main.transform.right;
                        transform.RotateAround(transform.position, upVector, inputH * rotationSpeedMouse);
                        transform.RotateAround(transform.position, rightVector, -inputV * rotationSpeedMouse);
                    }

                    targetRotation = transform.localRotation;                           // Make sure it doesn't auto-rotate back.
                }
            }
            else if (inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.ViveController)
            {
                // Let left Vive controller handle rotation:
                LeftController lc = InputDeviceManager.instance.leftController;
                if (lc != null)
                {
                    UnityEngine.EventSystems.PointerEventData.FramePressState triggerState = lc.triggerButtonState;
                    if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Pressed)
                    {
                        rotating        = true;
                        previousVivePos = lc.transform.localPosition;
                    }
                    else if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Released)
                    {
                        rotating        = false;
                        previousVivePos = new Vector3(0, 0, 0);
                    }
                    if (rotating)
                    {
                        float inputH = (previousVivePos.x - lc.transform.localPosition.x);
                        float inputV = -(previousVivePos.y - lc.transform.localPosition.y);
                        if (viveRotationMode == RotationMode.MODE_STANDARD)
                        {
                            // Standard Mode maps Controller left-right movement to rotation around the global "up" vector
                            // and up-down movement to a global axis from left to right, so it ignores the model's current orientation.
                            Vector3 upVector    = Camera.main.transform.up;
                            Vector3 rightVector = Camera.main.transform.right;
                            transform.RotateAround(transform.position, upVector, inputH * rotationSpeedVive);
                            transform.RotateAround(transform.position, rightVector, inputV * rotationSpeedVive);
                        }
                        else if (viveRotationMode == RotationMode.MODE_FIXED_UP_AXIS)
                        {
                            // Fixed-Up-Axis Mode maps up-down movement to the model's tilt value. Also has a maximum tilt rotation of
                            // +- 85 degrees

                            leftRightAng = leftRightAng + inputH * rotationSpeedVive;

                            Quaternion leftRightQuat = Quaternion.AngleAxis(leftRightAng, new Vector3(0, 1, 0));
                            transform.rotation = leftRightQuat * Quaternion.AngleAxis(90f, new Vector3(1, 0, 0));

                            // Decide in which direction to rotate by checking if the Y-Vector is facing towards the camera:
                            float dist1 = Vector3.Distance(transform.TransformPoint(new Vector3(0, 1, 0)), Camera.main.transform.position);
                            float dist2 = Vector3.Distance(transform.TransformPoint(new Vector3(0, -1, 0)), Camera.main.transform.position);
                            if (dist1 <= dist2)
                            {
                                inputV = -inputV;
                            }
                            upDownAng = Mathf.Clamp(upDownAng + inputV * rotationSpeedVive, -85, 85);
                            Quaternion upDownQuat = Quaternion.AngleAxis(upDownAng, new Vector3(1, 0, 0));
                            transform.localRotation *= upDownQuat;
                        }
                        else if (viveRotationMode == RotationMode.MODE_MAP_ROTATION)
                        {
                            // previousControllerRotation * delta = lc.transform.rotation;
                            Quaternion delta = lc.transform.rotation * Quaternion.Inverse(previousControllerRotation);
                            float      angle;
                            Vector3    axis;
                            delta.ToAngleAxis(out angle, out axis);
                            angle *= manualRotationSpeed;
                            Quaternion scaledDelta = Quaternion.AngleAxis(angle, axis);
                            transform.rotation = scaledDelta * transform.rotation;
                        }
                        else if (viveRotationMode == RotationMode.MODE_STANDARD_PLUS_ROLL)
                        {
                            // Standard Mode maps Controller left-right movement to rotation around the global "up" vector
                            // and up-down movement to a global axis from left to right, so it ignores the model's current orientation.
                            Vector3 upVector    = Camera.main.transform.up;
                            Vector3 rightVector = Camera.main.transform.right;
                            transform.RotateAround(transform.position, upVector, inputH * rotationSpeedVive);
                            transform.RotateAround(transform.position, rightVector, inputV * rotationSpeedVive);

                            // Additional Rotation (rolling):
                            Quaternion delta     = lc.transform.rotation * Quaternion.Inverse(previousControllerRotation);
                            float      inputRoll = delta.eulerAngles.z;
                            while (inputRoll > 180)
                            {
                                inputRoll -= 360;
                            }
                            while (inputRoll < -180)
                            {
                                inputRoll += 360;
                            }
                            Vector3 forwardVector = Camera.main.transform.forward;
                            transform.RotateAround(transform.position, forwardVector, inputRoll * rollSpeed);
                        }

                        targetRotation  = transform.localRotation;                              // Make sure it doesn't auto-rotate back.
                        previousVivePos = lc.transform.localPosition;
                    }
                    previousControllerRotation = lc.transform.rotation;
                }
            }
        }

        // Slowly rotate towards target, if any:
        //float step =  Time.time;
        transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, (Time.time - rotationStartTime) / rotationTime);
    }