-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanNavigationManager.cs
113 lines (88 loc) · 4.26 KB
/
PanNavigationManager.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PanNavigatioManager
{
Vector3 initialPlanePosition;
Vector3 extentsVector;
Vector2 fielOfViewVector;
float cameraMinimumDistance;
public void Init(Vector3 _extentsVector, /*Vector2 _fielOfViewVector,*/ float _cameraMinimumDistance, Vector3 _currentPlanePosition)
{
this.extentsVector = _extentsVector;
//this.fielOfViewVector = _fielOfViewVector;
this.cameraMinimumDistance = _cameraMinimumDistance;
this.initialPlanePosition = _currentPlanePosition;
}
public Vector3 CalculatePlanePosition(float pseudoMouseMovementX, float pseudoMouseMovementY, Vector3 currentPlanePosition, Vector2 _fielOfViewVector)
{
Vector3 newPlanePosition;
// Update field of view
this.fielOfViewVector = _fielOfViewVector;
///////
// X
///////
//Get camera X axis without rotation
Vector3 directionXVector = -1.0f * Camera.main.transform.right;// - new Vector3(Camera.main.transform.localRotation.x, 0, 0);
//Camera movement must be in the opposite direction that the mouse with an offset
Vector3 incrementXVector = directionXVector * pseudoMouseMovementX * CalculateCorrectionParameterX();
///////
// Y
///////
Vector3 directionYVector = -1.0f * Camera.main.transform.up;// - new Vector3(0, Camera.main.transform.localRotation.y, 0);
//Get camera X axis without rotation
Vector3 incrementYVector = directionYVector * pseudoMouseMovementY * CalculateCorrectionParameterY();
//Move plane
newPlanePosition = currentPlanePosition + incrementXVector + incrementYVector;
newPlanePosition = CalculateLimitPosition(currentPlanePosition, newPlanePosition);
return newPlanePosition;
}
public Vector3 ResetPlanePosition()
{
return initialPlanePosition;
}
private Vector3 CalculateLimitPosition(Vector3 _currentPlanePosition, Vector3 _newPlanePositionCandidate)
{
Vector3 newPlanePosition = _newPlanePositionCandidate;
Vector3 limit1 = initialPlanePosition - extentsVector;
Vector3 limit2 = initialPlanePosition + extentsVector;
if ((newPlanePosition.x < limit1.x) || (newPlanePosition.x > limit2.x)) { newPlanePosition.x = _currentPlanePosition.x; }
if ((newPlanePosition.y < limit1.y) || (newPlanePosition.y > limit2.y)) { newPlanePosition.y = _currentPlanePosition.y; }
if ((newPlanePosition.z < limit1.z) || (newPlanePosition.z > limit2.z)) { newPlanePosition.z = _currentPlanePosition.z; }
return newPlanePosition;
}
private float CalculateCorrectionParameterX()
{
float k;
Vector3 cameraPositionInPlane = Camera.main.transform.position;
float inscribedCircunfereRadius = GetInscribedCircunferenceRadiousX();
//
float distanceObject = cameraPositionInPlane.magnitude - inscribedCircunfereRadius; // One of this should work
k = 2 * distanceObject * Mathf.Tan(fielOfViewVector.x);
// When there are no limit in the maximun zoom I need to avoid a too low interaction speed.
// when the camera is to close to the object k is to low
if (k < 10) { k = 10f; } // TODO this is a magic number
//Debug.Log(k);
return k;
}
private float CalculateCorrectionParameterY()
{
float k;
Vector3 cameraPositionInPlane = Camera.main.transform.position;
float inscribedCircunfereRadius = GetInscribedCircunferenceRadiousY();
float distanceObject = cameraPositionInPlane.magnitude - inscribedCircunfereRadius; // One of this should work
k = 2 * distanceObject * Mathf.Tan(fielOfViewVector.y);
if (k < 10) { k = 10f; }
return k;
}
private float GetInscribedCircunferenceRadiousX()
{
float rMin = MathHom3r.Max(extentsVector.x, extentsVector.z);
return rMin;
}
private float GetInscribedCircunferenceRadiousY()
{
float rMin = MathHom3r.Max(extentsVector.y, extentsVector.z);
return rMin;
}
}