-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoverControls.js
145 lines (106 loc) · 4.31 KB
/
HoverControls.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Poorly Coded by jerome.
* Date: 08/12/2011
*
* This is a JS port of the Away3D 3.6 HoverCamera Class that I had ported to Away3D 4!
* It loosely follows the Controls already present in the Three.js library.
*
*/
THREE.HoverControls = function (object, domElement) {
// API
this.object = object;
this.domElement = ( domElement !== undefined ) ? domElement : document;
this.panAngle = 0;
this.tiltAngle = 0;
this.distance = 1;
this.minTiltAngle = -90;
this.maxTiltAngle = 90;
this.minPanAngle = -Infinity;
this.maxPanAngle = Infinity;
this.steps = 10;
this.yfactor = 2;
this.wrapPanAngle = false;
this.target = new THREE.Vector3(0, 0, 0);
this.enabled = true;
// internals
var _this = this;
var _currentPanAngle = 0;
var _currentTiltAngle = 0;
var _cameraSpeed = 0.2;
var _lastMouseX = 0;
var _lastMouseY = 0;
var _lastPanAngle = 0;
var _lastTiltAngle = 0;
var _move = false;
var _toRadian = Math.PI/180;
this.update = function(jumpTo) {
if (!_this.enabled) return;
jumpTo = jumpTo || false;
if (_this.tiltAngle != _currentTiltAngle || _this.panAngle != _currentPanAngle) {
_this.tiltAngle = Math.max(_this.minTiltAngle, Math.min(_this.maxTiltAngle, _this.tiltAngle));
_this.panAngle = Math.max(_this.minPanAngle, Math.min(_this.maxPanAngle, _this.panAngle));
if (_this.wrapPanAngle) {
if (_this.panAngle < 0)
_this.panAngle = (_this.panAngle % 360) + 360;
else
_this.panAngle = _this.panAngle % 360;
if (_this.panAngle - _currentPanAngle < -180)
_this.panAngle += 360;
else if (_this.panAngle - _currentPanAngle > 180)
_this.panAngle -= 360;
}
if (jumpTo) {
_currentTiltAngle = _this.tiltAngle;
_currentPanAngle = _this.panAngle;
} else {
_currentTiltAngle += (_this.tiltAngle - _currentTiltAngle) / (_this.steps + 1);
_currentPanAngle += (_this.panAngle - _currentPanAngle) / (_this.steps + 1);
}
//snap coords if angle differences are close
if ((Math.abs(_this.tiltAngle - _currentTiltAngle) < 0.001) && (Math.abs(_this.panAngle - _currentPanAngle) < 0.001)) {
_currentTiltAngle = _this.tiltAngle;
_currentPanAngle = _this.panAngle;
}
}
var gx = _this.target.x + _this.distance * Math.sin(_currentPanAngle * _toRadian) * Math.cos(_currentTiltAngle * _toRadian);
var gz = _this.target.z + _this.distance * Math.cos(_currentPanAngle * _toRadian) * Math.cos(_currentTiltAngle * _toRadian);
var gy = _this.target.y + _this.distance * Math.sin(_currentTiltAngle* _toRadian) * _this.yfactor;
if ((_this.object.x == gx) && (_this.object.y == gy) && (_this.object.z == gz))
return false;
_this.object.position.x = gx;
_this.object.position.y = gy;
_this.object.position.z = gz;
_this.object.lookAt(_this.target);
console.log(_this.tiltAngle, this.panAngle);
return true;
};
function mousedown(event) {
if (!_this.enabled) return;
event.preventDefault();
event.stopPropagation();
_lastPanAngle = _this.panAngle;
_lastTiltAngle = _this.tiltAngle;
_lastMouseX = event.clientX;
_lastMouseY = event.clientY;
_move = true;
}
function mouseup(event) {
if (!_this.enabled) return;
event.preventDefault();
event.stopPropagation();
_move = false;
}
function mousemove(event) {
if (!_this.enabled) return;
if (_move) {
_this.panAngle = _cameraSpeed * (event.clientX - _lastMouseX) + _lastPanAngle;
_this.tiltAngle = _cameraSpeed * (_lastMouseY - event.clientY) + _lastTiltAngle;
}
}
this.domElement.addEventListener('contextmenu', function (event) {
event.preventDefault();
}, false);
this.domElement.addEventListener('mousemove', mousemove, false);
this.domElement.addEventListener('mousedown', mousedown, false);
this.domElement.addEventListener('mouseup', mouseup, false);
};