chenxin
2020-11-26 392f839c8ddff781479e3383d9b1fd99c8ed663c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using Core.Camera;
using UnityEngine;
 
namespace Core.Input
{
    /// <summary>
    /// Abstract base input scheme for schemes that control the CameraRig
    /// </summary>
    public abstract class CameraInputScheme : InputScheme
    {
        /// <summary>
        /// Camera rig to control
        /// </summary>
        public CameraRig cameraRig;
 
        /// <summary>
        /// Pan speed factor when fully zoomed-in
        /// </summary>
        public float nearZoomPanSpeedModifier = 0.2f;
 
        /// <summary>
        /// Gets our pan speed multiplier for the given zoom level
        /// </summary>
        /// <returns></returns>
        protected float GetPanSpeedForZoomLevel()
        {
            return cameraRig != null ? 
                Mathf.Lerp(nearZoomPanSpeedModifier, 1, cameraRig.CalculateZoomRatio()) : 
                1.0f;
        }
 
        /// <summary>
        /// Do screen edge panning with the given screen coordinates
        /// </summary>
        /// <param name="screenPosition">The screen position of the cursor panning the camera</param>
        /// <param name="screenEdgeThreshold">The screen edge threshold in pixels</param>
        /// <param name="panSpeed">Speed of panning</param>
        protected void PanWithScreenCoordinates(Vector2 screenPosition, float screenEdgeThreshold, float panSpeed)
        {
            // Calculate zoom ratio
            float zoomRatio = GetPanSpeedForZoomLevel();
 
            // Left
            if ((screenPosition.x < screenEdgeThreshold))
            {
                float panAmount = (screenEdgeThreshold - screenPosition.x) / screenEdgeThreshold;
                panAmount = Mathf.Clamp01(Mathf.Log(panAmount) + 1);
 
                if (cameraRig.trackingObject == null)
                {
                    cameraRig.PanCamera(Vector3.left * Time.deltaTime * panSpeed * panAmount * zoomRatio);
 
                    cameraRig.StopTracking();
                }
            }
 
            // Right
            if ((screenPosition.x > Screen.width - screenEdgeThreshold))
            {
                float panAmount = ((screenEdgeThreshold - Screen.width) + screenPosition.x) / screenEdgeThreshold;
                panAmount = Mathf.Clamp01(Mathf.Log(panAmount) + 1);
 
                if (cameraRig.trackingObject == null)
                {
                    cameraRig.PanCamera(Vector3.right * Time.deltaTime * panSpeed * panAmount * zoomRatio);
                }
                cameraRig.StopTracking();
            }
 
            // Down
            if ((screenPosition.y < screenEdgeThreshold))
            {
                float panAmount = (screenEdgeThreshold - screenPosition.y) / screenEdgeThreshold;
                panAmount = Mathf.Clamp01(Mathf.Log(panAmount) + 1);
 
                if (cameraRig.trackingObject == null)
                {
                    cameraRig.PanCamera(Vector3.back * Time.deltaTime * panSpeed * panAmount * zoomRatio);
 
                    cameraRig.StopTracking();
                }
            }
 
            // Up
            if ((screenPosition.y > Screen.height - screenEdgeThreshold))
            {
                float panAmount = ((screenEdgeThreshold - Screen.height) + screenPosition.y) / screenEdgeThreshold;
                panAmount = Mathf.Clamp01(Mathf.Log(panAmount) + 1);
 
                if (cameraRig.trackingObject == null)
                {
                    cameraRig.PanCamera(Vector3.forward * Time.deltaTime * panSpeed * panAmount * zoomRatio);
 
                    cameraRig.StopTracking();
                }
            }
        }
    }
}