Ejemplo n.º 1
0
    public void MouseDrag(BaseEventData eventData)
    {
        //Debug.Log("MouseDrag");
        var pointerEvent = eventData as PointerEventData;

        if (pointerEvent != null)
        {
            if (!follow)
            {
                if (pointerEvent.button == PointerEventData.InputButton.Left)
                {
                    var currentMousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

                    var delta = ProjectionUtils.CameraToPlaneProjection(mouseScreenPos, cam, transformPlane) -
                                ProjectionUtils.CameraToPlaneProjection(currentMousePos, cam, transformPlane);

                    targetLocation = targetLocation + delta;

                    interactionPoint += delta;

                    mouseScreenPos = currentMousePos;
                }
            }
        }
    }
Ejemplo n.º 2
0
 /// <inheritdoc />
 public override Vector3 ProjectTo(Vector2 screenPosition, Plane projectionPlane)
 {
     if (_camera == null)
     {
         return(base.ProjectTo(screenPosition, projectionPlane));
     }
     return(ProjectionUtils.CameraToPlaneProjection(screenPosition, _camera, projectionPlane));
 }
 /// <inheritdoc />
 public override Vector3 ProjectTo(Vector2 screenPosition, Plane projectionPlane)
 {
     if (mode == RenderMode.ScreenSpaceOverlay)
     {
         return(base.ProjectTo(screenPosition, projectionPlane));
     }
     return(ProjectionUtils.CameraToPlaneProjection(screenPosition, camera, projectionPlane));
 }
Ejemplo n.º 4
0
    private void ScaleAndRotateGesture_Transformed(object sender, System.EventArgs e)
    {
        Vector2 midPoint     = Vector2.zero;
        Vector2 midPointPrev = Vector2.zero;
        int     numPoints    = ScaleAndRotateGesture.ActivePointers.Count;

        for (int i = 0; i < numPoints; i++)
        {
            midPoint     += ScaleAndRotateGesture.ActivePointers[i].Position;
            midPointPrev += ScaleAndRotateGesture.ActivePointers[i].PreviousPosition;
        }
        midPoint     /= numPoints;
        midPointPrev /= numPoints;

        float dY = midPointPrev.y - midPoint.y;


        float distanceToMid     = 0.0f;
        float distanceToMidPrev = 0.0f;

        for (int i = 0; i < numPoints; i++)
        {
            distanceToMid     += Vector2.Distance(midPoint, ScaleAndRotateGesture.ActivePointers[i].Position);
            distanceToMidPrev += Vector2.Distance(midPointPrev, ScaleAndRotateGesture.ActivePointers[i].PreviousPosition);
        }
        distanceToMid     /= numPoints;
        distanceToMidPrev /= numPoints;
        float deltaDistanceToMid = distanceToMidPrev - distanceToMid;


        lastWorldHit = ProjectionUtils.CameraToPlaneProjection(midPoint, cam, transformPlane);

        var tempPos = pivot.localPosition;
        var tempRot = pivot.localRotation;

        pivot.localPosition = pivotTargetPosition;
        pivot.localRotation = pivotTargetRotation;

        var camPos = pivot.position;
        var dirVec = lastWorldHit - camPos;

        float distanceToLastWorldHit = dirVec.magnitude;

        //Debug.Log("Eval, distanceToLastWorldHit: " + heightFactorCurve.Evaluate(distanceToLastWorldHit) + ", " + distanceToLastWorldHit);
        float customScale = 1.0f + deltaDistanceToMid / zoomScale * heightFactorCurve.Evaluate(distanceToLastWorldHit);

        pivot.position = lastWorldHit - dirVec * customScale;

        pivot.RotateAround(lastWorldHit, Vector3.up, -ScaleAndRotateGesture.DeltaRotation);

        pivot.RotateAround(lastWorldHit, cam.transform.right, dY * tiltScale);

        pivotTargetPosition = pivot.localPosition;
        pivotTargetRotation = pivot.localRotation;

        pivot.localPosition = tempPos;
        pivot.localRotation = tempRot;
    }
