-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
152 lines (120 loc) · 4.59 KB
/
index.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
var defined = require('defined')
var clamp = require('clamp')
var inputEvents = require('./lib/input')
var quatFromVec3 = require('quat-from-unit-vec3')
var quatInvert = require('gl-quat/invert')
var glVec3 = {
length: require('gl-vec3/length'),
add: require('gl-vec3/add'),
subtract: require('gl-vec3/subtract'),
transformQuat: require('gl-vec3/transformQuat'),
copy: require('gl-vec3/copy'),
normalize: require('gl-vec3/normalize'),
cross: require('gl-vec3/cross')
}
var Y_UP = [0, 1, 0]
var EPSILON = 1e-10
var tmpVec3 = [0, 0, 0]
module.exports = createOrbitControls
function createOrbitControls (opt) {
opt = opt || {}
var inputDelta = [0, 0, 0] // x, y, zoom
var offset = [0, 0, 0]
var upQuat = [0, 0, 0, 1]
var upQuatInverse = upQuat.slice()
var controls = {
update: update,
copyInto: copyInto,
position: opt.position ? opt.position.slice() : [0, 0, 1],
direction: [0, 0, -1],
up: opt.up ? opt.up.slice() : [0, 1, 0],
target: opt.target ? opt.target.slice() : [0, 0, 0],
phi: opt.phi || Math.PI / 2,
theta: opt.theta || 0,
distance: defined(opt.distance, 1),
damping: defined(opt.damping, 0.25),
rotateSpeed: defined(opt.rotateSpeed, 0.28),
zoomSpeed: defined(opt.zoomSpeed, 0.0075),
pinchSpeed: defined(opt.pinchSpeed, 0.0075),
pinch: opt.pinching !== false,
zoom: opt.zoom !== false,
rotate: opt.rotate !== false,
phiBounds: opt.phiBounds || [0, Math.PI],
thetaBounds: opt.thetaBounds || [-Infinity, Infinity],
distanceBounds: opt.distanceBounds || [0, Infinity]
}
// Compute distance if not defined in user options
if (typeof opt.distance !== 'number') {
glVec3.subtract(tmpVec3, controls.position, controls.target)
controls.distance = glVec3.length(tmpVec3)
}
// Apply an initial phi and theta
applyPhiTheta()
inputEvents({
parent: opt.parent || window,
element: opt.element,
rotate: opt.rotate !== false ? inputRotate : null,
zoom: opt.zoom !== false ? inputZoom : null,
pinch: opt.pinch !== false ? inputPinch : null
})
return controls
function inputRotate (dx, dy) {
var PI2 = Math.PI * 2
inputDelta[0] -= PI2 * dx * controls.rotateSpeed
inputDelta[1] -= PI2 * dy * controls.rotateSpeed
}
function inputZoom (delta) {
inputDelta[2] += delta * controls.zoomSpeed
}
function inputPinch (delta) {
inputDelta[2] -= delta * controls.pinchSpeed
}
function update () {
var cameraUp = controls.up || Y_UP
quatFromVec3(upQuat, cameraUp, Y_UP)
quatInvert(upQuatInverse, upQuat)
var distance = controls.distance
glVec3.subtract(offset, controls.position, controls.target)
glVec3.transformQuat(offset, offset, upQuat)
var theta = Math.atan2(offset[0], offset[2])
var phi = Math.atan2(Math.sqrt(offset[0] * offset[0] + offset[2] * offset[2]), offset[1])
theta += inputDelta[0]
phi += inputDelta[1]
theta = clamp(theta, controls.thetaBounds[0], controls.thetaBounds[1])
phi = clamp(phi, controls.phiBounds[0], controls.phiBounds[1])
phi = clamp(phi, EPSILON, Math.PI - EPSILON)
distance += inputDelta[2]
distance = clamp(distance, controls.distanceBounds[0], controls.distanceBounds[1])
var radius = Math.abs(distance) <= EPSILON ? EPSILON : distance
offset[0] = radius * Math.sin(phi) * Math.sin(theta)
offset[1] = radius * Math.cos(phi)
offset[2] = radius * Math.sin(phi) * Math.cos(theta)
controls.phi = phi
controls.theta = theta
controls.distance = distance
glVec3.transformQuat(offset, offset, upQuatInverse)
glVec3.add(controls.position, controls.target, offset)
camLookAt(controls.direction, cameraUp, controls.position, controls.target)
var damp = typeof controls.damping === 'number' ? controls.damping : 1
for (var i = 0; i < inputDelta.length; i++) {
inputDelta[i] *= 1 - damp
}
}
function copyInto (position, direction, up) {
if (position) glVec3.copy(position, controls.position)
if (direction) glVec3.copy(direction, controls.direction)
if (up) glVec3.copy(up, controls.up)
}
function applyPhiTheta () {
var dist = Math.max(EPSILON, controls.distance)
controls.position[0] = dist * Math.sin(controls.phi) * Math.sin(controls.theta)
controls.position[1] = dist * Math.cos(controls.phi)
controls.position[2] = dist * Math.sin(controls.phi) * Math.cos(controls.theta)
glVec3.add(controls.position, controls.position, controls.target)
}
}
function camLookAt (direction, up, position, target) {
glVec3.copy(direction, target)
glVec3.subtract(direction, direction, position)
glVec3.normalize(direction, direction)
}