-
Notifications
You must be signed in to change notification settings - Fork 3
/
orbiter.js
289 lines (259 loc) · 8.43 KB
/
orbiter.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
'use strict'
const vec3 = require('pex-math/vec3')
const mat4 = require('pex-math/mat4')
const ray = require('pex-geom/ray')
const clamp = require('pex-math/utils').clamp
const raf = require('raf')
const interpolateAngle = require('interpolate-angle')
const lerp = require('pex-math/utils').lerp
const toRadians = require('pex-math/utils').toRadians
const toDegrees = require('pex-math/utils').toDegrees
const latLonToXyz = require('latlon-to-xyz')
const xyzToLatLon = require('xyz-to-latlon')
const eventOffset = require('mouse-event-offset')
function offset (e, target) {
if (e.touches) return eventOffset(e.touches[0], target)
else return eventOffset(e, target)
}
function Orbiter (opts) {
// TODO: split into internal state and public state
const initialState = {
camera: opts.camera,
invViewMatrix: mat4.create(),
dragging: false,
lat: 0, // Y
minLat: -89.5,
maxLat: 89.5,
lon: 0, // XZ
minLon: -Infinity,
maxLon: Infinity,
currentLat: 0,
currentLon: 0,
easing: 1,
element: opts.element || window,
width: 0,
height: 0,
clickPosWindow: [0, 0],
dragPos: [0, 0, 0],
dragPosWindow: [0, 0],
distance: 1,
currentDistance: 1,
minDistance: 1,
maxDistance: 1,
zoomSlowdown: 400,
zoom: true,
pan: true,
drag: true,
dragSlowdown: 4,
clickTarget: [0, 0, 0],
clickPosPlane: [0, 0, 0],
dragPosPlane: [0, 0, 0],
clickPosWorld: [0, 0, 0],
dragPosWorld: [0, 0, 0],
panPlane: null,
autoUpdate: true
}
this.set(initialState)
this.set(opts)
this.setup()
}
Orbiter.prototype.set = function (opts) {
if (opts.camera) {
const distance = vec3.distance(opts.camera.position, opts.camera.target)
const latLon = xyzToLatLon(vec3.normalize(vec3.sub(vec3.copy(opts.camera.position), opts.camera.target)))
this.lat = latLon[0]
this.lon = latLon[1]
this.currentLat = this.lat
this.currentLon = this.lon
this.distance = distance
this.currentDistance = this.distance
this.minDistance = opts.minDistance || distance / 10
this.maxDistance = opts.maxDistance || distance * 10
}
Object.assign(this, opts)
}
Orbiter.prototype.updateWindowSize = function () {
const width = this.element.clientWidth || this.element.innerWidth
const height = this.element.clientHeight || this.element.innerHeight
if (width !== this.width) {
this.width = width
this.height = height
this.radius = Math.min(this.width / 2, this.height / 2)
}
}
Orbiter.prototype.updateCamera = function () {
// instad of rotating the object we want to move camera around it
// state.currRot[3] *= -1
if (!this.camera) return
const position = this.camera.position
const target = this.camera.target
this.lat = clamp(this.lat, this.minLat, this.maxLat)
this.lon = clamp(this.lon, this.minLon, this.maxLon) % 360
this.currentLat = toDegrees(
interpolateAngle(
(toRadians(this.currentLat) + 2 * Math.PI) % (2 * Math.PI),
(toRadians(this.lat) + 2 * Math.PI) % (2 * Math.PI),
this.easing
)
)
this.currentLon = toDegrees(
interpolateAngle(
(toRadians(this.currentLon) + 2 * Math.PI) % (2 * Math.PI),
(toRadians(this.lon) + 2 * Math.PI) % (2 * Math.PI),
this.easing
)
)
this.currentDistance = lerp(this.currentDistance, this.distance, this.easing)
// set new camera position according to the current
// rotation at distance relative to target
latLonToXyz(this.currentLat, this.currentLon, position)
vec3.scale(position, this.currentDistance)
vec3.add(position, target)
this.camera.set({
position: position
})
}
Orbiter.prototype.setup = function () {
var orbiter = this
function down (x, y, shift) {
orbiter.dragging = true
orbiter.dragPos[0] = x
orbiter.dragPos[1] = y
if (shift && orbiter.pan) {
orbiter.clickPosWindow[0] = x
orbiter.clickPosWindow[1] = y
vec3.set(orbiter.clickTarget, orbiter.camera.target)
const targetInViewSpace = vec3.multMat4(vec3.copy(orbiter.clickTarget), orbiter.camera.viewMatrix)
orbiter.panPlane = [targetInViewSpace, [0, 0, 1]]
ray.hitTestPlane(
orbiter.camera.getViewRay(orbiter.clickPosWindow[0], orbiter.clickPosWindow[1], orbiter.width, orbiter.height),
orbiter.panPlane[0],
orbiter.panPlane[1],
orbiter.clickPosPlane
)
ray.hitTestPlane(
orbiter.camera.getViewRay(orbiter.dragPosWindow[0], orbiter.dragPosWindow[1], orbiter.width, orbiter.height),
orbiter.panPlane[0],
orbiter.panPlane[1],
orbiter.dragPosPlane
)
} else {
orbiter.panPlane = null
}
}
function move (x, y, shift) {
if (!orbiter.dragging) {
return
}
if (shift && orbiter.panPlane) {
orbiter.dragPosWindow[0] = x
orbiter.dragPosWindow[1] = y
ray.hitTestPlane(
orbiter.camera.getViewRay(orbiter.clickPosWindow[0], orbiter.clickPosWindow[1], orbiter.width, orbiter.height),
orbiter.panPlane[0],
orbiter.panPlane[1],
orbiter.clickPosPlane
)
ray.hitTestPlane(
orbiter.camera.getViewRay(orbiter.dragPosWindow[0], orbiter.dragPosWindow[1], orbiter.width, orbiter.height),
orbiter.panPlane[0],
orbiter.panPlane[1],
orbiter.dragPosPlane
)
mat4.set(orbiter.invViewMatrix, orbiter.camera.viewMatrix)
mat4.invert(orbiter.invViewMatrix)
vec3.multMat4(vec3.set(orbiter.clickPosWorld, orbiter.clickPosPlane), orbiter.invViewMatrix)
vec3.multMat4(vec3.set(orbiter.dragPosWorld, orbiter.dragPosPlane), orbiter.invViewMatrix)
const diffWorld = vec3.sub(vec3.copy(orbiter.dragPosWorld), orbiter.clickPosWorld)
const target = vec3.sub(vec3.copy(orbiter.clickTarget), diffWorld)
orbiter.camera.set({ target: target })
orbiter.updateCamera()
} else if (orbiter.drag) {
const dx = x - orbiter.dragPos[0]
const dy = y - orbiter.dragPos[1]
orbiter.dragPos[0] = x
orbiter.dragPos[1] = y
orbiter.lat += dy / orbiter.dragSlowdown
orbiter.lon -= dx / orbiter.dragSlowdown
// TODO: how to have resolution independed scaling? will this code behave differently with retina/pixelRatio=2?
orbiter.updateCamera()
}
}
function up () {
orbiter.dragging = false
orbiter.panPlane = null
}
function scroll (dy) {
if (!orbiter.zoom) {
return false
}
orbiter.distance *= 1 + dy / orbiter.zoomSlowdown
orbiter.distance = clamp(orbiter.distance, orbiter.minDistance, orbiter.maxDistance)
orbiter.updateCamera()
return true
}
function onMouseDown (e) {
orbiter.updateWindowSize()
const pos = offset(e, orbiter.element)
down(
pos[0],
pos[1],
e.shiftKey || (e.touches && e.touches.length === 2)
)
}
function onMouseMove (e) {
const pos = offset(e, orbiter.element)
move(
pos[0],
pos[1],
e.shiftKey || (e.touches && e.touches.length === 2)
)
}
function onMouseUp (e) {
up()
}
function onWheel (e) {
if (scroll(e.deltaY) === true) {
e.preventDefault()
return false
}
}
function onTouchStart (e) {
e.preventDefault()
onMouseDown(e)
}
this._onMouseDown = onMouseDown
this._onTouchStart = onTouchStart
this._onMouseMove = onMouseMove
this._onMouseUp = onMouseUp
this._onWheel = onWheel
this.element.addEventListener('mousedown', onMouseDown)
this.element.addEventListener('touchstart', onTouchStart)
this.element.addEventListener('wheel', onWheel)
window.addEventListener('mousemove', onMouseMove)
window.addEventListener('touchmove', onMouseMove, { passive: false })
window.addEventListener('mouseup', onMouseUp)
window.addEventListener('touchend', onMouseUp)
this.updateCamera()
if (this.autoUpdate) {
const self = this
this._rafHandle = raf(function tick () {
orbiter.updateCamera()
self._rafHandle = raf(tick)
})
}
}
Orbiter.prototype.dispose = function () {
this.element.removeEventListener('mousedown', this._onMouseDown)
this.element.removeEventListener('touchstart', this._onTouchStart)
this.element.removeEventListener('wheel', this._onWheel)
window.removeEventListener('mousemove', this._onMouseMove)
window.removeEventListener('touchmove', this._onMouseMove)
window.removeEventListener('mouseup', this._onMouseUp)
window.removeEventListener('touchend', this._onMouseUp)
raf.cancel(this._rafHandle)
this.camera = null
}
module.exports = function createOrbiter (opts) {
return new Orbiter(opts)
}