Ejemplo n.º 5
0
        /// <inheritdoc />
        protected override void touchesMoved(IList <ITouch> touches)
        {
            base.touchesMoved(touches);

            var     worldDelta = Vector3.zero;
            Vector3 oldWorldCenter, newWorldCenter;

            Vector2 oldScreenCenter = PreviousScreenPosition;
            Vector2 newScreenCenter = ScreenPosition;

            if (isMoving)
            {
                oldWorldCenter = ProjectionUtils.CameraToPlaneProjection(oldScreenCenter, projectionCamera, WorldTransformPlane);
                newWorldCenter = ProjectionUtils.CameraToPlaneProjection(newScreenCenter, projectionCamera, WorldTransformPlane);
                worldDelta     = newWorldCenter - oldWorldCenter;
            }
            else
            {
                movementBuffer += newScreenCenter - oldScreenCenter;
                var dpiMovementThreshold = MovementThreshold * touchManager.DotsPerCentimeter;
                if (movementBuffer.sqrMagnitude > dpiMovementThreshold * dpiMovementThreshold)
                {
                    isMoving       = true;
                    oldWorldCenter = ProjectionUtils.CameraToPlaneProjection(oldScreenCenter - movementBuffer, projectionCamera, WorldTransformPlane);
                    newWorldCenter = ProjectionUtils.CameraToPlaneProjection(newScreenCenter, projectionCamera, WorldTransformPlane);
                    worldDelta     = newWorldCenter - oldWorldCenter;
                }
                else
                {
                    newWorldCenter = ProjectionUtils.CameraToPlaneProjection(newScreenCenter - movementBuffer, projectionCamera, WorldTransformPlane);
                    oldWorldCenter = newWorldCenter;
                }
            }

            if (worldDelta != Vector3.zero)
            {
                switch (State)
                {
                case GestureState.Possible:
                case GestureState.Began:
                case GestureState.Changed:
                    PreviousWorldTransformCenter = oldWorldCenter;
                    WorldTransformCenter         = newWorldCenter;
                    WorldDeltaPosition           = worldDelta;

                    if (State == GestureState.Possible)
                    {
                        setState(GestureState.Began);
                    }
                    else
                    {
                        setState(GestureState.Changed);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 6
0
        /// <inheritdoc />
        protected override void touchesMoved(IList <ITouch> touches)
        {
            if (!gotEnoughTouches())
            {
                return;
            }
            if (!relevantTouches(touches))
            {
                return;
            }

            Vector3 oldWorldCenter, newWorldCenter;
            var     deltaRotation = 0f;

            var newScreenPos1  = getPointScreenPosition(0);
            var newScreenPos2  = getPointScreenPosition(1);
            var newScreenDelta = newScreenPos2 - newScreenPos1;

            if (newScreenDelta.sqrMagnitude < minPixelDistanceSquared)
            {
                return;
            }

            base.touchesMoved(touches);

            var oldScreenPos1 = getPointPreviousScreenPosition(0);
            var oldScreenPos2 = getPointPreviousScreenPosition(1);
            var oldWorldPos1  = ProjectionUtils.CameraToPlaneProjection(oldScreenPos1, projectionCamera, WorldTransformPlane);
            var oldWorldPos2  = ProjectionUtils.CameraToPlaneProjection(oldScreenPos2, projectionCamera, WorldTransformPlane);
            var newWorldPos1  = ProjectionUtils.CameraToPlaneProjection(newScreenPos1, projectionCamera, WorldTransformPlane);
            var newWorldPos2  = ProjectionUtils.CameraToPlaneProjection(newScreenPos2, projectionCamera, WorldTransformPlane);
            var newVector     = newWorldPos2 - newWorldPos1;
            var oldVector     = oldWorldPos2 - oldWorldPos1;

            Vector2 oldScreenCenter = (oldScreenPos1 + oldScreenPos2) * .5f;
            Vector2 newScreenCenter = (newScreenPos1 + newScreenPos2) * .5f;

            var angle = Vector3.Angle(oldVector, newVector);

            if (Vector3.Dot(Vector3.Cross(oldVector, newVector), WorldTransformPlane.normal) < 0)
            {
                angle = -angle;
            }
            if (isRotating)
            {
                deltaRotation = angle;
            }
            else
            {
                rotationBuffer += angle;
                if (rotationBuffer * rotationBuffer >= RotationThreshold * RotationThreshold)
                {
                    isRotating    = true;
                    deltaRotation = rotationBuffer;
                }
            }

            oldWorldCenter = ProjectionUtils.CameraToPlaneProjection(oldScreenCenter, projectionCamera, WorldTransformPlane);
            newWorldCenter = ProjectionUtils.CameraToPlaneProjection(newScreenCenter, projectionCamera, WorldTransformPlane);

            if (Math.Abs(deltaRotation) > 0.00001)
            {
                switch (State)
                {
                case GestureState.Possible:
                case GestureState.Began:
                case GestureState.Changed:
                    screenPosition               = newScreenCenter;
                    previousScreenPosition       = oldScreenCenter;
                    PreviousWorldTransformCenter = oldWorldCenter;
                    WorldTransformCenter         = newWorldCenter;

                    DeltaRotation = deltaRotation;

                    if (State == GestureState.Possible)
                    {
                        setState(GestureState.Began);
                    }
                    else
                    {
                        setState(GestureState.Changed);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 7
0
        /// <inheritdoc />
        protected override void touchesMoved(IList <ITouch> touches)
        {
            if (!gotEnoughTouches())
            {
                return;
            }
            if (!relevantTouches(touches))
            {
                return;
            }

            Vector3 oldWorldCenter, newWorldCenter;
            var     deltaScale = 1f;

            var newScreenPos1  = getPointScreenPosition(0);
            var newScreenPos2  = getPointScreenPosition(1);
            var newScreenDelta = newScreenPos2 - newScreenPos1;

            if (newScreenDelta.sqrMagnitude < minPixelDistanceSquared)
            {
                return;
            }

            base.touchesMoved(touches);

            var oldScreenPos1 = getPointPreviousScreenPosition(0);
            var oldScreenPos2 = getPointPreviousScreenPosition(1);
            var oldWorldPos1  = ProjectionUtils.CameraToPlaneProjection(oldScreenPos1, projectionCamera, WorldTransformPlane);
            var oldWorldPos2  = ProjectionUtils.CameraToPlaneProjection(oldScreenPos2, projectionCamera, WorldTransformPlane);
            var newWorldPos1  = ProjectionUtils.CameraToPlaneProjection(newScreenPos1, projectionCamera, WorldTransformPlane);
            var newWorldPos2  = ProjectionUtils.CameraToPlaneProjection(newScreenPos2, projectionCamera, WorldTransformPlane);
            var newVector     = newWorldPos2 - newWorldPos1;

            Vector2 oldScreenCenter = (oldScreenPos1 + oldScreenPos2) * .5f;
            Vector2 newScreenCenter = (newScreenPos1 + newScreenPos2) * .5f;

            if (isScaling)
            {
                deltaScale = newVector.magnitude / Vector3.Distance(oldWorldPos2, oldWorldPos1);
            }
            else
            {
                var oldScreenDistance   = Vector2.Distance(oldScreenPos1, oldScreenPos2);
                var newScreenDistance   = newScreenDelta.magnitude;
                var screenDeltaDistance = newScreenDistance - oldScreenDistance;
                scalingBuffer += screenDeltaDistance;
                var dpiScalingThreshold = ScalingThreshold * touchManager.DotsPerCentimeter;
                if (scalingBuffer * scalingBuffer >= dpiScalingThreshold * dpiScalingThreshold)
                {
                    isScaling = true;
                    var oldScreenDirection = (oldScreenPos2 - oldScreenPos1).normalized;
                    var startScale         = (newScreenDistance - scalingBuffer) * .5f;
                    var startVector        = oldScreenDirection * startScale;
                    deltaScale = newVector.magnitude / (ProjectionUtils.CameraToPlaneProjection(oldScreenCenter + startVector, projectionCamera, WorldTransformPlane) - ProjectionUtils.CameraToPlaneProjection(oldScreenCenter - startVector, projectionCamera, WorldTransformPlane)).magnitude;
                }
            }

            oldWorldCenter = ProjectionUtils.CameraToPlaneProjection(oldScreenCenter, projectionCamera, WorldTransformPlane);
            newWorldCenter = ProjectionUtils.CameraToPlaneProjection(newScreenCenter, projectionCamera, WorldTransformPlane);

            if (Mathf.Abs(deltaScale - 1f) > 0.00001)
            {
                switch (State)
                {
                case GestureState.Possible:
                case GestureState.Began:
                case GestureState.Changed:
                    screenPosition               = newScreenCenter;
                    previousScreenPosition       = oldScreenCenter;
                    PreviousWorldTransformCenter = oldWorldCenter;
                    WorldTransformCenter         = newWorldCenter;

                    LocalDeltaScale = deltaScale;

                    if (State == GestureState.Possible)
                    {
                        setState(GestureState.Began);
                    }
                    else
                    {
                        setState(GestureState.Changed);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        protected override void touchesMoved(IList <TouchPoint> touches)
        {
            base.touchesMoved(touches);

            if (!gotEnoughTouchPoints())
            {
                return;
            }

            Vector3 oldGlobalCenter3DPos, oldLocalCenter3DPos, newGlobalCenter3DPos, newLocalCenter3DPos;
            var     deltaScale = 1f;

            var old2DPos1 = getPointPreviousScreenPosition(0);
            var old2DPos2 = getPointPreviousScreenPosition(1);
            var new2DPos1 = getPointScreenPosition(0);
            var new2DPos2 = getPointScreenPosition(1);
            var old3DPos1 = ProjectionUtils.CameraToPlaneProjection(old2DPos1, projectionCamera, WorldTransformPlane);
            var old3DPos2 = ProjectionUtils.CameraToPlaneProjection(old2DPos2, projectionCamera, WorldTransformPlane);
            var new3DPos1 = ProjectionUtils.CameraToPlaneProjection(new2DPos1, projectionCamera, WorldTransformPlane);
            var new3DPos2 = ProjectionUtils.CameraToPlaneProjection(new2DPos2, projectionCamera, WorldTransformPlane);
            var newVector = new3DPos2 - new3DPos1;

            Vector2 oldCenter2DPos = (old2DPos1 + old2DPos2) * .5f;
            Vector2 newCenter2DPos = (new2DPos1 + new2DPos2) * .5f;

            if (isScaling)
            {
                deltaScale = newVector.magnitude / Vector3.Distance(old3DPos2, old3DPos1);
            }
            else
            {
                var old2DDist   = Vector2.Distance(old2DPos1, old2DPos2);
                var new2DDist   = Vector2.Distance(new2DPos1, new2DPos2);
                var delta2DDist = new2DDist - old2DDist;
                scalingBuffer += delta2DDist;
                var dpiScalingThreshold = ScalingThreshold * manager.DotsPerCentimeter;
                if (scalingBuffer * scalingBuffer >= dpiScalingThreshold * dpiScalingThreshold)
                {
                    isScaling = true;
                    var oldVector2D = (old2DPos2 - old2DPos1).normalized;
                    var startScale  = (new2DDist - scalingBuffer) * .5f;
                    var startVector = oldVector2D * startScale;
                    deltaScale = newVector.magnitude / (ProjectionUtils.CameraToPlaneProjection(oldCenter2DPos + startVector, projectionCamera, WorldTransformPlane) - ProjectionUtils.CameraToPlaneProjection(oldCenter2DPos - startVector, projectionCamera, WorldTransformPlane)).magnitude;
                }
            }

            oldGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(oldCenter2DPos, projectionCamera, WorldTransformPlane);
            newGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(newCenter2DPos, projectionCamera, WorldTransformPlane);
            oldLocalCenter3DPos  = globalToLocalPosition(oldGlobalCenter3DPos);
            newLocalCenter3DPos  = globalToLocalPosition(newGlobalCenter3DPos);

            if (Mathf.Abs(deltaScale - 1f) > 0.00001)
            {
                switch (State)
                {
                case GestureState.Possible:
                case GestureState.Began:
                case GestureState.Changed:
                    screenPosition               = newCenter2DPos;
                    previousScreenPosition       = oldCenter2DPos;
                    PreviousWorldTransformCenter = oldGlobalCenter3DPos;
                    WorldTransformCenter         = newGlobalCenter3DPos;
                    PreviousWorldTransformCenter = oldGlobalCenter3DPos;
                    LocalTransformCenter         = newLocalCenter3DPos;
                    PreviousLocalTransformCenter = oldLocalCenter3DPos;

                    LocalDeltaScale = deltaScale;

                    if (State == GestureState.Possible)
                    {
                        setState(GestureState.Began);
                    }
                    else
                    {
                        setState(GestureState.Changed);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 9
0
 /// <inheritdoc />
 public override Vector3 ProjectTo(Vector2 screenPosition, Plane projectionPlane)
 {
     return(ProjectionUtils.CameraToPlaneProjection(screenPosition, camera, projectionPlane));
 }
Ejemplo n.º 10
0
        /// <inheritdoc />
        protected override void touchesMoved(IList <TouchPoint> touches)
        {
            if (!gotEnoughTouchPoints())
            {
                return;
            }
            if (!relevantTouchPoints(touches))
            {
                return;
            }

            Vector3 oldGlobalCenter3DPos, oldLocalCenter3DPos, newGlobalCenter3DPos, newLocalCenter3DPos;
            var     deltaRotation = 0f;

            var new2DPos1 = getPointScreenPosition(0);
            var new2DPos2 = getPointScreenPosition(1);

            if (Vector2.Distance(new2DPos1, new2DPos2) < minPointsDistanceInPixels)
            {
                return;
            }

            base.touchesMoved(touches);

            var old2DPos1 = getPointPreviousScreenPosition(0);
            var old2DPos2 = getPointPreviousScreenPosition(1);
            var old3DPos1 = ProjectionUtils.CameraToPlaneProjection(old2DPos1, projectionCamera, WorldTransformPlane);
            var old3DPos2 = ProjectionUtils.CameraToPlaneProjection(old2DPos2, projectionCamera, WorldTransformPlane);
            var new3DPos1 = ProjectionUtils.CameraToPlaneProjection(new2DPos1, projectionCamera, WorldTransformPlane);
            var new3DPos2 = ProjectionUtils.CameraToPlaneProjection(new2DPos2, projectionCamera, WorldTransformPlane);
            var newVector = new3DPos2 - new3DPos1;
            var oldVector = old3DPos2 - old3DPos1;

            Vector2 oldCenter2DPos = (old2DPos1 + old2DPos2) * .5f;
            Vector2 newCenter2DPos = (new2DPos1 + new2DPos2) * .5f;

            var angle = Vector3.Angle(oldVector, newVector);

            if (Vector3.Dot(Vector3.Cross(oldVector, newVector), WorldTransformPlane.normal) < 0)
            {
                angle = -angle;
            }
            if (isRotating)
            {
                deltaRotation = angle;
            }
            else
            {
                rotationBuffer += angle;
                if (rotationBuffer * rotationBuffer >= RotationThreshold * RotationThreshold)
                {
                    isRotating    = true;
                    deltaRotation = rotationBuffer;
                }
            }

            oldGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(oldCenter2DPos, projectionCamera, WorldTransformPlane);
            newGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(newCenter2DPos, projectionCamera, WorldTransformPlane);
            oldLocalCenter3DPos  = globalToLocalPosition(oldGlobalCenter3DPos);
            newLocalCenter3DPos  = globalToLocalPosition(newGlobalCenter3DPos);

            if (Math.Abs(deltaRotation) > 0.00001)
            {
                switch (State)
                {
                case GestureState.Possible:
                case GestureState.Began:
                case GestureState.Changed:
                    screenPosition               = newCenter2DPos;
                    previousScreenPosition       = oldCenter2DPos;
                    PreviousWorldTransformCenter = oldGlobalCenter3DPos;
                    WorldTransformCenter         = newGlobalCenter3DPos;
                    PreviousWorldTransformCenter = oldGlobalCenter3DPos;
                    LocalTransformCenter         = newLocalCenter3DPos;
                    PreviousLocalTransformCenter = oldLocalCenter3DPos;

                    LocalDeltaRotation = deltaRotation;

                    if (State == GestureState.Possible)
                    {
                        setState(GestureState.Began);
                    }
                    else
                    {
                        setState(GestureState.Changed);
                    }
                    break;
                }
            }
        }
Ejemplo n.º 11
0
        /// <inheritdoc />
        protected override void touchesMoved(IList <TouchPoint> touches)
        {
            base.touchesMoved(touches);

            var     globalDelta3DPos = Vector3.zero;
            var     localDelta3DPos = Vector3.zero;
            Vector3 oldGlobalCenter3DPos, oldLocalCenter3DPos, newGlobalCenter3DPos, newLocalCenter3DPos;

            Vector2 oldCenter2DPos = PreviousScreenPosition;
            Vector2 newCenter2DPos = ScreenPosition;

            if (isMoving)
            {
                oldGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(oldCenter2DPos, projectionCamera, WorldTransformPlane);
                newGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(newCenter2DPos, projectionCamera, WorldTransformPlane);
                globalDelta3DPos     = newGlobalCenter3DPos - oldGlobalCenter3DPos;
                oldLocalCenter3DPos  = globalToLocalPosition(oldGlobalCenter3DPos);
                newLocalCenter3DPos  = globalToLocalPosition(newGlobalCenter3DPos);
                localDelta3DPos      = newLocalCenter3DPos - globalToLocalPosition(oldGlobalCenter3DPos);
            }
            else
            {
                movementBuffer += newCenter2DPos - oldCenter2DPos;
                var dpiMovementThreshold = MovementThreshold * manager.DotsPerCentimeter;
                if (movementBuffer.sqrMagnitude > dpiMovementThreshold * dpiMovementThreshold)
                {
                    isMoving             = true;
                    oldGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(oldCenter2DPos - movementBuffer, projectionCamera, WorldTransformPlane);
                    newGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(newCenter2DPos, projectionCamera, WorldTransformPlane);
                    globalDelta3DPos     = newGlobalCenter3DPos - oldGlobalCenter3DPos;
                    oldLocalCenter3DPos  = globalToLocalPosition(oldGlobalCenter3DPos);
                    newLocalCenter3DPos  = globalToLocalPosition(newGlobalCenter3DPos);
                    localDelta3DPos      = newLocalCenter3DPos - globalToLocalPosition(oldGlobalCenter3DPos);
                }
                else
                {
                    newGlobalCenter3DPos = ProjectionUtils.CameraToPlaneProjection(newCenter2DPos - movementBuffer, projectionCamera, WorldTransformPlane);
                    newLocalCenter3DPos  = globalToLocalPosition(newGlobalCenter3DPos);
                    oldGlobalCenter3DPos = newGlobalCenter3DPos;
                    oldLocalCenter3DPos  = newLocalCenter3DPos;
                }
            }

            if (globalDelta3DPos != Vector3.zero)
            {
                switch (State)
                {
                case GestureState.Possible:
                case GestureState.Began:
                case GestureState.Changed:
                    PreviousWorldTransformCenter = oldGlobalCenter3DPos;
                    WorldTransformCenter         = newGlobalCenter3DPos;
                    WorldDeltaPosition           = globalDelta3DPos;
                    PreviousWorldTransformCenter = oldGlobalCenter3DPos;
                    LocalTransformCenter         = newLocalCenter3DPos;
                    LocalDeltaPosition           = localDelta3DPos;
                    PreviousLocalTransformCenter = oldLocalCenter3DPos;

                    if (State == GestureState.Possible)
                    {
                        setState(GestureState.Began);
                    }
                    else
                    {
                        setState(GestureState.Changed);
                    }
                    break;
                }
            }
        }