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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
// Usage:
//
// var control = new MX.RotationControl()
// control.init( object{MX.Object3D} [, listener{HTMLElement}] )
//
// In animation loop:
//
// control.update()
//
// The above code will register handler functions on `listener`
// and will be updating `object`s rotationX and rotationY
// If no `listener` is provided, will default to `object`s el.
MX.RotationControl = function () {
var object,
locked = false
var down = false,
active = false,
lastX,
lastY
var pointerLockPrefix =
'pointerLockElement' in document ? '' :
'mozPointerLockElement' in document ? 'moz' :
'webkitPointerLockElement' in document ? 'webkit' : null,
hasPointerLock = !(pointerLockPrefix === null)
pointerLockEnabled = false
var pub = {
sensitivity : .5,
ease : 10,
drag : true,
inverseX : false,
inverseY : false,
disableX : false,
disableY : false,
rotationX : 0,
rotationY : 0,
upperBoundX : undefined,
lowerBoundX : undefined,
upperBoundY : undefined,
lowerBoundY : undefined,
usePreset: function (name) {
var ops = presets[name]
if (ops) {
if (currentPreset && presets[currentPreset].teardown) {
presets[currentPreset].teardown()
}
for (var op in ops) {
if (op !== 'setup' && op !== 'teardown') {
pub[op] = ops[op]
}
}
if (op.setup) ops.setup()
}
}
}
var currentPreset
var presets = {
firstPerson: {
drag: false,
ease: 2,
sensitivity: .18,
inverseX: true,
inverseY: true,
upperBoundX: MX.rotationUnit === 'deg' ? 90 : Math.PI / 2,
lowerBoundX: MX.rotationUnit === 'deg' ? -90 : -Math.PI / 2
},
skybox: {
sensitivity: .18,
inverseX: true,
inverseY: true,
upperBoundX: MX.rotationUnit === 'deg' ? 90 : Math.PI / 2,
lowerBoundX: MX.rotationUnit === 'deg' ? -90 : -Math.PI / 2
}
}
function init (obj, lis) {
if (active) return
object = obj
pub.rotationX = object.rotationX
pub.rotationY = object.rotationY
if (lis instanceof HTMLElement) {
listener = lis
} else if (lis instanceof MX.Object3D) {
listener = lis.el
} else {
listener = window.document
}
listener.addEventListener('mousedown', onDown)
listener.addEventListener('mousemove', onMove)
listener.addEventListener('mouseup', onUp)
listener.addEventListener('touchstart', onDown)
listener.addEventListener('touchmove', onMove)
listener.addEventListener('touchend', onUp)
active = true
}
function changeObject (obj) {
object = obj
pub.rotationX = object.rotationX
pub.rotationY = object.rotationY
}
function changeListener (lis) {
remove()
active = false
init(object, lis)
if (pointerLockEnabled) {
initPointerLock()
}
}
function remove () {
if (!active) return
listener.removeEventListener('mousedown', onDown)
listener.removeEventListener('mousemove', onMove)
listener.removeEventListener('mouseup', onUp)
listener.removeEventListener('touchstart', onDown)
listener.removeEventListener('touchmove', onMove)
listener.removeEventListener('touchend', onUp)
if (hasPointerLock) {
document.removeEventListener(pointerLockPrefix + 'pointerlockchange', onPointerLockChange)
document.removeEventListener('mousemove', onPointerLockMove)
document.body[pointerLockPrefix + (pointerLockPrefix ? 'E' : 'e') + 'xitPointerLock']()
}
active = false
}
function onDown (e) {
e = normalizeEvent(e)
if (!e) return
down = true
lastX = e.pageX
lastY = e.pageY
}
function onMove (e) {
if (app.dragging) return;
if (e.type = 'touchmove') {
e.preventDefault()
}
if (pub.drag && !down) return
e = normalizeEvent(e)
if (!e) return
lastX = lastX || e.pageX
lastY = lastY || e.pageY
var dx = e.pageX - lastX,
dy = e.pageY - lastY
lastX = e.pageX
lastY = e.pageY
updateTarget(dx, dy)
}
function onUp () {
app.dragging = down = false
}
function initPointerLock () {
if (pointerLockEnabled) return
document.addEventListener(pointerLockPrefix + 'pointerlockchange', onPointerLockChange)
document.addEventListener('mousemove', onPointerLockMove)
document.body[pointerLockPrefix + (pointerLockPrefix ? 'R' : 'r') + 'equestPointerLock']()
}
function onPointerLockChange () {
var el = document.body
if (document[pointerLockPrefix + (pointerLockPrefix ? 'P' : 'p') + 'ointerLockElement'] === el) {
pointerLockEnabled = true
} else {
pointerLockEnabled = false
}
}
function onPointerLockMove (e) {
if (!pointerLockEnabled) return
var dx = e[pointerLockPrefix + (pointerLockPrefix ? 'M' : 'm') + 'ovementX'],
dy = e[pointerLockPrefix + (pointerLockPrefix ? 'M' : 'm') + 'ovementY']
updateTarget(dx, dy)
}
function normalizeEvent (e) {
if (e.touches) {
return e.touches.length > 1 ? false : e.touches[0]
} else {
return e
}
}
function updateTarget (dx, dy) {
if (pub.inverseX) dx = -dx
if (pub.inverseY) dy = -dy
if (MX.rotationUnit !== 'deg') {
dx = MX.toRad(dx)
dy = MX.toRad(dy)
}
if (!pub.disableX) {
pub.rotationX -= dy * pub.sensitivity
if (pub.upperBoundX) pub.rotationX = Math.min(pub.rotationX, pub.upperBoundX)
if (pub.lowerBoundX) pub.rotationX = Math.max(pub.rotationX, pub.lowerBoundX)
}
if (!pub.disableY) {
pub.rotationY += dx * pub.sensitivity
if (pub.upperBoundY) pub.rotationY = Math.min(pub.rotationY, pub.upperBoundY)
if (pub.lowerBoundY) pub.rotationY = Math.max(pub.rotationY, pub.lowerBoundY)
}
}
function update () {
if (!object || locked) return
var dx = pub.rotationX - object.rotationX,
dy = pub.rotationY - object.rotationY
if (Math.abs(dx) < 0.0001) {
object.rotationX = pub.rotationX
} else {
object.rotationX += dx / pub.ease
}
if (Math.abs(dy) < 0.0001) {
object.rotationY = pub.rotationY
} else {
object.rotationY += dy / pub.ease
}
}
function lock () {
locked = true
}
function unlock () {
pub.rotationX = object.rotationX
pub.rotationY = object.rotationY
locked = false
}
pub.init = init
pub.remove = remove
pub.update = update
pub.lock = lock
pub.unlock = unlock
pub.initPointerLock = initPointerLock
pub.changeObject = changeObject
pub.changeListener = changeListener
return pub
}
|