forked from regl-project/regl-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regl-camera.js
253 lines (209 loc) · 7.05 KB
/
regl-camera.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
var mouseChange = require('mouse-change')
var mouseWheel = require('mouse-wheel')
var identity = require('gl-mat4/identity')
var perspective = require('gl-mat4/perspective')
var lookAt = require('gl-mat4/lookAt')
module.exports = createCamera
var isBrowser = typeof window !== 'undefined'
function createCamera (regl, props_) {
var props = props_ || {}
// Preserve backward-compatibilty while renaming preventDefault -> noScroll
if (typeof props.noScroll === 'undefined') {
props.noScroll = props.preventDefault
}
var cameraState = {
view: identity(new Float32Array(16)),
projection: identity(new Float32Array(16)),
center: new Float32Array(props.center || 3),
theta: props.theta || 0,
phi: props.phi || 0,
distance: Math.log(props.distance || 10.0),
eye: new Float32Array(3),
up: new Float32Array(props.up || [0, 1, 0]),
fovy: props.fovy || Math.PI / 4.0,
near: typeof props.near !== 'undefined' ? props.near : 0.01,
far: typeof props.far !== 'undefined' ? props.far : 1000.0,
noScroll: typeof props.noScroll !== 'undefined' ? props.noScroll : false,
flipY: !!props.flipY,
dtheta: 0,
dphi: 0,
rotationSpeed: typeof props.rotationSpeed !== 'undefined' ? props.rotationSpeed : 1,
zoomSpeed: typeof props.zoomSpeed !== 'undefined' ? props.zoomSpeed : 1,
renderOnDirty: typeof props.renderOnDirty !== undefined ? !!props.renderOnDirty : false
}
var element = props.element
var damping = typeof props.damping !== 'undefined' ? props.damping : 0.9
var right = new Float32Array([1, 0, 0])
var front = new Float32Array([0, 0, 1])
var minDistance = Math.log('minDistance' in props ? props.minDistance : 0.1)
var maxDistance = Math.log('maxDistance' in props ? props.maxDistance : 1000)
var ddistance = 0
var prevX = 0
var prevY = 0
var prevDistance = null
if (isBrowser && props.mouse !== false) {
var source = element || regl._gl.canvas
function getWidth () {
return element ? element.offsetWidth : window.innerWidth
}
function getHeight () {
return element ? element.offsetHeight : window.innerHeight
}
mouseChange(source, function (buttons, x, y) {
if (buttons & 1) {
var dx = (x - prevX) / getWidth()
var dy = (y - prevY) / getHeight()
cameraState.dtheta += cameraState.rotationSpeed * 4.0 * dx
cameraState.dphi += cameraState.rotationSpeed * 4.0 * dy
cameraState.dirty = true
}
prevX = x
prevY = y
})
source.addEventListener('touchstart', function (ev) {
if (ev.cancelable) {
ev.preventDefault()
}
var touch = ev.touches[0]
var bounds = source.getBoundingClientRect()
var x = touch.clientX - bounds.left
var y = touch.clientY - bounds.top
prevX = x
prevY = y
})
source.addEventListener('touchend', function (ev) {
prevDistance = null
// reset prevX and prevY to prevent jumping
if (ev.touches.length === 1) {
var touch = ev.touches[0]
var bounds = source.getBoundingClientRect()
var x = touch.clientX - bounds.left
var y = touch.clientY - bounds.top
prevX = x
prevY = y
}
})
source.addEventListener('touchmove', function (ev) {
if (ev.cancelable) {
ev.preventDefault()
}
// One touch, treat as a rotate
if (ev.touches.length === 1) {
var touch = ev.touches[0]
var bounds = source.getBoundingClientRect()
var x = touch.clientX - bounds.left
var y = touch.clientY - bounds.top
var dx = (x - prevX) / getWidth()
var dy = (y - prevY) / getHeight()
cameraState.dtheta += cameraState.rotationSpeed * 4.0 * dx
cameraState.dphi += cameraState.rotationSpeed * 4.0 * dy
cameraState.dirty = true
prevX = x
prevY = y
// Two fingers, treat as a pinch zoom
} else if (ev.touches.length === 2) {
var touch1 = ev.touches[0]
var touch2 = ev.touches[1]
var pinchX = touch1.clientX - touch2.clientX
var pinchY = touch1.clientY - touch2.clientY
var distance = Math.sqrt(pinchX * pinchX + pinchY * pinchY)
if (prevDistance != null) {
var distanceDelta = prevDistance - distance
ddistance +=
(distanceDelta / getHeight()) * cameraState.zoomSpeed * 2.0
cameraState.dirty = true
}
prevDistance = distance
}
})
mouseWheel(source, function (dx, dy) {
ddistance += dy / getHeight() * cameraState.zoomSpeed
cameraState.dirty = true
}, props.noScroll)
}
function damp (x) {
var xd = x * damping
if (Math.abs(xd) < 0.1) {
return 0
}
cameraState.dirty = true
return xd
}
function clamp (x, lo, hi) {
return Math.min(Math.max(x, lo), hi)
}
function updateCamera (props) {
Object.keys(props).forEach(function (prop) {
cameraState[prop] = props[prop]
})
var center = cameraState.center
var eye = cameraState.eye
var up = cameraState.up
var dtheta = cameraState.dtheta
var dphi = cameraState.dphi
cameraState.theta += dtheta
cameraState.phi = clamp(
cameraState.phi + dphi,
-Math.PI / 2.0,
Math.PI / 2.0)
cameraState.distance = clamp(
cameraState.distance + ddistance,
minDistance,
maxDistance)
cameraState.dtheta = damp(dtheta)
cameraState.dphi = damp(dphi)
ddistance = damp(ddistance)
var theta = cameraState.theta
var phi = cameraState.phi
var r = Math.exp(cameraState.distance)
var vf = r * Math.sin(theta) * Math.cos(phi)
var vr = r * Math.cos(theta) * Math.cos(phi)
var vu = r * Math.sin(phi)
for (var i = 0; i < 3; ++i) {
eye[i] = center[i] + vf * front[i] + vr * right[i] + vu * up[i]
}
lookAt(cameraState.view, eye, center, up)
}
cameraState.dirty = true
var injectContext = regl({
context: Object.assign({}, cameraState, {
dirty: function () {
return cameraState.dirty
},
projection: function (context) {
perspective(cameraState.projection,
cameraState.fovy,
context.viewportWidth / context.viewportHeight,
cameraState.near,
cameraState.far)
if (cameraState.flipY) { cameraState.projection[5] *= -1 }
return cameraState.projection
}
}),
uniforms: Object.keys(cameraState).reduce(function (uniforms, name) {
uniforms[name] = regl.context(name)
return uniforms
}, {})
})
function setupCamera (props, block) {
if (typeof setupCamera.dirty !== 'undefined') {
cameraState.dirty = setupCamera.dirty || cameraState.dirty
setupCamera.dirty = undefined
}
if (props && block) {
cameraState.dirty = true
}
if (cameraState.renderOnDirty && !cameraState.dirty) return
if (!block) {
block = props
props = {}
}
updateCamera(props)
injectContext(block)
cameraState.dirty = false
}
Object.keys(cameraState).forEach(function (name) {
setupCamera[name] = cameraState[name]
})
return setupCamera
}